blob: 8c2036ca2868bb80ff1aaa0b347a21c48475f36f [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>
26
27#define vl_typedefs /* define message structures */
28#include <vpp/api/vpe_all_api_h.h>
29#undef vl_typedefs
30
31/* declare message handlers for each api */
32
33#define vl_endianfun /* define message structures */
34#include <vpp/api/vpe_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...)
39#define vl_printfun
40#include <vpp/api/vpe_all_api_h.h>
41#undef vl_printfun
42
43#if (CLIB_DEBUG > 0)
Dave Wallace498b3a52017-11-09 13:00:34 -050044/* Set VPPCOM_DEBUG_INIT 2 for connection debug,
45 * 3 for read/write debug output
46 * or
47 * export VCL_DEBUG=<#> to set dynamically.
48 */
49#define VPPCOM_DEBUG_INIT 1
Dave Wallace543852a2017-08-03 02:11:34 -040050#else
Dave Wallace498b3a52017-11-09 13:00:34 -050051#define VPPCOM_DEBUG_INIT 0
Dave Wallace543852a2017-08-03 02:11:34 -040052#endif
53
Dave Wallace498b3a52017-11-09 13:00:34 -050054#define VPPCOM_DEBUG vcm->debug
55
Dave Wallace543852a2017-08-03 02:11:34 -040056/*
57 * VPPCOM Private definitions and functions.
58 */
59typedef enum
60{
61 STATE_APP_START,
62 STATE_APP_CONN_VPP,
63 STATE_APP_ENABLED,
64 STATE_APP_ATTACHED,
65} app_state_t;
66
67typedef enum
68{
69 STATE_START,
70 STATE_CONNECT,
71 STATE_LISTEN,
72 STATE_ACCEPT,
73 STATE_DISCONNECT,
74 STATE_FAILED
75} session_state_t;
76
Dave Wallacef7f809c2017-10-03 01:48:42 -040077typedef struct epoll_event vppcom_epoll_event_t;
78
79typedef struct
80{
81 u32 next_sid;
82 u32 prev_sid;
83 u32 vep_idx;
84 vppcom_epoll_event_t ev;
85#define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
Dave Wallace60caa062017-11-10 17:07:13 -050086#define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
Dave Wallacef7f809c2017-10-03 01:48:42 -040087 u32 et_mask;
88} vppcom_epoll_t;
89
Dave Wallace543852a2017-08-03 02:11:34 -040090typedef struct
91{
Dave Wallace35830af2017-10-09 01:43:42 -040092 u8 is_ip4;
93 ip46_address_t ip46;
94} vppcom_ip46_t;
95
96typedef struct
97{
Dave Wallace543852a2017-08-03 02:11:34 -040098 volatile session_state_t state;
99
100 svm_fifo_t *server_rx_fifo;
101 svm_fifo_t *server_tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500102 u8 *segment_name;
Dave Wallace543852a2017-08-03 02:11:34 -0400103 u32 sm_seg_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500104 u32 client_context;
105 u64 vpp_handle;
Dave Wallace33e002b2017-09-06 01:20:02 -0400106 unix_shared_memory_queue_t *vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400107
108 /* Socket configuration state */
Dave Wallacef7f809c2017-10-03 01:48:42 -0400109 /* TBD: covert 'is_*' vars to bit in u8 flags; */
Dave Wallace543852a2017-08-03 02:11:34 -0400110 u8 is_server;
111 u8 is_listen;
112 u8 is_cut_thru;
113 u8 is_nonblocking;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400114 u8 is_vep;
115 u8 is_vep_session;
116 u32 wait_cont_idx;
117 vppcom_epoll_t vep;
Dave Wallace543852a2017-08-03 02:11:34 -0400118 u32 vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400119 vppcom_ip46_t lcl_addr;
120 vppcom_ip46_t peer_addr;
Stevenac1f96d2017-10-24 16:03:58 -0700121 u16 lcl_port; // network order
122 u16 peer_port; // network order
Dave Wallace543852a2017-08-03 02:11:34 -0400123 u8 proto;
124 u64 client_queue_address;
125 u64 options[16];
126} session_t;
127
128typedef struct vppcom_cfg_t_
129{
130 u64 heapsize;
131 u64 segment_baseva;
132 u32 segment_size;
133 u32 add_segment_size;
134 u32 preallocated_fifo_pairs;
135 u32 rx_fifo_size;
136 u32 tx_fifo_size;
137 u32 event_queue_size;
138 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400139 u8 app_proxy_transport_tcp;
140 u8 app_proxy_transport_udp;
141 u8 app_scope_local;
142 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400143 u8 *namespace_id;
144 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400145 f64 app_timeout;
146 f64 session_timeout;
147 f64 accept_timeout;
148} vppcom_cfg_t;
149
150typedef struct vppcom_main_t_
151{
152 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500153 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400154 u32 *client_session_index_fifo;
155 volatile u32 bind_session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400156 int main_cpu;
157
158 /* vpe input queue */
159 unix_shared_memory_queue_t *vl_input_queue;
160
161 /* API client handle */
162 u32 my_client_index;
163
164 /* Session pool */
165 clib_spinlock_t sessions_lockp;
166 session_t *sessions;
167
168 /* Hash table for disconnect processing */
169 uword *session_index_by_vpp_handles;
170
171 /* Select bitmaps */
172 clib_bitmap_t *rd_bitmap;
173 clib_bitmap_t *wr_bitmap;
174 clib_bitmap_t *ex_bitmap;
175
176 /* Our event queue */
177 unix_shared_memory_queue_t *app_event_queue;
178
179 /* unique segment name counter */
180 u32 unique_segment_index;
181
Dave Wallace543852a2017-08-03 02:11:34 -0400182 /* For deadman timers */
183 clib_time_t clib_time;
184
185 /* State of the connection, shared between msg RX thread and main thread */
186 volatile app_state_t app_state;
187
188 vppcom_cfg_t cfg;
189
190 /* VNET_API_ERROR_FOO -> "Foo" hash table */
191 uword *error_string_by_error_number;
192} vppcom_main_t;
193
Dave Wallace2e005bb2017-11-07 01:21:39 -0500194/* NOTE: _vppcom_main is only used until the heap is allocated.
195 * Do not access it directly -- use vcm which will point to
196 * the heap allocated copy after init.
197 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500198static vppcom_main_t _vppcom_main = {
199 .debug = VPPCOM_DEBUG_INIT,
200 .my_client_index = ~0
201};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500202
203static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400204
Dave Wallace60caa062017-11-10 17:07:13 -0500205#define VCL_LOCK_AND_GET_SESSION(I, S) \
206do { \
207 clib_spinlock_lock (&vcm->sessions_lockp); \
208 rv = vppcom_session_at_index (I, S); \
209 if (PREDICT_FALSE (rv)) \
210 { \
211 clib_spinlock_unlock (&vcm->sessions_lockp); \
212 \
213 if (VPPCOM_DEBUG > 0) \
214 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
215 getpid (), I); \
216 \
217 goto done; \
218 } \
219} while (0)
220
Dave Wallace543852a2017-08-03 02:11:34 -0400221static const char *
222vppcom_app_state_str (app_state_t state)
223{
224 char *st;
225
226 switch (state)
227 {
228 case STATE_APP_START:
229 st = "STATE_APP_START";
230 break;
231
232 case STATE_APP_CONN_VPP:
233 st = "STATE_APP_CONN_VPP";
234 break;
235
236 case STATE_APP_ENABLED:
237 st = "STATE_APP_ENABLED";
238 break;
239
240 case STATE_APP_ATTACHED:
241 st = "STATE_APP_ATTACHED";
242 break;
243
244 default:
245 st = "UNKNOWN_APP_STATE";
246 break;
247 }
248
249 return st;
250}
251
252static const char *
253vppcom_session_state_str (session_state_t state)
254{
255 char *st;
256
257 switch (state)
258 {
259 case STATE_START:
260 st = "STATE_START";
261 break;
262
263 case STATE_CONNECT:
264 st = "STATE_CONNECT";
265 break;
266
267 case STATE_LISTEN:
268 st = "STATE_LISTEN";
269 break;
270
271 case STATE_ACCEPT:
272 st = "STATE_ACCEPT";
273 break;
274
275 case STATE_DISCONNECT:
276 st = "STATE_DISCONNECT";
277 break;
278
279 case STATE_FAILED:
280 st = "STATE_FAILED";
281 break;
282
283 default:
284 st = "UNKNOWN_STATE";
285 break;
286 }
287
288 return st;
289}
290
291/*
292 * VPPCOM Utility Functions
293 */
294static inline int
295vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
296{
Dave Wallace543852a2017-08-03 02:11:34 -0400297 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
298 if (PREDICT_FALSE ((session_index == ~0) ||
299 pool_is_free_index (vcm->sessions, session_index)))
300 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400301 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500302 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400303 return VPPCOM_EBADFD;
304 }
305 *sess = pool_elt_at_index (vcm->sessions, session_index);
306 return VPPCOM_OK;
307}
308
Florin Corasdcf55ce2017-11-16 15:32:50 -0800309static inline void
310vppcom_session_table_add_listener (u64 listener_handle, u32 value)
311{
312 /* Session and listener handles have different formats. The latter has
313 * the thread index in the upper 32 bits while the former has the session
314 * type. Knowing that, for listeners we just flip the MSB to 1 */
315 listener_handle |= 1ULL << 63;
316 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
317}
318
319static inline session_t *
320vppcom_session_table_lookup_listener (u64 listener_handle)
321{
322 uword *p;
323 u64 handle = listener_handle | (1ULL << 63);
324 p = hash_get (vcm->session_index_by_vpp_handles, handle);
325 if (!p)
326 {
327 clib_warning ("[%d] couldn't find listen session: unknown vpp "
328 "listener handle %llx", getpid (), listener_handle);
329 return 0;
330 }
331 if (pool_is_free_index (vcm->sessions, p[0]))
332 {
333 if (VPPCOM_DEBUG > 1)
334 clib_warning ("[%d] invalid listen session, sid (%u)", getpid (),
335 p[0]);
336 return 0;
337 }
338
339 return pool_elt_at_index (vcm->sessions, p[0]);
340}
341
342static inline void
343vppcom_session_table_del_listener (u64 listener_handle)
344{
345 listener_handle |= 1ULL << 63;
346 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
347}
348
Dave Wallace543852a2017-08-03 02:11:34 -0400349static int
350vppcom_connect_to_vpp (char *app_name)
351{
352 api_main_t *am = &api_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400353
354 if (VPPCOM_DEBUG > 0)
355 printf ("\nConnecting to VPP api...");
356 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
357 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500358 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400359 return VPPCOM_ECONNREFUSED;
360 }
361
362 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
363 vcm->my_client_index = am->my_client_index;
364 if (VPPCOM_DEBUG > 0)
365 printf (" connected!\n");
366
367 vcm->app_state = STATE_APP_CONN_VPP;
368 return VPPCOM_OK;
369}
370
371static u8 *
372format_api_error (u8 * s, va_list * args)
373{
Dave Wallace543852a2017-08-03 02:11:34 -0400374 i32 error = va_arg (*args, u32);
375 uword *p;
376
377 p = hash_get (vcm->error_string_by_error_number, -error);
378
379 if (p)
380 s = format (s, "%s (%d)", p[0], error);
381 else
382 s = format (s, "%d", error);
383 return s;
384}
385
386static void
387vppcom_init_error_string_table (void)
388{
Dave Wallace543852a2017-08-03 02:11:34 -0400389 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
390
391#define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
392 foreach_vnet_api_error;
393#undef _
394
395 hash_set (vcm->error_string_by_error_number, 99, "Misc");
396}
397
398static inline int
399vppcom_wait_for_app_state_change (app_state_t app_state)
400{
Dave Wallace543852a2017-08-03 02:11:34 -0400401 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
402
403 while (clib_time_now (&vcm->clib_time) < timeout)
404 {
405 if (vcm->app_state == app_state)
406 return VPPCOM_OK;
407 }
408 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500409 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400410 vppcom_app_state_str (app_state), app_state);
411 return VPPCOM_ETIMEDOUT;
412}
413
414static inline int
415vppcom_wait_for_session_state_change (u32 session_index,
416 session_state_t state,
417 f64 wait_for_time)
418{
Dave Wallace543852a2017-08-03 02:11:34 -0400419 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
420 session_t *volatile session;
421 int rv;
422
423 do
424 {
425 clib_spinlock_lock (&vcm->sessions_lockp);
426 rv = vppcom_session_at_index (session_index, &session);
427 if (PREDICT_FALSE (rv))
428 {
429 clib_spinlock_unlock (&vcm->sessions_lockp);
430 return rv;
431 }
432 if (session->state == state)
433 {
434 clib_spinlock_unlock (&vcm->sessions_lockp);
435 return VPPCOM_OK;
436 }
437 clib_spinlock_unlock (&vcm->sessions_lockp);
438 }
439 while (clib_time_now (&vcm->clib_time) < timeout);
440
441 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500442 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400443 vppcom_session_state_str (state), state);
444 return VPPCOM_ETIMEDOUT;
445}
446
447static inline int
448vppcom_wait_for_client_session_index (f64 wait_for_time)
449{
Dave Wallace543852a2017-08-03 02:11:34 -0400450 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
451
452 do
453 {
454 if (clib_fifo_elts (vcm->client_session_index_fifo))
455 return VPPCOM_OK;
456 }
457 while (clib_time_now (&vcm->clib_time) < timeout);
458
459 if (wait_for_time == 0)
460 return VPPCOM_EAGAIN;
461
462 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500463 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400464 return VPPCOM_ETIMEDOUT;
465}
466
467/*
468 * VPP-API message functions
469 */
470static void
471vppcom_send_session_enable_disable (u8 is_enable)
472{
Dave Wallace543852a2017-08-03 02:11:34 -0400473 vl_api_session_enable_disable_t *bmp;
474 bmp = vl_msg_api_alloc (sizeof (*bmp));
475 memset (bmp, 0, sizeof (*bmp));
476
477 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
478 bmp->client_index = vcm->my_client_index;
479 bmp->context = htonl (0xfeedface);
480 bmp->is_enable = is_enable;
481 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
482}
483
484static int
485vppcom_app_session_enable (void)
486{
Dave Wallace543852a2017-08-03 02:11:34 -0400487 int rv;
488
489 if (vcm->app_state != STATE_APP_ENABLED)
490 {
491 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
492 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
493 if (PREDICT_FALSE (rv))
494 {
495 if (VPPCOM_DEBUG > 0)
496 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500497 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400498 return rv;
499 }
500 }
501 return VPPCOM_OK;
502}
503
504static void
505 vl_api_session_enable_disable_reply_t_handler
506 (vl_api_session_enable_disable_reply_t * mp)
507{
Dave Wallace543852a2017-08-03 02:11:34 -0400508 if (mp->retval)
509 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500510 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400511 format_api_error, ntohl (mp->retval));
512 }
513 else
514 vcm->app_state = STATE_APP_ENABLED;
515}
516
517static void
518vppcom_app_send_attach (void)
519{
Dave Wallace543852a2017-08-03 02:11:34 -0400520 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400521 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400522 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
523 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400524
Dave Wallace543852a2017-08-03 02:11:34 -0400525 bmp = vl_msg_api_alloc (sizeof (*bmp));
526 memset (bmp, 0, sizeof (*bmp));
527
528 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
529 bmp->client_index = vcm->my_client_index;
530 bmp->context = htonl (0xfeedface);
531 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700532 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400533 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
534 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
535 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
536 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
537 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
538 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Dave Wallace543852a2017-08-03 02:11:34 -0400539 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
540 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
541 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
542 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400543 if (nsid_len)
544 {
545 bmp->namespace_id_len = nsid_len;
546 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
547 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
548 }
Dave Wallace543852a2017-08-03 02:11:34 -0400549 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
550}
551
552static int
553vppcom_app_attach (void)
554{
Dave Wallace543852a2017-08-03 02:11:34 -0400555 int rv;
556
557 vppcom_app_send_attach ();
558 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
559 if (PREDICT_FALSE (rv))
560 {
561 if (VPPCOM_DEBUG > 0)
562 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500563 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400564 return rv;
565 }
566 return VPPCOM_OK;
567}
568
569static void
570vppcom_app_detach (void)
571{
Dave Wallace543852a2017-08-03 02:11:34 -0400572 vl_api_application_detach_t *bmp;
573 bmp = vl_msg_api_alloc (sizeof (*bmp));
574 memset (bmp, 0, sizeof (*bmp));
575
576 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
577 bmp->client_index = vcm->my_client_index;
578 bmp->context = htonl (0xfeedface);
579 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
580}
581
582static void
583vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
584 mp)
585{
Dave Wallace543852a2017-08-03 02:11:34 -0400586 static svm_fifo_segment_create_args_t _a;
587 svm_fifo_segment_create_args_t *a = &_a;
588 int rv;
589
590 memset (a, 0, sizeof (*a));
591 if (mp->retval)
592 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500593 clib_warning ("[%d] attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400594 format_api_error, ntohl (mp->retval));
595 return;
596 }
597
598 if (mp->segment_name_length == 0)
599 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500600 clib_warning ("[%d] segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400601 return;
602 }
603
604 a->segment_name = (char *) mp->segment_name;
605 a->segment_size = mp->segment_size;
606
607 ASSERT (mp->app_event_queue_address);
608
609 /* Attach to the segment vpp created */
610 rv = svm_fifo_segment_attach (a);
611 vec_reset_length (a->new_segment_indices);
612 if (PREDICT_FALSE (rv))
613 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500614 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400615 mp->segment_name);
616 return;
617 }
618
619 vcm->app_event_queue =
620 uword_to_pointer (mp->app_event_queue_address,
621 unix_shared_memory_queue_t *);
622
623 vcm->app_state = STATE_APP_ATTACHED;
624}
625
626static void
627vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
628 mp)
629{
Dave Wallace543852a2017-08-03 02:11:34 -0400630 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500631 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400632 ntohl (mp->retval));
633
634 vcm->app_state = STATE_APP_ENABLED;
635}
636
637static void
638vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
639 mp)
640{
Dave Wallace543852a2017-08-03 02:11:34 -0400641 uword *p;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500642 u32 session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -0400643
644 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
645 if (p)
646 {
647 session_t *session = 0;
648 int rv;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500649
650 session_index = p[0];
651 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400652 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace3bd43b82017-11-11 22:45:38 -0500653 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400654 if (PREDICT_FALSE (rv))
655 {
656 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500657 clib_warning ("[%d] invalid session, sid %u has been closed!",
658 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400659 }
Dave Wallace3bd43b82017-11-11 22:45:38 -0500660 else
661 {
662 if (VPPCOM_DEBUG > 1)
663 clib_warning ("[%d] vpp handle 0x%llx, sid %u disconnected!",
664 getpid (), mp->handle, session_index);
665
666 session->state = STATE_DISCONNECT;
667 }
Dave Wallace543852a2017-08-03 02:11:34 -0400668 clib_spinlock_unlock (&vcm->sessions_lockp);
669 }
670 else
671 {
672 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500673 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
674 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400675 }
676
677 if (mp->retval)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500678 clib_warning ("[%d] disconnect_session vpp handle 0x%llx, sid %u "
679 "failed: %U", getpid (), mp->handle, session_index,
Dave Wallace543852a2017-08-03 02:11:34 -0400680 format_api_error, ntohl (mp->retval));
681}
682
683static void
684vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
685{
Dave Wallace543852a2017-08-03 02:11:34 -0400686 static svm_fifo_segment_create_args_t _a;
687 svm_fifo_segment_create_args_t *a = &_a;
688 int rv;
689
690 memset (a, 0, sizeof (*a));
691 a->segment_name = (char *) mp->segment_name;
692 a->segment_size = mp->segment_size;
693 /* Attach to the segment vpp created */
694 rv = svm_fifo_segment_attach (a);
695 vec_reset_length (a->new_segment_indices);
696 if (PREDICT_FALSE (rv))
697 {
698 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500699 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400700 return;
701 }
702 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500703 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400704 mp->segment_name, mp->segment_size);
705}
706
707static void
708vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
709{
Dave Wallace543852a2017-08-03 02:11:34 -0400710 session_t *session = 0;
711 vl_api_disconnect_session_reply_t *rmp;
712 uword *p;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800713 int rv = 0, rval;
Dave Wallace543852a2017-08-03 02:11:34 -0400714
715 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800716 if (VPPCOM_DEBUG > 0)
717 {
718 if (!p)
719 {
720 clib_warning ("[%d] request to disconnect invalid handle (%u)!",
721 getpid (), mp->handle);
722 rv = -11;
723 goto done;
724 }
725 clib_warning ("[%d] disconnecting handle %u sid (%u)!", getpid (),
726 mp->handle, p[0]);
727 }
728
729 goto done;
730
Dave Wallace543852a2017-08-03 02:11:34 -0400731 if (p)
732 {
Dave Wallace543852a2017-08-03 02:11:34 -0400733 clib_spinlock_lock (&vcm->sessions_lockp);
734 rval = vppcom_session_at_index (p[0], &session);
735 if (PREDICT_FALSE (rval))
736 {
737 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400738 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500739 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400740 }
741 else
742 pool_put (vcm->sessions, session);
743 clib_spinlock_unlock (&vcm->sessions_lockp);
744 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
745 }
746 else
747 {
Dave Wallace227867f2017-11-13 21:21:53 -0500748 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500749 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400750 rv = -11;
751 }
752
Florin Corasdcf55ce2017-11-16 15:32:50 -0800753done:
Dave Wallace543852a2017-08-03 02:11:34 -0400754 rmp = vl_msg_api_alloc (sizeof (*rmp));
755 memset (rmp, 0, sizeof (*rmp));
756
757 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
758 rmp->retval = htonl (rv);
759 rmp->handle = mp->handle;
760 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
761}
762
763static void
764vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
765{
Dave Wallace543852a2017-08-03 02:11:34 -0400766 session_t *session = 0;
767 vl_api_reset_session_reply_t *rmp;
768 uword *p;
769 int rv = 0;
770
771 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
772 if (p)
773 {
774 int rval;
775 clib_spinlock_lock (&vcm->sessions_lockp);
776 rval = vppcom_session_at_index (p[0], &session);
777 if (PREDICT_FALSE (rval))
778 {
779 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400780 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500781 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400782 }
783 else
784 pool_put (vcm->sessions, session);
785 clib_spinlock_unlock (&vcm->sessions_lockp);
786 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
787 }
788 else
789 {
Dave Wallace3bd43b82017-11-11 22:45:38 -0500790 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
791 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400792 rv = -11;
793 }
794
795 rmp = vl_msg_api_alloc (sizeof (*rmp));
796 memset (rmp, 0, sizeof (*rmp));
797 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
798 rmp->retval = htonl (rv);
799 rmp->handle = mp->handle;
800 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
801}
802
803static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400804vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400805{
Dave Wallace543852a2017-08-03 02:11:34 -0400806 session_t *session;
807 u32 session_index;
808 svm_fifo_t *rx_fifo, *tx_fifo;
809 u8 is_cut_thru = 0;
810 int rv;
811
812 if (mp->retval)
813 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500814 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400815 ntohl (mp->retval));
816 return;
817 }
818
Dave Wallace33e002b2017-09-06 01:20:02 -0400819 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -0400820 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500821 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400822 session_index, session_index);
823
824 clib_spinlock_lock (&vcm->sessions_lockp);
825 if (pool_is_free_index (vcm->sessions, session_index))
826 {
827 clib_spinlock_unlock (&vcm->sessions_lockp);
828 if (VPPCOM_DEBUG > 1)
829 clib_warning ("[%d] invalid session, sid %d is closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500830 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400831 return;
832 }
833
834 /* We've been redirected */
835 if (mp->segment_name_length > 0)
836 {
837 static svm_fifo_segment_create_args_t _a;
838 svm_fifo_segment_create_args_t *a = &_a;
839
840 is_cut_thru = 1;
841 memset (a, 0, sizeof (*a));
842 a->segment_name = (char *) mp->segment_name;
843 if (VPPCOM_DEBUG > 1)
Dave Wallace60caa062017-11-10 17:07:13 -0500844 clib_warning ("[%d] cut-thru segment: %s\n",
845 getpid (), a->segment_name);
846
Dave Wallace543852a2017-08-03 02:11:34 -0400847 rv = svm_fifo_segment_attach (a);
848 vec_reset_length (a->new_segment_indices);
849 if (PREDICT_FALSE (rv))
850 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400851 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400852 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500853 getpid (), a->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400854 return;
855 }
856 }
857
858 /*
859 * Setup session
860 */
Dave Wallace543852a2017-08-03 02:11:34 -0400861 session = pool_elt_at_index (vcm->sessions, session_index);
862 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -0400863 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
864 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400865
866 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
867 rx_fifo->client_session_index = session_index;
868 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
869 tx_fifo->client_session_index = session_index;
870
871 session->server_rx_fifo = rx_fifo;
872 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500873 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400874 session->state = STATE_CONNECT;
875
876 /* Add it to lookup table */
877 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500878
879 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500880 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
Dave Wallace60caa062017-11-10 17:07:13 -0500881 " session_rx_fifo %p, refcnt %d\n"
882 " session_tx_fifo %p, refcnt %d",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500883 getpid (), session_index, mp->handle,
Dave Wallace60caa062017-11-10 17:07:13 -0500884 session->server_rx_fifo,
885 session->server_rx_fifo->refcnt,
886 session->server_tx_fifo, session->server_tx_fifo->refcnt);
887
Dave Wallace543852a2017-08-03 02:11:34 -0400888 clib_spinlock_unlock (&vcm->sessions_lockp);
889}
890
891static void
892vppcom_send_connect_sock (session_t * session, u32 session_index)
893{
Dave Wallace543852a2017-08-03 02:11:34 -0400894 vl_api_connect_sock_t *cmp;
895
896 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
897 session->is_server = 0;
898 cmp = vl_msg_api_alloc (sizeof (*cmp));
899 memset (cmp, 0, sizeof (*cmp));
900 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
901 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400902 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400903
904 if (VPPCOM_DEBUG > 1)
Dave Wallace33e002b2017-09-06 01:20:02 -0400905 clib_warning ("[%d] session_index = %d 0x%08x",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500906 getpid (), session_index, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400907
908 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400909 cmp->is_ip4 = session->peer_addr.is_ip4;
910 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -0700911 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400912 cmp->proto = session->proto;
913 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
914 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
915}
916
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400917static inline void
918vppcom_send_disconnect (session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -0400919{
Dave Wallace543852a2017-08-03 02:11:34 -0400920 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400921
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400922 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400923 dmp = vl_msg_api_alloc (sizeof (*dmp));
924 memset (dmp, 0, sizeof (*dmp));
925 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
926 dmp->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500927 dmp->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400928 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400929}
930
931static void
932vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
933{
Dave Wallace543852a2017-08-03 02:11:34 -0400934 session_t *session = 0;
935 int rv;
936
937 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500938 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400939 ntohl (mp->retval));
940
941 ASSERT (vcm->bind_session_index != ~0);
942
943 clib_spinlock_lock (&vcm->sessions_lockp);
944 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
945 if (rv == VPPCOM_OK)
946 {
Dave Wallace60caa062017-11-10 17:07:13 -0500947 session->vpp_handle = mp->handle;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800948 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
949 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
950 sizeof (session->peer_addr.ip46));
951 session->lcl_port = mp->lcl_port;
952 vppcom_session_table_add_listener (mp->handle, vcm->bind_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400953 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
954 vcm->bind_session_index = ~0;
955 }
956 clib_spinlock_unlock (&vcm->sessions_lockp);
957}
958
959static void
960vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
961{
Dave Wallace543852a2017-08-03 02:11:34 -0400962 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400963
964 clib_spinlock_lock (&vcm->sessions_lockp);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800965 session = vppcom_session_table_lookup_listener (vcm->bind_session_index);
966
967 if (session)
Dave Wallace543852a2017-08-03 02:11:34 -0400968 {
Dave Wallace19481612017-09-15 18:47:44 -0400969 if ((VPPCOM_DEBUG > 1) && (mp->retval))
Dave Wallace2e005bb2017-11-07 01:21:39 -0500970 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
Dave Wallace19481612017-09-15 18:47:44 -0400971 ntohl (mp->retval));
972
Florin Corasdcf55ce2017-11-16 15:32:50 -0800973 vppcom_session_table_del_listener (vcm->bind_session_index);
Dave Wallace19481612017-09-15 18:47:44 -0400974 vcm->bind_session_index = ~0;
975 session->state = STATE_START;
Dave Wallace543852a2017-08-03 02:11:34 -0400976 }
Dave Wallace543852a2017-08-03 02:11:34 -0400977 clib_spinlock_unlock (&vcm->sessions_lockp);
978}
979
980u8 *
981format_ip4_address (u8 * s, va_list * args)
982{
983 u8 *a = va_arg (*args, u8 *);
984 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
985}
986
987u8 *
988format_ip6_address (u8 * s, va_list * args)
989{
990 ip6_address_t *a = va_arg (*args, ip6_address_t *);
991 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
992
993 i_max_n_zero = ARRAY_LEN (a->as_u16);
994 max_n_zeros = 0;
995 i_first_zero = i_max_n_zero;
996 n_zeros = 0;
997 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
998 {
999 u32 is_zero = a->as_u16[i] == 0;
1000 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1001 {
1002 i_first_zero = i;
1003 n_zeros = 0;
1004 }
1005 n_zeros += is_zero;
1006 if ((!is_zero && n_zeros > max_n_zeros)
1007 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1008 {
1009 i_max_n_zero = i_first_zero;
1010 max_n_zeros = n_zeros;
1011 i_first_zero = ARRAY_LEN (a->as_u16);
1012 n_zeros = 0;
1013 }
1014 }
1015
1016 last_double_colon = 0;
1017 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1018 {
1019 if (i == i_max_n_zero && max_n_zeros > 1)
1020 {
1021 s = format (s, "::");
1022 i += max_n_zeros - 1;
1023 last_double_colon = 1;
1024 }
1025 else
1026 {
1027 s = format (s, "%s%x",
1028 (last_double_colon || i == 0) ? "" : ":",
1029 clib_net_to_host_u16 (a->as_u16[i]));
1030 last_double_colon = 0;
1031 }
1032 }
1033
1034 return s;
1035}
1036
1037/* Format an IP46 address. */
1038u8 *
1039format_ip46_address (u8 * s, va_list * args)
1040{
1041 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1042 ip46_type_t type = va_arg (*args, ip46_type_t);
1043 int is_ip4 = 1;
1044
1045 switch (type)
1046 {
1047 case IP46_TYPE_ANY:
1048 is_ip4 = ip46_address_is_ip4 (ip46);
1049 break;
1050 case IP46_TYPE_IP4:
1051 is_ip4 = 1;
1052 break;
1053 case IP46_TYPE_IP6:
1054 is_ip4 = 0;
1055 break;
1056 }
1057
1058 return is_ip4 ?
1059 format (s, "%U", format_ip4_address, &ip46->ip4) :
1060 format (s, "%U", format_ip6_address, &ip46->ip6);
1061}
1062
Dave Wallace60caa062017-11-10 17:07:13 -05001063static inline void
1064vppcom_send_accept_session_reply (u32 handle, int retval)
1065{
1066 vl_api_accept_session_reply_t *rmp;
1067
1068 rmp = vl_msg_api_alloc (sizeof (*rmp));
1069 memset (rmp, 0, sizeof (*rmp));
1070 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1071 rmp->retval = htonl (retval);
1072 rmp->handle = handle;
1073 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1074}
1075
Dave Wallace543852a2017-08-03 02:11:34 -04001076static void
1077vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1078{
Dave Wallace543852a2017-08-03 02:11:34 -04001079 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001080 session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001081 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001082
Dave Wallace60caa062017-11-10 17:07:13 -05001083 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001084 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1085 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001086 clib_warning ("[%d] client session queue is full!", getpid ());
Florin Corasdcf55ce2017-11-16 15:32:50 -08001087 vppcom_send_accept_session_reply (mp->handle,
1088 VNET_API_ERROR_QUEUE_FULL);
1089 clib_spinlock_unlock (&vcm->sessions_lockp);
1090 return;
1091 }
1092
1093 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1094 if (!listen_session)
1095 {
1096 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1097 "listener handle %llx", getpid (), mp->listener_handle);
Dave Wallace60caa062017-11-10 17:07:13 -05001098 clib_spinlock_unlock (&vcm->sessions_lockp);
1099 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001100 }
1101
Dave Wallace543852a2017-08-03 02:11:34 -04001102 /* Allocate local session and set it up */
1103 pool_get (vcm->sessions, session);
1104 memset (session, 0, sizeof (*session));
1105 session_index = session - vcm->sessions;
1106
1107 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1108 rx_fifo->client_session_index = session_index;
1109 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1110 tx_fifo->client_session_index = session_index;
1111
Dave Wallace60caa062017-11-10 17:07:13 -05001112 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001113 session->server_rx_fifo = rx_fifo;
1114 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001115 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1116 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001117 session->state = STATE_ACCEPT;
1118 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001119 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001120 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001121 session->peer_addr.is_ip4 = mp->is_ip4;
1122 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1123 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001124
1125 /* Add it to lookup table */
1126 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001127 session->lcl_port = listen_session->lcl_port;
1128 session->lcl_addr = listen_session->lcl_addr;
Dave Wallace227867f2017-11-13 21:21:53 -05001129
1130 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001131 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001132
Dave Wallacef7f809c2017-10-03 01:48:42 -04001133 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001134
Dave Wallace60caa062017-11-10 17:07:13 -05001135 if (VPPCOM_DEBUG > 1)
1136 {
1137 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1138 clib_warning ("[%d] received request to accept session (sid %d) "
1139 "from %s:%d", getpid (), session_index, ip_str,
1140 clib_net_to_host_u16 (mp->port));
1141 vec_free (ip_str);
1142 }
1143}
1144
1145static void
Dave Wallace227867f2017-11-13 21:21:53 -05001146vppcom_send_connect_session_reply (session_t * session, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001147{
1148 vl_api_connect_session_reply_t *rmp;
1149 u32 len;
1150 unix_shared_memory_queue_t *client_q;
1151
Dave Wallace543852a2017-08-03 02:11:34 -04001152 rmp = vl_msg_api_alloc (sizeof (*rmp));
1153 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001154
1155 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallace227867f2017-11-13 21:21:53 -05001156 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001157 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001158 rmp->handle = session->vpp_handle;
1159 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1160 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1161 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1162 rmp->segment_size = vcm->cfg.segment_size;
1163 len = vec_len (session->segment_name);
1164 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1165 clib_memcpy (rmp->segment_name, session->segment_name,
1166 rmp->segment_name_length - 1);
1167 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1168 sizeof (rmp->lcl_ip));
1169 rmp->is_ip4 = session->lcl_addr.is_ip4;
Dave Wallace60caa062017-11-10 17:07:13 -05001170 client_q = uword_to_pointer (session->client_queue_address,
1171 unix_shared_memory_queue_t *);
1172 ASSERT (client_q);
1173 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001174}
1175
1176/*
1177 * Acting as server for redirected connect requests
1178 */
1179static void
1180vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1181{
Dave Wallace543852a2017-08-03 02:11:34 -04001182 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001183 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001184
Dave Wallacef7f809c2017-10-03 01:48:42 -04001185 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001186 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1187 {
1188 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001189 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001190 clib_spinlock_unlock (&vcm->sessions_lockp);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001191 /* TBD: fix handle */
1192 vppcom_send_accept_session_reply (0, VNET_API_ERROR_QUEUE_FULL);
Dave Wallace60caa062017-11-10 17:07:13 -05001193 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001194 }
1195
Dave Wallace543852a2017-08-03 02:11:34 -04001196 pool_get (vcm->sessions, session);
1197 memset (session, 0, sizeof (*session));
1198 session_index = session - vcm->sessions;
1199
Dave Wallace60caa062017-11-10 17:07:13 -05001200 session->client_context = mp->context;
1201 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001202 session->client_queue_address = mp->client_queue_address;
1203 session->is_cut_thru = 1;
1204 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001205 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001206 session->peer_addr.is_ip4 = mp->is_ip4;
1207 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1208 sizeof (session->peer_addr.ip46));
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001209
Dave Wallace543852a2017-08-03 02:11:34 -04001210 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001211 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001212 if (VPPCOM_DEBUG > 1)
1213 clib_warning
Dave Wallace60caa062017-11-10 17:07:13 -05001214 ("[%d] Got a cut-thru connect request to client: "
1215 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
Dave Wallace2e005bb2017-11-07 01:21:39 -05001216 clib_fifo_elts (vcm->client_session_index_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001217 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001218}
1219
1220static void
1221vppcom_send_bind_sock (session_t * session)
1222{
Dave Wallace543852a2017-08-03 02:11:34 -04001223 vl_api_bind_sock_t *bmp;
1224
1225 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1226 session->is_server = 1;
1227 bmp = vl_msg_api_alloc (sizeof (*bmp));
1228 memset (bmp, 0, sizeof (*bmp));
1229
1230 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1231 bmp->client_index = vcm->my_client_index;
1232 bmp->context = htonl (0xfeedface);
1233 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001234 bmp->is_ip4 = session->lcl_addr.is_ip4;
1235 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001236 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001237 bmp->proto = session->proto;
1238 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1239 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1240}
1241
1242static void
1243vppcom_send_unbind_sock (u32 session_index)
1244{
Dave Wallace543852a2017-08-03 02:11:34 -04001245 vl_api_unbind_sock_t *ump;
1246 session_t *session = 0;
1247 int rv;
1248
1249 clib_spinlock_lock (&vcm->sessions_lockp);
1250 rv = vppcom_session_at_index (session_index, &session);
1251 if (PREDICT_FALSE (rv))
1252 {
1253 clib_spinlock_unlock (&vcm->sessions_lockp);
1254 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001255 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001256 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001257 return;
1258 }
1259
1260 ump = vl_msg_api_alloc (sizeof (*ump));
1261 memset (ump, 0, sizeof (*ump));
1262
1263 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1264 ump->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -05001265 ump->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001266 clib_spinlock_unlock (&vcm->sessions_lockp);
1267 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1268}
1269
1270static int
Dave Wallace60caa062017-11-10 17:07:13 -05001271vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001272{
1273 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1274 svm_fifo_segment_private_t *seg;
1275 int rv = VPPCOM_OK;
1276
Dave Wallace60caa062017-11-10 17:07:13 -05001277 if (VPPCOM_DEBUG > 1)
1278 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1279 " server_rx_fifo %p, refcnt = %d\n"
1280 " server_tx_fifo %p, refcnt = %d",
1281 getpid (), session_index, session->sm_seg_index,
1282 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1283 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1284
Dave Wallace543852a2017-08-03 02:11:34 -04001285 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1286 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1287 FIFO_SEGMENT_RX_FREELIST);
1288 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1289 FIFO_SEGMENT_TX_FREELIST);
1290 svm_fifo_segment_delete (seg);
1291
1292 return rv;
1293}
1294
1295static int
1296vppcom_session_unbind (u32 session_index)
1297{
Dave Wallace543852a2017-08-03 02:11:34 -04001298 int rv;
1299
1300 clib_spinlock_lock (&vcm->sessions_lockp);
1301 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1302 {
1303 clib_spinlock_unlock (&vcm->sessions_lockp);
1304 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001305 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001306 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001307 return VPPCOM_EBADFD;
1308 }
1309 clib_spinlock_unlock (&vcm->sessions_lockp);
1310
Florin Corasdcf55ce2017-11-16 15:32:50 -08001311 if (vcm->bind_session_index != session_index)
1312 clib_warning ("[%d] ERROR: unbinding a not bound listener %u (%u)",
1313 session_index, vcm->bind_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001314 vcm->bind_session_index = session_index;
1315 vppcom_send_unbind_sock (session_index);
1316 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1317 vcm->cfg.session_timeout);
1318 if (PREDICT_FALSE (rv))
1319 {
1320 vcm->bind_session_index = ~0;
1321 if (VPPCOM_DEBUG > 0)
1322 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001323 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001324 return rv;
1325 }
1326 return VPPCOM_OK;
1327}
1328
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001329static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001330vppcom_session_disconnect (u32 session_index)
1331{
Dave Wallace543852a2017-08-03 02:11:34 -04001332 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001333 session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001334
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001335 clib_spinlock_lock (&vcm->sessions_lockp);
1336 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04001337 if (PREDICT_FALSE (rv))
1338 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001339 clib_spinlock_unlock (&vcm->sessions_lockp);
1340 if (VPPCOM_DEBUG > 1)
1341 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001342 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001343 return rv;
1344 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001345
1346 if (!session->is_cut_thru)
1347 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001348 if (VPPCOM_DEBUG > 1)
1349 clib_warning ("[%d] disconnecting sid (%u)", getpid (),
1350 session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001351 vppcom_send_disconnect (session);
1352 clib_spinlock_unlock (&vcm->sessions_lockp);
1353
1354 rv = vppcom_wait_for_session_state_change (session_index,
1355 STATE_DISCONNECT, 1.0);
Dave Wallace227867f2017-11-13 21:21:53 -05001356 /* TBD: Force clean up on error/timeout since there is no other
1357 * way to recover from a failed disconnect.
1358 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001359 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1360 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001361 getpid (), session_index, vppcom_retval_str (rv), rv);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001362 }
1363 else
Dave Wallace227867f2017-11-13 21:21:53 -05001364 {
1365 /* TBD: Handle cut-thru disconnect */
1366 clib_spinlock_unlock (&vcm->sessions_lockp);
1367 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001368
Dave Wallace543852a2017-08-03 02:11:34 -04001369 return VPPCOM_OK;
1370}
1371
1372#define foreach_sock_msg \
1373_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1374_(BIND_SOCK_REPLY, bind_sock_reply) \
1375_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1376_(ACCEPT_SESSION, accept_session) \
1377_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001378_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001379_(DISCONNECT_SESSION, disconnect_session) \
1380_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1381_(RESET_SESSION, reset_session) \
1382_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1383_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1384_(MAP_ANOTHER_SEGMENT, map_another_segment)
1385
1386static void
1387vppcom_api_hookup (void)
1388{
1389#define _(N,n) \
1390 vl_msg_api_set_handlers(VL_API_##N, #n, \
1391 vl_api_##n##_t_handler, \
1392 vl_noop_handler, \
1393 vl_api_##n##_t_endian, \
1394 vl_api_##n##_t_print, \
1395 sizeof(vl_api_##n##_t), 1);
1396 foreach_sock_msg;
1397#undef _
1398}
1399
1400static void
1401vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1402{
1403 ASSERT (vcl_cfg);
1404
1405 vcl_cfg->heapsize = (256ULL << 20);
1406 vcl_cfg->segment_baseva = 0x200000000ULL;
1407 vcl_cfg->segment_size = (256 << 20);
1408 vcl_cfg->add_segment_size = (128 << 20);
1409 vcl_cfg->preallocated_fifo_pairs = 8;
1410 vcl_cfg->rx_fifo_size = (1 << 20);
1411 vcl_cfg->tx_fifo_size = (1 << 20);
1412 vcl_cfg->event_queue_size = 2048;
1413 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1414 vcl_cfg->app_timeout = 10 * 60.0;
1415 vcl_cfg->session_timeout = 10 * 60.0;
1416 vcl_cfg->accept_timeout = 60.0;
1417}
1418
1419static void
1420vppcom_cfg_heapsize (char *conf_fname)
1421{
Dave Wallace543852a2017-08-03 02:11:34 -04001422 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1423 FILE *fp;
1424 char inbuf[4096];
1425 int argc = 1;
1426 char **argv = NULL;
1427 char *arg = NULL;
1428 char *p;
1429 int i;
1430 u8 *sizep;
1431 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001432 void *vcl_mem;
1433 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001434
1435 fp = fopen (conf_fname, "r");
1436 if (fp == NULL)
1437 {
1438 if (VPPCOM_DEBUG > 0)
1439 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1440 goto defaulted;
1441 }
1442 argv = calloc (1, sizeof (char *));
1443 if (argv == NULL)
1444 goto defaulted;
1445
1446 while (1)
1447 {
1448 if (fgets (inbuf, 4096, fp) == 0)
1449 break;
1450 p = strtok (inbuf, " \t\n");
1451 while (p != NULL)
1452 {
1453 if (*p == '#')
1454 break;
1455 argc++;
1456 char **tmp = realloc (argv, argc * sizeof (char *));
1457 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001458 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001459 argv = tmp;
1460 arg = strndup (p, 1024);
1461 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001462 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001463 argv[argc - 1] = arg;
1464 p = strtok (NULL, " \t\n");
1465 }
1466 }
1467
1468 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001469 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001470
1471 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1472 if (tmp == NULL)
1473 goto defaulted;
1474 argv = tmp;
1475 argv[argc] = NULL;
1476
1477 /*
1478 * Look for and parse the "heapsize" config parameter.
1479 * Manual since none of the clib infra has been bootstrapped yet.
1480 *
1481 * Format: heapsize <nn>[mM][gG]
1482 */
1483
1484 for (i = 1; i < (argc - 1); i++)
1485 {
1486 if (!strncmp (argv[i], "heapsize", 8))
1487 {
1488 sizep = (u8 *) argv[i + 1];
1489 size = 0;
1490 while (*sizep >= '0' && *sizep <= '9')
1491 {
1492 size *= 10;
1493 size += *sizep++ - '0';
1494 }
1495 if (size == 0)
1496 {
1497 if (VPPCOM_DEBUG > 0)
1498 clib_warning ("[%d] parse error '%s %s', "
1499 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001500 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001501 vcl_cfg->heapsize, vcl_cfg->heapsize);
1502 goto defaulted;
1503 }
1504
1505 if (*sizep == 'g' || *sizep == 'G')
1506 vcl_cfg->heapsize = size << 30;
1507 else if (*sizep == 'm' || *sizep == 'M')
1508 vcl_cfg->heapsize = size << 20;
1509 else
1510 {
1511 if (VPPCOM_DEBUG > 0)
1512 clib_warning ("[%d] parse error '%s %s', "
1513 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001514 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001515 vcl_cfg->heapsize, vcl_cfg->heapsize);
1516 goto defaulted;
1517 }
1518 }
1519 }
1520
1521defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001522 if (fp != NULL)
1523 fclose (fp);
1524 if (argv != NULL)
1525 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001526
Dave Wallace2e005bb2017-11-07 01:21:39 -05001527 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1528 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001529 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001530 {
1531 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1532 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1533 "-1, 0) failed!",
1534 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1535 return;
1536 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001537 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1538 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001539 {
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001540 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1541 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001542 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001543 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1544 if (!vcl_mem)
1545 {
1546 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1547 return;
1548 }
1549
1550 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1551 vcm = vcl_mem;
1552
1553 if (VPPCOM_DEBUG > 0)
1554 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1555 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001556}
1557
1558static void
1559vppcom_cfg_read (char *conf_fname)
1560{
Dave Wallace543852a2017-08-03 02:11:34 -04001561 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1562 int fd;
1563 unformat_input_t _input, *input = &_input;
1564 unformat_input_t _line_input, *line_input = &_line_input;
1565 u8 vc_cfg_input = 0;
1566 u8 *chroot_path;
1567 struct stat s;
1568 u32 uid, gid;
1569
1570 fd = open (conf_fname, O_RDONLY);
1571 if (fd < 0)
1572 {
1573 if (VPPCOM_DEBUG > 0)
1574 clib_warning ("[%d] open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001575 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001576 goto file_done;
1577 }
1578
1579 if (fstat (fd, &s) < 0)
1580 {
1581 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001582 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001583 goto file_done;
1584 }
1585
1586 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1587 {
1588 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001589 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001590 goto file_done;
1591 }
1592
Dave Barach59b25652017-09-10 15:04:27 -04001593 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001594
1595 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1596 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001597 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001598 unformat_skip_white_space (line_input);
1599
Dave Wallace8af20542017-10-26 03:29:30 -04001600 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001601 {
1602 vc_cfg_input = 1;
1603 continue;
1604 }
1605
1606 if (vc_cfg_input)
1607 {
1608 if (unformat (line_input, "heapsize %s", &chroot_path))
1609 {
1610 vec_terminate_c_string (chroot_path);
1611 if (VPPCOM_DEBUG > 0)
1612 clib_warning ("[%d] configured heapsize %s, "
1613 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001614 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001615 vcl_cfg->heapsize);
1616 vec_free (chroot_path);
1617 }
1618 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1619 {
1620 vec_terminate_c_string (chroot_path);
1621 vl_set_memory_root_path ((char *) chroot_path);
1622 if (VPPCOM_DEBUG > 0)
1623 clib_warning ("[%d] configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001624 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001625 chroot_path = 0; /* Don't vec_free() it! */
1626 }
1627 else if (unformat (line_input, "uid %d", &uid))
1628 {
1629 vl_set_memory_uid (uid);
1630 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001631 clib_warning ("[%d] configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001632 }
1633 else if (unformat (line_input, "gid %d", &gid))
1634 {
1635 vl_set_memory_gid (gid);
1636 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001637 clib_warning ("[%d] configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001638 }
Dave Wallace8af20542017-10-26 03:29:30 -04001639 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001640 &vcl_cfg->segment_baseva))
1641 {
1642 if (VPPCOM_DEBUG > 0)
Dave Wallace8af20542017-10-26 03:29:30 -04001643 clib_warning ("[%d] configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001644 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001645 }
1646 else if (unformat (line_input, "segment-size 0x%lx",
1647 &vcl_cfg->segment_size))
1648 {
1649 if (VPPCOM_DEBUG > 0)
1650 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001651 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001652 vcl_cfg->segment_size);
1653 }
1654 else if (unformat (line_input, "segment-size %ld",
1655 &vcl_cfg->segment_size))
1656 {
1657 if (VPPCOM_DEBUG > 0)
1658 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001659 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001660 vcl_cfg->segment_size);
1661 }
1662 else if (unformat (line_input, "add-segment-size 0x%lx",
1663 &vcl_cfg->add_segment_size))
1664 {
1665 if (VPPCOM_DEBUG > 0)
1666 clib_warning
1667 ("[%d] configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001668 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001669 vcl_cfg->add_segment_size);
1670 }
1671 else if (unformat (line_input, "add-segment-size %ld",
1672 &vcl_cfg->add_segment_size))
1673 {
1674 if (VPPCOM_DEBUG > 0)
1675 clib_warning
1676 ("[%d] configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001677 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001678 vcl_cfg->add_segment_size);
1679 }
1680 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1681 &vcl_cfg->preallocated_fifo_pairs))
1682 {
1683 if (VPPCOM_DEBUG > 0)
1684 clib_warning ("[%d] configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001685 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001686 vcl_cfg->preallocated_fifo_pairs,
1687 vcl_cfg->preallocated_fifo_pairs);
1688 }
1689 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1690 &vcl_cfg->rx_fifo_size))
1691 {
1692 if (VPPCOM_DEBUG > 0)
1693 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001694 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001695 vcl_cfg->rx_fifo_size);
1696 }
1697 else if (unformat (line_input, "rx-fifo-size %ld",
1698 &vcl_cfg->rx_fifo_size))
1699 {
1700 if (VPPCOM_DEBUG > 0)
1701 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001702 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001703 vcl_cfg->rx_fifo_size);
1704 }
1705 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1706 &vcl_cfg->tx_fifo_size))
1707 {
1708 if (VPPCOM_DEBUG > 0)
1709 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001710 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001711 vcl_cfg->tx_fifo_size);
1712 }
1713 else if (unformat (line_input, "tx-fifo-size %ld",
1714 &vcl_cfg->tx_fifo_size))
1715 {
1716 if (VPPCOM_DEBUG > 0)
1717 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001718 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001719 vcl_cfg->tx_fifo_size);
1720 }
1721 else if (unformat (line_input, "event-queue-size 0x%lx",
1722 &vcl_cfg->event_queue_size))
1723 {
1724 if (VPPCOM_DEBUG > 0)
1725 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001726 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001727 vcl_cfg->event_queue_size);
1728 }
1729 else if (unformat (line_input, "event-queue-size %ld",
1730 &vcl_cfg->event_queue_size))
1731 {
1732 if (VPPCOM_DEBUG > 0)
1733 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001734 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001735 vcl_cfg->event_queue_size);
1736 }
1737 else if (unformat (line_input, "listen-queue-size 0x%lx",
1738 &vcl_cfg->listen_queue_size))
1739 {
1740 if (VPPCOM_DEBUG > 0)
1741 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001742 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001743 vcl_cfg->listen_queue_size);
1744 }
1745 else if (unformat (line_input, "listen-queue-size %ld",
1746 &vcl_cfg->listen_queue_size))
1747 {
1748 if (VPPCOM_DEBUG > 0)
1749 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001750 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001751 vcl_cfg->listen_queue_size);
1752 }
1753 else if (unformat (line_input, "app-timeout %f",
1754 &vcl_cfg->app_timeout))
1755 {
1756 if (VPPCOM_DEBUG > 0)
1757 clib_warning ("[%d] configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001758 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001759 }
1760 else if (unformat (line_input, "session-timeout %f",
1761 &vcl_cfg->session_timeout))
1762 {
1763 if (VPPCOM_DEBUG > 0)
1764 clib_warning ("[%d] configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001765 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001766 }
1767 else if (unformat (line_input, "accept-timeout %f",
1768 &vcl_cfg->accept_timeout))
1769 {
1770 if (VPPCOM_DEBUG > 0)
1771 clib_warning ("[%d] configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001772 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001773 }
Dave Wallace774169b2017-11-01 20:07:40 -04001774 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001775 {
Dave Wallace774169b2017-11-01 20:07:40 -04001776 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001777 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001778 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001779 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001780 }
Dave Wallace774169b2017-11-01 20:07:40 -04001781 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001782 {
Dave Wallace774169b2017-11-01 20:07:40 -04001783 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001784 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001785 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001786 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001787 }
Dave Wallace774169b2017-11-01 20:07:40 -04001788 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001789 {
Dave Wallace774169b2017-11-01 20:07:40 -04001790 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001791 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001792 clib_warning ("[%d] configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001793 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001794 }
1795 else if (unformat (line_input, "app-scope-global"))
1796 {
1797 vcl_cfg->app_scope_global = 1;
1798 if (VPPCOM_DEBUG > 0)
1799 clib_warning ("[%d] configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001800 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001801 }
1802 else if (unformat (line_input, "namespace-secret %lu",
1803 &vcl_cfg->namespace_secret))
1804 {
1805 if (VPPCOM_DEBUG > 0)
1806 clib_warning
1807 ("[%d] configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001808 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04001809 vcl_cfg->namespace_secret);
1810 }
1811 else if (unformat (line_input, "namespace-id %v",
1812 &vcl_cfg->namespace_id))
1813 {
1814 vl_api_application_attach_t *mp;
1815 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1816 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1817 if (nsid_vec_len > max_nsid_vec_len)
1818 {
1819 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1820 if (VPPCOM_DEBUG > 0)
1821 clib_warning ("[%d] configured namespace_id is too long,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05001822 " truncated to %d characters!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001823 max_nsid_vec_len);
1824 }
1825
1826 if (VPPCOM_DEBUG > 0)
1827 clib_warning ("[%d] configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001828 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001829 }
Dave Wallace543852a2017-08-03 02:11:34 -04001830 else if (unformat (line_input, "}"))
1831 {
1832 vc_cfg_input = 0;
1833 if (VPPCOM_DEBUG > 0)
1834 clib_warning ("[%d] completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001835 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001836 goto input_done;
1837 }
1838 else
1839 {
1840 if (line_input->buffer[line_input->index] != '#')
1841 {
1842 clib_warning ("[%d] Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001843 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001844 &line_input->buffer[line_input->index]);
1845 }
1846 }
1847 }
1848 }
1849
1850input_done:
1851 unformat_free (input);
1852
1853file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001854 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001855 close (fd);
1856}
1857
1858/*
1859 * VPPCOM Public API functions
1860 */
1861int
1862vppcom_app_create (char *app_name)
1863{
Dave Wallace543852a2017-08-03 02:11:34 -04001864 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1865 u8 *heap;
1866 mheap_t *h;
1867 int rv;
1868
1869 if (!vcm->init)
1870 {
1871 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001872 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001873
1874 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001875 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001876 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1877 if (env_var_str)
1878 {
1879 u32 tmp;
1880 if (sscanf (env_var_str, "%u", &tmp) != 1)
1881 clib_warning ("[%d] Invalid debug level specified in "
1882 "the environment variable "
1883 VPPCOM_ENV_DEBUG
1884 " (%s)!\n", getpid (), env_var_str);
1885 else
1886 {
1887 vcm->debug = tmp;
1888 clib_warning ("[%d] configured debug level (%u) from "
1889 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1890 }
1891 }
Dave Wallace8af20542017-10-26 03:29:30 -04001892 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001893 if (!conf_fname)
1894 {
1895 conf_fname = VPPCOM_CONF_DEFAULT;
1896 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001897 clib_warning ("[%d] getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001898 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001899 }
1900 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001901 clib_fifo_validate (vcm->client_session_index_fifo,
1902 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001903 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04001904 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1905 if (env_var_str)
1906 {
1907 u32 ns_id_vec_len = strlen (env_var_str);
1908
1909 vec_reset_length (vcm->cfg.namespace_id);
1910 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1911 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1912
1913 if (VPPCOM_DEBUG > 0)
1914 clib_warning ("[%d] configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001915 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001916 vcm->cfg.namespace_id);
1917 }
1918 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1919 if (env_var_str)
1920 {
1921 u64 tmp;
1922 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1923 clib_warning ("[%d] Invalid namespace secret specified in "
1924 "the environment variable "
1925 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001926 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001927 else
1928 {
1929 vcm->cfg.namespace_secret = tmp;
1930 if (VPPCOM_DEBUG > 0)
1931 clib_warning ("[%d] configured namespace secret (%lu) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001932 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001933 vcm->cfg.namespace_secret);
1934 }
1935 }
Dave Wallace774169b2017-11-01 20:07:40 -04001936 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001937 {
Dave Wallace774169b2017-11-01 20:07:40 -04001938 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001939 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001940 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001941 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001942 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001943 }
Dave Wallace774169b2017-11-01 20:07:40 -04001944 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001945 {
Dave Wallace774169b2017-11-01 20:07:40 -04001946 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001947 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001948 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001949 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001950 vcm->cfg.app_proxy_transport_udp);
1951 }
1952 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1953 {
1954 vcm->cfg.app_scope_local = 1;
1955 if (VPPCOM_DEBUG > 0)
1956 clib_warning ("[%d] configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001957 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001958 vcm->cfg.app_scope_local);
1959 }
1960 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1961 {
1962 vcm->cfg.app_scope_global = 1;
1963 if (VPPCOM_DEBUG > 0)
1964 clib_warning ("[%d] configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001965 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001966 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001967 }
1968
Dave Wallace543852a2017-08-03 02:11:34 -04001969 vcm->bind_session_index = ~0;
1970 vcm->main_cpu = os_get_thread_index ();
1971 heap = clib_mem_get_per_cpu_heap ();
1972 h = mheap_header (heap);
1973
1974 /* make the main heap thread-safe */
1975 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1976
1977 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1978
1979 clib_time_init (&vcm->clib_time);
1980 vppcom_init_error_string_table ();
1981 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1982 20 /* timeout in secs */ );
1983 clib_spinlock_init (&vcm->sessions_lockp);
1984 vppcom_api_hookup ();
1985 }
1986
1987 if (vcm->my_client_index == ~0)
1988 {
1989 vcm->app_state = STATE_APP_START;
1990 rv = vppcom_connect_to_vpp (app_name);
1991 if (rv)
1992 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001993 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001994 return rv;
1995 }
1996
1997 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001998 clib_warning ("[%d] sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001999
2000 rv = vppcom_app_session_enable ();
2001 if (rv)
2002 {
2003 clib_warning ("[%d] vppcom_app_session_enable() failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002004 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002005 return rv;
2006 }
2007
2008 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002009 clib_warning ("[%d] sending app attach", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002010
2011 rv = vppcom_app_attach ();
2012 if (rv)
2013 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002014 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002015 return rv;
2016 }
Dave Wallace543852a2017-08-03 02:11:34 -04002017
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002018 if (VPPCOM_DEBUG > 0)
2019 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002020 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002021 vcm->my_client_index);
2022 }
Dave Wallace543852a2017-08-03 02:11:34 -04002023
2024 return VPPCOM_OK;
2025}
2026
2027void
2028vppcom_app_destroy (void)
2029{
Dave Wallace543852a2017-08-03 02:11:34 -04002030 int rv;
2031
2032 if (vcm->my_client_index == ~0)
2033 return;
2034
2035 if (VPPCOM_DEBUG > 0)
2036 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002037 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002038
2039 vppcom_app_detach ();
2040 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2041 if (PREDICT_FALSE (rv))
2042 {
2043 if (VPPCOM_DEBUG > 0)
2044 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002045 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002046 }
2047 vl_client_disconnect_from_vlib ();
2048 vcm->my_client_index = ~0;
2049 vcm->app_state = STATE_APP_START;
2050}
2051
2052int
2053vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2054{
Dave Wallace543852a2017-08-03 02:11:34 -04002055 session_t *session;
2056 u32 session_index;
2057
2058 clib_spinlock_lock (&vcm->sessions_lockp);
2059 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002060 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002061 session_index = session - vcm->sessions;
2062
2063 session->vrf = vrf;
2064 session->proto = proto;
2065 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002066 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002067 clib_spinlock_unlock (&vcm->sessions_lockp);
2068
2069 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002070 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002071
2072 return (int) session_index;
2073}
2074
2075int
2076vppcom_session_close (uint32_t session_index)
2077{
Dave Wallace543852a2017-08-03 02:11:34 -04002078 session_t *session = 0;
2079 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002080 u8 is_server;
2081 u8 is_listen;
2082 u8 is_cut_thru;
2083 u8 is_vep;
2084 u8 is_vep_session;
2085 u32 next_sid;
2086 u32 vep_idx;
2087 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002088
2089 clib_spinlock_lock (&vcm->sessions_lockp);
2090 rv = vppcom_session_at_index (session_index, &session);
2091 if (PREDICT_FALSE (rv))
2092 {
Dave Wallace543852a2017-08-03 02:11:34 -04002093 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002094 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002095 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002096 clib_spinlock_unlock (&vcm->sessions_lockp);
2097 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002098 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002099 is_server = session->is_server;
2100 is_listen = session->is_listen;
2101 is_cut_thru = session->is_cut_thru;
2102 is_vep = session->is_vep;
2103 is_vep_session = session->is_vep_session;
2104 next_sid = session->vep.next_sid;
2105 vep_idx = session->vep.vep_idx;
2106 state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04002107 clib_spinlock_unlock (&vcm->sessions_lockp);
2108
2109 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002110 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002111
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002112 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002113 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002114 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002115 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002116 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallace19481612017-09-15 18:47:44 -04002117 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002118 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002119 "rv = %s (%d)", getpid (), vep_idx, next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002120 vppcom_retval_str (rv), rv);
2121
2122 clib_spinlock_lock (&vcm->sessions_lockp);
2123 rv = vppcom_session_at_index (session_index, &session);
2124 if (PREDICT_FALSE (rv))
2125 {
2126 if (VPPCOM_DEBUG > 0)
2127 clib_warning
2128 ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002129 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002130 clib_spinlock_unlock (&vcm->sessions_lockp);
2131 goto done;
2132 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002133 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002134 clib_spinlock_unlock (&vcm->sessions_lockp);
2135 }
2136 }
2137 else
2138 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002139 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002140 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002141 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2142 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2143 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002144 "rv = %s (%d)", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002145 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002146 }
2147
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002148 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002149 {
Dave Wallace60caa062017-11-10 17:07:13 -05002150 rv = vppcom_session_unbind_cut_thru (session, session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002151 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2152 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2153 "rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002154 getpid (), session_index,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002155 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002156 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002157 else if (is_server && is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002158 {
2159 rv = vppcom_session_unbind (session_index);
2160 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2161 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002162 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002163 vppcom_retval_str (rv), rv);
2164 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002165 else if (state == STATE_CONNECT)
2166 if (vppcom_session_disconnect (session_index))
2167 goto done;
Dave Wallace19481612017-09-15 18:47:44 -04002168 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002169 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002170 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002171 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002172done:
Dave Wallace543852a2017-08-03 02:11:34 -04002173 return rv;
2174}
2175
2176int
2177vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2178{
Dave Wallace543852a2017-08-03 02:11:34 -04002179 session_t *session = 0;
2180 int rv;
2181
2182 if (!ep || !ep->ip)
2183 return VPPCOM_EINVAL;
2184
2185 clib_spinlock_lock (&vcm->sessions_lockp);
2186 rv = vppcom_session_at_index (session_index, &session);
2187 if (PREDICT_FALSE (rv))
2188 {
2189 clib_spinlock_unlock (&vcm->sessions_lockp);
2190 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002191 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002192 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002193 return rv;
2194 }
2195
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002196 if (session->is_vep)
2197 {
2198 clib_spinlock_unlock (&vcm->sessions_lockp);
2199 if (VPPCOM_DEBUG > 0)
2200 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002201 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002202 return VPPCOM_EBADFD;
2203 }
2204
Dave Wallace543852a2017-08-03 02:11:34 -04002205 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002206 session->lcl_addr.is_ip4 = ep->is_ip4;
2207 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002208 session->lcl_port = ep->port;
2209
2210 if (VPPCOM_DEBUG > 0)
2211 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002212 getpid (), session_index, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07002213 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2214 clib_net_to_host_u16 (session->lcl_port));
Dave Wallace543852a2017-08-03 02:11:34 -04002215
2216 clib_spinlock_unlock (&vcm->sessions_lockp);
2217 return VPPCOM_OK;
2218}
2219
2220int
Dave Wallace33e002b2017-09-06 01:20:02 -04002221vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002222{
Dave Wallace33e002b2017-09-06 01:20:02 -04002223 session_t *listen_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002224 int rv;
2225
2226 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002227 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002228 if (PREDICT_FALSE (rv))
2229 {
2230 clib_spinlock_unlock (&vcm->sessions_lockp);
2231 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002232 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002233 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002234 return rv;
2235 }
2236
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002237 if (listen_session->is_vep)
2238 {
2239 clib_spinlock_unlock (&vcm->sessions_lockp);
2240 if (VPPCOM_DEBUG > 0)
2241 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002242 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002243 return VPPCOM_EBADFD;
2244 }
2245
Dave Wallacee695cb42017-11-02 22:04:42 -04002246 if (listen_session->is_listen)
2247 {
2248 clib_spinlock_unlock (&vcm->sessions_lockp);
2249 if (VPPCOM_DEBUG > 0)
2250 clib_warning ("[%d] sid (%u) is already in listen state!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002251 getpid (), listen_session_index);
Dave Wallacee695cb42017-11-02 22:04:42 -04002252 return VPPCOM_OK;
2253 }
2254
Dave Wallace543852a2017-08-03 02:11:34 -04002255 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002256 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002257
2258 ASSERT (vcm->bind_session_index == ~0);
Dave Wallace33e002b2017-09-06 01:20:02 -04002259 vcm->bind_session_index = listen_session_index;
2260 vppcom_send_bind_sock (listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002261 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002262 rv =
2263 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2264 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002265 if (PREDICT_FALSE (rv))
2266 {
2267 vcm->bind_session_index = ~0;
2268 if (VPPCOM_DEBUG > 0)
2269 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002270 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002271 return rv;
2272 }
2273
2274 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002275 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002276 if (PREDICT_FALSE (rv))
2277 {
2278 clib_spinlock_unlock (&vcm->sessions_lockp);
2279 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002280 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002281 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002282 return rv;
2283 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002284 listen_session->is_listen = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002285 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002286 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002287
2288 return VPPCOM_OK;
2289}
2290
2291int
2292vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace227867f2017-11-13 21:21:53 -05002293 uint32_t flags, double wait_for_time)
Dave Wallace543852a2017-08-03 02:11:34 -04002294{
Dave Wallace33e002b2017-09-06 01:20:02 -04002295 session_t *listen_session = 0;
2296 session_t *client_session = 0;
Dave Wallace227867f2017-11-13 21:21:53 -05002297 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002298 int rv;
2299 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002300 char *cut_thru_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002301
2302 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002303 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002304 if (PREDICT_FALSE (rv))
2305 {
2306 clib_spinlock_unlock (&vcm->sessions_lockp);
2307 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002308 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002309 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002310 return rv;
2311 }
2312
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002313 if (listen_session->is_vep)
2314 {
2315 clib_spinlock_unlock (&vcm->sessions_lockp);
2316 if (VPPCOM_DEBUG > 0)
2317 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002318 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002319 return VPPCOM_EBADFD;
2320 }
2321
Dave Wallace33e002b2017-09-06 01:20:02 -04002322 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002323 {
2324 clib_spinlock_unlock (&vcm->sessions_lockp);
2325 if (VPPCOM_DEBUG > 0)
2326 clib_warning ("[%d] session not in listen state, state = %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002327 getpid (),
Dave Wallace33e002b2017-09-06 01:20:02 -04002328 vppcom_session_state_str (listen_session->state));
Dave Wallace543852a2017-08-03 02:11:34 -04002329 return VPPCOM_EBADFD;
2330 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002331 wait_for = listen_session->is_nonblocking ? 0 :
Dave Wallace543852a2017-08-03 02:11:34 -04002332 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2333
2334 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002335 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002336 listen_session_index,
Dave Wallace33e002b2017-09-06 01:20:02 -04002337 vppcom_session_state_str (listen_session->state),
2338 listen_session->state);
Dave Wallace543852a2017-08-03 02:11:34 -04002339 clib_spinlock_unlock (&vcm->sessions_lockp);
2340
2341 while (1)
2342 {
2343 rv = vppcom_wait_for_client_session_index (wait_for);
2344 if (rv)
2345 {
2346 if ((VPPCOM_DEBUG > 0))
2347 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002348 getpid (), listen_session_index,
Dave Wallace543852a2017-08-03 02:11:34 -04002349 vppcom_retval_str (rv), rv);
2350 if ((wait_for == 0) || (wait_for_time > 0))
2351 return rv;
2352 }
2353 else
2354 break;
2355 }
2356
Dave Wallace543852a2017-08-03 02:11:34 -04002357 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002358 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002359 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002360 ASSERT (rv == VPPCOM_OK);
Dave Wallace35830af2017-10-09 01:43:42 -04002361 ASSERT (client_session->peer_addr.is_ip4 ==
2362 listen_session->lcl_addr.is_ip4);
Dave Wallace543852a2017-08-03 02:11:34 -04002363
Dave Wallace227867f2017-11-13 21:21:53 -05002364 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002365 if (VPPCOM_DEBUG > 0)
Dave Wallace227867f2017-11-13 21:21:53 -05002366 clib_warning ("[%d] Got a request: client sid %d, flags %d, "
2367 " is_nonblocking %u", getpid (), client_session_index,
2368 flags, client_session->is_nonblocking);
Dave Wallace543852a2017-08-03 02:11:34 -04002369
Dave Wallace33e002b2017-09-06 01:20:02 -04002370 ep->vrf = client_session->vrf;
2371 ep->is_cut_thru = client_session->is_cut_thru;
Dave Wallace35830af2017-10-09 01:43:42 -04002372 ep->is_ip4 = client_session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07002373 ep->port = client_session->peer_port;
Dave Wallace35830af2017-10-09 01:43:42 -04002374 if (client_session->peer_addr.is_ip4)
2375 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2376 sizeof (ip4_address_t));
Dave Wallace33e002b2017-09-06 01:20:02 -04002377 else
Dave Wallace35830af2017-10-09 01:43:42 -04002378 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2379 sizeof (ip6_address_t));
Dave Wallace60caa062017-11-10 17:07:13 -05002380
2381 if (client_session->is_server && client_session->is_cut_thru)
2382 {
2383 static svm_fifo_segment_create_args_t _a;
2384 svm_fifo_segment_create_args_t *a = &_a;
2385 svm_fifo_segment_private_t *seg;
2386
2387 cut_thru_str = "cut-thru ";
2388
2389 /* Create the segment */
2390 memset (a, 0, sizeof (*a));
2391 a->segment_name = (char *)
2392 format ((u8 *) a->segment_name, "%d:segment%d%c",
2393 getpid (), vcm->unique_segment_index++, 0);
2394 a->segment_size = vcm->cfg.segment_size;
2395 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2396 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2397 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2398
2399 rv = svm_fifo_segment_create (a);
2400 if (PREDICT_FALSE (rv))
2401 {
2402 if (VPPCOM_DEBUG > 1)
2403 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2404 getpid (), a->segment_name);
2405 vec_reset_length (a->new_segment_indices);
2406 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002407 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002408 clib_spinlock_unlock (&vcm->sessions_lockp);
2409 return VPPCOM_ENOMEM;
2410 }
2411
2412 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2413 client_session->sm_seg_index = a->new_segment_indices[0];
2414 vec_free (a->new_segment_indices);
2415
2416 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2417 client_session->server_rx_fifo =
2418 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2419 FIFO_SEGMENT_RX_FREELIST);
2420 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2421 {
2422 svm_fifo_segment_delete (seg);
2423 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2424 getpid (), vcm->cfg.rx_fifo_size,
2425 vcm->cfg.rx_fifo_size);
2426 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002427 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002428 clib_spinlock_unlock (&vcm->sessions_lockp);
2429 return VPPCOM_ENOMEM;
2430 }
2431 client_session->server_rx_fifo->master_session_index =
2432 client_session_index;
2433
2434 client_session->server_tx_fifo =
2435 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2436 FIFO_SEGMENT_TX_FREELIST);
2437 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2438 {
2439 svm_fifo_segment_delete (seg);
2440 if (VPPCOM_DEBUG > 1)
2441 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2442 getpid (), vcm->cfg.tx_fifo_size,
2443 vcm->cfg.tx_fifo_size);
2444 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002445 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002446 clib_spinlock_unlock (&vcm->sessions_lockp);
2447 return VPPCOM_ENOMEM;
2448 }
2449 client_session->server_tx_fifo->master_session_index =
2450 client_session_index;
2451
2452 if (VPPCOM_DEBUG > 1)
2453 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2454 getpid (), client_session->segment_name,
2455 client_session->server_rx_fifo,
2456 client_session->server_tx_fifo);
2457
2458#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2459 {
2460 void *oldheap;
2461 ssvm_shared_header_t *sh = seg->ssvm.sh;
2462
2463 ssvm_lock_non_recursive (sh, 1);
2464 oldheap = ssvm_push_heap (sh);
2465 event_q = client_session->vpp_event_queue =
2466 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2467 sizeof (session_fifo_event_t),
2468 getpid (), 0 /* signal not sent */ );
2469 ssvm_pop_heap (oldheap);
2470 ssvm_unlock_non_recursive (sh);
2471 }
2472#endif
Dave Wallace227867f2017-11-13 21:21:53 -05002473 vppcom_send_connect_session_reply (client_session, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05002474 }
2475 else
2476 {
2477 cut_thru_str = " ";
Florin Corasdcf55ce2017-11-16 15:32:50 -08002478 vppcom_send_accept_session_reply (client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05002479 }
2480
Stevenac1f96d2017-10-24 16:03:58 -07002481 if (VPPCOM_DEBUG > 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002482 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2483 "%U peer port %u",
2484 getpid (), client_session_index, cut_thru_str,
2485 format_ip46_address, &client_session->peer_addr.ip46,
Stevenac1f96d2017-10-24 16:03:58 -07002486 client_session->peer_addr.is_ip4,
2487 clib_net_to_host_u16 (client_session->peer_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002488
Dave Wallace543852a2017-08-03 02:11:34 -04002489 clib_spinlock_unlock (&vcm->sessions_lockp);
2490 return (int) client_session_index;
2491}
2492
2493int
2494vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2495{
Dave Wallace543852a2017-08-03 02:11:34 -04002496 session_t *session = 0;
2497 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002498
2499 clib_spinlock_lock (&vcm->sessions_lockp);
2500 rv = vppcom_session_at_index (session_index, &session);
2501 if (PREDICT_FALSE (rv))
2502 {
2503 clib_spinlock_unlock (&vcm->sessions_lockp);
2504 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002506 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002507 return rv;
2508 }
2509
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002510 if (session->is_vep)
2511 {
2512 clib_spinlock_unlock (&vcm->sessions_lockp);
2513 if (VPPCOM_DEBUG > 0)
2514 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002515 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002516 return VPPCOM_EBADFD;
2517 }
2518
Dave Wallace543852a2017-08-03 02:11:34 -04002519 if (session->state == STATE_CONNECT)
2520 {
2521 clib_spinlock_unlock (&vcm->sessions_lockp);
2522 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002523 clib_warning ("[%d] session, sid (%u) already connected!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002524 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002525 return VPPCOM_OK;
2526 }
2527
2528 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002529 session->peer_addr.is_ip4 = server_ep->is_ip4;
2530 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002531 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002532
2533 if (VPPCOM_DEBUG > 0)
2534 {
2535 u8 *ip_str = format (0, "%U", format_ip46_address,
Dave Wallace35830af2017-10-09 01:43:42 -04002536 &session->peer_addr.ip46,
2537 session->peer_addr.is_ip4);
Dave Wallace7876d392017-10-19 03:53:57 -04002538 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002539 getpid (), session_index, ip_str,
Stevenac1f96d2017-10-24 16:03:58 -07002540 clib_net_to_host_u16 (session->peer_port),
Dave Wallace7876d392017-10-19 03:53:57 -04002541 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002542 vec_free (ip_str);
2543 }
2544
2545 vppcom_send_connect_sock (session, session_index);
2546 clib_spinlock_unlock (&vcm->sessions_lockp);
2547 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2548 vcm->cfg.session_timeout);
2549 if (PREDICT_FALSE (rv))
2550 {
2551 if (VPPCOM_DEBUG > 0)
2552 clib_warning ("[%d] connect timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002553 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002554 return rv;
2555 }
Dave Wallace7876d392017-10-19 03:53:57 -04002556 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002557 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04002558
Dave Wallace543852a2017-08-03 02:11:34 -04002559 return VPPCOM_OK;
2560}
2561
Steven58f464e2017-10-25 12:33:12 -07002562static inline int
2563vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2564 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002565{
Dave Wallace543852a2017-08-03 02:11:34 -04002566 session_t *session = 0;
2567 svm_fifo_t *rx_fifo;
2568 int n_read = 0;
2569 int rv;
2570 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002571 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002572
2573 ASSERT (buf);
2574
2575 clib_spinlock_lock (&vcm->sessions_lockp);
2576 rv = vppcom_session_at_index (session_index, &session);
2577 if (PREDICT_FALSE (rv))
2578 {
2579 clib_spinlock_unlock (&vcm->sessions_lockp);
2580 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002582 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002583 return rv;
2584 }
2585
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002586 if (session->is_vep)
2587 {
2588 clib_spinlock_unlock (&vcm->sessions_lockp);
2589 if (VPPCOM_DEBUG > 0)
2590 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002591 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002592 return VPPCOM_EBADFD;
2593 }
2594
Dave Wallace33e002b2017-09-06 01:20:02 -04002595 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002596 {
Dave Wallace543852a2017-08-03 02:11:34 -04002597 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002598 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002599 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002600 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002601 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002602 }
Dave Wallace543852a2017-08-03 02:11:34 -04002603
Dave Wallace33e002b2017-09-06 01:20:02 -04002604 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2605 session->server_rx_fifo : session->server_tx_fifo);
2606 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2607 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace60caa062017-11-10 17:07:13 -05002608 poll_et =
2609 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002610 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002611
2612 do
2613 {
Steven58f464e2017-10-25 12:33:12 -07002614 if (peek)
2615 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2616 else
2617 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002618 }
2619 while (!session->is_nonblocking && (n_read <= 0));
2620
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002621 if (poll_et && (n_read <= 0))
2622 {
2623 clib_spinlock_lock (&vcm->sessions_lockp);
2624 session->vep.et_mask |= EPOLLIN;
2625 clib_spinlock_unlock (&vcm->sessions_lockp);
2626 }
Dave Wallace543852a2017-08-03 02:11:34 -04002627
Dave Wallacef7f809c2017-10-03 01:48:42 -04002628 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
Dave Wallace2e005bb2017-11-07 01:21:39 -05002629 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002630 session_index, n_read, fifo_str, rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002631
2632 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002633}
2634
Steven58f464e2017-10-25 12:33:12 -07002635int
2636vppcom_session_read (uint32_t session_index, void *buf, int n)
2637{
2638 return (vppcom_session_read_internal (session_index, buf, n, 0));
2639}
2640
2641static int
2642vppcom_session_peek (uint32_t session_index, void *buf, int n)
2643{
2644 return (vppcom_session_read_internal (session_index, buf, n, 1));
2645}
2646
Dave Wallace543852a2017-08-03 02:11:34 -04002647static inline int
2648vppcom_session_read_ready (session_t * session, u32 session_index)
2649{
Dave Wallace498b3a52017-11-09 13:00:34 -05002650 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002651 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002652 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002653
2654 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002655 if (session->is_vep)
2656 {
2657 clib_spinlock_unlock (&vcm->sessions_lockp);
2658 if (VPPCOM_DEBUG > 0)
2659 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002660 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002661 return VPPCOM_EBADFD;
2662 }
2663
Dave Wallace33e002b2017-09-06 01:20:02 -04002664 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002665 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002666 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002667 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002668 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002669 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002670 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002671
2672 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04002673 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2674 else
2675 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002676 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2677 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002678
Dave Wallace33e002b2017-09-06 01:20:02 -04002679 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002680 }
2681
Dave Wallace60caa062017-11-10 17:07:13 -05002682 poll_et =
2683 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2684 if (poll_et && (ready == 0))
2685 {
2686 if (VPPCOM_DEBUG > 11)
2687 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2688 session_index, session->vep.et_mask);
2689 session->vep.et_mask |= EPOLLIN;
2690 if (VPPCOM_DEBUG > 11)
2691 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2692 session_index, session->vep.et_mask);
2693 }
2694
2695 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2696 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2697 "et_mask 0x%x",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002698 getpid (), session_index, session->is_listen,
Dave Wallace543852a2017-08-03 02:11:34 -04002699 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
Dave Wallace60caa062017-11-10 17:07:13 -05002700 rx_fifo, ready, session->vep.et_mask);
Dave Wallace543852a2017-08-03 02:11:34 -04002701 return ready;
2702}
2703
2704int
2705vppcom_session_write (uint32_t session_index, void *buf, int n)
2706{
Dave Wallace543852a2017-08-03 02:11:34 -04002707 session_t *session = 0;
2708 svm_fifo_t *tx_fifo;
2709 unix_shared_memory_queue_t *q;
2710 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002712 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002713 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002714
2715 ASSERT (buf);
2716
2717 clib_spinlock_lock (&vcm->sessions_lockp);
2718 rv = vppcom_session_at_index (session_index, &session);
2719 if (PREDICT_FALSE (rv))
2720 {
2721 clib_spinlock_unlock (&vcm->sessions_lockp);
2722 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002723 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002724 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002725 return rv;
2726 }
2727
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002728 if (session->is_vep)
2729 {
2730 clib_spinlock_unlock (&vcm->sessions_lockp);
2731 if (VPPCOM_DEBUG > 0)
2732 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002733 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002734 return VPPCOM_EBADFD;
2735 }
2736
Dave Wallace33e002b2017-09-06 01:20:02 -04002737 if (session->state == STATE_DISCONNECT)
2738 {
2739 clib_spinlock_unlock (&vcm->sessions_lockp);
2740 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002742 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002743 return VPPCOM_ECONNRESET;
2744 }
2745
Dave Wallace543852a2017-08-03 02:11:34 -04002746 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2747 session->server_tx_fifo : session->server_rx_fifo);
2748 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2749 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002750 q = session->vpp_event_queue;
Dave Wallace60caa062017-11-10 17:07:13 -05002751 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2752 (EPOLLET | EPOLLOUT));
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002753 clib_spinlock_unlock (&vcm->sessions_lockp);
2754
Dave Wallace543852a2017-08-03 02:11:34 -04002755 do
2756 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002758 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759 while (!session->is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002760
2761 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04002762 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002763 {
2764 int rval;
2765
2766 /* Fabricate TX event, send to vpp */
2767 evt.fifo = tx_fifo;
2768 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002769
Dave Wallace543852a2017-08-03 02:11:34 -04002770 rval = vppcom_session_at_index (session_index, &session);
2771 if (PREDICT_FALSE (rval))
2772 {
Dave Wallace543852a2017-08-03 02:11:34 -04002773 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002774 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002775 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002776 return rval;
2777 }
Dave Wallace543852a2017-08-03 02:11:34 -04002778 ASSERT (q);
2779 unix_shared_memory_queue_add (q, (u8 *) & evt,
2780 0 /* do wait for mutex */ );
2781 }
2782
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002783 if (poll_et && (n_write <= 0))
2784 {
2785 clib_spinlock_lock (&vcm->sessions_lockp);
2786 session->vep.et_mask |= EPOLLOUT;
2787 clib_spinlock_unlock (&vcm->sessions_lockp);
2788 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789
Dave Wallace543852a2017-08-03 02:11:34 -04002790 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002791 {
2792 if (n_write == -2)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002793 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002794 session_index, fifo_str, tx_fifo);
2795 else
Dave Wallace2e005bb2017-11-07 01:21:39 -05002796 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002797 session_index, n_write, fifo_str, tx_fifo);
2798 }
2799 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002800}
2801
2802static inline int
2803vppcom_session_write_ready (session_t * session, u32 session_index)
2804{
Dave Wallace543852a2017-08-03 02:11:34 -04002805 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04002806 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002807 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002808 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002809
2810 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002811 if (session->is_vep)
2812 {
2813 clib_spinlock_unlock (&vcm->sessions_lockp);
2814 if (VPPCOM_DEBUG > 0)
2815 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002816 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002817 return VPPCOM_EBADFD;
2818 }
2819
Dave Wallace33e002b2017-09-06 01:20:02 -04002820 if (session->state == STATE_DISCONNECT)
2821 {
2822 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002823 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002824 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002825 return VPPCOM_ECONNRESET;
2826 }
2827
Dave Wallace543852a2017-08-03 02:11:34 -04002828 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2829 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002830 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2831 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04002832
Dave Wallacef7f809c2017-10-03 01:48:42 -04002833 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002834
Dave Wallace33e002b2017-09-06 01:20:02 -04002835 if (VPPCOM_DEBUG > 3)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002836 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002837 session_index, fifo_str, tx_fifo, ready);
Dave Wallace60caa062017-11-10 17:07:13 -05002838 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2839 (EPOLLET | EPOLLOUT));
2840 if (poll_et && (ready == 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002841 session->vep.et_mask |= EPOLLOUT;
2842
2843 return ready;
Dave Wallace543852a2017-08-03 02:11:34 -04002844}
2845
2846int
2847vppcom_select (unsigned long n_bits, unsigned long *read_map,
2848 unsigned long *write_map, unsigned long *except_map,
2849 double time_to_wait)
2850{
Dave Wallace543852a2017-08-03 02:11:34 -04002851 u32 session_index;
2852 session_t *session = 0;
2853 int rv, bits_set = 0;
2854 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2855 u32 minbits = clib_max (n_bits, BITS (uword));
2856
2857 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2858
Dave Wallace7876d392017-10-19 03:53:57 -04002859 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002860 {
2861 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2862 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2863 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2864 }
Dave Wallace7876d392017-10-19 03:53:57 -04002865 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002866 {
2867 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2868 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2869 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2870 }
Dave Wallace7876d392017-10-19 03:53:57 -04002871 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002872 {
2873 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2874 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2875 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2876 }
2877
2878 do
2879 {
2880 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002881 if (n_bits)
2882 {
2883 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002884 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002885 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2886 ({
2887 clib_spinlock_lock (&vcm->sessions_lockp);
2888 rv = vppcom_session_at_index (session_index, &session);
2889 if (rv < 0)
2890 {
2891 clib_spinlock_unlock (&vcm->sessions_lockp);
2892 if (VPPCOM_DEBUG > 1)
2893 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002894 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002895 session_index);
2896 bits_set = VPPCOM_EBADFD;
2897 goto select_done;
2898 }
2899
2900 rv = vppcom_session_read_ready (session, session_index);
2901 clib_spinlock_unlock (&vcm->sessions_lockp);
2902 if (except_map && vcm->ex_bitmap &&
2903 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2904 (rv < 0))
2905 {
2906 // TBD: clib_warning
2907 clib_bitmap_set_no_check (except_map, session_index, 1);
2908 bits_set++;
2909 }
2910 else if (rv > 0)
2911 {
2912 // TBD: clib_warning
2913 clib_bitmap_set_no_check (read_map, session_index, 1);
2914 bits_set++;
2915 }
2916 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002917 }
2918
Dave Wallacee22aa742017-10-20 12:30:38 -04002919 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002920 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002921 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2922 ({
2923 clib_spinlock_lock (&vcm->sessions_lockp);
2924 rv = vppcom_session_at_index (session_index, &session);
2925 if (rv < 0)
2926 {
2927 clib_spinlock_unlock (&vcm->sessions_lockp);
2928 if (VPPCOM_DEBUG > 0)
2929 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002930 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002931 session_index);
2932 bits_set = VPPCOM_EBADFD;
2933 goto select_done;
2934 }
Dave Wallace543852a2017-08-03 02:11:34 -04002935
Dave Wallacee22aa742017-10-20 12:30:38 -04002936 rv = vppcom_session_write_ready (session, session_index);
2937 clib_spinlock_unlock (&vcm->sessions_lockp);
2938 if (write_map && (rv > 0))
2939 {
2940 // TBD: clib_warning
2941 clib_bitmap_set_no_check (write_map, session_index, 1);
2942 bits_set++;
2943 }
2944 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002945 }
2946
Dave Wallacee22aa742017-10-20 12:30:38 -04002947 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002948 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002949 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2950 ({
2951 clib_spinlock_lock (&vcm->sessions_lockp);
2952 rv = vppcom_session_at_index (session_index, &session);
2953 if (rv < 0)
2954 {
2955 clib_spinlock_unlock (&vcm->sessions_lockp);
2956 if (VPPCOM_DEBUG > 1)
2957 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002958 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002959 session_index);
2960 bits_set = VPPCOM_EBADFD;
2961 goto select_done;
2962 }
Dave Wallace543852a2017-08-03 02:11:34 -04002963
Dave Wallacee22aa742017-10-20 12:30:38 -04002964 rv = vppcom_session_read_ready (session, session_index);
2965 clib_spinlock_unlock (&vcm->sessions_lockp);
2966 if (rv < 0)
2967 {
2968 // TBD: clib_warning
2969 clib_bitmap_set_no_check (except_map, session_index, 1);
2970 bits_set++;
2971 }
2972 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002973 }
Dave Wallacee22aa742017-10-20 12:30:38 -04002974 }
Dave Wallace543852a2017-08-03 02:11:34 -04002975 /* *INDENT-ON* */
2976 }
2977 while (clib_time_now (&vcm->clib_time) < timeout);
2978
2979select_done:
2980 return (bits_set);
2981}
2982
Dave Wallacef7f809c2017-10-03 01:48:42 -04002983static inline void
2984vep_verify_epoll_chain (u32 vep_idx)
2985{
2986 session_t *session;
2987 vppcom_epoll_t *vep;
2988 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05002989 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002990
Dave Wallace498b3a52017-11-09 13:00:34 -05002991 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992 return;
2993
2994 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2995 rv = vppcom_session_at_index (vep_idx, &session);
2996 if (PREDICT_FALSE (rv))
2997 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002998 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002999 goto done;
3000 }
3001 if (PREDICT_FALSE (!session->is_vep))
3002 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003003 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003004 vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003005 goto done;
3006 }
Dave Wallace498b3a52017-11-09 13:00:34 -05003007 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3008 "{\n"
3009 " is_vep = %u\n"
3010 " is_vep_session = %u\n"
3011 " wait_cont_idx = 0x%x (%u)\n"
3012 "}\n", getpid (),
3013 vep_idx, session->is_vep, session->is_vep_session,
3014 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003015 do
3016 {
3017 vep = &session->vep;
Dave Wallacee695cb42017-11-02 22:04:42 -04003018 sid = vep->next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003019 if (sid != ~0)
3020 {
3021 rv = vppcom_session_at_index (sid, &session);
3022 if (PREDICT_FALSE (rv))
3023 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003024 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003025 goto done;
3026 }
3027 if (PREDICT_FALSE (session->is_vep))
Dave Wallace774169b2017-11-01 20:07:40 -04003028 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003029 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003030 else if (PREDICT_FALSE (!session->is_vep_session))
3031 {
Dave Wallace774169b2017-11-01 20:07:40 -04003032 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003033 getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003034 goto done;
3035 }
3036 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
Dave Wallace774169b2017-11-01 20:07:40 -04003037 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003038 "vep_idx (%u)!", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04003039 sid, session->vep.vep_idx, vep_idx);
Dave Wallace498b3a52017-11-09 13:00:34 -05003040 if (session->is_vep_session)
3041 {
3042 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3043 "{\n"
3044 " next_sid = 0x%x (%u)\n"
3045 " prev_sid = 0x%x (%u)\n"
3046 " vep_idx = 0x%x (%u)\n"
3047 " ev.events = 0x%x\n"
3048 " ev.data.u64 = 0x%llx\n"
3049 " et_mask = 0x%x\n"
3050 "}\n",
3051 vep_idx, sid, sid,
3052 vep->next_sid, vep->next_sid,
3053 vep->prev_sid, vep->prev_sid,
3054 vep->vep_idx, vep->vep_idx,
3055 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3056 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003057 }
3058 }
3059 while (sid != ~0);
3060
3061done:
Dave Wallace498b3a52017-11-09 13:00:34 -05003062 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003063}
3064
3065int
3066vppcom_epoll_create (void)
3067{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003068 session_t *vep_session;
3069 u32 vep_idx;
3070
3071 clib_spinlock_lock (&vcm->sessions_lockp);
3072 pool_get (vcm->sessions, vep_session);
3073 memset (vep_session, 0, sizeof (*vep_session));
3074 vep_idx = vep_session - vcm->sessions;
3075
3076 vep_session->is_vep = 1;
3077 vep_session->vep.vep_idx = ~0;
3078 vep_session->vep.next_sid = ~0;
3079 vep_session->vep.prev_sid = ~0;
3080 vep_session->wait_cont_idx = ~0;
3081 clib_spinlock_unlock (&vcm->sessions_lockp);
3082
3083 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05003084 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003085
3086 return (vep_idx);
3087}
3088
3089int
3090vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3091 struct epoll_event *event)
3092{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003093 session_t *vep_session;
3094 session_t *session;
3095 int rv;
3096
3097 if (vep_idx == session_index)
3098 {
3099 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003100 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003101 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003102 return VPPCOM_EINVAL;
3103 }
3104
3105 clib_spinlock_lock (&vcm->sessions_lockp);
3106 rv = vppcom_session_at_index (vep_idx, &vep_session);
3107 if (PREDICT_FALSE (rv))
3108 {
3109 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003110 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003111 goto done;
3112 }
3113 if (PREDICT_FALSE (!vep_session->is_vep))
3114 {
3115 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003116 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003117 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003118 rv = VPPCOM_EINVAL;
3119 goto done;
3120 }
3121
3122 ASSERT (vep_session->vep.vep_idx == ~0);
3123 ASSERT (vep_session->vep.prev_sid == ~0);
3124
3125 rv = vppcom_session_at_index (session_index, &session);
3126 if (PREDICT_FALSE (rv))
3127 {
3128 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003129 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003130 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003131 goto done;
3132 }
3133 if (PREDICT_FALSE (session->is_vep))
3134 {
3135 if (VPPCOM_DEBUG > 0)
3136 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3137 rv = VPPCOM_EINVAL;
3138 goto done;
3139 }
3140
3141 switch (op)
3142 {
3143 case EPOLL_CTL_ADD:
3144 if (PREDICT_FALSE (!event))
3145 {
Dave Wallace774169b2017-11-01 20:07:40 -04003146 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003147 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003148 rv = VPPCOM_EINVAL;
3149 goto done;
3150 }
3151 if (vep_session->vep.next_sid != ~0)
3152 {
3153 session_t *next_session;
3154 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3155 &next_session);
3156 if (PREDICT_FALSE (rv))
3157 {
3158 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003159 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3160 "vep.next_sid (%u) on vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003161 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003162 goto done;
3163 }
3164 ASSERT (next_session->vep.prev_sid == vep_idx);
3165 next_session->vep.prev_sid = session_index;
3166 }
3167 session->vep.next_sid = vep_session->vep.next_sid;
3168 session->vep.prev_sid = vep_idx;
3169 session->vep.vep_idx = vep_idx;
3170 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3171 session->vep.ev = *event;
3172 session->is_vep_session = 1;
3173 vep_session->vep.next_sid = session_index;
3174 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003175 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003176 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003177 event->events, event->data.u64);
3178 break;
3179
3180 case EPOLL_CTL_MOD:
3181 if (PREDICT_FALSE (!event))
3182 {
Dave Wallace774169b2017-11-01 20:07:40 -04003183 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003184 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003185 rv = VPPCOM_EINVAL;
3186 goto done;
3187 }
3188 if (PREDICT_FALSE (!session->is_vep_session &&
3189 (session->vep.vep_idx != vep_idx)))
3190 {
3191 if (VPPCOM_DEBUG > 0)
3192 {
3193 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003194 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3195 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003196 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003197 else
Dave Wallace774169b2017-11-01 20:07:40 -04003198 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3199 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003200 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003201 session->vep.vep_idx, vep_idx);
3202 }
3203 rv = VPPCOM_EINVAL;
3204 goto done;
3205 }
3206 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3207 session->vep.ev = *event;
3208 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003209 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003210 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003211 event->events, event->data.u64);
3212 break;
3213
3214 case EPOLL_CTL_DEL:
3215 if (PREDICT_FALSE (!session->is_vep_session &&
3216 (session->vep.vep_idx != vep_idx)))
3217 {
3218 if (VPPCOM_DEBUG > 0)
3219 {
3220 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003221 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3222 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003223 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003224 else
Dave Wallace774169b2017-11-01 20:07:40 -04003225 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3226 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003227 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003228 session->vep.vep_idx, vep_idx);
3229 }
3230 rv = VPPCOM_EINVAL;
3231 goto done;
3232 }
3233
3234 vep_session->wait_cont_idx =
3235 (vep_session->wait_cont_idx == session_index) ?
3236 session->vep.next_sid : vep_session->wait_cont_idx;
3237
3238 if (session->vep.prev_sid == vep_idx)
3239 vep_session->vep.next_sid = session->vep.next_sid;
3240 else
3241 {
3242 session_t *prev_session;
3243 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3244 if (PREDICT_FALSE (rv))
3245 {
3246 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003247 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3248 "vep.prev_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003249 getpid (), session->vep.prev_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003250 session_index);
3251 goto done;
3252 }
3253 ASSERT (prev_session->vep.next_sid == session_index);
3254 prev_session->vep.next_sid = session->vep.next_sid;
3255 }
3256 if (session->vep.next_sid != ~0)
3257 {
3258 session_t *next_session;
3259 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3260 if (PREDICT_FALSE (rv))
3261 {
3262 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003263 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3264 "vep.next_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003265 getpid (), session->vep.next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003266 session_index);
3267 goto done;
3268 }
3269 ASSERT (next_session->vep.prev_sid == session_index);
3270 next_session->vep.prev_sid = session->vep.prev_sid;
3271 }
3272
3273 memset (&session->vep, 0, sizeof (session->vep));
3274 session->vep.next_sid = ~0;
3275 session->vep.prev_sid = ~0;
3276 session->vep.vep_idx = ~0;
3277 session->is_vep_session = 0;
3278 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003279 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003280 getpid (), vep_idx, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003281 break;
3282
3283 default:
Dave Wallace2e005bb2017-11-07 01:21:39 -05003284 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003285 rv = VPPCOM_EINVAL;
3286 }
3287
3288 vep_verify_epoll_chain (vep_idx);
3289
3290done:
3291 clib_spinlock_unlock (&vcm->sessions_lockp);
3292 return rv;
3293}
3294
Dave Wallacef7f809c2017-10-03 01:48:42 -04003295int
3296vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3297 int maxevents, double wait_for_time)
3298{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003299 session_t *vep_session;
3300 int rv;
3301 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003302 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003303 int num_ev = 0;
3304 u32 vep_next_sid, wait_cont_idx;
3305 u8 is_vep;
3306
3307 if (PREDICT_FALSE (maxevents <= 0))
3308 {
3309 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003310 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003311 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003312 return VPPCOM_EINVAL;
3313 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003314 memset (events, 0, sizeof (*events) * maxevents);
3315
3316 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3317 vep_next_sid = vep_session->vep.next_sid;
3318 is_vep = vep_session->is_vep;
3319 wait_cont_idx = vep_session->wait_cont_idx;
3320 clib_spinlock_unlock (&vcm->sessions_lockp);
3321
3322 if (PREDICT_FALSE (!is_vep))
3323 {
3324 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003325 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003326 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003327 rv = VPPCOM_EINVAL;
3328 goto done;
3329 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003330 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003331 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003332 if (VPPCOM_DEBUG > 0)
3333 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003334 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003335 goto done;
3336 }
3337
3338 do
3339 {
3340 u32 sid;
3341 u32 next_sid = ~0;
3342 session_t *session;
3343
3344 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3345 sid != ~0; sid = next_sid)
3346 {
3347 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3348 u8 add_event, is_vep_session;
3349 int ready;
3350 u64 session_ev_data;
3351
3352 VCL_LOCK_AND_GET_SESSION (sid, &session);
3353 next_sid = session->vep.next_sid;
3354 session_events = session->vep.ev.events;
3355 et_mask = session->vep.et_mask;
3356 is_vep = session->is_vep;
3357 is_vep_session = session->is_vep_session;
3358 session_vep_idx = session->vep.vep_idx;
3359 session_ev_data = session->vep.ev.data.u64;
3360 clib_spinlock_unlock (&vcm->sessions_lockp);
3361
3362 if (PREDICT_FALSE (is_vep))
3363 {
3364 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003365 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003366 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003367 rv = VPPCOM_EINVAL;
3368 goto done;
3369 }
3370 if (PREDICT_FALSE (!is_vep_session))
3371 {
3372 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003373 clib_warning ("[%d] ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003374 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003375 rv = VPPCOM_EINVAL;
3376 goto done;
3377 }
3378 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3379 {
Dave Wallace774169b2017-11-01 20:07:40 -04003380 clib_warning ("[%d] ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003381 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003382 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003383 rv = VPPCOM_EINVAL;
3384 goto done;
3385 }
3386
3387 add_event = clear_et_mask = 0;
3388
Dave Wallace60caa062017-11-10 17:07:13 -05003389 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003390 {
3391 VCL_LOCK_AND_GET_SESSION (sid, &session);
3392 ready = vppcom_session_read_ready (session, sid);
3393 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003394 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003395 {
3396 add_event = 1;
3397 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003398 if (((EPOLLET | EPOLLIN) & session_events) ==
3399 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003400 clear_et_mask |= EPOLLIN;
3401 }
3402 else if (ready < 0)
3403 {
3404 add_event = 1;
3405 switch (ready)
3406 {
3407 case VPPCOM_ECONNRESET:
3408 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3409 break;
3410
3411 default:
3412 events[num_ev].events |= EPOLLERR;
3413 break;
3414 }
3415 }
3416 }
3417
Dave Wallace60caa062017-11-10 17:07:13 -05003418 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003419 {
3420 VCL_LOCK_AND_GET_SESSION (sid, &session);
3421 ready = vppcom_session_write_ready (session, sid);
3422 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003423 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003424 {
3425 add_event = 1;
3426 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003427 if (((EPOLLET | EPOLLOUT) & session_events) ==
3428 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003429 clear_et_mask |= EPOLLOUT;
3430 }
3431 else if (ready < 0)
3432 {
3433 add_event = 1;
3434 switch (ready)
3435 {
3436 case VPPCOM_ECONNRESET:
3437 events[num_ev].events |= EPOLLHUP;
3438 break;
3439
3440 default:
3441 events[num_ev].events |= EPOLLERR;
3442 break;
3443 }
3444 }
3445 }
3446
3447 if (add_event)
3448 {
3449 events[num_ev].data.u64 = session_ev_data;
3450 if (EPOLLONESHOT & session_events)
3451 {
3452 VCL_LOCK_AND_GET_SESSION (sid, &session);
3453 session->vep.ev.events = 0;
3454 clib_spinlock_unlock (&vcm->sessions_lockp);
3455 }
3456 num_ev++;
3457 if (num_ev == maxevents)
3458 {
3459 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3460 vep_session->wait_cont_idx = next_sid;
3461 clib_spinlock_unlock (&vcm->sessions_lockp);
3462 goto done;
3463 }
3464 }
3465 if (wait_cont_idx != ~0)
3466 {
3467 if (next_sid == ~0)
3468 next_sid = vep_next_sid;
3469 else if (next_sid == wait_cont_idx)
3470 next_sid = ~0;
3471 }
3472 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003473 if (wait_for_time != -1)
3474 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003475 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003476 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003477
3478 if (wait_cont_idx != ~0)
3479 {
3480 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3481 vep_session->wait_cont_idx = ~0;
3482 clib_spinlock_unlock (&vcm->sessions_lockp);
3483 }
3484done:
3485 return (rv != VPPCOM_OK) ? rv : num_ev;
3486}
3487
Dave Wallace35830af2017-10-09 01:43:42 -04003488int
3489vppcom_session_attr (uint32_t session_index, uint32_t op,
3490 void *buffer, uint32_t * buflen)
3491{
Dave Wallace35830af2017-10-09 01:43:42 -04003492 session_t *session;
3493 int rv = VPPCOM_OK;
3494 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003495 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003496
3497 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3498 switch (op)
3499 {
3500 case VPPCOM_ATTR_GET_NREAD:
3501 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05003502 if (VPPCOM_DEBUG > 2)
3503 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003504 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003505
3506 break;
3507
Dave Wallace227867f2017-11-13 21:21:53 -05003508 case VPPCOM_ATTR_GET_NWRITE:
3509 rv = vppcom_session_write_ready (session, session_index);
3510 if (VPPCOM_DEBUG > 2)
3511 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3512 getpid (), session_index, rv);
3513
Dave Wallace35830af2017-10-09 01:43:42 -04003514 break;
3515
3516 case VPPCOM_ATTR_GET_FLAGS:
3517 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3518 {
3519 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3520 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05003521 if (VPPCOM_DEBUG > 2)
3522 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3523 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3524 session_index, *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003525 }
3526 else
3527 rv = VPPCOM_EINVAL;
3528 break;
3529
3530 case VPPCOM_ATTR_SET_FLAGS:
3531 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3532 {
3533 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace227867f2017-11-13 21:21:53 -05003534 if (VPPCOM_DEBUG > 2)
3535 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3536 "flags = 0x%08x, is_nonblocking = %u",
3537 getpid (), *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003538 }
3539 else
3540 rv = VPPCOM_EINVAL;
3541 break;
3542
3543 case VPPCOM_ATTR_GET_PEER_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003544 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003545 {
Steven2199aab2017-10-15 20:18:47 -07003546 ep->vrf = session->vrf;
3547 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003548 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07003549 if (session->peer_addr.is_ip4)
3550 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3551 sizeof (ip4_address_t));
3552 else
3553 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3554 sizeof (ip6_address_t));
3555 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003556 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003557 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003558 "%u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003559 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003560 &session->peer_addr.ip46, ep->is_ip4,
3561 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003562 }
3563 else
3564 rv = VPPCOM_EINVAL;
3565 break;
3566
3567 case VPPCOM_ATTR_GET_LCL_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003568 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003569 {
Steven2199aab2017-10-15 20:18:47 -07003570 ep->vrf = session->vrf;
3571 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003572 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07003573 if (session->lcl_addr.is_ip4)
3574 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3575 sizeof (ip4_address_t));
3576 else
3577 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3578 sizeof (ip6_address_t));
3579 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003580 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003581 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003582 "%u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003583 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003584 &session->lcl_addr.ip46, ep->is_ip4,
3585 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003586 }
3587 else
3588 rv = VPPCOM_EINVAL;
3589 break;
Stevenb5a11602017-10-11 09:59:30 -07003590
3591 case VPPCOM_ATTR_SET_REUSEADDR:
3592 break;
3593
3594 case VPPCOM_ATTR_SET_BROADCAST:
3595 break;
3596
3597 case VPPCOM_ATTR_SET_V6ONLY:
3598 break;
Stevenbccd3392017-10-12 20:42:21 -07003599
3600 case VPPCOM_ATTR_SET_KEEPALIVE:
3601 break;
3602
3603 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3604 break;
3605
3606 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3607 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003608
3609 default:
3610 rv = VPPCOM_EINVAL;
3611 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003612 }
3613
3614done:
3615 clib_spinlock_unlock (&vcm->sessions_lockp);
3616 return rv;
3617}
3618
Stevenac1f96d2017-10-24 16:03:58 -07003619int
3620vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3621 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3622{
Stevenac1f96d2017-10-24 16:03:58 -07003623 int rv = VPPCOM_OK;
3624 session_t *session = 0;
3625
3626 if (ep)
3627 {
3628 clib_spinlock_lock (&vcm->sessions_lockp);
3629 rv = vppcom_session_at_index (session_index, &session);
3630 if (PREDICT_FALSE (rv))
3631 {
3632 clib_spinlock_unlock (&vcm->sessions_lockp);
3633 if (VPPCOM_DEBUG > 0)
3634 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003635 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003636 rv = VPPCOM_EBADFD;
3637 clib_spinlock_unlock (&vcm->sessions_lockp);
3638 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07003639 }
3640 ep->vrf = session->vrf;
3641 ep->is_ip4 = session->peer_addr.is_ip4;
3642 ep->port = session->peer_port;
3643 if (session->peer_addr.is_ip4)
3644 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3645 sizeof (ip4_address_t));
3646 else
3647 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3648 sizeof (ip6_address_t));
3649 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07003650 }
Steven58f464e2017-10-25 12:33:12 -07003651
3652 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003653 rv = vppcom_session_read (session_index, buffer, buflen);
3654 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003655 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003656 else
3657 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003658 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07003659 rv = VPPCOM_EAFNOSUPPORT;
3660 }
3661
Dave Wallacefaf9d772017-10-26 16:12:04 -04003662done:
Stevenac1f96d2017-10-24 16:03:58 -07003663 return rv;
3664}
3665
3666int
3667vppcom_session_sendto (uint32_t session_index, void *buffer,
3668 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3669{
Dave Wallace617dffa2017-10-26 14:47:06 -04003670 if (!buffer)
3671 return VPPCOM_EINVAL;
3672
3673 if (ep)
3674 {
3675 // TBD
3676 return VPPCOM_EINVAL;
3677 }
3678
3679 if (flags)
3680 {
3681 // TBD check the flags and do the right thing
3682 if (VPPCOM_DEBUG > 2)
3683 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003684 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003685 }
3686
3687 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003688}
3689
Dave Wallacee22aa742017-10-20 12:30:38 -04003690/*
3691 * fd.io coding-style-patch-verification: ON
3692 *
3693 * Local Variables:
3694 * eval: (c-set-style "gnu")
3695 * End:
3696 */