blob: 3ba94be4895986fa9515f5ffa7b13e6146cccaf4 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <signal.h>
19#include <svm/svm_fifo_segment.h>
20#include <vlibmemory/api.h>
21#include <vpp/api/vpe_msg_enum.h>
22#include <vnet/session/application_interface.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040023#include <vcl/vppcom.h>
Dave Wallace543852a2017-08-03 02:11:34 -040024#include <vlib/unix/unix.h>
25#include <vppinfra/vec_bootstrap.h>
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -080026#include <vppinfra/elog.h>
Dave Wallace543852a2017-08-03 02:11:34 -040027
28#define vl_typedefs /* define message structures */
29#include <vpp/api/vpe_all_api_h.h>
30#undef vl_typedefs
31
32/* declare message handlers for each api */
33
34#define vl_endianfun /* define message structures */
35#include <vpp/api/vpe_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...)
40#define vl_printfun
41#include <vpp/api/vpe_all_api_h.h>
42#undef vl_printfun
43
44#if (CLIB_DEBUG > 0)
Dave Wallace498b3a52017-11-09 13:00:34 -050045/* Set VPPCOM_DEBUG_INIT 2 for connection debug,
46 * 3 for read/write debug output
47 * or
48 * export VCL_DEBUG=<#> to set dynamically.
49 */
50#define VPPCOM_DEBUG_INIT 1
Dave Wallace543852a2017-08-03 02:11:34 -040051#else
Dave Wallace498b3a52017-11-09 13:00:34 -050052#define VPPCOM_DEBUG_INIT 0
Dave Wallace543852a2017-08-03 02:11:34 -040053#endif
54
Dave Wallace498b3a52017-11-09 13:00:34 -050055#define VPPCOM_DEBUG vcm->debug
56
Dave Wallace543852a2017-08-03 02:11:34 -040057/*
58 * VPPCOM Private definitions and functions.
59 */
60typedef enum
61{
62 STATE_APP_START,
63 STATE_APP_CONN_VPP,
64 STATE_APP_ENABLED,
65 STATE_APP_ATTACHED,
66} app_state_t;
67
68typedef enum
69{
Dave Wallace4878cbe2017-11-21 03:45:09 -050070 STATE_START = 0x01,
71 STATE_CONNECT = 0x02,
72 STATE_LISTEN = 0x04,
73 STATE_ACCEPT = 0x08,
74 STATE_CLOSE_ON_EMPTY = 0x10,
75 STATE_DISCONNECT = 0x20,
76 STATE_FAILED = 0x40
Dave Wallace543852a2017-08-03 02:11:34 -040077} session_state_t;
78
Dave Wallace4878cbe2017-11-21 03:45:09 -050079#define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
80#define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
81
Dave Wallacef7f809c2017-10-03 01:48:42 -040082typedef struct epoll_event vppcom_epoll_event_t;
83
84typedef struct
85{
86 u32 next_sid;
87 u32 prev_sid;
88 u32 vep_idx;
89 vppcom_epoll_event_t ev;
90#define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
Dave Wallace60caa062017-11-10 17:07:13 -050091#define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
Dave Wallacef7f809c2017-10-03 01:48:42 -040092 u32 et_mask;
93} vppcom_epoll_t;
94
Dave Wallace543852a2017-08-03 02:11:34 -040095typedef struct
96{
Dave Wallace35830af2017-10-09 01:43:42 -040097 u8 is_ip4;
98 ip46_address_t ip46;
99} vppcom_ip46_t;
100
Dave Wallace048b1d62018-01-03 22:24:41 -0500101enum
102{
103 VCL_SESS_ATTR_SERVER,
104 VCL_SESS_ATTR_CUT_THRU,
105 VCL_SESS_ATTR_VEP,
106 VCL_SESS_ATTR_VEP_SESSION,
107 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
108 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
109 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
110 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
111 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
112 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
113 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
114 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
115 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
116 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
117 VCL_SESS_ATTR_MAX
118} vppcom_session_attr_t;
119
120#define VCL_SESS_ATTR_SET(ATTR, VAL) \
121do { \
122 (ATTR) |= 1 << (VAL); \
123 } while (0)
124
125#define VCL_SESS_ATTR_CLR(ATTR, VAL) \
126do { \
127 (ATTR) &= ~(1 << (VAL)); \
128 } while (0)
129
130#define VCL_SESS_ATTR_TEST(ATTR, VAL) \
131 ((ATTR) & (1 << (VAL)) ? 1 : 0)
132
Dave Wallace35830af2017-10-09 01:43:42 -0400133typedef struct
134{
Dave Wallace543852a2017-08-03 02:11:34 -0400135 volatile session_state_t state;
136
137 svm_fifo_t *server_rx_fifo;
138 svm_fifo_t *server_tx_fifo;
Dave Wallace048b1d62018-01-03 22:24:41 -0500139 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
140 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
141 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
Dave Wallace60caa062017-11-10 17:07:13 -0500142 u8 *segment_name;
Dave Wallace543852a2017-08-03 02:11:34 -0400143 u32 sm_seg_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500144 u32 client_context;
145 u64 vpp_handle;
Florin Corase86a8ed2018-01-05 03:20:25 -0800146 svm_queue_t *vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400147
148 /* Socket configuration state */
Dave Wallace048b1d62018-01-03 22:24:41 -0500149 /* TBD: covert 'is_*' vars to bit in session->attr; */
Dave Wallace543852a2017-08-03 02:11:34 -0400150 u8 is_server;
151 u8 is_listen;
152 u8 is_cut_thru;
153 u8 is_nonblocking;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400154 u8 is_vep;
155 u8 is_vep_session;
Dave Wallace048b1d62018-01-03 22:24:41 -0500156 u32 attr;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400157 u32 wait_cont_idx;
158 vppcom_epoll_t vep;
Dave Wallace048b1d62018-01-03 22:24:41 -0500159 int libc_epfd;
Dave Wallace543852a2017-08-03 02:11:34 -0400160 u32 vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400161 vppcom_ip46_t lcl_addr;
162 vppcom_ip46_t peer_addr;
Stevenac1f96d2017-10-24 16:03:58 -0700163 u16 lcl_port; // network order
164 u16 peer_port; // network order
Dave Wallace543852a2017-08-03 02:11:34 -0400165 u8 proto;
166 u64 client_queue_address;
167 u64 options[16];
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800168 elog_track_t elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -0400169} session_t;
170
171typedef struct vppcom_cfg_t_
172{
173 u64 heapsize;
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500174 u32 vpp_api_q_length;
Dave Wallace543852a2017-08-03 02:11:34 -0400175 u64 segment_baseva;
176 u32 segment_size;
177 u32 add_segment_size;
178 u32 preallocated_fifo_pairs;
179 u32 rx_fifo_size;
180 u32 tx_fifo_size;
181 u32 event_queue_size;
182 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400183 u8 app_proxy_transport_tcp;
184 u8 app_proxy_transport_udp;
185 u8 app_scope_local;
186 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400187 u8 *namespace_id;
188 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400189 f64 app_timeout;
190 f64 session_timeout;
191 f64 accept_timeout;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800192 u32 event_ring_size;
193 char *event_log_path;
Dave Wallace543852a2017-08-03 02:11:34 -0400194} vppcom_cfg_t;
195
196typedef struct vppcom_main_t_
197{
198 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500199 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400200 u32 *client_session_index_fifo;
Dave Wallace543852a2017-08-03 02:11:34 -0400201 int main_cpu;
202
203 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800204 svm_queue_t *vl_input_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400205
206 /* API client handle */
207 u32 my_client_index;
208
209 /* Session pool */
210 clib_spinlock_t sessions_lockp;
211 session_t *sessions;
212
213 /* Hash table for disconnect processing */
214 uword *session_index_by_vpp_handles;
215
216 /* Select bitmaps */
217 clib_bitmap_t *rd_bitmap;
218 clib_bitmap_t *wr_bitmap;
219 clib_bitmap_t *ex_bitmap;
220
221 /* Our event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800222 svm_queue_t *app_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400223
224 /* unique segment name counter */
225 u32 unique_segment_index;
226
Dave Wallace543852a2017-08-03 02:11:34 -0400227 /* For deadman timers */
228 clib_time_t clib_time;
229
230 /* State of the connection, shared between msg RX thread and main thread */
231 volatile app_state_t app_state;
232
233 vppcom_cfg_t cfg;
234
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800235 /* Event logging */
236 elog_main_t elog_main;
237 elog_track_t elog_track;
238
Dave Wallace543852a2017-08-03 02:11:34 -0400239 /* VNET_API_ERROR_FOO -> "Foo" hash table */
240 uword *error_string_by_error_number;
241} vppcom_main_t;
242
Dave Wallace2e005bb2017-11-07 01:21:39 -0500243/* NOTE: _vppcom_main is only used until the heap is allocated.
244 * Do not access it directly -- use vcm which will point to
245 * the heap allocated copy after init.
246 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500247static vppcom_main_t _vppcom_main = {
248 .debug = VPPCOM_DEBUG_INIT,
249 .my_client_index = ~0
250};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500251
252static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400253
Dave Wallace048b1d62018-01-03 22:24:41 -0500254#define VCL_LOCK_AND_GET_SESSION(I, S) \
255do { \
256 clib_spinlock_lock (&vcm->sessions_lockp); \
257 rv = vppcom_session_at_index (I, S); \
258 if (PREDICT_FALSE (rv)) \
259 { \
260 clib_spinlock_unlock (&vcm->sessions_lockp); \
261 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
262 getpid (), I); \
263 goto done; \
264 } \
Dave Wallace60caa062017-11-10 17:07:13 -0500265} while (0)
266
Dave Wallace543852a2017-08-03 02:11:34 -0400267static const char *
268vppcom_app_state_str (app_state_t state)
269{
270 char *st;
271
272 switch (state)
273 {
274 case STATE_APP_START:
275 st = "STATE_APP_START";
276 break;
277
278 case STATE_APP_CONN_VPP:
279 st = "STATE_APP_CONN_VPP";
280 break;
281
282 case STATE_APP_ENABLED:
283 st = "STATE_APP_ENABLED";
284 break;
285
286 case STATE_APP_ATTACHED:
287 st = "STATE_APP_ATTACHED";
288 break;
289
290 default:
291 st = "UNKNOWN_APP_STATE";
292 break;
293 }
294
295 return st;
296}
297
298static const char *
299vppcom_session_state_str (session_state_t state)
300{
301 char *st;
302
303 switch (state)
304 {
305 case STATE_START:
306 st = "STATE_START";
307 break;
308
309 case STATE_CONNECT:
310 st = "STATE_CONNECT";
311 break;
312
313 case STATE_LISTEN:
314 st = "STATE_LISTEN";
315 break;
316
317 case STATE_ACCEPT:
318 st = "STATE_ACCEPT";
319 break;
320
Dave Wallace4878cbe2017-11-21 03:45:09 -0500321 case STATE_CLOSE_ON_EMPTY:
322 st = "STATE_CLOSE_ON_EMPTY";
323 break;
324
Dave Wallace543852a2017-08-03 02:11:34 -0400325 case STATE_DISCONNECT:
326 st = "STATE_DISCONNECT";
327 break;
328
329 case STATE_FAILED:
330 st = "STATE_FAILED";
331 break;
332
333 default:
334 st = "UNKNOWN_STATE";
335 break;
336 }
337
338 return st;
339}
340
341/*
342 * VPPCOM Utility Functions
343 */
344static inline int
345vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
346{
Dave Wallace543852a2017-08-03 02:11:34 -0400347 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
348 if (PREDICT_FALSE ((session_index == ~0) ||
349 pool_is_free_index (vcm->sessions, session_index)))
350 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500351 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500352 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400353 return VPPCOM_EBADFD;
354 }
355 *sess = pool_elt_at_index (vcm->sessions, session_index);
356 return VPPCOM_OK;
357}
358
Florin Corasdcf55ce2017-11-16 15:32:50 -0800359static inline void
360vppcom_session_table_add_listener (u64 listener_handle, u32 value)
361{
362 /* Session and listener handles have different formats. The latter has
363 * the thread index in the upper 32 bits while the former has the session
364 * type. Knowing that, for listeners we just flip the MSB to 1 */
365 listener_handle |= 1ULL << 63;
366 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
367}
368
369static inline session_t *
370vppcom_session_table_lookup_listener (u64 listener_handle)
371{
372 uword *p;
373 u64 handle = listener_handle | (1ULL << 63);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500374 session_t *session;
375
Florin Corasdcf55ce2017-11-16 15:32:50 -0800376 p = hash_get (vcm->session_index_by_vpp_handles, handle);
377 if (!p)
378 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500379 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
Florin Corasdcf55ce2017-11-16 15:32:50 -0800380 "listener handle %llx", getpid (), listener_handle);
381 return 0;
382 }
383 if (pool_is_free_index (vcm->sessions, p[0]))
384 {
385 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500386 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
387 getpid (), p[0]);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800388 return 0;
389 }
390
Dave Wallace4878cbe2017-11-21 03:45:09 -0500391 session = pool_elt_at_index (vcm->sessions, p[0]);
392 ASSERT (session->is_listen);
393 return session;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800394}
395
396static inline void
397vppcom_session_table_del_listener (u64 listener_handle)
398{
399 listener_handle |= 1ULL << 63;
400 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
401}
402
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800403static void
404write_elog (void)
405{
406 elog_main_t *em = &vcm->elog_main;
407 char *chroot_file;
408 clib_error_t *error = 0;
409
410 chroot_file =
411 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
412 vcm->my_client_index, getpid (), 0);
413 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
414 if (error)
415 {
416 clib_error_report (error);
417 }
418 if (VPPCOM_DEBUG > 0)
419 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
420
421}
422
Dave Wallace543852a2017-08-03 02:11:34 -0400423static int
424vppcom_connect_to_vpp (char *app_name)
425{
426 api_main_t *am = &api_main;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800427 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400428
429 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500430 clib_warning ("VCL<%d>: app (%s) connecting to VPP api...",
431 getpid (), app_name);
432
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500433 if (vl_client_connect_to_vlib ("/vpe-api", app_name,
434 vcm->cfg.vpp_api_q_length) < 0)
Dave Wallace543852a2017-08-03 02:11:34 -0400435 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800437 rv = VPPCOM_ECONNREFUSED;
438 }
439 else
440 {
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800441 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
442 vcm->my_client_index = am->my_client_index;
443 vcm->app_state = STATE_APP_CONN_VPP;
Dave Wallace9b954252018-01-18 17:01:40 -0500444
445 if (VPPCOM_DEBUG > 0)
446 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
447 getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400448 }
449
Dave Wallace543852a2017-08-03 02:11:34 -0400450 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800451 {
452 vcm->elog_main.lock =
453 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
454 vcm->elog_main.lock[0] = 0;
455 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
456 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
457 elog_enable_disable (&vcm->elog_main, 1);
Dave Wallace543852a2017-08-03 02:11:34 -0400458
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800459 vcm->elog_track.name =
460 (char *) format (0, "P:%d:C:%d%c", getpid (),
461 vcm->my_client_index, 0);
462 elog_track_register (&vcm->elog_main, &vcm->elog_track);
463
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800464 /* *INDENT-OFF* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800465 ELOG_TYPE_DECLARE (e) =
466 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800467 .format = "connect_vpp:rv:%d",
468 .format_args = "i4",
469 };
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800470 struct
471 {
472 u32 data;
473 } *ed;
474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
475 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800476 /* *INDENT-ON* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800477 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800478 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400479}
480
481static u8 *
482format_api_error (u8 * s, va_list * args)
483{
Dave Wallace543852a2017-08-03 02:11:34 -0400484 i32 error = va_arg (*args, u32);
485 uword *p;
486
487 p = hash_get (vcm->error_string_by_error_number, -error);
488
489 if (p)
490 s = format (s, "%s (%d)", p[0], error);
491 else
492 s = format (s, "%d", error);
493 return s;
494}
495
496static void
497vppcom_init_error_string_table (void)
498{
Dave Wallace543852a2017-08-03 02:11:34 -0400499 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
500
501#define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
502 foreach_vnet_api_error;
503#undef _
504
505 hash_set (vcm->error_string_by_error_number, 99, "Misc");
506}
507
508static inline int
509vppcom_wait_for_app_state_change (app_state_t app_state)
510{
Dave Wallace543852a2017-08-03 02:11:34 -0400511 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
512
513 while (clib_time_now (&vcm->clib_time) < timeout)
514 {
515 if (vcm->app_state == app_state)
516 return VPPCOM_OK;
517 }
518 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500519 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400520 vppcom_app_state_str (app_state), app_state);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800521
522 if (VPPCOM_DEBUG > 0)
523 {
524 /* *INDENT-OFF* */
525 ELOG_TYPE_DECLARE (e) =
526 {
527 .format = "ERR: timeout state:%d",
528 .format_args = "i4",
529 };
530 struct
531 {
532 u32 data;
533 } *ed;
534
535 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
536
537 ed->data = app_state;
538 /* *INDENT-ON* */
539 }
540
Dave Wallace543852a2017-08-03 02:11:34 -0400541 return VPPCOM_ETIMEDOUT;
542}
543
544static inline int
545vppcom_wait_for_session_state_change (u32 session_index,
546 session_state_t state,
547 f64 wait_for_time)
548{
Dave Wallace543852a2017-08-03 02:11:34 -0400549 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
550 session_t *volatile session;
551 int rv;
552
553 do
554 {
555 clib_spinlock_lock (&vcm->sessions_lockp);
556 rv = vppcom_session_at_index (session_index, &session);
557 if (PREDICT_FALSE (rv))
558 {
559 clib_spinlock_unlock (&vcm->sessions_lockp);
560 return rv;
561 }
562 if (session->state == state)
563 {
564 clib_spinlock_unlock (&vcm->sessions_lockp);
565 return VPPCOM_OK;
566 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500567 if (session->state == STATE_FAILED)
568 {
569 clib_spinlock_unlock (&vcm->sessions_lockp);
570 return VPPCOM_ECONNREFUSED;
571 }
572
Dave Wallace543852a2017-08-03 02:11:34 -0400573 clib_spinlock_unlock (&vcm->sessions_lockp);
574 }
575 while (clib_time_now (&vcm->clib_time) < timeout);
576
577 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500578 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
Dave Wallace4878cbe2017-11-21 03:45:09 -0500579 state, vppcom_session_state_str (state));
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800580
581 if (VPPCOM_DEBUG > 0)
582 {
583 /* *INDENT-OFF* */
584 ELOG_TYPE_DECLARE (e) =
585 {
586 .format = "ERR: timeout state:%d",
587 .format_args = "i4",
588 };
589 struct
590 {
591 u32 data;
592 } *ed;
593
594 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
595
596 ed->data = state;
597 /* *INDENT-ON* */
598 }
599
Dave Wallace543852a2017-08-03 02:11:34 -0400600 return VPPCOM_ETIMEDOUT;
601}
602
603static inline int
604vppcom_wait_for_client_session_index (f64 wait_for_time)
605{
Dave Wallace543852a2017-08-03 02:11:34 -0400606 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
607
608 do
609 {
610 if (clib_fifo_elts (vcm->client_session_index_fifo))
611 return VPPCOM_OK;
612 }
613 while (clib_time_now (&vcm->clib_time) < timeout);
614
615 if (wait_for_time == 0)
616 return VPPCOM_EAGAIN;
617
618 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500619 clib_warning ("VCL<%d>: timeout waiting for client_session_index",
620 getpid ());
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800621
622 if (VPPCOM_DEBUG > 0)
623 {
624 /* *INDENT-OFF* */
625 ELOG_TYPE_DECLARE (e) =
626 {
627 .format = "ERR: timeout waiting for session index :%d",
628 .format_args = "i4",
629 };
630 struct
631 {
632 u32 data;
633 } *ed;
634
635 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
636
637 ed->data = getpid();
638 /* *INDENT-ON* */
639 }
640
Dave Wallace543852a2017-08-03 02:11:34 -0400641 return VPPCOM_ETIMEDOUT;
642}
643
644/*
645 * VPP-API message functions
646 */
647static void
648vppcom_send_session_enable_disable (u8 is_enable)
649{
Dave Wallace543852a2017-08-03 02:11:34 -0400650 vl_api_session_enable_disable_t *bmp;
651 bmp = vl_msg_api_alloc (sizeof (*bmp));
652 memset (bmp, 0, sizeof (*bmp));
653
654 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
655 bmp->client_index = vcm->my_client_index;
656 bmp->context = htonl (0xfeedface);
657 bmp->is_enable = is_enable;
658 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
659}
660
661static int
662vppcom_app_session_enable (void)
663{
Dave Wallace543852a2017-08-03 02:11:34 -0400664 int rv;
665
666 if (vcm->app_state != STATE_APP_ENABLED)
667 {
668 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
669 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
670 if (PREDICT_FALSE (rv))
671 {
672 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500673 clib_warning ("VCL<%d>: application session enable timed out! "
Dave Wallaceee45d412017-11-24 21:44:06 -0500674 "returning %d (%s)",
675 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400676 return rv;
677 }
678 }
679 return VPPCOM_OK;
680}
681
682static void
683 vl_api_session_enable_disable_reply_t_handler
684 (vl_api_session_enable_disable_reply_t * mp)
685{
Dave Wallace543852a2017-08-03 02:11:34 -0400686 if (mp->retval)
687 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500688 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400689 format_api_error, ntohl (mp->retval));
690 }
691 else
692 vcm->app_state = STATE_APP_ENABLED;
693}
694
695static void
696vppcom_app_send_attach (void)
697{
Dave Wallace543852a2017-08-03 02:11:34 -0400698 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400699 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400700 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
701 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400702
Dave Wallace543852a2017-08-03 02:11:34 -0400703 bmp = vl_msg_api_alloc (sizeof (*bmp));
704 memset (bmp, 0, sizeof (*bmp));
705
706 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
707 bmp->client_index = vcm->my_client_index;
708 bmp->context = htonl (0xfeedface);
709 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700710 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400711 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
712 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
713 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
714 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
715 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
716 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Florin Corasff6e7692017-12-11 04:59:01 -0800717 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
718 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
719 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
720 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Florin Corasf32cff62017-12-09 08:15:00 -0800721 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
722 vcm->cfg.preallocated_fifo_pairs;
Florin Corasff6e7692017-12-11 04:59:01 -0800723 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400724 if (nsid_len)
725 {
726 bmp->namespace_id_len = nsid_len;
727 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
728 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
729 }
Dave Wallace543852a2017-08-03 02:11:34 -0400730 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
731}
732
733static int
734vppcom_app_attach (void)
735{
Dave Wallace543852a2017-08-03 02:11:34 -0400736 int rv;
737
738 vppcom_app_send_attach ();
739 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
740 if (PREDICT_FALSE (rv))
741 {
742 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 clib_warning ("VCL<%d>: application attach timed out! "
744 "returning %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -0500745 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400746 return rv;
747 }
748 return VPPCOM_OK;
749}
750
751static void
752vppcom_app_detach (void)
753{
Dave Wallace543852a2017-08-03 02:11:34 -0400754 vl_api_application_detach_t *bmp;
755 bmp = vl_msg_api_alloc (sizeof (*bmp));
756 memset (bmp, 0, sizeof (*bmp));
757
758 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
759 bmp->client_index = vcm->my_client_index;
760 bmp->context = htonl (0xfeedface);
761 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
762}
763
764static void
765vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
766 mp)
767{
Dave Wallace543852a2017-08-03 02:11:34 -0400768 static svm_fifo_segment_create_args_t _a;
769 svm_fifo_segment_create_args_t *a = &_a;
770 int rv;
771
772 memset (a, 0, sizeof (*a));
773 if (mp->retval)
774 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500775 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400776 format_api_error, ntohl (mp->retval));
777 return;
778 }
779
780 if (mp->segment_name_length == 0)
781 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400783 return;
784 }
785
786 a->segment_name = (char *) mp->segment_name;
787 a->segment_size = mp->segment_size;
788
789 ASSERT (mp->app_event_queue_address);
790
791 /* Attach to the segment vpp created */
792 rv = svm_fifo_segment_attach (a);
793 vec_reset_length (a->new_segment_indices);
794 if (PREDICT_FALSE (rv))
795 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500796 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
797 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400798 return;
799 }
800
801 vcm->app_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800802 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400803
804 vcm->app_state = STATE_APP_ATTACHED;
805}
806
807static void
808vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
809 mp)
810{
Dave Wallace543852a2017-08-03 02:11:34 -0400811 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500812 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400813 ntohl (mp->retval));
814
815 vcm->app_state = STATE_APP_ENABLED;
816}
817
818static void
819vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
820 mp)
821{
Dave Wallace543852a2017-08-03 02:11:34 -0400822 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500823 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500824 getpid (), mp->handle, format_api_error,
825 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -0400826}
827
828static void
829vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
830{
Dave Wallace543852a2017-08-03 02:11:34 -0400831 static svm_fifo_segment_create_args_t _a;
832 svm_fifo_segment_create_args_t *a = &_a;
833 int rv;
834
835 memset (a, 0, sizeof (*a));
836 a->segment_name = (char *) mp->segment_name;
837 a->segment_size = mp->segment_size;
838 /* Attach to the segment vpp created */
839 rv = svm_fifo_segment_attach (a);
840 vec_reset_length (a->new_segment_indices);
841 if (PREDICT_FALSE (rv))
842 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500843 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500844 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400845 return;
846 }
847 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500848 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400849 mp->segment_name, mp->segment_size);
850}
851
852static void
853vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
854{
Dave Wallace543852a2017-08-03 02:11:34 -0400855 uword *p;
Dave Wallace543852a2017-08-03 02:11:34 -0400856
857 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
858 if (p)
859 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500860 int rv;
861 session_t *session = 0;
862 u32 session_index = p[0];
863
864 VCL_LOCK_AND_GET_SESSION (session_index, &session);
865 session->state = STATE_CLOSE_ON_EMPTY;
866
867 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500868 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500869 "setting state to 0x%x (%s)",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500870 getpid (), mp->handle, session_index, session->state,
871 vppcom_session_state_str (session->state));
Dave Wallace543852a2017-08-03 02:11:34 -0400872 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500873 return;
874
875 done:
876 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500877 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500878 "session lookup failed!",
879 getpid (), mp->handle, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400880 }
881 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500882 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500883 "handle failed!", getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400884}
885
886static void
887vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
888{
Dave Wallace543852a2017-08-03 02:11:34 -0400889 session_t *session = 0;
890 vl_api_reset_session_reply_t *rmp;
891 uword *p;
892 int rv = 0;
893
894 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
895 if (p)
896 {
897 int rval;
898 clib_spinlock_lock (&vcm->sessions_lockp);
899 rval = vppcom_session_at_index (p[0], &session);
900 if (PREDICT_FALSE (rval))
901 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500902 rv = VNET_API_ERROR_INVALID_VALUE_2;
Dave Wallace048b1d62018-01-03 22:24:41 -0500903 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500904 "session lookup failed! returning %d %U",
905 getpid (), mp->handle, p[0],
906 rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400907 }
908 else
Dave Wallace4878cbe2017-11-21 03:45:09 -0500909 {
910 /* TBD: should this disconnect immediately and
911 * flush the fifos?
912 */
913 session->state = STATE_CLOSE_ON_EMPTY;
914
915 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500916 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500917 "state set to %d (%s)!", getpid (),
918 mp->handle, p[0], session->state,
919 vppcom_session_state_str (session->state));
920 }
Dave Wallace543852a2017-08-03 02:11:34 -0400921 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400922 }
923 else
924 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500925 rv = VNET_API_ERROR_INVALID_VALUE;
Dave Wallace048b1d62018-01-03 22:24:41 -0500926 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500927 "failed! returning %d %U",
928 getpid (), mp->handle, rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400929 }
930
931 rmp = vl_msg_api_alloc (sizeof (*rmp));
932 memset (rmp, 0, sizeof (*rmp));
933 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
934 rmp->retval = htonl (rv);
935 rmp->handle = mp->handle;
936 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
937}
938
939static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400940vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400941{
Dave Wallaceee45d412017-11-24 21:44:06 -0500942 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400943 u32 session_index;
944 svm_fifo_t *rx_fifo, *tx_fifo;
945 u8 is_cut_thru = 0;
946 int rv;
947
Dave Wallace4878cbe2017-11-21 03:45:09 -0500948 session_index = mp->context;
Dave Wallaceee45d412017-11-24 21:44:06 -0500949 VCL_LOCK_AND_GET_SESSION (session_index, &session);
950done:
Dave Wallace543852a2017-08-03 02:11:34 -0400951 if (mp->retval)
952 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500953 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500954 "connect failed! %U",
955 getpid (), mp->handle, session_index,
956 format_api_error, ntohl (mp->retval));
957 if (rv == VPPCOM_OK)
958 {
959 session->state = STATE_FAILED;
960 session->vpp_handle = mp->handle;
961 }
962 else
963 {
964 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
965 "Invalid session index (%u)!",
966 getpid (), mp->handle, session_index);
967 }
968 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400969 }
970
Dave Wallaceee45d412017-11-24 21:44:06 -0500971 if (rv)
972 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400973
974 /* We've been redirected */
975 if (mp->segment_name_length > 0)
976 {
977 static svm_fifo_segment_create_args_t _a;
978 svm_fifo_segment_create_args_t *a = &_a;
979
980 is_cut_thru = 1;
981 memset (a, 0, sizeof (*a));
982 a->segment_name = (char *) mp->segment_name;
983 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 clib_warning ("VCL<%d>: cut-thru segment: %s\n",
Dave Wallace60caa062017-11-10 17:07:13 -0500985 getpid (), a->segment_name);
986
Dave Wallace543852a2017-08-03 02:11:34 -0400987 rv = svm_fifo_segment_attach (a);
988 vec_reset_length (a->new_segment_indices);
989 if (PREDICT_FALSE (rv))
990 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500991 clib_warning ("VCL<%d>: sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500992 getpid (), a->segment_name);
Dave Wallaceee45d412017-11-24 21:44:06 -0500993 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400994 }
995 }
996
997 /*
998 * Setup session
999 */
Dave Wallace543852a2017-08-03 02:11:34 -04001000 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -04001001 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -08001002 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001003
1004 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1005 rx_fifo->client_session_index = session_index;
1006 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1007 tx_fifo->client_session_index = session_index;
1008
1009 session->server_rx_fifo = rx_fifo;
1010 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -05001011 session->vpp_handle = mp->handle;
Dave Wallace9d1d73a2017-11-20 02:31:48 -05001012 session->lcl_addr.is_ip4 = mp->is_ip4;
1013 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1014 sizeof (session->peer_addr.ip46));
1015 session->lcl_port = mp->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001016 session->state = STATE_CONNECT;
1017
1018 /* Add it to lookup table */
1019 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -05001020
1021 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001022 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
Dave Wallaceee45d412017-11-24 21:44:06 -05001023 " session_rx_fifo %p, refcnt %d,"
1024 " session_tx_fifo %p, refcnt %d",
1025 getpid (), mp->handle, session_index,
Dave Wallace60caa062017-11-10 17:07:13 -05001026 session->server_rx_fifo,
1027 session->server_rx_fifo->refcnt,
1028 session->server_tx_fifo, session->server_tx_fifo->refcnt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001029done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001030 clib_spinlock_unlock (&vcm->sessions_lockp);
1031}
1032
1033static void
1034vppcom_send_connect_sock (session_t * session, u32 session_index)
1035{
Dave Wallace543852a2017-08-03 02:11:34 -04001036 vl_api_connect_sock_t *cmp;
1037
1038 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1039 session->is_server = 0;
1040 cmp = vl_msg_api_alloc (sizeof (*cmp));
1041 memset (cmp, 0, sizeof (*cmp));
1042 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1043 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -04001044 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001045
Dave Wallace543852a2017-08-03 02:11:34 -04001046 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001047 cmp->is_ip4 = session->peer_addr.is_ip4;
1048 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001049 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001050 cmp->proto = session->proto;
1051 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1052 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1053}
1054
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001055static inline void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001056vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1057 int rv)
1058{
1059 vl_api_disconnect_session_reply_t *rmp;
1060
1061 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001062 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1063 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001064 getpid (), vpp_handle, session_index);
1065
1066 rmp = vl_msg_api_alloc (sizeof (*rmp));
1067 memset (rmp, 0, sizeof (*rmp));
1068
1069 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1070 rmp->retval = htonl (rv);
1071 rmp->handle = vpp_handle;
1072 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1073}
1074
1075static inline void
1076vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001077{
Dave Wallace543852a2017-08-03 02:11:34 -04001078 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -04001079
Dave Wallace4878cbe2017-11-21 03:45:09 -05001080 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001081 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1082 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001083 getpid (), vpp_handle, session_index);
1084
Dave Wallace543852a2017-08-03 02:11:34 -04001085 dmp = vl_msg_api_alloc (sizeof (*dmp));
1086 memset (dmp, 0, sizeof (*dmp));
1087 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1088 dmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001089 dmp->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001090 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001091}
1092
1093static void
1094vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1095{
Dave Wallace543852a2017-08-03 02:11:34 -04001096 session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001097 u32 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -04001098 int rv;
1099
Dave Wallaceee45d412017-11-24 21:44:06 -05001100 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1101done:
Dave Wallace543852a2017-08-03 02:11:34 -04001102 if (mp->retval)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001103 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001104 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1105 "sid %u: bind failed: %U",
1106 getpid (), mp->handle, session_index,
1107 format_api_error, ntohl (mp->retval));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001108 rv = vppcom_session_at_index (session_index, &session);
1109 if (rv == VPPCOM_OK)
Dave Wallaceee45d412017-11-24 21:44:06 -05001110 {
1111 session->state = STATE_FAILED;
1112 session->vpp_handle = mp->handle;
1113 }
1114 else
1115 {
1116 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1117 "Invalid session index (%u)!",
1118 getpid (), mp->handle, session_index);
1119 }
1120 goto done_unlock;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001121 }
Dave Wallace543852a2017-08-03 02:11:34 -04001122
Dave Wallaceee45d412017-11-24 21:44:06 -05001123 session->vpp_handle = mp->handle;
1124 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1125 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1126 sizeof (session->peer_addr.ip46));
1127 session->lcl_port = mp->lcl_port;
1128 vppcom_session_table_add_listener (mp->handle, session_index);
1129 session->is_listen = 1;
1130 session->state = STATE_LISTEN;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001131
Dave Wallaceee45d412017-11-24 21:44:06 -05001132 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001133 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
Dave Wallaceee45d412017-11-24 21:44:06 -05001134 getpid (), mp->handle, mp->context);
1135done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001136 clib_spinlock_unlock (&vcm->sessions_lockp);
1137}
1138
1139static void
1140vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1141{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001142 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -05001143 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001144 getpid (), mp->context, format_api_error,
1145 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -04001146
Dave Wallace4878cbe2017-11-21 03:45:09 -05001147 else if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001148 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1149 getpid (), mp->context);
Dave Wallace543852a2017-08-03 02:11:34 -04001150}
1151
1152u8 *
1153format_ip4_address (u8 * s, va_list * args)
1154{
1155 u8 *a = va_arg (*args, u8 *);
1156 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1157}
1158
1159u8 *
1160format_ip6_address (u8 * s, va_list * args)
1161{
1162 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1163 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1164
1165 i_max_n_zero = ARRAY_LEN (a->as_u16);
1166 max_n_zeros = 0;
1167 i_first_zero = i_max_n_zero;
1168 n_zeros = 0;
1169 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1170 {
1171 u32 is_zero = a->as_u16[i] == 0;
1172 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1173 {
1174 i_first_zero = i;
1175 n_zeros = 0;
1176 }
1177 n_zeros += is_zero;
1178 if ((!is_zero && n_zeros > max_n_zeros)
1179 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1180 {
1181 i_max_n_zero = i_first_zero;
1182 max_n_zeros = n_zeros;
1183 i_first_zero = ARRAY_LEN (a->as_u16);
1184 n_zeros = 0;
1185 }
1186 }
1187
1188 last_double_colon = 0;
1189 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1190 {
1191 if (i == i_max_n_zero && max_n_zeros > 1)
1192 {
1193 s = format (s, "::");
1194 i += max_n_zeros - 1;
1195 last_double_colon = 1;
1196 }
1197 else
1198 {
1199 s = format (s, "%s%x",
1200 (last_double_colon || i == 0) ? "" : ":",
1201 clib_net_to_host_u16 (a->as_u16[i]));
1202 last_double_colon = 0;
1203 }
1204 }
1205
1206 return s;
1207}
1208
1209/* Format an IP46 address. */
1210u8 *
1211format_ip46_address (u8 * s, va_list * args)
1212{
1213 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1214 ip46_type_t type = va_arg (*args, ip46_type_t);
1215 int is_ip4 = 1;
1216
1217 switch (type)
1218 {
1219 case IP46_TYPE_ANY:
1220 is_ip4 = ip46_address_is_ip4 (ip46);
1221 break;
1222 case IP46_TYPE_IP4:
1223 is_ip4 = 1;
1224 break;
1225 case IP46_TYPE_IP6:
1226 is_ip4 = 0;
1227 break;
1228 }
1229
1230 return is_ip4 ?
1231 format (s, "%U", format_ip4_address, &ip46->ip4) :
1232 format (s, "%U", format_ip6_address, &ip46->ip6);
1233}
1234
Dave Wallace60caa062017-11-10 17:07:13 -05001235static inline void
Florin Coras50e8bdb2017-11-27 10:37:05 -08001236vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001237{
1238 vl_api_accept_session_reply_t *rmp;
1239
1240 rmp = vl_msg_api_alloc (sizeof (*rmp));
1241 memset (rmp, 0, sizeof (*rmp));
1242 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1243 rmp->retval = htonl (retval);
Dave Wallaced2931962017-11-25 04:17:39 -05001244 rmp->context = context;
Dave Wallace60caa062017-11-10 17:07:13 -05001245 rmp->handle = handle;
1246 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1247}
1248
Dave Wallace543852a2017-08-03 02:11:34 -04001249static void
1250vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1251{
Dave Wallace543852a2017-08-03 02:11:34 -04001252 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001253 session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001254 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001255
Dave Wallace60caa062017-11-10 17:07:13 -05001256 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001257 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1258 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001259 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallaced2931962017-11-25 04:17:39 -05001260 vppcom_send_accept_session_reply (mp->handle, mp->context,
Florin Corasdcf55ce2017-11-16 15:32:50 -08001261 VNET_API_ERROR_QUEUE_FULL);
1262 clib_spinlock_unlock (&vcm->sessions_lockp);
1263 return;
1264 }
1265
1266 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1267 if (!listen_session)
1268 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001269 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1270 "unknown vpp listener handle %llx",
1271 getpid (), mp->listener_handle);
Dave Wallace60caa062017-11-10 17:07:13 -05001272 clib_spinlock_unlock (&vcm->sessions_lockp);
1273 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001274 }
1275
Dave Wallace543852a2017-08-03 02:11:34 -04001276 /* Allocate local session and set it up */
1277 pool_get (vcm->sessions, session);
1278 memset (session, 0, sizeof (*session));
1279 session_index = session - vcm->sessions;
1280
1281 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1282 rx_fifo->client_session_index = session_index;
1283 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1284 tx_fifo->client_session_index = session_index;
1285
Dave Wallace60caa062017-11-10 17:07:13 -05001286 session->vpp_handle = mp->handle;
Dave Wallaced2931962017-11-25 04:17:39 -05001287 session->client_context = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -04001288 session->server_rx_fifo = rx_fifo;
1289 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001290 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -08001291 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001292 session->state = STATE_ACCEPT;
1293 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001294 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001295 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001296 session->peer_addr.is_ip4 = mp->is_ip4;
1297 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1298 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001299
1300 /* Add it to lookup table */
1301 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001302 session->lcl_port = listen_session->lcl_port;
1303 session->lcl_addr = listen_session->lcl_addr;
Dave Wallace227867f2017-11-13 21:21:53 -05001304
1305 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001306 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001307
Dave Wallacef7f809c2017-10-03 01:48:42 -04001308 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001309
Dave Wallace60caa062017-11-10 17:07:13 -05001310 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
Florin Coras50e8bdb2017-11-27 10:37:05 -08001312 "request from %s address %U port %d queue %p!", getpid (),
Dave Wallaced2931962017-11-25 04:17:39 -05001313 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1314 format_ip46_address, &mp->ip, mp->is_ip4,
Florin Coras50e8bdb2017-11-27 10:37:05 -08001315 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001316
1317 if (VPPCOM_DEBUG > 0)
1318 {
1319 session->elog_track.name =
1320 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1321 session_index, 0);
1322 elog_track_register (&vcm->elog_main, &session->elog_track);
1323
1324 if (session->peer_addr.is_ip4)
1325 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001326 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001327 ELOG_TYPE_DECLARE (e) =
1328 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001329 .format =
1330 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1331 .format_args = "i8i1i1i1i1i2",
1332 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001333
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001334 CLIB_PACKED (struct {
1335 u64 handle; //8
1336 u8 addr[4]; //4
1337 u16 port; //2
1338 }) * ed;
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001339
1340 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1341
1342 ed->handle = mp->handle;
1343 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1344 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1345 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1346 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08001347 ed->port = clib_net_to_host_u16 (session->peer_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001348 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001349 }
1350 else
1351 {
1352 clib_warning ("ip6");
1353 }
1354 }
1355
Dave Wallace60caa062017-11-10 17:07:13 -05001356}
1357
1358static void
Dave Wallaceee45d412017-11-24 21:44:06 -05001359vppcom_send_connect_session_reply (session_t * session, u32 session_index,
Dave Wallaced2931962017-11-25 04:17:39 -05001360 u64 vpp_handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001361{
1362 vl_api_connect_session_reply_t *rmp;
1363 u32 len;
Florin Corase86a8ed2018-01-05 03:20:25 -08001364 svm_queue_t *client_q;
Dave Wallace60caa062017-11-10 17:07:13 -05001365
Dave Wallace543852a2017-08-03 02:11:34 -04001366 rmp = vl_msg_api_alloc (sizeof (*rmp));
1367 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001368 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallaced2931962017-11-25 04:17:39 -05001369
1370 if (!session)
1371 {
1372 rmp->context = context;
1373 rmp->handle = vpp_handle;
1374 rmp->retval = htonl (retval);
1375 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1376 return;
1377 }
1378
Dave Wallace227867f2017-11-13 21:21:53 -05001379 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001380 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001381 rmp->handle = session->vpp_handle;
1382 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1383 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1384 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1385 rmp->segment_size = vcm->cfg.segment_size;
1386 len = vec_len (session->segment_name);
1387 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1388 clib_memcpy (rmp->segment_name, session->segment_name,
1389 rmp->segment_name_length - 1);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001390 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
Dave Wallace227867f2017-11-13 21:21:53 -05001391 sizeof (rmp->lcl_ip));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001392 rmp->is_ip4 = session->peer_addr.is_ip4;
1393 rmp->lcl_port = session->peer_port;
Florin Corase86a8ed2018-01-05 03:20:25 -08001394 client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
Dave Wallace60caa062017-11-10 17:07:13 -05001395 ASSERT (client_q);
1396 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001397}
1398
1399/*
1400 * Acting as server for redirected connect requests
1401 */
1402static void
1403vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1404{
Dave Wallace543852a2017-08-03 02:11:34 -04001405 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001406 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001407
Dave Wallacef7f809c2017-10-03 01:48:42 -04001408 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001409 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1410 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001411 clib_spinlock_unlock (&vcm->sessions_lockp);
1412
Dave Wallace543852a2017-08-03 02:11:34 -04001413 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001414 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallace4878cbe2017-11-21 03:45:09 -05001415
Dave Wallaced2931962017-11-25 04:17:39 -05001416 /* TBD: Fix api to include vpp handle */
1417 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1418 0 /* handle */ , mp->context,
1419 VNET_API_ERROR_QUEUE_FULL);
Dave Wallace60caa062017-11-10 17:07:13 -05001420 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001421 }
1422
Dave Wallace543852a2017-08-03 02:11:34 -04001423 pool_get (vcm->sessions, session);
1424 memset (session, 0, sizeof (*session));
1425 session_index = session - vcm->sessions;
1426
Dave Wallace60caa062017-11-10 17:07:13 -05001427 session->client_context = mp->context;
1428 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001429 session->client_queue_address = mp->client_queue_address;
1430 session->is_cut_thru = 1;
1431 session->is_server = 1;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001432 session->lcl_port = mp->port;
1433 session->lcl_addr.is_ip4 = mp->is_ip4;
1434 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1435 sizeof (session->lcl_addr.ip46));
1436
1437 /* TBD: missing peer info in api msg.
1438 */
Dave Wallace35830af2017-10-09 01:43:42 -04001439 session->peer_addr.is_ip4 = mp->is_ip4;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001440 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001441
Dave Wallace543852a2017-08-03 02:11:34 -04001442 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001443 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001444 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001445 clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001446 "clib_fifo_elts %u!\n", getpid (), session_index,
1447 clib_fifo_elts (vcm->client_session_index_fifo));
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001448
1449 if (VPPCOM_DEBUG > 0)
1450 {
1451 session->elog_track.name =
1452 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1453 session_index, 0);
1454 elog_track_register (&vcm->elog_main, &session->elog_track);
1455
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001456 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001457 ELOG_TYPE_DECLARE (e) =
1458 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001459 .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1460 .format_args = "i4i4",
1461 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001462
1463 struct
1464 {
1465 u32 data[2];
1466 } *ed;
1467
1468 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1469
1470 ed->data[0] = session_index;
1471 ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001472 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001473 }
1474
Dave Wallacef7f809c2017-10-03 01:48:42 -04001475 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001476}
1477
1478static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001479vppcom_send_bind_sock (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001480{
Dave Wallace543852a2017-08-03 02:11:34 -04001481 vl_api_bind_sock_t *bmp;
1482
1483 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1484 session->is_server = 1;
1485 bmp = vl_msg_api_alloc (sizeof (*bmp));
1486 memset (bmp, 0, sizeof (*bmp));
1487
1488 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1489 bmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001490 bmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001491 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001492 bmp->is_ip4 = session->lcl_addr.is_ip4;
1493 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001494 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001495 bmp->proto = session->proto;
1496 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1497 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1498}
1499
1500static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001501vppcom_send_unbind_sock (u64 vpp_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001502{
Dave Wallace543852a2017-08-03 02:11:34 -04001503 vl_api_unbind_sock_t *ump;
Dave Wallace543852a2017-08-03 02:11:34 -04001504
1505 ump = vl_msg_api_alloc (sizeof (*ump));
1506 memset (ump, 0, sizeof (*ump));
1507
1508 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1509 ump->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001510 ump->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001511 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1512}
1513
1514static int
Dave Wallace543852a2017-08-03 02:11:34 -04001515vppcom_session_unbind (u32 session_index)
1516{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001517 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001518 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001519 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001520
Dave Wallace4878cbe2017-11-21 03:45:09 -05001521 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1522
1523 vpp_handle = session->vpp_handle;
1524 vppcom_session_table_del_listener (vpp_handle);
1525 session->vpp_handle = ~0;
1526 session->state = STATE_DISCONNECT;
1527
Dave Wallace543852a2017-08-03 02:11:34 -04001528 clib_spinlock_unlock (&vcm->sessions_lockp);
1529
Dave Wallace4878cbe2017-11-21 03:45:09 -05001530 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001531 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001532 "sending unbind msg! new state 0x%x (%s)",
1533 getpid (), vpp_handle, session_index,
1534 session->state, vppcom_session_state_str (session->state));
1535
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001536 if (VPPCOM_DEBUG > 0)
1537 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001538 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001539 ELOG_TYPE_DECLARE (e) =
1540 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001541 .format = "unbind: handle:%x",
1542 .format_args = "i8",
1543 };
1544
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001545 struct
1546 {
1547 u64 handle;
1548 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001549
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001550 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1551 ed->handle = vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001552 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001553 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001554 vppcom_send_unbind_sock (vpp_handle);
1555
1556done:
1557 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001558}
1559
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001560static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001561vppcom_session_disconnect (u32 session_index)
1562{
Dave Wallace543852a2017-08-03 02:11:34 -04001563 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001564 session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001565 u8 is_cut_thru, is_listen, is_server;
1566 u64 vpp_handle;
1567 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04001568
Dave Wallace4878cbe2017-11-21 03:45:09 -05001569 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1570
1571 vpp_handle = session->vpp_handle;
1572 is_server = session->is_server;
1573 is_listen = session->is_listen;
1574 is_cut_thru = session->is_cut_thru;
1575 state = session->state;
1576 clib_spinlock_unlock (&vcm->sessions_lockp);
1577
1578 if (VPPCOM_DEBUG > 1)
Dave Wallace543852a2017-08-03 02:11:34 -04001579 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001580 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s "
1581 "state 0x%x (%s), is_cut_thru %d, is_listen %d",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001582 getpid (), vpp_handle, session_index,
1583 is_server ? "server" : "client",
1584 state, vppcom_session_state_str (state),
1585 is_cut_thru, is_listen);
Dave Wallace543852a2017-08-03 02:11:34 -04001586 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001587
Dave Wallace4878cbe2017-11-21 03:45:09 -05001588 if (PREDICT_FALSE (is_listen))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001589 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001590 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001591 "Cannot disconnect a listen socket!",
1592 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001593 rv = VPPCOM_EBADFD;
1594 goto done;
1595 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001596
Dave Wallace4878cbe2017-11-21 03:45:09 -05001597 /* Through the VPP host stack...
1598 */
1599 else if (!is_cut_thru)
1600 {
1601 /* The peer has already initiated the close,
1602 * so send the disconnect session reply.
Dave Wallace227867f2017-11-13 21:21:53 -05001603 */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001604 if (state & STATE_CLOSE_ON_EMPTY)
1605 {
1606 vppcom_send_disconnect_session_reply (vpp_handle,
1607 session_index, 0 /* rv */ );
1608 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001609 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001610 "sending disconnect REPLY...",
1611 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001612 }
1613
1614 /* Otherwise, send a disconnect session msg...
1615 */
1616 else
1617 {
1618 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001619 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001620 "sending disconnect...",
1621 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001622
1623 vppcom_send_disconnect_session (vpp_handle, session_index);
1624 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001625 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001626
1627 /* Cut-thru connections...
1628 *
1629 * server: free fifos and segment allocated during connect/redirect
1630 * client: no cleanup required
1631 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001632 else
Dave Wallace227867f2017-11-13 21:21:53 -05001633 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001634 if (is_server)
1635 {
1636 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1637 svm_fifo_segment_private_t *seg;
1638
1639 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1640
1641 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001642 clib_warning ("VCL<%d>: sid %d: freeing cut-thru fifos in "
Dave Wallaceee45d412017-11-24 21:44:06 -05001643 "sm_seg_index %d! "
1644 " server_rx_fifo %p, refcnt = %d"
1645 " server_tx_fifo %p, refcnt = %d",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001646 getpid (), session_index, session->sm_seg_index,
1647 session->server_rx_fifo,
1648 session->server_rx_fifo->refcnt,
1649 session->server_tx_fifo,
1650 session->server_tx_fifo->refcnt);
1651
1652 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1653 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1654 FIFO_SEGMENT_RX_FREELIST);
1655 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1656 FIFO_SEGMENT_TX_FREELIST);
1657 svm_fifo_segment_delete (seg);
1658
1659 /* TBD: Send cut-thru disconnect event to client */
1660
1661 clib_spinlock_unlock (&vcm->sessions_lockp);
1662 }
1663 else
1664 {
1665 /* TBD: Send cut-thru disconnect event to server */
1666 }
Dave Wallace227867f2017-11-13 21:21:53 -05001667 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001668
Dave Wallace4878cbe2017-11-21 03:45:09 -05001669done:
1670 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001671}
1672
1673#define foreach_sock_msg \
1674_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1675_(BIND_SOCK_REPLY, bind_sock_reply) \
1676_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1677_(ACCEPT_SESSION, accept_session) \
1678_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001679_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001680_(DISCONNECT_SESSION, disconnect_session) \
1681_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1682_(RESET_SESSION, reset_session) \
1683_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1684_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1685_(MAP_ANOTHER_SEGMENT, map_another_segment)
1686
1687static void
1688vppcom_api_hookup (void)
1689{
1690#define _(N,n) \
1691 vl_msg_api_set_handlers(VL_API_##N, #n, \
1692 vl_api_##n##_t_handler, \
1693 vl_noop_handler, \
1694 vl_api_##n##_t_endian, \
1695 vl_api_##n##_t_print, \
1696 sizeof(vl_api_##n##_t), 1);
1697 foreach_sock_msg;
1698#undef _
1699}
1700
1701static void
1702vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1703{
1704 ASSERT (vcl_cfg);
1705
1706 vcl_cfg->heapsize = (256ULL << 20);
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001707 vcl_cfg->vpp_api_q_length = 1024;
Dave Wallace543852a2017-08-03 02:11:34 -04001708 vcl_cfg->segment_baseva = 0x200000000ULL;
1709 vcl_cfg->segment_size = (256 << 20);
1710 vcl_cfg->add_segment_size = (128 << 20);
1711 vcl_cfg->preallocated_fifo_pairs = 8;
1712 vcl_cfg->rx_fifo_size = (1 << 20);
1713 vcl_cfg->tx_fifo_size = (1 << 20);
1714 vcl_cfg->event_queue_size = 2048;
1715 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1716 vcl_cfg->app_timeout = 10 * 60.0;
1717 vcl_cfg->session_timeout = 10 * 60.0;
1718 vcl_cfg->accept_timeout = 60.0;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001719 vcl_cfg->event_ring_size = (128 << 10);
1720 vcl_cfg->event_log_path = "/dev/shm";
Dave Wallace543852a2017-08-03 02:11:34 -04001721}
1722
1723static void
1724vppcom_cfg_heapsize (char *conf_fname)
1725{
Dave Wallace543852a2017-08-03 02:11:34 -04001726 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1727 FILE *fp;
1728 char inbuf[4096];
1729 int argc = 1;
1730 char **argv = NULL;
1731 char *arg = NULL;
1732 char *p;
1733 int i;
1734 u8 *sizep;
1735 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001736 void *vcl_mem;
1737 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001738
1739 fp = fopen (conf_fname, "r");
1740 if (fp == NULL)
1741 {
1742 if (VPPCOM_DEBUG > 0)
1743 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1744 goto defaulted;
1745 }
1746 argv = calloc (1, sizeof (char *));
1747 if (argv == NULL)
1748 goto defaulted;
1749
1750 while (1)
1751 {
1752 if (fgets (inbuf, 4096, fp) == 0)
1753 break;
1754 p = strtok (inbuf, " \t\n");
1755 while (p != NULL)
1756 {
1757 if (*p == '#')
1758 break;
1759 argc++;
1760 char **tmp = realloc (argv, argc * sizeof (char *));
1761 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001762 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001763 argv = tmp;
1764 arg = strndup (p, 1024);
1765 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001766 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001767 argv[argc - 1] = arg;
1768 p = strtok (NULL, " \t\n");
1769 }
1770 }
1771
1772 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001773 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001774
1775 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1776 if (tmp == NULL)
1777 goto defaulted;
1778 argv = tmp;
1779 argv[argc] = NULL;
1780
1781 /*
1782 * Look for and parse the "heapsize" config parameter.
1783 * Manual since none of the clib infra has been bootstrapped yet.
1784 *
1785 * Format: heapsize <nn>[mM][gG]
1786 */
1787
1788 for (i = 1; i < (argc - 1); i++)
1789 {
1790 if (!strncmp (argv[i], "heapsize", 8))
1791 {
1792 sizep = (u8 *) argv[i + 1];
1793 size = 0;
1794 while (*sizep >= '0' && *sizep <= '9')
1795 {
1796 size *= 10;
1797 size += *sizep++ - '0';
1798 }
1799 if (size == 0)
1800 {
1801 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001802 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001803 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001804 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001805 vcl_cfg->heapsize, vcl_cfg->heapsize);
1806 goto defaulted;
1807 }
1808
1809 if (*sizep == 'g' || *sizep == 'G')
1810 vcl_cfg->heapsize = size << 30;
1811 else if (*sizep == 'm' || *sizep == 'M')
1812 vcl_cfg->heapsize = size << 20;
1813 else
1814 {
1815 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001816 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001817 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001818 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001819 vcl_cfg->heapsize, vcl_cfg->heapsize);
1820 goto defaulted;
1821 }
1822 }
1823 }
1824
1825defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001826 if (fp != NULL)
1827 fclose (fp);
1828 if (argv != NULL)
1829 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001830
Dave Wallace2e005bb2017-11-07 01:21:39 -05001831 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1832 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001833 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001834 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001835 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001836 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1837 "-1, 0) failed!",
1838 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1839 return;
1840 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001841 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1842 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001843 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001844 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001845 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001846 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001847 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1848 if (!vcl_mem)
1849 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001850 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001851 return;
1852 }
1853
1854 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1855 vcm = vcl_mem;
1856
1857 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001858 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001859 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001860}
1861
1862static void
1863vppcom_cfg_read (char *conf_fname)
1864{
Dave Wallace543852a2017-08-03 02:11:34 -04001865 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1866 int fd;
1867 unformat_input_t _input, *input = &_input;
1868 unformat_input_t _line_input, *line_input = &_line_input;
1869 u8 vc_cfg_input = 0;
1870 u8 *chroot_path;
1871 struct stat s;
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001872 u32 uid, gid, q_len;
Dave Wallace543852a2017-08-03 02:11:34 -04001873
1874 fd = open (conf_fname, O_RDONLY);
1875 if (fd < 0)
1876 {
1877 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001878 clib_warning ("VCL<%d>: open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001879 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001880 goto file_done;
1881 }
1882
1883 if (fstat (fd, &s) < 0)
1884 {
1885 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001886 clib_warning ("VCL<%d>: failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001887 goto file_done;
1888 }
1889
1890 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1891 {
1892 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001893 clib_warning ("VCL<%d>: not a regular file `%s'",
1894 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001895 goto file_done;
1896 }
1897
Dave Barach59b25652017-09-10 15:04:27 -04001898 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001899
1900 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1901 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001902 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001903 unformat_skip_white_space (line_input);
1904
Dave Wallace8af20542017-10-26 03:29:30 -04001905 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001906 {
1907 vc_cfg_input = 1;
1908 continue;
1909 }
1910
1911 if (vc_cfg_input)
1912 {
1913 if (unformat (line_input, "heapsize %s", &chroot_path))
1914 {
1915 vec_terminate_c_string (chroot_path);
1916 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001917 clib_warning ("VCL<%d>: configured heapsize %s, "
Dave Wallace543852a2017-08-03 02:11:34 -04001918 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001919 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001920 vcl_cfg->heapsize);
1921 vec_free (chroot_path);
1922 }
1923 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1924 {
1925 vec_terminate_c_string (chroot_path);
1926 vl_set_memory_root_path ((char *) chroot_path);
1927 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 clib_warning ("VCL<%d>: configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001929 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001930 chroot_path = 0; /* Don't vec_free() it! */
1931 }
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001932 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1933 {
1934 if (q_len < vcl_cfg->vpp_api_q_length)
1935 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001937 "(%u) is too small! Using default: %u ",
1938 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1939 }
1940 else
1941 {
1942 vcl_cfg->vpp_api_q_length = q_len;
1943
1944 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001945 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001946 getpid (), vcl_cfg->vpp_api_q_length);
1947 }
1948 }
Dave Wallace543852a2017-08-03 02:11:34 -04001949 else if (unformat (line_input, "uid %d", &uid))
1950 {
1951 vl_set_memory_uid (uid);
1952 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001953 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001954 }
1955 else if (unformat (line_input, "gid %d", &gid))
1956 {
1957 vl_set_memory_gid (gid);
1958 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001960 }
Dave Wallace8af20542017-10-26 03:29:30 -04001961 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001962 &vcl_cfg->segment_baseva))
1963 {
1964 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001965 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001966 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001967 }
1968 else if (unformat (line_input, "segment-size 0x%lx",
1969 &vcl_cfg->segment_size))
1970 {
1971 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001973 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001974 vcl_cfg->segment_size);
1975 }
1976 else if (unformat (line_input, "segment-size %ld",
1977 &vcl_cfg->segment_size))
1978 {
1979 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001980 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001981 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001982 vcl_cfg->segment_size);
1983 }
1984 else if (unformat (line_input, "add-segment-size 0x%lx",
1985 &vcl_cfg->add_segment_size))
1986 {
1987 if (VPPCOM_DEBUG > 0)
1988 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001989 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001990 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001991 vcl_cfg->add_segment_size);
1992 }
1993 else if (unformat (line_input, "add-segment-size %ld",
1994 &vcl_cfg->add_segment_size))
1995 {
1996 if (VPPCOM_DEBUG > 0)
1997 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001998 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001999 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002000 vcl_cfg->add_segment_size);
2001 }
2002 else if (unformat (line_input, "preallocated-fifo-pairs %d",
2003 &vcl_cfg->preallocated_fifo_pairs))
2004 {
2005 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002006 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002007 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002008 vcl_cfg->preallocated_fifo_pairs,
2009 vcl_cfg->preallocated_fifo_pairs);
2010 }
2011 else if (unformat (line_input, "rx-fifo-size 0x%lx",
2012 &vcl_cfg->rx_fifo_size))
2013 {
2014 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002015 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002016 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002017 vcl_cfg->rx_fifo_size);
2018 }
2019 else if (unformat (line_input, "rx-fifo-size %ld",
2020 &vcl_cfg->rx_fifo_size))
2021 {
2022 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002023 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002024 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002025 vcl_cfg->rx_fifo_size);
2026 }
2027 else if (unformat (line_input, "tx-fifo-size 0x%lx",
2028 &vcl_cfg->tx_fifo_size))
2029 {
2030 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002031 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002032 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002033 vcl_cfg->tx_fifo_size);
2034 }
2035 else if (unformat (line_input, "tx-fifo-size %ld",
2036 &vcl_cfg->tx_fifo_size))
2037 {
2038 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002039 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002040 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002041 vcl_cfg->tx_fifo_size);
2042 }
2043 else if (unformat (line_input, "event-queue-size 0x%lx",
2044 &vcl_cfg->event_queue_size))
2045 {
2046 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002047 clib_warning ("VCL<%d>: configured event_queue_size "
2048 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002049 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002050 vcl_cfg->event_queue_size);
2051 }
2052 else if (unformat (line_input, "event-queue-size %ld",
2053 &vcl_cfg->event_queue_size))
2054 {
2055 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002056 clib_warning ("VCL<%d>: configured event_queue_size "
2057 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002058 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002059 vcl_cfg->event_queue_size);
2060 }
2061 else if (unformat (line_input, "listen-queue-size 0x%lx",
2062 &vcl_cfg->listen_queue_size))
2063 {
2064 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002065 clib_warning ("VCL<%d>: configured listen_queue_size "
2066 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002067 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002068 vcl_cfg->listen_queue_size);
2069 }
2070 else if (unformat (line_input, "listen-queue-size %ld",
2071 &vcl_cfg->listen_queue_size))
2072 {
2073 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002074 clib_warning ("VCL<%d>: configured listen_queue_size "
2075 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002076 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002077 vcl_cfg->listen_queue_size);
2078 }
2079 else if (unformat (line_input, "app-timeout %f",
2080 &vcl_cfg->app_timeout))
2081 {
2082 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002083 clib_warning ("VCL<%d>: configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002084 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002085 }
2086 else if (unformat (line_input, "session-timeout %f",
2087 &vcl_cfg->session_timeout))
2088 {
2089 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 clib_warning ("VCL<%d>: configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002091 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002092 }
2093 else if (unformat (line_input, "accept-timeout %f",
2094 &vcl_cfg->accept_timeout))
2095 {
2096 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002097 clib_warning ("VCL<%d>: configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002098 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002099 }
Dave Wallace774169b2017-11-01 20:07:40 -04002100 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002101 {
Dave Wallace774169b2017-11-01 20:07:40 -04002102 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002103 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002104 clib_warning ("VCL<%d>: configured "
2105 "app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002106 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002107 }
Dave Wallace774169b2017-11-01 20:07:40 -04002108 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002109 {
Dave Wallace774169b2017-11-01 20:07:40 -04002110 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002111 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002112 clib_warning ("VCL<%d>: configured "
2113 "app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002114 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04002115 }
Dave Wallace774169b2017-11-01 20:07:40 -04002116 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04002117 {
Dave Wallace774169b2017-11-01 20:07:40 -04002118 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002119 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002120 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002121 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04002122 }
2123 else if (unformat (line_input, "app-scope-global"))
2124 {
2125 vcl_cfg->app_scope_global = 1;
2126 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002127 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002128 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04002129 }
2130 else if (unformat (line_input, "namespace-secret %lu",
2131 &vcl_cfg->namespace_secret))
2132 {
2133 if (VPPCOM_DEBUG > 0)
2134 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05002135 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002136 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04002137 vcl_cfg->namespace_secret);
2138 }
2139 else if (unformat (line_input, "namespace-id %v",
2140 &vcl_cfg->namespace_id))
2141 {
2142 vl_api_application_attach_t *mp;
2143 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2144 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2145 if (nsid_vec_len > max_nsid_vec_len)
2146 {
2147 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2148 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002149 clib_warning ("VCL<%d>: configured namespace_id is "
2150 "too long, truncated to %d characters!",
2151 getpid (), max_nsid_vec_len);
Dave Wallace8af20542017-10-26 03:29:30 -04002152 }
2153
2154 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002155 clib_warning ("VCL<%d>: configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002156 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04002157 }
Dave Wallace543852a2017-08-03 02:11:34 -04002158 else if (unformat (line_input, "}"))
2159 {
2160 vc_cfg_input = 0;
2161 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002162 clib_warning ("VCL<%d>: completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002163 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002164 goto input_done;
2165 }
2166 else
2167 {
2168 if (line_input->buffer[line_input->index] != '#')
2169 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002170 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002171 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04002172 &line_input->buffer[line_input->index]);
2173 }
2174 }
2175 }
2176 }
2177
2178input_done:
2179 unformat_free (input);
2180
2181file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04002182 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04002183 close (fd);
2184}
2185
2186/*
2187 * VPPCOM Public API functions
2188 */
2189int
2190vppcom_app_create (char *app_name)
2191{
Dave Wallace543852a2017-08-03 02:11:34 -04002192 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2193 u8 *heap;
2194 mheap_t *h;
2195 int rv;
2196
2197 if (!vcm->init)
2198 {
2199 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04002200 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002201
2202 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002203 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05002204 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2205 if (env_var_str)
2206 {
2207 u32 tmp;
2208 if (sscanf (env_var_str, "%u", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002209 clib_warning ("VCL<%d>: Invalid debug level specified in "
Dave Wallace498b3a52017-11-09 13:00:34 -05002210 "the environment variable "
2211 VPPCOM_ENV_DEBUG
2212 " (%s)!\n", getpid (), env_var_str);
2213 else
2214 {
2215 vcm->debug = tmp;
Dave Wallace048b1d62018-01-03 22:24:41 -05002216 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
Dave Wallace498b3a52017-11-09 13:00:34 -05002217 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2218 }
2219 }
Dave Wallace8af20542017-10-26 03:29:30 -04002220 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04002221 if (!conf_fname)
2222 {
2223 conf_fname = VPPCOM_CONF_DEFAULT;
2224 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002225 clib_warning ("VCL<%d>: getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04002226 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04002227 }
2228 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05002229 clib_fifo_validate (vcm->client_session_index_fifo,
2230 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04002231 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04002232 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2233 if (env_var_str)
2234 {
2235 u32 ns_id_vec_len = strlen (env_var_str);
2236
2237 vec_reset_length (vcm->cfg.namespace_id);
2238 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2239 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2240
2241 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002242 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002243 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04002244 vcm->cfg.namespace_id);
2245 }
2246 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2247 if (env_var_str)
2248 {
2249 u64 tmp;
2250 if (sscanf (env_var_str, "%lu", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002251 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
Dave Wallace8af20542017-10-26 03:29:30 -04002252 "the environment variable "
2253 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05002254 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04002255 else
2256 {
2257 vcm->cfg.namespace_secret = tmp;
2258 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002259 clib_warning ("VCL<%d>: configured namespace secret "
2260 "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2261 getpid (), vcm->cfg.namespace_secret);
Dave Wallace8af20542017-10-26 03:29:30 -04002262 }
2263 }
Dave Wallace774169b2017-11-01 20:07:40 -04002264 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04002265 {
Dave Wallace774169b2017-11-01 20:07:40 -04002266 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002267 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002268 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2269 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2270 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002271 }
Dave Wallace774169b2017-11-01 20:07:40 -04002272 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04002273 {
Dave Wallace774169b2017-11-01 20:07:40 -04002274 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002275 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002276 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2277 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2278 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
Dave Wallace774169b2017-11-01 20:07:40 -04002279 }
2280 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2281 {
2282 vcm->cfg.app_scope_local = 1;
2283 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002284 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002285 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002286 vcm->cfg.app_scope_local);
2287 }
2288 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2289 {
2290 vcm->cfg.app_scope_global = 1;
2291 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002292 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002293 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002294 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04002295 }
2296
Dave Wallace543852a2017-08-03 02:11:34 -04002297 vcm->main_cpu = os_get_thread_index ();
2298 heap = clib_mem_get_per_cpu_heap ();
2299 h = mheap_header (heap);
2300
2301 /* make the main heap thread-safe */
2302 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2303
2304 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2305
2306 clib_time_init (&vcm->clib_time);
2307 vppcom_init_error_string_table ();
2308 svm_fifo_segment_init (vcl_cfg->segment_baseva,
2309 20 /* timeout in secs */ );
2310 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002311 }
2312
2313 if (vcm->my_client_index == ~0)
2314 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002315 vppcom_api_hookup ();
Dave Wallace543852a2017-08-03 02:11:34 -04002316 vcm->app_state = STATE_APP_START;
2317 rv = vppcom_connect_to_vpp (app_name);
2318 if (rv)
2319 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002320 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002321 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002322 return rv;
2323 }
2324
2325 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002326 clib_warning ("VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002327
Dave Wallace048b1d62018-01-03 22:24:41 -05002328 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -04002329 if (rv)
2330 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002331 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2332 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002333 return rv;
2334 }
Dave Wallace543852a2017-08-03 02:11:34 -04002335
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002336 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002337 clib_warning ("VCL<%d>: sending app attach", getpid ());
2338
2339 rv = vppcom_app_attach ();
2340 if (rv)
2341 {
2342 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2343 getpid ());
2344 return rv;
2345 }
2346
2347 if (VPPCOM_DEBUG > 0)
2348 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002349 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002350 vcm->my_client_index);
2351 }
Dave Wallace543852a2017-08-03 02:11:34 -04002352
2353 return VPPCOM_OK;
2354}
2355
2356void
2357vppcom_app_destroy (void)
2358{
Dave Wallace543852a2017-08-03 02:11:34 -04002359 int rv;
2360
2361 if (vcm->my_client_index == ~0)
2362 return;
2363
2364 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002365 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002366 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002367
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002368 if (VPPCOM_DEBUG > 0)
2369 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002370 /* *INDENT-OFF* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002371 ELOG_TYPE_DECLARE (e) =
2372 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002373 .format = "app_detach:C:%d",
2374 .format_args = "i4",
2375 };
2376
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002377 struct
2378 {
2379 u32 data;
2380 } *ed;
2381 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2382 ed->data = vcm->my_client_index;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002383 /* *INDENT-ON* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002384 }
2385
Dave Wallace543852a2017-08-03 02:11:34 -04002386 vppcom_app_detach ();
2387 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2388 if (PREDICT_FALSE (rv))
2389 {
2390 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002391 clib_warning ("VCL<%d>: application detach timed out! "
2392 "returning %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05002393 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -04002394 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002395
2396 /* Finished with logging before client gets reset to ~0 */
2397 if (VPPCOM_DEBUG > 0)
2398 write_elog ();
2399
Dave Wallace543852a2017-08-03 02:11:34 -04002400 vl_client_disconnect_from_vlib ();
2401 vcm->my_client_index = ~0;
2402 vcm->app_state = STATE_APP_START;
2403}
2404
2405int
2406vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2407{
Dave Wallace543852a2017-08-03 02:11:34 -04002408 session_t *session;
2409 u32 session_index;
2410
2411 clib_spinlock_lock (&vcm->sessions_lockp);
2412 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002413 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002414 session_index = session - vcm->sessions;
2415
2416 session->vrf = vrf;
2417 session->proto = proto;
2418 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002419 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002420 session->vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002421 clib_spinlock_unlock (&vcm->sessions_lockp);
2422
2423 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002424 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002425
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002426 if (VPPCOM_DEBUG > 0)
2427 {
2428 session->elog_track.name =
2429 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2430 session_index, 0);
2431 elog_track_register (&vcm->elog_main, &session->elog_track);
2432
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002433 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002434 ELOG_TYPE_DECLARE (e) =
2435 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002436 .format = "session_create:vrf:%d proto:%d state:%d is_nonblocking:%d",
2437 .format_args = "i4i4i4i4",
2438 };
2439
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002440 struct
2441 {
2442 u32 data[4];
2443 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002444
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002445 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2446 ed->data[0] = session->vrf;
2447 ed->data[1] = session->proto;
2448 ed->data[2] = session->state;
2449 ed->data[3] = session->is_nonblocking;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002450 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002451 }
2452
Dave Wallace543852a2017-08-03 02:11:34 -04002453 return (int) session_index;
2454}
2455
2456int
2457vppcom_session_close (uint32_t session_index)
2458{
Dave Wallace543852a2017-08-03 02:11:34 -04002459 session_t *session = 0;
2460 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002461 u8 is_listen;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002462 u8 is_vep;
2463 u8 is_vep_session;
2464 u32 next_sid;
2465 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -05002466 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002467 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002468 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002469
Dave Wallace4878cbe2017-11-21 03:45:09 -05002470 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002471 is_listen = session->is_listen;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002472 is_vep = session->is_vep;
2473 is_vep_session = session->is_vep_session;
2474 next_sid = session->vep.next_sid;
2475 vep_idx = session->vep.vep_idx;
2476 state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05002477 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002478 clib_spinlock_unlock (&vcm->sessions_lockp);
2479
2480 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002481 {
2482 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002483 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2484 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002485 getpid (), session_index, session_index);
2486 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002487 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2488 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002489 getpid (), vpp_handle, session_index);
2490 }
Dave Wallace543852a2017-08-03 02:11:34 -04002491
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002492 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002493 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002494 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002495 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002496 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallaceee45d412017-11-24 21:44:06 -05002497 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002498 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2499 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2500 getpid (), vpp_handle, next_sid, vep_idx,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002501 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002502
Dave Wallace4878cbe2017-11-21 03:45:09 -05002503 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002504 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505 clib_spinlock_unlock (&vcm->sessions_lockp);
2506 }
2507 }
2508 else
2509 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002510 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002511 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002512 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2513 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002514 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2515 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05002516 getpid (), vpp_handle, session_index,
2517 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002518 }
2519
Dave Wallace4878cbe2017-11-21 03:45:09 -05002520 if (is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002522 if (state == STATE_LISTEN)
2523 {
2524 rv = vppcom_session_unbind (session_index);
2525 if (PREDICT_FALSE (rv < 0))
2526 {
2527 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002528 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002529 "listener unbind failed! rv %d (%s)",
2530 getpid (), vpp_handle, session_index,
2531 rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002532 }
2533 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002534 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002535
2536 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002537 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002538 rv = vppcom_session_disconnect (session_index);
2539 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002540 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002541 "session disconnect failed! rv %d (%s)",
2542 getpid (), vpp_handle, session_index,
2543 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002544 }
Dave Wallace19481612017-09-15 18:47:44 -04002545 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002546
2547 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002548 vpp_handle = session->vpp_handle;
2549 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002550 {
Dave Wallaceee45d412017-11-24 21:44:06 -05002551 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002552 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -05002553 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002554 }
Dave Wallace543852a2017-08-03 02:11:34 -04002555 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002556 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002557
2558 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002559 {
2560 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002561 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002562 getpid (), session_index, session_index);
2563 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002564 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002565 getpid (), vpp_handle, session_index);
2566 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002567done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002568
2569 if (VPPCOM_DEBUG > 0)
2570 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002571 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002572 ELOG_TYPE_DECLARE (e) =
2573 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002574 .format = "session_close:rv:%d",
2575 .format_args = "i4",
2576 };
2577
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002578 struct
2579 {
2580 u32 data;
2581 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002582
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002583 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2584 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002585 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002586 }
2587
Dave Wallace543852a2017-08-03 02:11:34 -04002588 return rv;
2589}
2590
2591int
2592vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2593{
Dave Wallace543852a2017-08-03 02:11:34 -04002594 session_t *session = 0;
2595 int rv;
2596
2597 if (!ep || !ep->ip)
2598 return VPPCOM_EINVAL;
2599
Dave Wallace4878cbe2017-11-21 03:45:09 -05002600 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04002601
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002602 if (session->is_vep)
2603 {
2604 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002605 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2606 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002607 rv = VPPCOM_EBADFD;
2608 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002609 }
2610
Dave Wallace543852a2017-08-03 02:11:34 -04002611 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002612 session->lcl_addr.is_ip4 = ep->is_ip4;
2613 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002614 session->lcl_port = ep->port;
2615
2616 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002617 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
Dave Wallaceee45d412017-11-24 21:44:06 -05002618 "port %u, proto %s", getpid (), session_index,
2619 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2620 format_ip46_address, &session->lcl_addr.ip46,
2621 session->lcl_addr.is_ip4,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002622 clib_net_to_host_u16 (session->lcl_port),
2623 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002624
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002625 if (VPPCOM_DEBUG > 0)
2626 {
2627 if (session->lcl_addr.is_ip4)
2628 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002629 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002630 ELOG_TYPE_DECLARE (e) =
2631 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002632 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2633 .format_args = "t1i1i1i1i1i2",
2634 .n_enum_strings = 2,
2635 .enum_strings = {"TCP", "UDP",},
2636 };
2637
2638 CLIB_PACKED (struct {
2639 u8 proto;
2640 u8 addr[4];
2641 u16 port;
2642 }) * ed;
2643
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002644 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2645 ed->proto = session->proto;
2646 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2647 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2648 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2649 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2650 ed->port = clib_net_to_host_u16 (session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002651 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002652 }
2653 }
2654
Dave Wallace543852a2017-08-03 02:11:34 -04002655 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002656done:
2657 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002658}
2659
2660int
Dave Wallace33e002b2017-09-06 01:20:02 -04002661vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002662{
Dave Wallace33e002b2017-09-06 01:20:02 -04002663 session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002664 u64 listen_vpp_handle;
2665 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -04002666
Dave Wallace4878cbe2017-11-21 03:45:09 -05002667 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002668
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002669 if (listen_session->is_vep)
2670 {
2671 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002672 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002673 "epoll session!", getpid (), listen_session_index);
2674 rv = VPPCOM_EBADFD;
2675 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002676 }
2677
Dave Wallaceee45d412017-11-24 21:44:06 -05002678 listen_vpp_handle = listen_session->vpp_handle;
Dave Wallacee695cb42017-11-02 22:04:42 -04002679 if (listen_session->is_listen)
2680 {
2681 clib_spinlock_unlock (&vcm->sessions_lockp);
2682 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002683 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002684 "already in listen state!",
2685 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002686 rv = VPPCOM_OK;
2687 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -04002688 }
2689
Dave Wallace543852a2017-08-03 02:11:34 -04002690 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002691 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2692 "sid %u: sending bind request...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002693 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002694
Dave Wallace4878cbe2017-11-21 03:45:09 -05002695 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002696 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallaceee45d412017-11-24 21:44:06 -05002697 retval =
Dave Wallace33e002b2017-09-06 01:20:02 -04002698 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2699 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002700
Dave Wallace4878cbe2017-11-21 03:45:09 -05002701 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002702 if (PREDICT_FALSE (retval))
2703 {
2704 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002705 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002706 "returning %d (%s)", getpid (),
2707 listen_session->vpp_handle, listen_session_index,
2708 retval, vppcom_retval_str (retval));
2709 clib_spinlock_unlock (&vcm->sessions_lockp);
2710 rv = retval;
2711 goto done;
2712 }
2713
Dave Wallace543852a2017-08-03 02:11:34 -04002714 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002716done:
2717 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002718}
2719
2720int
2721vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05002722 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04002723{
Dave Wallace33e002b2017-09-06 01:20:02 -04002724 session_t *listen_session = 0;
2725 session_t *client_session = 0;
Dave Wallace227867f2017-11-13 21:21:53 -05002726 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002727 int rv;
2728 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002729 char *cut_thru_str;
Dave Wallaceee45d412017-11-24 21:44:06 -05002730 u64 listen_vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002731
Dave Wallace4878cbe2017-11-21 03:45:09 -05002732 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002733
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002734 if (listen_session->is_vep)
2735 {
2736 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002737 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002738 "epoll session!", getpid (), listen_session_index);
2739 rv = VPPCOM_EBADFD;
2740 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002741 }
2742
Dave Wallaceee45d412017-11-24 21:44:06 -05002743 listen_vpp_handle = listen_session->vpp_handle;
Dave Wallace33e002b2017-09-06 01:20:02 -04002744 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002745 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002746 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002747 "not in listen state! state 0x%x (%s)", getpid (),
2748 listen_vpp_handle, listen_session_index,
2749 listen_session->state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002750 vppcom_session_state_str (listen_session->state));
Dave Wallaceee45d412017-11-24 21:44:06 -05002751 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002752 rv = VPPCOM_EBADFD;
2753 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002754 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002755 wait_for = (listen_session->is_nonblocking) ? 0 : vcm->cfg.accept_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002756
Dave Wallace543852a2017-08-03 02:11:34 -04002757 clib_spinlock_unlock (&vcm->sessions_lockp);
2758
2759 while (1)
2760 {
2761 rv = vppcom_wait_for_client_session_index (wait_for);
2762 if (rv)
2763 {
2764 if ((VPPCOM_DEBUG > 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002765 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2766 "accept failed! returning %d (%s)", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05002767 listen_vpp_handle, listen_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002768 rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05002769 if (wait_for == 0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002770 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002771 }
2772 else
2773 break;
2774 }
2775
Dave Wallace543852a2017-08-03 02:11:34 -04002776 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002778 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002779 if (PREDICT_FALSE (rv))
2780 {
2781 rv = VPPCOM_ECONNABORTED;
Dave Wallace048b1d62018-01-03 22:24:41 -05002782 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002783 "lookup failed! returning %d (%s)", getpid (),
2784 listen_vpp_handle, listen_session_index,
2785 client_session_index, rv, vppcom_retval_str (rv));
2786 goto done;
2787 }
Dave Wallace543852a2017-08-03 02:11:34 -04002788
Dave Wallace227867f2017-11-13 21:21:53 -05002789 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002790 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002791 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002792 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2793 getpid (), listen_vpp_handle, listen_session_index,
2794 client_session->vpp_handle, client_session_index,
Dave Wallace227867f2017-11-13 21:21:53 -05002795 flags, client_session->is_nonblocking);
Dave Wallace543852a2017-08-03 02:11:34 -04002796
Dave Wallace048b1d62018-01-03 22:24:41 -05002797 if (ep)
2798 {
2799 ep->vrf = client_session->vrf;
2800 ep->is_cut_thru = client_session->is_cut_thru;
2801 ep->is_ip4 = client_session->peer_addr.is_ip4;
2802 ep->port = client_session->peer_port;
2803 if (client_session->peer_addr.is_ip4)
2804 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2805 sizeof (ip4_address_t));
2806 else
2807 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2808 sizeof (ip6_address_t));
2809 }
Dave Wallace60caa062017-11-10 17:07:13 -05002810
2811 if (client_session->is_server && client_session->is_cut_thru)
2812 {
2813 static svm_fifo_segment_create_args_t _a;
2814 svm_fifo_segment_create_args_t *a = &_a;
2815 svm_fifo_segment_private_t *seg;
2816
Dave Wallace4878cbe2017-11-21 03:45:09 -05002817 cut_thru_str = " cut-thru ";
Dave Wallace60caa062017-11-10 17:07:13 -05002818
2819 /* Create the segment */
2820 memset (a, 0, sizeof (*a));
2821 a->segment_name = (char *)
2822 format ((u8 *) a->segment_name, "%d:segment%d%c",
2823 getpid (), vcm->unique_segment_index++, 0);
2824 a->segment_size = vcm->cfg.segment_size;
2825 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2826 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2827 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2828
2829 rv = svm_fifo_segment_create (a);
2830 if (PREDICT_FALSE (rv))
2831 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002832 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002833 "client sid %u svm_fifo_segment_create ('%s') "
2834 "failed! rv %d", getpid (), listen_vpp_handle,
2835 listen_session_index, client_session_index,
2836 a->segment_name, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002837 vec_reset_length (a->new_segment_indices);
2838 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002839 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002840 client_session_index,
2841 client_session->vpp_handle,
2842 client_session->client_context,
2843 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002844 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002845 rv = VPPCOM_ENOMEM;
2846 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002847 }
2848
2849 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2850 client_session->sm_seg_index = a->new_segment_indices[0];
2851 vec_free (a->new_segment_indices);
2852
2853 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2854 client_session->server_rx_fifo =
2855 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2856 FIFO_SEGMENT_RX_FREELIST);
2857 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2858 {
2859 svm_fifo_segment_delete (seg);
Dave Wallace048b1d62018-01-03 22:24:41 -05002860 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002861 "client sid %u rx fifo alloc failed! "
2862 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2863 listen_session_index, client_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002864 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
Dave Wallace60caa062017-11-10 17:07:13 -05002865 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002866 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002867 client_session_index,
2868 client_session->vpp_handle,
2869 client_session->client_context,
2870 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002871 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002872 rv = VPPCOM_ENOMEM;
2873 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002874 }
2875 client_session->server_rx_fifo->master_session_index =
2876 client_session_index;
2877
2878 client_session->server_tx_fifo =
2879 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2880 FIFO_SEGMENT_TX_FREELIST);
2881 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2882 {
2883 svm_fifo_segment_delete (seg);
Dave Wallace048b1d62018-01-03 22:24:41 -05002884 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002885 "client sid %u tx fifo alloc failed! "
2886 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2887 listen_session_index, client_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002888 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
Dave Wallace60caa062017-11-10 17:07:13 -05002889 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002890 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002891 client_session_index,
2892 client_session->vpp_handle,
2893 client_session->client_context,
2894 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002895 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002896 rv = VPPCOM_ENOMEM;
2897 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002898 }
2899 client_session->server_tx_fifo->master_session_index =
2900 client_session_index;
2901
2902 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002903 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002904 "created segment '%s', rx_fifo %p, tx_fifo %p",
2905 getpid (), listen_vpp_handle, listen_session_index,
2906 client_session_index, client_session->segment_name,
Dave Wallace60caa062017-11-10 17:07:13 -05002907 client_session->server_rx_fifo,
2908 client_session->server_tx_fifo);
2909
2910#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2911 {
2912 void *oldheap;
2913 ssvm_shared_header_t *sh = seg->ssvm.sh;
2914
2915 ssvm_lock_non_recursive (sh, 1);
2916 oldheap = ssvm_push_heap (sh);
2917 event_q = client_session->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -08002918 svm_queue_init (vcm->cfg.event_queue_size,
2919 sizeof (session_fifo_event_t),
2920 getpid (), 0 /* signal not sent */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002921 ssvm_pop_heap (oldheap);
2922 ssvm_unlock_non_recursive (sh);
2923 }
2924#endif
Dave Wallaceee45d412017-11-24 21:44:06 -05002925 vppcom_send_connect_session_reply (client_session,
2926 client_session_index,
Dave Wallaced2931962017-11-25 04:17:39 -05002927 client_session->vpp_handle,
2928 client_session->client_context,
Dave Wallaceee45d412017-11-24 21:44:06 -05002929 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002930 }
2931 else
2932 {
2933 cut_thru_str = " ";
Dave Wallaced2931962017-11-25 04:17:39 -05002934 vppcom_send_accept_session_reply (client_session->vpp_handle,
2935 client_session->client_context,
2936 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002937 }
2938
Stevenac1f96d2017-10-24 16:03:58 -07002939 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002940 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
Dave Wallaceee45d412017-11-24 21:44:06 -05002941 "0x%llx, sid %u%sconnection to local %s address "
2942 "%U port %u", getpid (), listen_vpp_handle,
2943 listen_session_index, client_session->vpp_handle,
2944 client_session_index, cut_thru_str,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002945 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2946 format_ip46_address, &client_session->lcl_addr.ip46,
2947 client_session->lcl_addr.is_ip4,
2948 clib_net_to_host_u16 (client_session->lcl_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002949
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002950 if (VPPCOM_DEBUG > 0)
2951 {
2952 client_session->elog_track.name =
2953 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2954 client_session_index, 0);
2955 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2956
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002957 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002958 ELOG_TYPE_DECLARE (e) =
2959 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002960 .format = "accept cut-thru: listen_handle:%x from_handle:%x",
2961 .format_args = "i8i8",
2962 };
2963
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002964 struct
2965 {
2966 u64 handle[2];
2967 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002968
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002969 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2970 ed->handle[0] = listen_vpp_handle;
2971 ed->handle[1] = client_session->vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002972 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002973
2974 if (client_session->lcl_addr.is_ip4)
2975 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002976 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002977 ELOG_TYPE_DECLARE (e2) =
2978 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002979 .format = "accept cut-thru: S:%d %d.%d.%d.%d:%d ",
2980 .format_args = "i4i1i1i1i1i2",
2981 };
2982
2983 CLIB_PACKED (struct {
2984 u32 session;
2985 u8 addr[4];
2986 u16 port;
2987 }) * ed2;
2988
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002989 ed2 =
2990 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2991 ed2->session = client_session_index;
2992 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2993 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2994 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2995 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2996 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002997 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002998 }
2999 }
3000
Dave Wallace543852a2017-08-03 02:11:34 -04003001 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003002 rv = (int) client_session_index;
3003done:
3004 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003005}
3006
3007int
3008vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
3009{
Dave Wallace543852a2017-08-03 02:11:34 -04003010 session_t *session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05003011 int rv, retval = VPPCOM_OK;
3012 u64 vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04003013
Dave Wallace4878cbe2017-11-21 03:45:09 -05003014 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3015
3016 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 "connect on 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 Wallaceee45d412017-11-24 21:44:06 -05003025 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003026 if (PREDICT_FALSE (session->is_server))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003027 {
3028 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003029 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: is in use "
Dave Wallaceee45d412017-11-24 21:44:06 -05003030 "as a server session!", getpid (), vpp_handle,
3031 session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003032 rv = VPPCOM_EBADFD;
3033 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003034 }
3035
Dave Wallace4878cbe2017-11-21 03:45:09 -05003036 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04003037 {
Dave Wallace543852a2017-08-03 02:11:34 -04003038 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003039 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
Dave Wallaceee45d412017-11-24 21:44:06 -05003040 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3041 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003042 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3043 format_ip46_address,
3044 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3045 clib_net_to_host_u16 (session->peer_port),
3046 session->proto ? "UDP" : "TCP", session->state,
3047 vppcom_session_state_str (session->state));
3048
3049 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003050 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003051 }
3052
3053 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04003054 session->peer_addr.is_ip4 = server_ep->is_ip4;
3055 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07003056 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04003057
3058 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003059 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
Dave Wallaceee45d412017-11-24 21:44:06 -05003060 "%s %U port %d proto %s",
3061 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003062 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3063 format_ip46_address,
3064 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3065 clib_net_to_host_u16 (session->peer_port),
3066 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04003067
3068 vppcom_send_connect_sock (session, session_index);
3069 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003070
Dave Wallaceee45d412017-11-24 21:44:06 -05003071 retval =
3072 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3073 vcm->cfg.session_timeout);
3074
3075 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3076 vpp_handle = session->vpp_handle;
3077 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace7876d392017-10-19 03:53:57 -04003078
Dave Wallace4878cbe2017-11-21 03:45:09 -05003079done:
Dave Wallaceee45d412017-11-24 21:44:06 -05003080 if (PREDICT_FALSE (retval))
3081 {
3082 rv = retval;
3083 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003084 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
Dave Wallaceee45d412017-11-24 21:44:06 -05003085 "returning %d (%s)", getpid (), vpp_handle,
3086 session_index, rv, vppcom_retval_str (rv));
3087 }
3088 else if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003089 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Dave Wallaceee45d412017-11-24 21:44:06 -05003090 getpid (), vpp_handle, session_index);
3091
Dave Wallace4878cbe2017-11-21 03:45:09 -05003092 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003093}
3094
Steven58f464e2017-10-25 12:33:12 -07003095static inline int
3096vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3097 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04003098{
Dave Wallace543852a2017-08-03 02:11:34 -04003099 session_t *session = 0;
3100 svm_fifo_t *rx_fifo;
3101 int n_read = 0;
3102 int rv;
3103 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003104 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003105 session_state_t state;
3106 u8 is_server;
3107 u8 is_nonblocking;
Dave Wallaceee45d412017-11-24 21:44:06 -05003108 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003109
3110 ASSERT (buf);
3111
Dave Wallace4878cbe2017-11-21 03:45:09 -05003112 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3113
3114 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003115 {
3116 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003117 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3118 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003119 rv = VPPCOM_EBADFD;
3120 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003121 }
3122
Dave Wallaceee45d412017-11-24 21:44:06 -05003123 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003124 is_server = session->is_server;
3125 is_nonblocking = session->is_nonblocking;
3126 state = session->state;
3127 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003128 {
3129 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003130 rv = ((state == STATE_DISCONNECT) ?
3131 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3132
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003133 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003134 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003135 "not open! state 0x%x (%s), returning %d (%s)",
3136 getpid (), vpp_handle, session_index,
3137 is_server ? "server" : "client", state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003138 vppcom_session_state_str (state),
3139 rv, vppcom_retval_str (rv));
3140 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003141 }
3142
Dave Wallace4878cbe2017-11-21 03:45:09 -05003143 rx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003144 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003145 fifo_str = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003146 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003147 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003148
3149 do
3150 {
Steven58f464e2017-10-25 12:33:12 -07003151 if (peek)
3152 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3153 else
3154 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003155 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003156 while (!is_nonblocking && (n_read <= 0));
Dave Wallacef7f809c2017-10-03 01:48:42 -04003157
Dave Wallace4878cbe2017-11-21 03:45:09 -05003158 if (n_read <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003159 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003160 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3161
3162 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3163 (EPOLLET | EPOLLIN));
3164 if (poll_et)
3165 session->vep.et_mask |= EPOLLIN;
3166
3167 if (state == STATE_CLOSE_ON_EMPTY)
3168 {
3169 session_state_t new_state = STATE_DISCONNECT;
3170 rv = VPPCOM_ECONNRESET;
3171
3172 if (VPPCOM_DEBUG > 1)
3173 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003174 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
Dave Wallaceee45d412017-11-24 21:44:06 -05003175 "with %s session state 0x%x (%s)!"
3176 " Setting state to 0x%x (%s), returning %d (%s)",
3177 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003178 is_server ? "server" : "client",
3179 state, vppcom_session_state_str (state),
3180 new_state, vppcom_session_state_str (new_state),
3181 rv, vppcom_retval_str (rv));
3182 }
3183
3184 session->state = new_state;
3185 }
3186 else
3187 rv = VPPCOM_EAGAIN;
3188
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003189 clib_spinlock_unlock (&vcm->sessions_lockp);
3190 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003191 else
3192 rv = n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04003193
Dave Wallace4878cbe2017-11-21 03:45:09 -05003194 if (VPPCOM_DEBUG > 2)
3195 {
3196 if (rv > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003197 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Dave Wallaceee45d412017-11-24 21:44:06 -05003198 "from %s (%p)", getpid (), vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003199 session_index, n_read, fifo_str, rx_fifo);
3200 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003201 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Dave Wallaceee45d412017-11-24 21:44:06 -05003202 "returning %d (%s)", getpid (), vpp_handle,
3203 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003204 }
3205done:
3206 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003207}
3208
Steven58f464e2017-10-25 12:33:12 -07003209int
Dave Wallace048b1d62018-01-03 22:24:41 -05003210vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07003211{
3212 return (vppcom_session_read_internal (session_index, buf, n, 0));
3213}
3214
3215static int
3216vppcom_session_peek (uint32_t session_index, void *buf, int n)
3217{
3218 return (vppcom_session_read_internal (session_index, buf, n, 1));
3219}
3220
Dave Wallace543852a2017-08-03 02:11:34 -04003221static inline int
3222vppcom_session_read_ready (session_t * session, u32 session_index)
3223{
Dave Wallace498b3a52017-11-09 13:00:34 -05003224 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04003225 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05003226 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003227 int rv;
3228 u8 is_server = session->is_server;
3229 session_state_t state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05003230 u64 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003231
3232 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003233 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003234 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003235 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003236 "epoll session!", getpid (), session_index);
3237 rv = VPPCOM_EBADFD;
3238 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003239 }
Dave Wallace33e002b2017-09-06 01:20:02 -04003240
3241 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04003242 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3243 else
3244 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003245 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3246 {
3247 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3248 VPPCOM_ENOTCONN);
3249
3250 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003251 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003252 "not open! state 0x%x (%s), returning %d (%s)",
3253 getpid (), vpp_handle, session_index,
3254 is_server ? "server" : "client",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003255 state, vppcom_session_state_str (state),
3256 rv, vppcom_retval_str (rv));
3257 goto done;
3258 }
3259
3260 rx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003261 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003262
Dave Wallace33e002b2017-09-06 01:20:02 -04003263 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003264 }
3265
Dave Wallace4878cbe2017-11-21 03:45:09 -05003266 if (ready == 0)
Dave Wallace60caa062017-11-10 17:07:13 -05003267 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003268 poll_et =
3269 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3270 if (poll_et)
3271 session->vep.et_mask |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003272
Dave Wallace4878cbe2017-11-21 03:45:09 -05003273 if (state == STATE_CLOSE_ON_EMPTY)
3274 {
3275 rv = VPPCOM_ECONNRESET;
3276 session_state_t new_state = STATE_DISCONNECT;
3277
3278 if (VPPCOM_DEBUG > 1)
3279 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003280 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3281 "sid %u: Empty fifo with"
Dave Wallaceee45d412017-11-24 21:44:06 -05003282 " %s session state 0x%x (%s)! Setting state to "
3283 "0x%x (%s), returning %d (%s)",
3284 getpid (), session_index, vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003285 is_server ? "server" : "client",
3286 state, vppcom_session_state_str (state),
3287 new_state, vppcom_session_state_str (new_state),
3288 rv, vppcom_retval_str (rv));
3289 }
3290 session->state = new_state;
3291 goto done;
3292 }
3293 }
3294 rv = ready;
Dave Wallace16cb4082017-11-29 03:24:06 -05003295
3296 if (vcm->app_event_queue->cursize &&
3297 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3298 {
3299 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3300 session_fifo_event_t e;
3301
3302 for (i = 0; i < n_to_dequeue; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -08003303 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
Dave Wallace16cb4082017-11-29 03:24:06 -05003304
3305 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3306 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003307done:
3308 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003309}
3310
3311int
Dave Wallace048b1d62018-01-03 22:24:41 -05003312vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04003313{
Dave Wallace543852a2017-08-03 02:11:34 -04003314 session_t *session = 0;
3315 svm_fifo_t *tx_fifo;
Florin Corase86a8ed2018-01-05 03:20:25 -08003316 svm_queue_t *q;
Dave Wallace543852a2017-08-03 02:11:34 -04003317 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003318 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04003319 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003320 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003321 u8 is_server;
3322 u8 is_nonblocking;
3323 session_state_t state;
Dave Wallaceee45d412017-11-24 21:44:06 -05003324 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003325
3326 ASSERT (buf);
3327
Dave Wallace4878cbe2017-11-21 03:45:09 -05003328 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3329
3330 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003331 {
3332 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003333 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003334 "cannot write to an epoll session!",
3335 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003336
3337 rv = VPPCOM_EBADFD;
3338 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003339 }
3340
Dave Wallace4878cbe2017-11-21 03:45:09 -05003341 is_server = session->is_server;
3342 is_nonblocking = session->is_nonblocking;
Dave Wallaceee45d412017-11-24 21:44:06 -05003343 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003344 state = session->state;
3345 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003346 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003347 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3348 VPPCOM_ENOTCONN);
3349
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003350 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003351 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003352 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003353 "%s session is not open! state 0x%x (%s)",
3354 getpid (), vpp_handle, session_index,
3355 is_server ? "server" : "client", state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003356 vppcom_session_state_str (state));
3357 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003358 }
3359
Dave Wallace4878cbe2017-11-21 03:45:09 -05003360 tx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace543852a2017-08-03 02:11:34 -04003361 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003362 fifo_str = ((!session->is_cut_thru || is_server) ?
Dave Wallace543852a2017-08-03 02:11:34 -04003363 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003364 clib_spinlock_unlock (&vcm->sessions_lockp);
3365
Dave Wallace543852a2017-08-03 02:11:34 -04003366 do
3367 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003368 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
Dave Wallace543852a2017-08-03 02:11:34 -04003369 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003370 while (!is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04003371
3372 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04003373 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04003374 {
Dave Wallace543852a2017-08-03 02:11:34 -04003375 /* Fabricate TX event, send to vpp */
3376 evt.fifo = tx_fifo;
3377 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04003378
Dave Wallace4878cbe2017-11-21 03:45:09 -05003379 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3380 q = session->vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -04003381 ASSERT (q);
Florin Corase86a8ed2018-01-05 03:20:25 -08003382 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Dave Wallace4878cbe2017-11-21 03:45:09 -05003383 clib_spinlock_unlock (&vcm->sessions_lockp);
3384 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003385 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003386 "added FIFO_EVENT_APP_TX to "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003387 "vpp_event_q %p, n_write %d", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05003388 vpp_handle, session_index, q, n_write);
Dave Wallace543852a2017-08-03 02:11:34 -04003389 }
3390
Dave Wallace4878cbe2017-11-21 03:45:09 -05003391 if (n_write <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003392 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003393 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3394
3395 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3396 (EPOLLET | EPOLLOUT));
3397 if (poll_et)
3398 session->vep.et_mask |= EPOLLOUT;
3399
3400 if (state == STATE_CLOSE_ON_EMPTY)
3401 {
3402 session_state_t new_state = STATE_DISCONNECT;
3403 rv = VPPCOM_ECONNRESET;
3404
3405 if (VPPCOM_DEBUG > 1)
3406 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003407 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003408 "Empty fifo with %s session state 0x%x (%s)!"
3409 " Setting state to 0x%x (%s), returning %d (%s)",
3410 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003411 is_server ? "server" : "client",
3412 state, vppcom_session_state_str (state),
3413 new_state, vppcom_session_state_str (new_state),
3414 rv, vppcom_retval_str (rv));
3415 }
3416
3417 session->state = new_state;
3418 }
3419 else
3420 rv = VPPCOM_EAGAIN;
3421
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003422 clib_spinlock_unlock (&vcm->sessions_lockp);
3423 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003424 else
3425 rv = n_write;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003426
Dave Wallace543852a2017-08-03 02:11:34 -04003427 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003428 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003429 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003430 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003431 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003432 session_index, fifo_str, tx_fifo);
3433 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003435 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003436 session_index, n_write, fifo_str, tx_fifo);
3437 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003438done:
3439 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003440}
3441
3442static inline int
3443vppcom_session_write_ready (session_t * session, u32 session_index)
3444{
Dave Wallace543852a2017-08-03 02:11:34 -04003445 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04003446 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003447 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05003448 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003449 int rv;
3450 u8 is_server = session->is_server;
3451 session_state_t state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04003452
3453 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003454 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003455 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003456 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003457 "cannot write to an epoll session!",
3458 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003459 rv = VPPCOM_EBADFD;
3460 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003461 }
3462
Dave Wallace4878cbe2017-11-21 03:45:09 -05003463 if (PREDICT_FALSE (session->is_listen))
Dave Wallace33e002b2017-09-06 01:20:02 -04003464 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003465 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003466 "cannot write to a listen session!",
3467 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003468 rv = VPPCOM_EBADFD;
3469 goto done;
3470 }
3471
3472 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3473 {
3474 session_state_t state = session->state;
3475
3476 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3477 VPPCOM_ENOTCONN);
3478
Dave Wallace048b1d62018-01-03 22:24:41 -05003479 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003480 "%s session is not open! state 0x%x (%s), "
3481 "returning %d (%s)", getpid (), session->vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003482 session_index, is_server ? "server" : "client",
3483 state, vppcom_session_state_str (state),
3484 rv, vppcom_retval_str (rv));
3485 goto done;
Dave Wallace33e002b2017-09-06 01:20:02 -04003486 }
3487
Dave Wallace543852a2017-08-03 02:11:34 -04003488 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3489 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04003490 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3491 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04003492
Dave Wallacef7f809c2017-10-03 01:48:42 -04003493 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003494
Dave Wallace33e002b2017-09-06 01:20:02 -04003495 if (VPPCOM_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003497 "peek %s (%p), ready = %d", getpid (),
3498 session->vpp_handle, session_index,
3499 fifo_str, tx_fifo, ready);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003500
Dave Wallace4878cbe2017-11-21 03:45:09 -05003501 if (ready == 0)
3502 {
3503 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3504 (EPOLLET | EPOLLOUT));
3505 if (poll_et)
3506 session->vep.et_mask |= EPOLLOUT;
3507
3508 if (state == STATE_CLOSE_ON_EMPTY)
3509 {
3510 rv = VPPCOM_ECONNRESET;
3511 session_state_t new_state = STATE_DISCONNECT;
3512
3513 if (VPPCOM_DEBUG > 1)
3514 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003516 "Empty fifo with %s session "
3517 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3518 "returning %d (%s)", getpid (),
3519 session->vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003520 is_server ? "server" : "client",
3521 state, vppcom_session_state_str (state),
3522 new_state, vppcom_session_state_str (new_state),
3523 rv, vppcom_retval_str (rv));
3524 }
3525 session->state = new_state;
3526 goto done;
3527 }
3528 }
3529 rv = ready;
3530done:
3531 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003532}
3533
3534int
3535vppcom_select (unsigned long n_bits, unsigned long *read_map,
3536 unsigned long *write_map, unsigned long *except_map,
3537 double time_to_wait)
3538{
Dave Wallace543852a2017-08-03 02:11:34 -04003539 u32 session_index;
3540 session_t *session = 0;
3541 int rv, bits_set = 0;
3542 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3543 u32 minbits = clib_max (n_bits, BITS (uword));
3544
3545 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3546
Dave Wallace7876d392017-10-19 03:53:57 -04003547 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003548 {
3549 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003550 clib_memcpy (vcm->rd_bitmap, read_map,
3551 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3552 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003553 }
Dave Wallace7876d392017-10-19 03:53:57 -04003554 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003555 {
3556 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003557 clib_memcpy (vcm->wr_bitmap, write_map,
3558 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3559 memset (write_map, 0,
3560 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003561 }
Dave Wallace7876d392017-10-19 03:53:57 -04003562 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003563 {
3564 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003565 clib_memcpy (vcm->ex_bitmap, except_map,
3566 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3567 memset (except_map, 0,
3568 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003569 }
3570
3571 do
3572 {
3573 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04003574 if (n_bits)
3575 {
3576 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003577 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003578 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3579 ({
3580 clib_spinlock_lock (&vcm->sessions_lockp);
3581 rv = vppcom_session_at_index (session_index, &session);
3582 if (rv < 0)
3583 {
3584 clib_spinlock_unlock (&vcm->sessions_lockp);
3585 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003586 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003587 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003588 session_index);
3589 bits_set = VPPCOM_EBADFD;
3590 goto select_done;
3591 }
3592
3593 rv = vppcom_session_read_ready (session, session_index);
3594 clib_spinlock_unlock (&vcm->sessions_lockp);
3595 if (except_map && vcm->ex_bitmap &&
3596 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3597 (rv < 0))
3598 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003599 clib_bitmap_set_no_check (except_map, session_index, 1);
3600 bits_set++;
3601 }
3602 else if (rv > 0)
3603 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003604 clib_bitmap_set_no_check (read_map, session_index, 1);
3605 bits_set++;
3606 }
3607 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003608 }
3609
Dave Wallacee22aa742017-10-20 12:30:38 -04003610 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003611 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003612 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3613 ({
3614 clib_spinlock_lock (&vcm->sessions_lockp);
3615 rv = vppcom_session_at_index (session_index, &session);
3616 if (rv < 0)
3617 {
3618 clib_spinlock_unlock (&vcm->sessions_lockp);
3619 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003620 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003621 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003622 session_index);
3623 bits_set = VPPCOM_EBADFD;
3624 goto select_done;
3625 }
Dave Wallace543852a2017-08-03 02:11:34 -04003626
Dave Wallacee22aa742017-10-20 12:30:38 -04003627 rv = vppcom_session_write_ready (session, session_index);
3628 clib_spinlock_unlock (&vcm->sessions_lockp);
3629 if (write_map && (rv > 0))
3630 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003631 clib_bitmap_set_no_check (write_map, session_index, 1);
3632 bits_set++;
3633 }
3634 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003635 }
3636
Dave Wallacee22aa742017-10-20 12:30:38 -04003637 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003638 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003639 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3640 ({
3641 clib_spinlock_lock (&vcm->sessions_lockp);
3642 rv = vppcom_session_at_index (session_index, &session);
3643 if (rv < 0)
3644 {
3645 clib_spinlock_unlock (&vcm->sessions_lockp);
3646 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003647 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003648 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003649 session_index);
3650 bits_set = VPPCOM_EBADFD;
3651 goto select_done;
3652 }
Dave Wallace543852a2017-08-03 02:11:34 -04003653
Dave Wallacee22aa742017-10-20 12:30:38 -04003654 rv = vppcom_session_read_ready (session, session_index);
3655 clib_spinlock_unlock (&vcm->sessions_lockp);
3656 if (rv < 0)
3657 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003658 clib_bitmap_set_no_check (except_map, session_index, 1);
3659 bits_set++;
3660 }
3661 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003662 }
Dave Wallacee22aa742017-10-20 12:30:38 -04003663 }
Dave Wallace543852a2017-08-03 02:11:34 -04003664 /* *INDENT-ON* */
3665 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
Dave Wallace543852a2017-08-03 02:11:34 -04003667
3668select_done:
3669 return (bits_set);
3670}
3671
Dave Wallacef7f809c2017-10-03 01:48:42 -04003672static inline void
3673vep_verify_epoll_chain (u32 vep_idx)
3674{
3675 session_t *session;
3676 vppcom_epoll_t *vep;
3677 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05003678 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003679
Dave Wallace498b3a52017-11-09 13:00:34 -05003680 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003681 return;
3682
3683 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3684 rv = vppcom_session_at_index (vep_idx, &session);
3685 if (PREDICT_FALSE (rv))
3686 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003687 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3688 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003689 goto done;
3690 }
3691 if (PREDICT_FALSE (!session->is_vep))
3692 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003693 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3694 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003695 goto done;
3696 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003697 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05003698 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003699 "{\n"
3700 " is_vep = %u\n"
3701 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003702 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003703 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003704 "}\n", getpid (), vep_idx,
3705 session->is_vep, session->is_vep_session,
3706 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05003707 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003708
3709 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003710 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003711 rv = vppcom_session_at_index (sid, &session);
3712 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003713 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003714 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003715 goto done;
3716 }
3717 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05003718 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3719 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003720 else if (PREDICT_FALSE (!session->is_vep_session))
3721 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 clib_warning ("VCL<%d>: ERROR: session (%u) "
3723 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003724 goto done;
3725 }
3726 vep = &session->vep;
3727 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003729 "vep_idx (%u)!", getpid (),
3730 sid, session->vep.vep_idx, vep_idx);
3731 if (session->is_vep_session)
3732 {
3733 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3734 "{\n"
3735 " next_sid = 0x%x (%u)\n"
3736 " prev_sid = 0x%x (%u)\n"
3737 " vep_idx = 0x%x (%u)\n"
3738 " ev.events = 0x%x\n"
3739 " ev.data.u64 = 0x%llx\n"
3740 " et_mask = 0x%x\n"
3741 "}\n",
3742 vep_idx, sid, sid,
3743 vep->next_sid, vep->next_sid,
3744 vep->prev_sid, vep->prev_sid,
3745 vep->vep_idx, vep->vep_idx,
3746 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003747 }
3748 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003749
3750done:
Dave Wallace048b1d62018-01-03 22:24:41 -05003751 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3752 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003753}
3754
3755int
3756vppcom_epoll_create (void)
3757{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003758 session_t *vep_session;
3759 u32 vep_idx;
3760
3761 clib_spinlock_lock (&vcm->sessions_lockp);
3762 pool_get (vcm->sessions, vep_session);
3763 memset (vep_session, 0, sizeof (*vep_session));
3764 vep_idx = vep_session - vcm->sessions;
3765
3766 vep_session->is_vep = 1;
3767 vep_session->vep.vep_idx = ~0;
3768 vep_session->vep.next_sid = ~0;
3769 vep_session->vep.prev_sid = ~0;
3770 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003771 vep_session->vpp_handle = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003772 clib_spinlock_unlock (&vcm->sessions_lockp);
3773
3774 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003775 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
Dave Wallaceee45d412017-11-24 21:44:06 -05003776 getpid (), vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003777
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003778 if (VPPCOM_DEBUG > 0)
3779 {
3780 vep_session->elog_track.name =
3781 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3782 vep_idx, 0);
3783 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3784
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003785 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003786 ELOG_TYPE_DECLARE (e) =
3787 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003788 .format = "created epoll session:%d",
3789 .format_args = "i4",
3790 };
3791
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003792 struct
3793 {
3794 u32 data;
3795 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003796
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003797 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
3798 ed->data = vep_idx;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003799 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003800 }
3801
Dave Wallacef7f809c2017-10-03 01:48:42 -04003802 return (vep_idx);
3803}
3804
3805int
3806vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3807 struct epoll_event *event)
3808{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003809 session_t *vep_session;
3810 session_t *session;
3811 int rv;
3812
3813 if (vep_idx == session_index)
3814 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003815 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003816 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003817 return VPPCOM_EINVAL;
3818 }
3819
3820 clib_spinlock_lock (&vcm->sessions_lockp);
3821 rv = vppcom_session_at_index (vep_idx, &vep_session);
3822 if (PREDICT_FALSE (rv))
3823 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003824 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003825 goto done;
3826 }
3827 if (PREDICT_FALSE (!vep_session->is_vep))
3828 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003830 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003831 rv = VPPCOM_EINVAL;
3832 goto done;
3833 }
3834
3835 ASSERT (vep_session->vep.vep_idx == ~0);
3836 ASSERT (vep_session->vep.prev_sid == ~0);
3837
3838 rv = vppcom_session_at_index (session_index, &session);
3839 if (PREDICT_FALSE (rv))
3840 {
3841 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003842 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003843 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003844 goto done;
3845 }
3846 if (PREDICT_FALSE (session->is_vep))
3847 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003848 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003849 rv = VPPCOM_EINVAL;
3850 goto done;
3851 }
3852
3853 switch (op)
3854 {
3855 case EPOLL_CTL_ADD:
3856 if (PREDICT_FALSE (!event))
3857 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003858 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003859 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003860 rv = VPPCOM_EINVAL;
3861 goto done;
3862 }
3863 if (vep_session->vep.next_sid != ~0)
3864 {
3865 session_t *next_session;
3866 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3867 &next_session);
3868 if (PREDICT_FALSE (rv))
3869 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003870 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003871 "vep.next_sid (%u) on vep_idx (%u)!",
3872 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003873 goto done;
3874 }
3875 ASSERT (next_session->vep.prev_sid == vep_idx);
3876 next_session->vep.prev_sid = session_index;
3877 }
3878 session->vep.next_sid = vep_session->vep.next_sid;
3879 session->vep.prev_sid = vep_idx;
3880 session->vep.vep_idx = vep_idx;
3881 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3882 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003883 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003884 session->is_vep_session = 1;
3885 vep_session->vep.next_sid = session_index;
3886 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003887 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3888 "sid %u, events 0x%x, data 0x%llx!",
3889 getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003890 event->events, event->data.u64);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003891 if (VPPCOM_DEBUG > 0)
3892 {
3893 /* *INDENT-OFF* */
3894 ELOG_TYPE_DECLARE (e) =
3895 {
3896 .format = "epoll_ctladd: events:%x data:%x",
3897 .format_args = "i4i4i8",
3898 };
3899 struct
3900 {
3901 u32 events;
3902 u64 event_data;
3903 } *ed;
3904
3905 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3906
3907 ed->events = event->events;
3908 ed->event_data = event->data.u64;
3909 /* *INDENT-ON* */
3910 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003911 break;
3912
3913 case EPOLL_CTL_MOD:
3914 if (PREDICT_FALSE (!event))
3915 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003916 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003917 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003918 rv = VPPCOM_EINVAL;
3919 goto done;
3920 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003921 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003922 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003923 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003924 "not a vep session!", getpid (), session_index);
3925 rv = VPPCOM_EINVAL;
3926 goto done;
3927 }
3928 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3929 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003930 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003931 "vep_idx (%u) != vep_idx (%u)!",
3932 getpid (), session_index,
3933 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003934 rv = VPPCOM_EINVAL;
3935 goto done;
3936 }
3937 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3938 session->vep.ev = *event;
3939 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003940 clib_warning
3941 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3942 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3943 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003944 break;
3945
3946 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05003947 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003948 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003949 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003950 "not a vep session!", getpid (), session_index);
3951 rv = VPPCOM_EINVAL;
3952 goto done;
3953 }
3954 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3955 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003956 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003957 "vep_idx (%u) != vep_idx (%u)!",
3958 getpid (), session_index,
3959 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003960 rv = VPPCOM_EINVAL;
3961 goto done;
3962 }
3963
3964 vep_session->wait_cont_idx =
3965 (vep_session->wait_cont_idx == session_index) ?
3966 session->vep.next_sid : vep_session->wait_cont_idx;
3967
3968 if (session->vep.prev_sid == vep_idx)
3969 vep_session->vep.next_sid = session->vep.next_sid;
3970 else
3971 {
3972 session_t *prev_session;
3973 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3974 if (PREDICT_FALSE (rv))
3975 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003976 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003977 "vep.prev_sid (%u) on sid (%u)!",
3978 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003979 goto done;
3980 }
3981 ASSERT (prev_session->vep.next_sid == session_index);
3982 prev_session->vep.next_sid = session->vep.next_sid;
3983 }
3984 if (session->vep.next_sid != ~0)
3985 {
3986 session_t *next_session;
3987 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3988 if (PREDICT_FALSE (rv))
3989 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003990 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003991 "vep.next_sid (%u) on sid (%u)!",
3992 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003993 goto done;
3994 }
3995 ASSERT (next_session->vep.prev_sid == session_index);
3996 next_session->vep.prev_sid = session->vep.prev_sid;
3997 }
3998
3999 memset (&session->vep, 0, sizeof (session->vep));
4000 session->vep.next_sid = ~0;
4001 session->vep.prev_sid = ~0;
4002 session->vep.vep_idx = ~0;
4003 session->is_vep_session = 0;
4004 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004005 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004006 getpid (), vep_idx, session_index);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004007 if (VPPCOM_DEBUG > 0)
4008 {
4009 /* *INDENT-OFF* */
4010 ELOG_TYPE_DECLARE (e) =
4011 {
4012 .format = "epoll_ctldel: vep:%d",
4013 .format_args = "i4",
4014 };
4015 struct
4016 {
4017 u32 data;
4018 } *ed;
4019
4020 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4021
4022 ed->data = vep_idx;
4023 /* *INDENT-ON* */
4024 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004025 break;
4026
4027 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05004028 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004029 rv = VPPCOM_EINVAL;
4030 }
4031
4032 vep_verify_epoll_chain (vep_idx);
4033
4034done:
4035 clib_spinlock_unlock (&vcm->sessions_lockp);
4036 return rv;
4037}
4038
Dave Wallacef7f809c2017-10-03 01:48:42 -04004039int
4040vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4041 int maxevents, double wait_for_time)
4042{
Dave Wallacef7f809c2017-10-03 01:48:42 -04004043 session_t *vep_session;
4044 int rv;
4045 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05004046 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04004047 int num_ev = 0;
4048 u32 vep_next_sid, wait_cont_idx;
4049 u8 is_vep;
4050
4051 if (PREDICT_FALSE (maxevents <= 0))
4052 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004053 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004054 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004055 return VPPCOM_EINVAL;
4056 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004057 memset (events, 0, sizeof (*events) * maxevents);
4058
4059 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4060 vep_next_sid = vep_session->vep.next_sid;
4061 is_vep = vep_session->is_vep;
4062 wait_cont_idx = vep_session->wait_cont_idx;
4063 clib_spinlock_unlock (&vcm->sessions_lockp);
4064
4065 if (PREDICT_FALSE (!is_vep))
4066 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004067 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004068 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004069 rv = VPPCOM_EINVAL;
4070 goto done;
4071 }
Dave Wallacee695cb42017-11-02 22:04:42 -04004072 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004073 {
Dave Wallacee695cb42017-11-02 22:04:42 -04004074 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004075 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004076 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004077 if (VPPCOM_DEBUG > 0)
4078 {
4079 /* *INDENT-OFF* */
4080 ELOG_TYPE_DECLARE (e) =
4081 {
4082 .format = "WRN: vep_idx:%d empty",
4083 .format_args = "i4",
4084 };
4085 struct
4086 {
4087 u32 data;
4088 } *ed;
4089
4090 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
4091
4092 ed->data = vep_idx;
4093 /* *INDENT-ON* */
4094 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004095 goto done;
4096 }
4097
4098 do
4099 {
4100 u32 sid;
4101 u32 next_sid = ~0;
4102 session_t *session;
4103
4104 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4105 sid != ~0; sid = next_sid)
4106 {
4107 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4108 u8 add_event, is_vep_session;
4109 int ready;
4110 u64 session_ev_data;
4111
4112 VCL_LOCK_AND_GET_SESSION (sid, &session);
4113 next_sid = session->vep.next_sid;
4114 session_events = session->vep.ev.events;
4115 et_mask = session->vep.et_mask;
4116 is_vep = session->is_vep;
4117 is_vep_session = session->is_vep_session;
4118 session_vep_idx = session->vep.vep_idx;
4119 session_ev_data = session->vep.ev.data.u64;
4120 clib_spinlock_unlock (&vcm->sessions_lockp);
4121
4122 if (PREDICT_FALSE (is_vep))
4123 {
4124 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004125 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004126 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004127 if (VPPCOM_DEBUG > 0)
4128 {
4129 /* *INDENT-OFF* */
4130 ELOG_TYPE_DECLARE (e) =
4131 {
4132 .format = "ERR:vep_idx:%d is vep",
4133 .format_args = "i4",
4134 };
4135 struct
4136 {
4137 u32 data;
4138 } *ed;
4139
4140 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4141
4142 ed->data = vep_idx;
4143 /* *INDENT-ON* */
4144 }
4145
Dave Wallacef7f809c2017-10-03 01:48:42 -04004146 rv = VPPCOM_EINVAL;
4147 goto done;
4148 }
4149 if (PREDICT_FALSE (!is_vep_session))
4150 {
4151 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004152 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05004153 "a vep session!", getpid (), sid);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004154 if (VPPCOM_DEBUG > 0)
4155 {
4156 /* *INDENT-OFF* */
4157 ELOG_TYPE_DECLARE (e) =
4158 {
4159 .format = "ERR:SID:%d not vep",
4160 .format_args = "i4",
4161 };
4162 struct
4163 {
4164 u32 data;
4165 } *ed;
4166
4167 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4168
4169 ed->data = sid;
4170 /* *INDENT-ON* */
4171 }
4172
Dave Wallacef7f809c2017-10-03 01:48:42 -04004173 rv = VPPCOM_EINVAL;
4174 goto done;
4175 }
4176 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4177 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004178 clib_warning ("VCL<%d>: ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04004179 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004180 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004181 rv = VPPCOM_EINVAL;
4182 goto done;
4183 }
4184
4185 add_event = clear_et_mask = 0;
4186
Dave Wallace60caa062017-11-10 17:07:13 -05004187 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004188 {
4189 VCL_LOCK_AND_GET_SESSION (sid, &session);
4190 ready = vppcom_session_read_ready (session, sid);
4191 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004192 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004193 {
4194 add_event = 1;
4195 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05004196 if (((EPOLLET | EPOLLIN) & session_events) ==
4197 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004198 clear_et_mask |= EPOLLIN;
4199 }
4200 else if (ready < 0)
4201 {
4202 add_event = 1;
4203 switch (ready)
4204 {
4205 case VPPCOM_ECONNRESET:
4206 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4207 break;
4208
4209 default:
4210 events[num_ev].events |= EPOLLERR;
4211 break;
4212 }
4213 }
4214 }
4215
Dave Wallace60caa062017-11-10 17:07:13 -05004216 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004217 {
4218 VCL_LOCK_AND_GET_SESSION (sid, &session);
4219 ready = vppcom_session_write_ready (session, sid);
4220 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004221 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004222 {
4223 add_event = 1;
4224 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05004225 if (((EPOLLET | EPOLLOUT) & session_events) ==
4226 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004227 clear_et_mask |= EPOLLOUT;
4228 }
4229 else if (ready < 0)
4230 {
4231 add_event = 1;
4232 switch (ready)
4233 {
4234 case VPPCOM_ECONNRESET:
4235 events[num_ev].events |= EPOLLHUP;
4236 break;
4237
4238 default:
4239 events[num_ev].events |= EPOLLERR;
4240 break;
4241 }
4242 }
4243 }
4244
4245 if (add_event)
4246 {
4247 events[num_ev].data.u64 = session_ev_data;
4248 if (EPOLLONESHOT & session_events)
4249 {
4250 VCL_LOCK_AND_GET_SESSION (sid, &session);
4251 session->vep.ev.events = 0;
4252 clib_spinlock_unlock (&vcm->sessions_lockp);
4253 }
4254 num_ev++;
4255 if (num_ev == maxevents)
4256 {
4257 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4258 vep_session->wait_cont_idx = next_sid;
4259 clib_spinlock_unlock (&vcm->sessions_lockp);
4260 goto done;
4261 }
4262 }
4263 if (wait_cont_idx != ~0)
4264 {
4265 if (next_sid == ~0)
4266 next_sid = vep_next_sid;
4267 else if (next_sid == wait_cont_idx)
4268 next_sid = ~0;
4269 }
4270 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004271 if (wait_for_time != -1)
4272 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04004273 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004274 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004275
4276 if (wait_cont_idx != ~0)
4277 {
4278 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4279 vep_session->wait_cont_idx = ~0;
4280 clib_spinlock_unlock (&vcm->sessions_lockp);
4281 }
4282done:
4283 return (rv != VPPCOM_OK) ? rv : num_ev;
4284}
4285
Dave Wallace35830af2017-10-09 01:43:42 -04004286int
4287vppcom_session_attr (uint32_t session_index, uint32_t op,
4288 void *buffer, uint32_t * buflen)
4289{
Dave Wallace35830af2017-10-09 01:43:42 -04004290 session_t *session;
4291 int rv = VPPCOM_OK;
4292 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07004293 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04004294
4295 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4296 switch (op)
4297 {
4298 case VPPCOM_ATTR_GET_NREAD:
4299 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05004300 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004301 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004302 getpid (), rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004303 if (VPPCOM_DEBUG > 0)
4304 {
4305 /* *INDENT-OFF* */
4306 ELOG_TYPE_DECLARE (e) =
4307 {
4308 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4309 .format_args = "i4",
4310 };
4311 struct
4312 {
4313 u32 data;
4314 } *ed;
4315
4316 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4317
4318 ed->data = rv;
4319 /* *INDENT-ON* */
4320 }
4321
Dave Wallace35830af2017-10-09 01:43:42 -04004322 break;
4323
Dave Wallace227867f2017-11-13 21:21:53 -05004324 case VPPCOM_ATTR_GET_NWRITE:
4325 rv = vppcom_session_write_ready (session, session_index);
4326 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004327 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Dave Wallace227867f2017-11-13 21:21:53 -05004328 getpid (), session_index, rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004329 if (VPPCOM_DEBUG > 0)
4330 {
4331 /* *INDENT-OFF* */
4332 ELOG_TYPE_DECLARE (e) =
4333 {
4334 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4335 .format_args = "i4",
4336 };
4337 struct
4338 {
4339 u32 data;
4340 } *ed;
4341
4342 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4343
4344 ed->data = rv;
4345 /* *INDENT-ON* */
4346 }
Dave Wallace35830af2017-10-09 01:43:42 -04004347 break;
4348
4349 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004350 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004351 {
4352 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
4353 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05004354 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004355 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004356 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4357 session_index, *flags, session->is_nonblocking);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004358 if (VPPCOM_DEBUG > 0)
4359 {
4360 /* *INDENT-OFF* */
4361 ELOG_TYPE_DECLARE (e) =
4362 {
4363 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004364 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004365 };
4366 struct
4367 {
4368 u32 flags;
4369 u32 is_nonblk;
4370 } *ed;
4371
4372 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4373
4374 ed->flags = *flags;
4375 ed->is_nonblk = session->is_nonblocking;
4376 /* *INDENT-ON* */
4377 }
4378
Dave Wallace35830af2017-10-09 01:43:42 -04004379 }
4380 else
4381 rv = VPPCOM_EINVAL;
4382 break;
4383
4384 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004385 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004386 {
4387 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace227867f2017-11-13 21:21:53 -05004388 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004389 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004390 "flags = 0x%08x, is_nonblocking = %u",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004391 getpid (), session_index, *flags,
4392 session->is_nonblocking);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004393 if (VPPCOM_DEBUG > 0)
4394 {
4395 /* *INDENT-OFF* */
4396 ELOG_TYPE_DECLARE (e) =
4397 {
4398 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004399 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004400 };
4401 struct
4402 {
4403 u32 flags;
4404 u32 is_nonblk;
4405 } *ed;
4406
4407 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4408
4409 ed->flags = *flags;
4410 ed->is_nonblk = session->is_nonblocking;
4411 /* *INDENT-ON* */
4412 }
Dave Wallace35830af2017-10-09 01:43:42 -04004413 }
4414 else
4415 rv = VPPCOM_EINVAL;
4416 break;
4417
4418 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004419 if (PREDICT_TRUE (buffer && buflen &&
4420 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004421 {
Steven2199aab2017-10-15 20:18:47 -07004422 ep->vrf = session->vrf;
4423 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004424 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07004425 if (session->peer_addr.is_ip4)
4426 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4427 sizeof (ip4_address_t));
4428 else
4429 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4430 sizeof (ip6_address_t));
4431 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004432 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004433 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4434 "is_ip4 = %u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004435 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004436 &session->peer_addr.ip46, ep->is_ip4,
4437 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004438 if (VPPCOM_DEBUG > 0)
4439 {
4440 if (ep->is_ip4)
4441 {
4442 /* *INDENT-OFF* */
4443 ELOG_TYPE_DECLARE (e) =
4444 {
4445 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4446 .format_args = "i1i1i1i1i2",
4447 };
4448 CLIB_PACKED (struct {
4449 u8 addr[4]; //4
4450 u16 port; //2
4451 }) * ed;
4452
4453 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4454
4455 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4456 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4457 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4458 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4459 ed->port = clib_net_to_host_u16 (session->peer_port);
4460 /* *INDENT-ON* */
4461 }
4462 else
4463 {
4464 /* *INDENT-OFF* */
4465 ELOG_TYPE_DECLARE (e) =
4466 {
4467 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4468 .format_args = "i2",
4469 };
4470 CLIB_PACKED (struct {
4471 u16 port; //2
4472 }) * ed;
4473
4474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4475
4476 ed->port = clib_net_to_host_u16 (session->peer_port);
4477 /* *INDENT-ON* */
4478 }
4479 }
Dave Wallace35830af2017-10-09 01:43:42 -04004480 }
4481 else
4482 rv = VPPCOM_EINVAL;
4483 break;
4484
4485 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004486 if (PREDICT_TRUE (buffer && buflen &&
4487 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004488 {
Steven2199aab2017-10-15 20:18:47 -07004489 ep->vrf = session->vrf;
4490 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004491 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07004492 if (session->lcl_addr.is_ip4)
4493 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4494 sizeof (ip4_address_t));
4495 else
4496 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4497 sizeof (ip6_address_t));
4498 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004499 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004500 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4501 "is_ip4 = %u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004502 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004503 &session->lcl_addr.ip46, ep->is_ip4,
4504 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004505 if (VPPCOM_DEBUG > 0)
4506 {
4507 if (ep->is_ip4)
4508 {
4509 /* *INDENT-OFF* */
4510 ELOG_TYPE_DECLARE (e) =
4511 {
4512 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4513 .format_args = "i1i1i1i1i2",
4514 };
4515 CLIB_PACKED (struct {
4516 u8 addr[4]; //4
4517 u16 port; //2
4518 }) * ed;
4519
4520 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4521
4522 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4523 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4524 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4525 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4526 ed->port = clib_net_to_host_u16 (session->peer_port);
4527 /* *INDENT-ON* */
4528 }
4529 else
4530 {
4531 /* *INDENT-OFF* */
4532 ELOG_TYPE_DECLARE (e) =
4533 {
4534 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4535 .format_args = "i2",
4536 };
4537 CLIB_PACKED (struct {
4538 u16 port; //2
4539 }) * ed;
4540
4541 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4542
4543 ed->port = clib_net_to_host_u16 (session->peer_port);
4544 /* *INDENT-ON* */
4545 }
4546 }
Dave Wallace35830af2017-10-09 01:43:42 -04004547 }
4548 else
4549 rv = VPPCOM_EINVAL;
4550 break;
Stevenb5a11602017-10-11 09:59:30 -07004551
Dave Wallace048b1d62018-01-03 22:24:41 -05004552 case VPPCOM_ATTR_GET_LIBC_EPFD:
4553 rv = session->libc_epfd;
4554 if (VPPCOM_DEBUG > 2)
4555 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4556 getpid (), rv);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004557 if (VPPCOM_DEBUG > 0)
4558 {
4559 /* *INDENT-OFF* */
4560 ELOG_TYPE_DECLARE (e) =
4561 {
4562 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4563 .format_args = "t1i4i4",
4564 .n_enum_strings = 2,
4565 .enum_strings = {"", "-",},
4566 };
4567 CLIB_PACKED (struct {
4568 u8 sign;
4569 u32 data[2];
4570 }) * ed;
4571
4572 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4573
4574 ed->sign = (session->libc_epfd < 0);
4575 ed->data[0] = abs(session->libc_epfd);
4576 ed->data[1] = *buflen;
4577 /* *INDENT-ON* */
4578 }
4579
Dave Wallace048b1d62018-01-03 22:24:41 -05004580 break;
4581
4582 case VPPCOM_ATTR_SET_LIBC_EPFD:
4583 if (PREDICT_TRUE (buffer && buflen &&
4584 (*buflen == sizeof (session->libc_epfd))))
4585 {
4586 session->libc_epfd = *(int *) buffer;
4587 *buflen = sizeof (session->libc_epfd);
4588
4589 if (VPPCOM_DEBUG > 2)
4590 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4591 "buflen %d", getpid (), session->libc_epfd,
4592 *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004593 if (VPPCOM_DEBUG > 0)
4594 {
4595 /* *INDENT-OFF* */
4596 ELOG_TYPE_DECLARE (e) =
4597 {
4598 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4599 .format_args = "t1i4i4",
4600 .n_enum_strings = 2,
4601 .enum_strings = {"", "-",},
4602 };
4603 CLIB_PACKED (struct {
4604 u8 sign;
4605 u32 data[2];
4606 }) * ed;
4607
4608 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4609
4610 ed->sign = (session->libc_epfd < 0);
4611 ed->data[0] = abs(session->libc_epfd);
4612 ed->data[1] = *buflen;
4613 /* *INDENT-ON* */
4614 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004615 }
4616 else
4617 rv = VPPCOM_EINVAL;
4618 break;
4619
4620 case VPPCOM_ATTR_GET_PROTOCOL:
4621 if (buffer && buflen && (*buflen >= sizeof (int)))
4622 {
4623 *(int *) buffer = session->proto;
4624 *buflen = sizeof (int);
4625
4626 if (VPPCOM_DEBUG > 2)
4627 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4628 "buflen %d", getpid (), *(int *) buffer,
4629 *(int *) buffer ? "UDP" : "TCP", *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004630 if (VPPCOM_DEBUG > 0)
4631 {
4632 /* *INDENT-OFF* */
4633 ELOG_TYPE_DECLARE (e) =
4634 {
4635 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4636 .format_args = "t1i4",
4637 .n_enum_strings = 2,
4638 .enum_strings = {"TCP", "UDP",},
4639 };
4640
4641 CLIB_PACKED (struct {
4642 u8 proto;
4643 u32 buflen;
4644 }) * ed;
4645
4646 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4647 ed->proto = session->proto;
4648 ed->buflen = *(int *) buffer;
4649 /* *INDENT-ON* */
4650 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004651 }
4652 else
4653 rv = VPPCOM_EINVAL;
4654 break;
4655
4656 case VPPCOM_ATTR_GET_LISTEN:
4657 if (buffer && buflen && (*buflen >= sizeof (int)))
4658 {
4659 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4660 VCL_SESS_ATTR_LISTEN);
4661 *buflen = sizeof (int);
4662
4663 if (VPPCOM_DEBUG > 2)
4664 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4665 "buflen %d", getpid (), *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004666 if (VPPCOM_DEBUG > 0)
4667 {
4668 /* *INDENT-OFF* */
4669 ELOG_TYPE_DECLARE (e) =
4670 {
4671 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4672 .format_args = "i4i4",
4673 };
4674
4675 struct {
4676 u32 data[2];
4677 } * ed;
4678
4679 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4680 ed->data[0] = *(int *) buffer;
4681 ed->data[1] = *buflen;
4682 /* *INDENT-ON* */
4683 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004684 }
4685 else
4686 rv = VPPCOM_EINVAL;
4687 break;
4688
4689 case VPPCOM_ATTR_GET_ERROR:
4690 if (buffer && buflen && (*buflen >= sizeof (int)))
4691 {
4692 *(int *) buffer = 0;
4693 *buflen = sizeof (int);
4694
4695 if (VPPCOM_DEBUG > 2)
4696 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4697 "buflen %d, #VPP-TBD#", getpid (),
4698 *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004699 if (VPPCOM_DEBUG > 0)
4700 {
4701 /* *INDENT-OFF* */
4702 ELOG_TYPE_DECLARE (e) =
4703 {
4704 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4705 .format_args = "i4i4",
4706 };
4707
4708 struct {
4709 u32 data[2];
4710 } * ed;
4711
4712 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4713 ed->data[0] = *(int *) buffer;
4714 ed->data[1] = *buflen;
4715 /* *INDENT-ON* */
4716 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004717 }
4718 else
4719 rv = VPPCOM_EINVAL;
4720 break;
4721
4722 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4723 if (buffer && buflen && (*buflen >= sizeof (u32)))
4724 {
4725 svm_fifo_t *tx_fifo;
4726
4727 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
4728 session->server_tx_fifo : session->server_rx_fifo);
4729
4730 /* VPP-TBD */
4731 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4732 tx_fifo ? tx_fifo->nitems :
4733 vcm->cfg.tx_fifo_size);
4734 *buflen = sizeof (u32);
4735
4736 if (VPPCOM_DEBUG > 2)
4737 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4738 "buflen %d, #VPP-TBD#", getpid (),
4739 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004740 if (VPPCOM_DEBUG > 0)
4741 {
4742 /* *INDENT-OFF* */
4743 ELOG_TYPE_DECLARE (e) =
4744 {
4745 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4746 .format_args = "i4i4",
4747 };
4748
4749 struct {
4750 u32 data[2];
4751 } * ed;
4752
4753 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4754 ed->data[0] = *(size_t *) buffer;
4755 ed->data[1] = *buflen;
4756 /* *INDENT-ON* */
4757 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004758 }
4759 else
4760 rv = VPPCOM_EINVAL;
4761 break;
4762
4763 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4764 if (buffer && buflen && (*buflen == sizeof (u32)))
4765 {
4766 /* VPP-TBD */
4767 session->sndbuf_size = *(u32 *) buffer;
4768 if (VPPCOM_DEBUG > 2)
4769 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4770 "buflen %d, #VPP-TBD#", getpid (),
4771 session->sndbuf_size, session->sndbuf_size,
4772 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004773 if (VPPCOM_DEBUG > 0)
4774 {
4775 /* *INDENT-OFF* */
4776 ELOG_TYPE_DECLARE (e) =
4777 {
4778 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4779 .format_args = "i4i4",
4780 };
4781
4782 struct {
4783 u32 data[2];
4784 } * ed;
4785
4786 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4787 ed->data[0] = session->sndbuf_size;
4788 ed->data[1] = *buflen;
4789 /* *INDENT-ON* */
4790 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004791 }
4792 else
4793 rv = VPPCOM_EINVAL;
4794 break;
4795
4796 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4797 if (buffer && buflen && (*buflen >= sizeof (u32)))
4798 {
4799 svm_fifo_t *rx_fifo;
4800
4801 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
4802 session->server_rx_fifo : session->server_tx_fifo);
4803
4804 /* VPP-TBD */
4805 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4806 rx_fifo ? rx_fifo->nitems :
4807 vcm->cfg.rx_fifo_size);
4808 *buflen = sizeof (u32);
4809
4810 if (VPPCOM_DEBUG > 2)
4811 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4812 "buflen %d, #VPP-TBD#", getpid (),
4813 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004814 if (VPPCOM_DEBUG > 0)
4815 {
4816 /* *INDENT-OFF* */
4817 ELOG_TYPE_DECLARE (e) =
4818 {
4819 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4820 .format_args = "i4i4",
4821 };
4822
4823 struct {
4824 u32 data[2];
4825 } * ed;
4826
4827 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4828 ed->data[0] = *(size_t *) buffer;
4829 ed->data[1] = *buflen;
4830 /* *INDENT-ON* */
4831 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004832 }
4833 else
4834 rv = VPPCOM_EINVAL;
4835 break;
4836
4837 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4838 if (buffer && buflen && (*buflen == sizeof (u32)))
4839 {
4840 /* VPP-TBD */
4841 session->rcvbuf_size = *(u32 *) buffer;
4842 if (VPPCOM_DEBUG > 2)
4843 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4844 "buflen %d, #VPP-TBD#", getpid (),
4845 session->sndbuf_size, session->sndbuf_size,
4846 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004847 if (VPPCOM_DEBUG > 0)
4848 {
4849 /* *INDENT-OFF* */
4850 ELOG_TYPE_DECLARE (e) =
4851 {
4852 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4853 .format_args = "i4i4",
4854 };
4855
4856 struct {
4857 u32 data[2];
4858 } * ed;
4859
4860 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4861 ed->data[0] = session->sndbuf_size;
4862 ed->data[1] = *buflen;
4863 /* *INDENT-ON* */
4864 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004865 }
4866 else
4867 rv = VPPCOM_EINVAL;
4868 break;
4869
4870 case VPPCOM_ATTR_GET_REUSEADDR:
4871 if (buffer && buflen && (*buflen >= sizeof (int)))
4872 {
4873 /* VPP-TBD */
4874 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4875 VCL_SESS_ATTR_REUSEADDR);
4876 *buflen = sizeof (int);
4877
4878 if (VPPCOM_DEBUG > 2)
4879 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4880 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4881 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004882 if (VPPCOM_DEBUG > 0)
4883 {
4884 /* *INDENT-OFF* */
4885 ELOG_TYPE_DECLARE (e) =
4886 {
4887 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4888 .format_args = "i4i4",
4889 };
4890
4891 struct {
4892 u32 data[2];
4893 } * ed;
4894
4895 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4896 ed->data[0] = *(int *) buffer;
4897 ed->data[1] = *buflen;
4898 /* *INDENT-ON* */
4899 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004900 }
4901 else
4902 rv = VPPCOM_EINVAL;
4903 break;
4904
Stevenb5a11602017-10-11 09:59:30 -07004905 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004906 if (buffer && buflen && (*buflen == sizeof (int)) &&
4907 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4908 {
4909 /* VPP-TBD */
4910 if (*(int *) buffer)
4911 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4912 else
4913 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4914
4915 if (VPPCOM_DEBUG > 2)
4916 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4917 "buflen %d, #VPP-TBD#", getpid (),
4918 VCL_SESS_ATTR_TEST (session->attr,
4919 VCL_SESS_ATTR_REUSEADDR),
4920 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004921 if (VPPCOM_DEBUG > 0)
4922 {
4923 /* *INDENT-OFF* */
4924 ELOG_TYPE_DECLARE (e) =
4925 {
4926 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4927 .format_args = "i4i4",
4928 };
4929
4930 struct {
4931 u32 data[2];
4932 } * ed;
4933
4934 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4935 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4936 VCL_SESS_ATTR_REUSEADDR);
4937 ed->data[1] = *buflen;
4938 /* *INDENT-ON* */
4939 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004940 }
4941 else
4942 rv = VPPCOM_EINVAL;
4943 break;
4944
4945 case VPPCOM_ATTR_GET_REUSEPORT:
4946 if (buffer && buflen && (*buflen >= sizeof (int)))
4947 {
4948 /* VPP-TBD */
4949 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4950 VCL_SESS_ATTR_REUSEPORT);
4951 *buflen = sizeof (int);
4952
4953 if (VPPCOM_DEBUG > 2)
4954 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4955 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4956 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004957 if (VPPCOM_DEBUG > 0)
4958 {
4959 /* *INDENT-OFF* */
4960 ELOG_TYPE_DECLARE (e) =
4961 {
4962 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4963 .format_args = "i4i4",
4964 };
4965
4966 struct {
4967 u32 data[2];
4968 } * ed;
4969
4970 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4971 ed->data[0] = *(int *) buffer;
4972 ed->data[1] = *buflen;
4973 /* *INDENT-ON* */
4974 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004975 }
4976 else
4977 rv = VPPCOM_EINVAL;
4978 break;
4979
4980 case VPPCOM_ATTR_SET_REUSEPORT:
4981 if (buffer && buflen && (*buflen == sizeof (int)) &&
4982 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4983 {
4984 /* VPP-TBD */
4985 if (*(int *) buffer)
4986 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4987 else
4988 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4989
4990 if (VPPCOM_DEBUG > 2)
4991 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4992 "buflen %d, #VPP-TBD#", getpid (),
4993 VCL_SESS_ATTR_TEST (session->attr,
4994 VCL_SESS_ATTR_REUSEPORT),
4995 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004996 if (VPPCOM_DEBUG > 0)
4997 {
4998 /* *INDENT-OFF* */
4999 ELOG_TYPE_DECLARE (e) =
5000 {
5001 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5002 .format_args = "i4i4",
5003 };
5004
5005 struct {
5006 u32 data[2];
5007 } * ed;
5008
5009 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5010 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5011 VCL_SESS_ATTR_REUSEPORT);
5012 ed->data[1] = *buflen;
5013 /* *INDENT-ON* */
5014 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005015 }
5016 else
5017 rv = VPPCOM_EINVAL;
5018 break;
5019
5020 case VPPCOM_ATTR_GET_BROADCAST:
5021 if (buffer && buflen && (*buflen >= sizeof (int)))
5022 {
5023 /* VPP-TBD */
5024 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5025 VCL_SESS_ATTR_BROADCAST);
5026 *buflen = sizeof (int);
5027
5028 if (VPPCOM_DEBUG > 2)
5029 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5030 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5031 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08005032 if (VPPCOM_DEBUG > 0)
5033 {
5034 /* *INDENT-OFF* */
5035 ELOG_TYPE_DECLARE (e) =
5036 {
5037 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5038 .format_args = "i4i4",
5039 };
5040
5041 struct {
5042 u32 data[2];
5043 } * ed;
5044
5045 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5046 ed->data[0] = *(int *) buffer;
5047 ed->data[1] = *buflen;
5048 /* *INDENT-ON* */
5049 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005050 }
5051 else
5052 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005053 break;
5054
5055 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05005056 if (buffer && buflen && (*buflen == sizeof (int)))
5057 {
5058 /* VPP-TBD */
5059 if (*(int *) buffer)
5060 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5061 else
5062 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5063
5064 if (VPPCOM_DEBUG > 2)
5065 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5066 "buflen %d, #VPP-TBD#", getpid (),
5067 VCL_SESS_ATTR_TEST (session->attr,
5068 VCL_SESS_ATTR_BROADCAST),
5069 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005070 if (VPPCOM_DEBUG > 0)
5071 {
5072 /* *INDENT-OFF* */
5073 ELOG_TYPE_DECLARE (e) =
5074 {
5075 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5076 .format_args = "i4i4",
5077 };
5078
5079 struct {
5080 u32 data[2];
5081 } * ed;
5082
5083 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5084 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5085 VCL_SESS_ATTR_BROADCAST);
5086 ed->data[1] = *buflen;
5087 /* *INDENT-ON* */
5088 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005089 }
5090 else
5091 rv = VPPCOM_EINVAL;
5092 break;
5093
5094 case VPPCOM_ATTR_GET_V6ONLY:
5095 if (buffer && buflen && (*buflen >= sizeof (int)))
5096 {
5097 /* VPP-TBD */
5098 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5099 VCL_SESS_ATTR_V6ONLY);
5100 *buflen = sizeof (int);
5101
5102 if (VPPCOM_DEBUG > 2)
5103 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5104 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5105 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005106 if (VPPCOM_DEBUG > 0)
5107 {
5108 /* *INDENT-OFF* */
5109 ELOG_TYPE_DECLARE (e) =
5110 {
5111 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5112 .format_args = "i4i4",
5113 };
5114
5115 struct {
5116 u32 data[2];
5117 } * ed;
5118
5119 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5120 ed->data[0] = *(int *) buffer;
5121 ed->data[1] = *buflen;
5122 /* *INDENT-ON* */
5123 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005124 }
5125 else
5126 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005127 break;
5128
5129 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05005130 if (buffer && buflen && (*buflen == sizeof (int)))
5131 {
5132 /* VPP-TBD */
5133 if (*(int *) buffer)
5134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5135 else
5136 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5137
5138 if (VPPCOM_DEBUG > 2)
5139 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5140 "buflen %d, #VPP-TBD#", getpid (),
5141 VCL_SESS_ATTR_TEST (session->attr,
5142 VCL_SESS_ATTR_V6ONLY), *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005143 if (VPPCOM_DEBUG > 0)
5144 {
5145 /* *INDENT-OFF* */
5146 ELOG_TYPE_DECLARE (e) =
5147 {
5148 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5149 .format_args = "i4i4",
5150 };
5151
5152 struct {
5153 u32 data[2];
5154 } * ed;
5155
5156 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5157 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5158 VCL_SESS_ATTR_V6ONLY);
5159 ed->data[1] = *buflen;
5160 /* *INDENT-ON* */
5161 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005162 }
5163 else
5164 rv = VPPCOM_EINVAL;
5165 break;
5166
5167 case VPPCOM_ATTR_GET_KEEPALIVE:
5168 if (buffer && buflen && (*buflen >= sizeof (int)))
5169 {
5170 /* VPP-TBD */
5171 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5172 VCL_SESS_ATTR_KEEPALIVE);
5173 *buflen = sizeof (int);
5174
5175 if (VPPCOM_DEBUG > 2)
5176 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5177 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5178 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005179 if (VPPCOM_DEBUG > 0)
5180 {
5181 /* *INDENT-OFF* */
5182 ELOG_TYPE_DECLARE (e) =
5183 {
5184 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5185 .format_args = "i4i4",
5186 };
5187
5188 struct {
5189 u32 data[2];
5190 } * ed;
5191
5192 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5193 ed->data[0] = *(int *) buffer;
5194 ed->data[1] = *buflen;
5195 /* *INDENT-ON* */
5196 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005197 }
5198 else
5199 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005200 break;
Stevenbccd3392017-10-12 20:42:21 -07005201
5202 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005203 if (buffer && buflen && (*buflen == sizeof (int)))
5204 {
5205 /* VPP-TBD */
5206 if (*(int *) buffer)
5207 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5208 else
5209 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5210
5211 if (VPPCOM_DEBUG > 2)
5212 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5213 "buflen %d, #VPP-TBD#", getpid (),
5214 VCL_SESS_ATTR_TEST (session->attr,
5215 VCL_SESS_ATTR_KEEPALIVE),
5216 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005217 if (VPPCOM_DEBUG > 0)
5218 {
5219 /* *INDENT-OFF* */
5220 ELOG_TYPE_DECLARE (e) =
5221 {
5222 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5223 .format_args = "i4i4",
5224 };
5225
5226 struct {
5227 u32 data[2];
5228 } * ed;
5229
5230 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5231 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5232 VCL_SESS_ATTR_KEEPALIVE);
5233 ed->data[1] = *buflen;
5234 /* *INDENT-ON* */
5235 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005236 }
5237 else
5238 rv = VPPCOM_EINVAL;
5239 break;
5240
5241 case VPPCOM_ATTR_GET_TCP_NODELAY:
5242 if (buffer && buflen && (*buflen >= sizeof (int)))
5243 {
5244 /* VPP-TBD */
5245 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5246 VCL_SESS_ATTR_TCP_NODELAY);
5247 *buflen = sizeof (int);
5248
5249 if (VPPCOM_DEBUG > 2)
5250 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5251 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5252 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005253 if (VPPCOM_DEBUG > 0)
5254 {
5255 /* *INDENT-OFF* */
5256 ELOG_TYPE_DECLARE (e) =
5257 {
5258 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5259 .format_args = "i4i4",
5260 };
5261
5262 struct {
5263 u32 data[2];
5264 } * ed;
5265
5266 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5267 ed->data[0] = *(int *) buffer;
5268 ed->data[1] = *buflen;
5269 /* *INDENT-ON* */
5270 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005271 }
5272 else
5273 rv = VPPCOM_EINVAL;
5274 break;
5275
5276 case VPPCOM_ATTR_SET_TCP_NODELAY:
5277 if (buffer && buflen && (*buflen == sizeof (int)))
5278 {
5279 /* VPP-TBD */
5280 if (*(int *) buffer)
5281 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5282 else
5283 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5284
5285 if (VPPCOM_DEBUG > 2)
5286 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5287 "buflen %d, #VPP-TBD#", getpid (),
5288 VCL_SESS_ATTR_TEST (session->attr,
5289 VCL_SESS_ATTR_TCP_NODELAY),
5290 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005291 if (VPPCOM_DEBUG > 0)
5292 {
5293 /* *INDENT-OFF* */
5294 ELOG_TYPE_DECLARE (e) =
5295 {
5296 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5297 .format_args = "i4i4",
5298 };
5299
5300 struct {
5301 u32 data[2];
5302 } * ed;
5303
5304 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5305 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5306 VCL_SESS_ATTR_TCP_NODELAY);
5307 ed->data[1] = *buflen;
5308 /* *INDENT-ON* */
5309 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005310 }
5311 else
5312 rv = VPPCOM_EINVAL;
5313 break;
5314
5315 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5316 if (buffer && buflen && (*buflen >= sizeof (int)))
5317 {
5318 /* VPP-TBD */
5319 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5320 VCL_SESS_ATTR_TCP_KEEPIDLE);
5321 *buflen = sizeof (int);
5322
5323 if (VPPCOM_DEBUG > 2)
5324 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5325 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5326 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005327 if (VPPCOM_DEBUG > 0)
5328 {
5329 /* *INDENT-OFF* */
5330 ELOG_TYPE_DECLARE (e) =
5331 {
5332 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5333 .format_args = "i4i4",
5334 };
5335
5336 struct {
5337 u32 data[2];
5338 } * ed;
5339
5340 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5341 ed->data[0] = *(int *) buffer;
5342 ed->data[1] = *buflen;
5343 /* *INDENT-ON* */
5344 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005345 }
5346 else
5347 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005348 break;
5349
5350 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005351 if (buffer && buflen && (*buflen == sizeof (int)))
5352 {
5353 /* VPP-TBD */
5354 if (*(int *) buffer)
5355 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5356 else
5357 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5358
5359 if (VPPCOM_DEBUG > 2)
5360 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5361 "buflen %d, #VPP-TBD#", getpid (),
5362 VCL_SESS_ATTR_TEST (session->attr,
5363 VCL_SESS_ATTR_TCP_KEEPIDLE),
5364 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005365 if (VPPCOM_DEBUG > 0)
5366 {
5367 /* *INDENT-OFF* */
5368 ELOG_TYPE_DECLARE (e) =
5369 {
5370 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5371 .format_args = "i4i4",
5372 };
5373
5374 struct {
5375 u32 data[2];
5376 } * ed;
5377
5378 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5379 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5380 VCL_SESS_ATTR_TCP_KEEPIDLE);
5381 ed->data[1] = *buflen;
5382 /* *INDENT-ON* */
5383 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005384 }
5385 else
5386 rv = VPPCOM_EINVAL;
5387 break;
5388
5389 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5390 if (buffer && buflen && (*buflen >= sizeof (int)))
5391 {
5392 /* VPP-TBD */
5393 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5394 VCL_SESS_ATTR_TCP_KEEPINTVL);
5395 *buflen = sizeof (int);
5396
5397 if (VPPCOM_DEBUG > 2)
5398 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5399 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5400 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005401 if (VPPCOM_DEBUG > 0)
5402 {
5403 /* *INDENT-OFF* */
5404 ELOG_TYPE_DECLARE (e) =
5405 {
5406 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5407 .format_args = "i4i4",
5408 };
5409
5410 struct {
5411 u32 data[2];
5412 } * ed;
5413
5414 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5415 ed->data[0] = *(int *) buffer;
5416 ed->data[1] = *buflen;
5417 /* *INDENT-ON* */
5418 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005419 }
5420 else
5421 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005422 break;
5423
5424 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05005425 if (buffer && buflen && (*buflen == sizeof (int)))
5426 {
5427 /* VPP-TBD */
5428 if (*(int *) buffer)
5429 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5430 else
5431 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5432
5433 if (VPPCOM_DEBUG > 2)
5434 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5435 "buflen %d, #VPP-TBD#", getpid (),
5436 VCL_SESS_ATTR_TEST (session->attr,
5437 VCL_SESS_ATTR_TCP_KEEPINTVL),
5438 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005439 if (VPPCOM_DEBUG > 0)
5440 {
5441 /* *INDENT-OFF* */
5442 ELOG_TYPE_DECLARE (e) =
5443 {
5444 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5445 .format_args = "i4i4",
5446 };
5447
5448 struct {
5449 u32 data[2];
5450 } * ed;
5451
5452 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5453 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5454 VCL_SESS_ATTR_TCP_KEEPINTVL);
5455 ed->data[1] = *buflen;
5456 /* *INDENT-ON* */
5457 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005458 }
5459 else
5460 rv = VPPCOM_EINVAL;
5461 break;
5462
5463 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5464 if (buffer && buflen && (*buflen >= sizeof (u32)))
5465 {
5466 /* VPP-TBD */
5467 *(u32 *) buffer = session->user_mss;
5468 *buflen = sizeof (int);
5469
5470 if (VPPCOM_DEBUG > 2)
5471 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5472 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5473 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005474 if (VPPCOM_DEBUG > 0)
5475 {
5476 /* *INDENT-OFF* */
5477 ELOG_TYPE_DECLARE (e) =
5478 {
5479 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5480 .format_args = "i4i4",
5481 };
5482
5483 struct {
5484 u32 data[2];
5485 } * ed;
5486
5487 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5488 ed->data[0] = *(int *) buffer;
5489 ed->data[1] = *buflen;
5490 /* *INDENT-ON* */
5491 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005492 }
5493 else
5494 rv = VPPCOM_EINVAL;
5495 break;
5496
5497 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5498 if (buffer && buflen && (*buflen == sizeof (u32)))
5499 {
5500 /* VPP-TBD */
5501 session->user_mss = *(u32 *) buffer;
5502
5503 if (VPPCOM_DEBUG > 2)
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005504 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
Dave Wallace048b1d62018-01-03 22:24:41 -05005505 "buflen %d, #VPP-TBD#", getpid (),
5506 session->user_mss, *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005507 if (VPPCOM_DEBUG > 0)
5508 {
5509 /* *INDENT-OFF* */
5510 ELOG_TYPE_DECLARE (e) =
5511 {
5512 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5513 .format_args = "i4i4",
5514 };
5515
5516 struct {
5517 u32 data[2];
5518 } * ed;
5519
5520 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5521 ed->data[0] = session->user_mss;
5522 ed->data[1] = *buflen;
5523 /* *INDENT-ON* */
5524 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005525 }
5526 else
5527 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005528 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04005529
5530 default:
5531 rv = VPPCOM_EINVAL;
5532 break;
Dave Wallace35830af2017-10-09 01:43:42 -04005533 }
5534
5535done:
5536 clib_spinlock_unlock (&vcm->sessions_lockp);
5537 return rv;
5538}
5539
Stevenac1f96d2017-10-24 16:03:58 -07005540int
5541vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5542 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5543{
Stevenac1f96d2017-10-24 16:03:58 -07005544 int rv = VPPCOM_OK;
5545 session_t *session = 0;
5546
5547 if (ep)
5548 {
5549 clib_spinlock_lock (&vcm->sessions_lockp);
5550 rv = vppcom_session_at_index (session_index, &session);
5551 if (PREDICT_FALSE (rv))
5552 {
5553 clib_spinlock_unlock (&vcm->sessions_lockp);
5554 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05005555 clib_warning ("VCL<%d>: invalid session, "
5556 "sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05005557 getpid (), session_index);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005558 if (VPPCOM_DEBUG > 0)
5559 {
5560 /* *INDENT-OFF* */
5561 ELOG_TYPE_DECLARE (e) =
5562 {
5563 .format = "invalid session: %d closed",
5564 .format_args = "i4",
5565 };
5566
5567 struct {
5568 u32 data;
5569 } * ed;
5570
5571 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5572 ed->data = session_index;
5573 /* *INDENT-ON* */
5574 }
Dave Wallacefaf9d772017-10-26 16:12:04 -04005575 rv = VPPCOM_EBADFD;
5576 clib_spinlock_unlock (&vcm->sessions_lockp);
5577 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07005578 }
5579 ep->vrf = session->vrf;
5580 ep->is_ip4 = session->peer_addr.is_ip4;
5581 ep->port = session->peer_port;
5582 if (session->peer_addr.is_ip4)
5583 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5584 sizeof (ip4_address_t));
5585 else
5586 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5587 sizeof (ip6_address_t));
5588 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07005589 }
Steven58f464e2017-10-25 12:33:12 -07005590
5591 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07005592 rv = vppcom_session_read (session_index, buffer, buflen);
5593 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07005594 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07005595 else
5596 {
Dave Wallace048b1d62018-01-03 22:24:41 -05005597 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5598 getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07005599 rv = VPPCOM_EAFNOSUPPORT;
5600 }
5601
Dave Wallacefaf9d772017-10-26 16:12:04 -04005602done:
Stevenac1f96d2017-10-24 16:03:58 -07005603 return rv;
5604}
5605
5606int
5607vppcom_session_sendto (uint32_t session_index, void *buffer,
5608 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5609{
Dave Wallace617dffa2017-10-26 14:47:06 -04005610 if (!buffer)
5611 return VPPCOM_EINVAL;
5612
5613 if (ep)
5614 {
5615 // TBD
5616 return VPPCOM_EINVAL;
5617 }
5618
5619 if (flags)
5620 {
5621 // TBD check the flags and do the right thing
5622 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05005623 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5624 "not implemented yet.", getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04005625 }
5626
5627 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07005628}
5629
Dave Wallace048b1d62018-01-03 22:24:41 -05005630int
5631vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5632{
5633 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5634 u32 i, keep_trying = 1;
5635 int rv, num_ev = 0;
5636
5637 if (VPPCOM_DEBUG > 3)
5638 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5639 getpid (), vp, n_sids, wait_for_time);
5640
5641 if (!vp)
5642 return VPPCOM_EFAULT;
5643
5644 do
5645 {
5646 session_t *session;
5647
5648 for (i = 0; i < n_sids; i++)
5649 {
5650 ASSERT (vp[i].revents);
5651
5652 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5653 clib_spinlock_unlock (&vcm->sessions_lockp);
5654
5655 if (*vp[i].revents)
5656 *vp[i].revents = 0;
5657
5658 if (POLLIN & vp[i].events)
5659 {
5660 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5661 rv = vppcom_session_read_ready (session, vp[i].sid);
5662 clib_spinlock_unlock (&vcm->sessions_lockp);
5663 if (rv > 0)
5664 {
5665 *vp[i].revents |= POLLIN;
5666 num_ev++;
5667 }
5668 else if (rv < 0)
5669 {
5670 switch (rv)
5671 {
5672 case VPPCOM_ECONNRESET:
5673 *vp[i].revents = POLLHUP;
5674 break;
5675
5676 default:
5677 *vp[i].revents = POLLERR;
5678 break;
5679 }
5680 num_ev++;
5681 }
5682 }
5683
5684 if (POLLOUT & vp[i].events)
5685 {
5686 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5687 rv = vppcom_session_write_ready (session, vp[i].sid);
5688 clib_spinlock_unlock (&vcm->sessions_lockp);
5689 if (rv > 0)
5690 {
5691 *vp[i].revents |= POLLOUT;
5692 num_ev++;
5693 }
5694 else if (rv < 0)
5695 {
5696 switch (rv)
5697 {
5698 case VPPCOM_ECONNRESET:
5699 *vp[i].revents = POLLHUP;
5700 break;
5701
5702 default:
5703 *vp[i].revents = POLLERR;
5704 break;
5705 }
5706 num_ev++;
5707 }
5708 }
5709
5710 if (0)
5711 {
5712 done:
5713 *vp[i].revents = POLLNVAL;
5714 num_ev++;
5715 }
5716 }
5717 if (wait_for_time != -1)
5718 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5719 }
5720 while ((num_ev == 0) && keep_trying);
5721
5722 if (VPPCOM_DEBUG > 3)
5723 {
5724 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5725 for (i = 0; i < n_sids; i++)
5726 {
5727 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5728 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5729 vp[i].events, *vp[i].revents);
5730 }
5731 }
5732 return num_ev;
5733}
5734
Dave Wallacee22aa742017-10-20 12:30:38 -04005735/*
5736 * fd.io coding-style-patch-verification: ON
5737 *
5738 * Local Variables:
5739 * eval: (c-set-style "gnu")
5740 * End:
5741 */