blob: 2e867c7f0364a2e232a4985d576481dd633004b7 [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 Wallace9d1d73a2017-11-20 02:31:48 -0500874 session->lcl_addr.is_ip4 = mp->is_ip4;
875 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
876 sizeof (session->peer_addr.ip46));
877 session->lcl_port = mp->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400878 session->state = STATE_CONNECT;
879
880 /* Add it to lookup table */
881 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500882
883 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500884 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
Dave Wallace60caa062017-11-10 17:07:13 -0500885 " session_rx_fifo %p, refcnt %d\n"
886 " session_tx_fifo %p, refcnt %d",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500887 getpid (), session_index, mp->handle,
Dave Wallace60caa062017-11-10 17:07:13 -0500888 session->server_rx_fifo,
889 session->server_rx_fifo->refcnt,
890 session->server_tx_fifo, session->server_tx_fifo->refcnt);
891
Dave Wallace543852a2017-08-03 02:11:34 -0400892 clib_spinlock_unlock (&vcm->sessions_lockp);
893}
894
895static void
896vppcom_send_connect_sock (session_t * session, u32 session_index)
897{
Dave Wallace543852a2017-08-03 02:11:34 -0400898 vl_api_connect_sock_t *cmp;
899
900 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
901 session->is_server = 0;
902 cmp = vl_msg_api_alloc (sizeof (*cmp));
903 memset (cmp, 0, sizeof (*cmp));
904 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
905 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400906 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400907
908 if (VPPCOM_DEBUG > 1)
Dave Wallace33e002b2017-09-06 01:20:02 -0400909 clib_warning ("[%d] session_index = %d 0x%08x",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500910 getpid (), session_index, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400911
912 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400913 cmp->is_ip4 = session->peer_addr.is_ip4;
914 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -0700915 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400916 cmp->proto = session->proto;
917 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
918 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
919}
920
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400921static inline void
922vppcom_send_disconnect (session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -0400923{
Dave Wallace543852a2017-08-03 02:11:34 -0400924 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400925
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400926 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400927 dmp = vl_msg_api_alloc (sizeof (*dmp));
928 memset (dmp, 0, sizeof (*dmp));
929 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
930 dmp->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500931 dmp->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400932 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400933}
934
935static void
936vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
937{
Dave Wallace543852a2017-08-03 02:11:34 -0400938 session_t *session = 0;
939 int rv;
940
941 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500942 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400943 ntohl (mp->retval));
944
945 ASSERT (vcm->bind_session_index != ~0);
946
947 clib_spinlock_lock (&vcm->sessions_lockp);
948 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
949 if (rv == VPPCOM_OK)
950 {
Dave Wallace60caa062017-11-10 17:07:13 -0500951 session->vpp_handle = mp->handle;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800952 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
953 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
954 sizeof (session->peer_addr.ip46));
955 session->lcl_port = mp->lcl_port;
956 vppcom_session_table_add_listener (mp->handle, vcm->bind_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400957 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
958 vcm->bind_session_index = ~0;
959 }
960 clib_spinlock_unlock (&vcm->sessions_lockp);
961}
962
963static void
964vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
965{
Dave Wallace543852a2017-08-03 02:11:34 -0400966 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400967
968 clib_spinlock_lock (&vcm->sessions_lockp);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800969 session = vppcom_session_table_lookup_listener (vcm->bind_session_index);
970
971 if (session)
Dave Wallace543852a2017-08-03 02:11:34 -0400972 {
Dave Wallace19481612017-09-15 18:47:44 -0400973 if ((VPPCOM_DEBUG > 1) && (mp->retval))
Dave Wallace2e005bb2017-11-07 01:21:39 -0500974 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
Dave Wallace19481612017-09-15 18:47:44 -0400975 ntohl (mp->retval));
976
Florin Corasdcf55ce2017-11-16 15:32:50 -0800977 vppcom_session_table_del_listener (vcm->bind_session_index);
Dave Wallace19481612017-09-15 18:47:44 -0400978 vcm->bind_session_index = ~0;
979 session->state = STATE_START;
Dave Wallace543852a2017-08-03 02:11:34 -0400980 }
Dave Wallace543852a2017-08-03 02:11:34 -0400981 clib_spinlock_unlock (&vcm->sessions_lockp);
982}
983
984u8 *
985format_ip4_address (u8 * s, va_list * args)
986{
987 u8 *a = va_arg (*args, u8 *);
988 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
989}
990
991u8 *
992format_ip6_address (u8 * s, va_list * args)
993{
994 ip6_address_t *a = va_arg (*args, ip6_address_t *);
995 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
996
997 i_max_n_zero = ARRAY_LEN (a->as_u16);
998 max_n_zeros = 0;
999 i_first_zero = i_max_n_zero;
1000 n_zeros = 0;
1001 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1002 {
1003 u32 is_zero = a->as_u16[i] == 0;
1004 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1005 {
1006 i_first_zero = i;
1007 n_zeros = 0;
1008 }
1009 n_zeros += is_zero;
1010 if ((!is_zero && n_zeros > max_n_zeros)
1011 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1012 {
1013 i_max_n_zero = i_first_zero;
1014 max_n_zeros = n_zeros;
1015 i_first_zero = ARRAY_LEN (a->as_u16);
1016 n_zeros = 0;
1017 }
1018 }
1019
1020 last_double_colon = 0;
1021 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1022 {
1023 if (i == i_max_n_zero && max_n_zeros > 1)
1024 {
1025 s = format (s, "::");
1026 i += max_n_zeros - 1;
1027 last_double_colon = 1;
1028 }
1029 else
1030 {
1031 s = format (s, "%s%x",
1032 (last_double_colon || i == 0) ? "" : ":",
1033 clib_net_to_host_u16 (a->as_u16[i]));
1034 last_double_colon = 0;
1035 }
1036 }
1037
1038 return s;
1039}
1040
1041/* Format an IP46 address. */
1042u8 *
1043format_ip46_address (u8 * s, va_list * args)
1044{
1045 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1046 ip46_type_t type = va_arg (*args, ip46_type_t);
1047 int is_ip4 = 1;
1048
1049 switch (type)
1050 {
1051 case IP46_TYPE_ANY:
1052 is_ip4 = ip46_address_is_ip4 (ip46);
1053 break;
1054 case IP46_TYPE_IP4:
1055 is_ip4 = 1;
1056 break;
1057 case IP46_TYPE_IP6:
1058 is_ip4 = 0;
1059 break;
1060 }
1061
1062 return is_ip4 ?
1063 format (s, "%U", format_ip4_address, &ip46->ip4) :
1064 format (s, "%U", format_ip6_address, &ip46->ip6);
1065}
1066
Dave Wallace60caa062017-11-10 17:07:13 -05001067static inline void
1068vppcom_send_accept_session_reply (u32 handle, int retval)
1069{
1070 vl_api_accept_session_reply_t *rmp;
1071
1072 rmp = vl_msg_api_alloc (sizeof (*rmp));
1073 memset (rmp, 0, sizeof (*rmp));
1074 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1075 rmp->retval = htonl (retval);
1076 rmp->handle = handle;
1077 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1078}
1079
Dave Wallace543852a2017-08-03 02:11:34 -04001080static void
1081vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1082{
Dave Wallace543852a2017-08-03 02:11:34 -04001083 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001084 session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001085 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001086
Dave Wallace60caa062017-11-10 17:07:13 -05001087 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001088 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1089 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001090 clib_warning ("[%d] client session queue is full!", getpid ());
Florin Corasdcf55ce2017-11-16 15:32:50 -08001091 vppcom_send_accept_session_reply (mp->handle,
1092 VNET_API_ERROR_QUEUE_FULL);
1093 clib_spinlock_unlock (&vcm->sessions_lockp);
1094 return;
1095 }
1096
1097 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1098 if (!listen_session)
1099 {
1100 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1101 "listener handle %llx", getpid (), mp->listener_handle);
Dave Wallace60caa062017-11-10 17:07:13 -05001102 clib_spinlock_unlock (&vcm->sessions_lockp);
1103 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001104 }
1105
Dave Wallace543852a2017-08-03 02:11:34 -04001106 /* Allocate local session and set it up */
1107 pool_get (vcm->sessions, session);
1108 memset (session, 0, sizeof (*session));
1109 session_index = session - vcm->sessions;
1110
1111 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1112 rx_fifo->client_session_index = session_index;
1113 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1114 tx_fifo->client_session_index = session_index;
1115
Dave Wallace60caa062017-11-10 17:07:13 -05001116 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001117 session->server_rx_fifo = rx_fifo;
1118 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001119 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1120 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001121 session->state = STATE_ACCEPT;
1122 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001123 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001124 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001125 session->peer_addr.is_ip4 = mp->is_ip4;
1126 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1127 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001128
1129 /* Add it to lookup table */
1130 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001131 session->lcl_port = listen_session->lcl_port;
1132 session->lcl_addr = listen_session->lcl_addr;
Dave Wallace227867f2017-11-13 21:21:53 -05001133
1134 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001135 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001136
Dave Wallacef7f809c2017-10-03 01:48:42 -04001137 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001138
Dave Wallace60caa062017-11-10 17:07:13 -05001139 if (VPPCOM_DEBUG > 1)
1140 {
1141 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1142 clib_warning ("[%d] received request to accept session (sid %d) "
1143 "from %s:%d", getpid (), session_index, ip_str,
1144 clib_net_to_host_u16 (mp->port));
1145 vec_free (ip_str);
1146 }
1147}
1148
1149static void
Dave Wallace227867f2017-11-13 21:21:53 -05001150vppcom_send_connect_session_reply (session_t * session, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001151{
1152 vl_api_connect_session_reply_t *rmp;
1153 u32 len;
1154 unix_shared_memory_queue_t *client_q;
1155
Dave Wallace543852a2017-08-03 02:11:34 -04001156 rmp = vl_msg_api_alloc (sizeof (*rmp));
1157 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001158
1159 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallace227867f2017-11-13 21:21:53 -05001160 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001161 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001162 rmp->handle = session->vpp_handle;
1163 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1164 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1165 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1166 rmp->segment_size = vcm->cfg.segment_size;
1167 len = vec_len (session->segment_name);
1168 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1169 clib_memcpy (rmp->segment_name, session->segment_name,
1170 rmp->segment_name_length - 1);
1171 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1172 sizeof (rmp->lcl_ip));
1173 rmp->is_ip4 = session->lcl_addr.is_ip4;
Dave Wallace60caa062017-11-10 17:07:13 -05001174 client_q = uword_to_pointer (session->client_queue_address,
1175 unix_shared_memory_queue_t *);
1176 ASSERT (client_q);
1177 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001178}
1179
1180/*
1181 * Acting as server for redirected connect requests
1182 */
1183static void
1184vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1185{
Dave Wallace543852a2017-08-03 02:11:34 -04001186 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001187 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001188
Dave Wallacef7f809c2017-10-03 01:48:42 -04001189 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001190 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1191 {
1192 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001193 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001194 clib_spinlock_unlock (&vcm->sessions_lockp);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001195 /* TBD: fix handle */
1196 vppcom_send_accept_session_reply (0, VNET_API_ERROR_QUEUE_FULL);
Dave Wallace60caa062017-11-10 17:07:13 -05001197 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001198 }
1199
Dave Wallace543852a2017-08-03 02:11:34 -04001200 pool_get (vcm->sessions, session);
1201 memset (session, 0, sizeof (*session));
1202 session_index = session - vcm->sessions;
1203
Dave Wallace60caa062017-11-10 17:07:13 -05001204 session->client_context = mp->context;
1205 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001206 session->client_queue_address = mp->client_queue_address;
1207 session->is_cut_thru = 1;
1208 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001209 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001210 session->peer_addr.is_ip4 = mp->is_ip4;
1211 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1212 sizeof (session->peer_addr.ip46));
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001213
Dave Wallace543852a2017-08-03 02:11:34 -04001214 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001215 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001216 if (VPPCOM_DEBUG > 1)
1217 clib_warning
Dave Wallace60caa062017-11-10 17:07:13 -05001218 ("[%d] Got a cut-thru connect request to client: "
1219 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
Dave Wallace2e005bb2017-11-07 01:21:39 -05001220 clib_fifo_elts (vcm->client_session_index_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001221 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001222}
1223
1224static void
1225vppcom_send_bind_sock (session_t * session)
1226{
Dave Wallace543852a2017-08-03 02:11:34 -04001227 vl_api_bind_sock_t *bmp;
1228
1229 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1230 session->is_server = 1;
1231 bmp = vl_msg_api_alloc (sizeof (*bmp));
1232 memset (bmp, 0, sizeof (*bmp));
1233
1234 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1235 bmp->client_index = vcm->my_client_index;
1236 bmp->context = htonl (0xfeedface);
1237 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001238 bmp->is_ip4 = session->lcl_addr.is_ip4;
1239 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001240 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001241 bmp->proto = session->proto;
1242 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1243 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1244}
1245
1246static void
1247vppcom_send_unbind_sock (u32 session_index)
1248{
Dave Wallace543852a2017-08-03 02:11:34 -04001249 vl_api_unbind_sock_t *ump;
1250 session_t *session = 0;
1251 int rv;
1252
1253 clib_spinlock_lock (&vcm->sessions_lockp);
1254 rv = vppcom_session_at_index (session_index, &session);
1255 if (PREDICT_FALSE (rv))
1256 {
1257 clib_spinlock_unlock (&vcm->sessions_lockp);
1258 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001259 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001260 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001261 return;
1262 }
1263
1264 ump = vl_msg_api_alloc (sizeof (*ump));
1265 memset (ump, 0, sizeof (*ump));
1266
1267 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1268 ump->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -05001269 ump->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001270 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1272}
1273
1274static int
Dave Wallace60caa062017-11-10 17:07:13 -05001275vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001276{
1277 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1278 svm_fifo_segment_private_t *seg;
1279 int rv = VPPCOM_OK;
1280
Dave Wallace60caa062017-11-10 17:07:13 -05001281 if (VPPCOM_DEBUG > 1)
1282 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1283 " server_rx_fifo %p, refcnt = %d\n"
1284 " server_tx_fifo %p, refcnt = %d",
1285 getpid (), session_index, session->sm_seg_index,
1286 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1287 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1288
Dave Wallace543852a2017-08-03 02:11:34 -04001289 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1290 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1291 FIFO_SEGMENT_RX_FREELIST);
1292 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1293 FIFO_SEGMENT_TX_FREELIST);
1294 svm_fifo_segment_delete (seg);
1295
1296 return rv;
1297}
1298
1299static int
1300vppcom_session_unbind (u32 session_index)
1301{
Dave Wallace543852a2017-08-03 02:11:34 -04001302 int rv;
1303
1304 clib_spinlock_lock (&vcm->sessions_lockp);
1305 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1306 {
1307 clib_spinlock_unlock (&vcm->sessions_lockp);
1308 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001309 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001310 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001311 return VPPCOM_EBADFD;
1312 }
1313 clib_spinlock_unlock (&vcm->sessions_lockp);
1314
Florin Corasdcf55ce2017-11-16 15:32:50 -08001315 if (vcm->bind_session_index != session_index)
1316 clib_warning ("[%d] ERROR: unbinding a not bound listener %u (%u)",
1317 session_index, vcm->bind_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001318 vcm->bind_session_index = session_index;
1319 vppcom_send_unbind_sock (session_index);
1320 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1321 vcm->cfg.session_timeout);
1322 if (PREDICT_FALSE (rv))
1323 {
1324 vcm->bind_session_index = ~0;
1325 if (VPPCOM_DEBUG > 0)
1326 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001327 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001328 return rv;
1329 }
1330 return VPPCOM_OK;
1331}
1332
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001333static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001334vppcom_session_disconnect (u32 session_index)
1335{
Dave Wallace543852a2017-08-03 02:11:34 -04001336 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001337 session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001338
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001339 clib_spinlock_lock (&vcm->sessions_lockp);
1340 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04001341 if (PREDICT_FALSE (rv))
1342 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001343 clib_spinlock_unlock (&vcm->sessions_lockp);
1344 if (VPPCOM_DEBUG > 1)
1345 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001346 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001347 return rv;
1348 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001349
1350 if (!session->is_cut_thru)
1351 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001352 if (VPPCOM_DEBUG > 1)
1353 clib_warning ("[%d] disconnecting sid (%u)", getpid (),
1354 session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001355 vppcom_send_disconnect (session);
1356 clib_spinlock_unlock (&vcm->sessions_lockp);
1357
1358 rv = vppcom_wait_for_session_state_change (session_index,
1359 STATE_DISCONNECT, 1.0);
Dave Wallace227867f2017-11-13 21:21:53 -05001360 /* TBD: Force clean up on error/timeout since there is no other
1361 * way to recover from a failed disconnect.
1362 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001363 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1364 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001365 getpid (), session_index, vppcom_retval_str (rv), rv);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001366 }
1367 else
Dave Wallace227867f2017-11-13 21:21:53 -05001368 {
1369 /* TBD: Handle cut-thru disconnect */
1370 clib_spinlock_unlock (&vcm->sessions_lockp);
1371 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001372
Dave Wallace543852a2017-08-03 02:11:34 -04001373 return VPPCOM_OK;
1374}
1375
1376#define foreach_sock_msg \
1377_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1378_(BIND_SOCK_REPLY, bind_sock_reply) \
1379_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1380_(ACCEPT_SESSION, accept_session) \
1381_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001382_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001383_(DISCONNECT_SESSION, disconnect_session) \
1384_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1385_(RESET_SESSION, reset_session) \
1386_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1387_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1388_(MAP_ANOTHER_SEGMENT, map_another_segment)
1389
1390static void
1391vppcom_api_hookup (void)
1392{
1393#define _(N,n) \
1394 vl_msg_api_set_handlers(VL_API_##N, #n, \
1395 vl_api_##n##_t_handler, \
1396 vl_noop_handler, \
1397 vl_api_##n##_t_endian, \
1398 vl_api_##n##_t_print, \
1399 sizeof(vl_api_##n##_t), 1);
1400 foreach_sock_msg;
1401#undef _
1402}
1403
1404static void
1405vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1406{
1407 ASSERT (vcl_cfg);
1408
1409 vcl_cfg->heapsize = (256ULL << 20);
1410 vcl_cfg->segment_baseva = 0x200000000ULL;
1411 vcl_cfg->segment_size = (256 << 20);
1412 vcl_cfg->add_segment_size = (128 << 20);
1413 vcl_cfg->preallocated_fifo_pairs = 8;
1414 vcl_cfg->rx_fifo_size = (1 << 20);
1415 vcl_cfg->tx_fifo_size = (1 << 20);
1416 vcl_cfg->event_queue_size = 2048;
1417 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1418 vcl_cfg->app_timeout = 10 * 60.0;
1419 vcl_cfg->session_timeout = 10 * 60.0;
1420 vcl_cfg->accept_timeout = 60.0;
1421}
1422
1423static void
1424vppcom_cfg_heapsize (char *conf_fname)
1425{
Dave Wallace543852a2017-08-03 02:11:34 -04001426 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1427 FILE *fp;
1428 char inbuf[4096];
1429 int argc = 1;
1430 char **argv = NULL;
1431 char *arg = NULL;
1432 char *p;
1433 int i;
1434 u8 *sizep;
1435 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001436 void *vcl_mem;
1437 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001438
1439 fp = fopen (conf_fname, "r");
1440 if (fp == NULL)
1441 {
1442 if (VPPCOM_DEBUG > 0)
1443 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1444 goto defaulted;
1445 }
1446 argv = calloc (1, sizeof (char *));
1447 if (argv == NULL)
1448 goto defaulted;
1449
1450 while (1)
1451 {
1452 if (fgets (inbuf, 4096, fp) == 0)
1453 break;
1454 p = strtok (inbuf, " \t\n");
1455 while (p != NULL)
1456 {
1457 if (*p == '#')
1458 break;
1459 argc++;
1460 char **tmp = realloc (argv, argc * sizeof (char *));
1461 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001462 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001463 argv = tmp;
1464 arg = strndup (p, 1024);
1465 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001466 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001467 argv[argc - 1] = arg;
1468 p = strtok (NULL, " \t\n");
1469 }
1470 }
1471
1472 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001473 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001474
1475 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1476 if (tmp == NULL)
1477 goto defaulted;
1478 argv = tmp;
1479 argv[argc] = NULL;
1480
1481 /*
1482 * Look for and parse the "heapsize" config parameter.
1483 * Manual since none of the clib infra has been bootstrapped yet.
1484 *
1485 * Format: heapsize <nn>[mM][gG]
1486 */
1487
1488 for (i = 1; i < (argc - 1); i++)
1489 {
1490 if (!strncmp (argv[i], "heapsize", 8))
1491 {
1492 sizep = (u8 *) argv[i + 1];
1493 size = 0;
1494 while (*sizep >= '0' && *sizep <= '9')
1495 {
1496 size *= 10;
1497 size += *sizep++ - '0';
1498 }
1499 if (size == 0)
1500 {
1501 if (VPPCOM_DEBUG > 0)
1502 clib_warning ("[%d] parse error '%s %s', "
1503 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001504 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001505 vcl_cfg->heapsize, vcl_cfg->heapsize);
1506 goto defaulted;
1507 }
1508
1509 if (*sizep == 'g' || *sizep == 'G')
1510 vcl_cfg->heapsize = size << 30;
1511 else if (*sizep == 'm' || *sizep == 'M')
1512 vcl_cfg->heapsize = size << 20;
1513 else
1514 {
1515 if (VPPCOM_DEBUG > 0)
1516 clib_warning ("[%d] parse error '%s %s', "
1517 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001518 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001519 vcl_cfg->heapsize, vcl_cfg->heapsize);
1520 goto defaulted;
1521 }
1522 }
1523 }
1524
1525defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001526 if (fp != NULL)
1527 fclose (fp);
1528 if (argv != NULL)
1529 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001530
Dave Wallace2e005bb2017-11-07 01:21:39 -05001531 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1532 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001533 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001534 {
1535 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1536 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1537 "-1, 0) failed!",
1538 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1539 return;
1540 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001541 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1542 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001543 {
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001544 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1545 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001546 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001547 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1548 if (!vcl_mem)
1549 {
1550 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1551 return;
1552 }
1553
1554 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1555 vcm = vcl_mem;
1556
1557 if (VPPCOM_DEBUG > 0)
1558 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1559 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001560}
1561
1562static void
1563vppcom_cfg_read (char *conf_fname)
1564{
Dave Wallace543852a2017-08-03 02:11:34 -04001565 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1566 int fd;
1567 unformat_input_t _input, *input = &_input;
1568 unformat_input_t _line_input, *line_input = &_line_input;
1569 u8 vc_cfg_input = 0;
1570 u8 *chroot_path;
1571 struct stat s;
1572 u32 uid, gid;
1573
1574 fd = open (conf_fname, O_RDONLY);
1575 if (fd < 0)
1576 {
1577 if (VPPCOM_DEBUG > 0)
1578 clib_warning ("[%d] open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001579 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001580 goto file_done;
1581 }
1582
1583 if (fstat (fd, &s) < 0)
1584 {
1585 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001586 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001587 goto file_done;
1588 }
1589
1590 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1591 {
1592 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001593 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001594 goto file_done;
1595 }
1596
Dave Barach59b25652017-09-10 15:04:27 -04001597 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001598
1599 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1600 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001601 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001602 unformat_skip_white_space (line_input);
1603
Dave Wallace8af20542017-10-26 03:29:30 -04001604 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001605 {
1606 vc_cfg_input = 1;
1607 continue;
1608 }
1609
1610 if (vc_cfg_input)
1611 {
1612 if (unformat (line_input, "heapsize %s", &chroot_path))
1613 {
1614 vec_terminate_c_string (chroot_path);
1615 if (VPPCOM_DEBUG > 0)
1616 clib_warning ("[%d] configured heapsize %s, "
1617 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001618 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001619 vcl_cfg->heapsize);
1620 vec_free (chroot_path);
1621 }
1622 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1623 {
1624 vec_terminate_c_string (chroot_path);
1625 vl_set_memory_root_path ((char *) chroot_path);
1626 if (VPPCOM_DEBUG > 0)
1627 clib_warning ("[%d] configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001628 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001629 chroot_path = 0; /* Don't vec_free() it! */
1630 }
1631 else if (unformat (line_input, "uid %d", &uid))
1632 {
1633 vl_set_memory_uid (uid);
1634 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001635 clib_warning ("[%d] configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001636 }
1637 else if (unformat (line_input, "gid %d", &gid))
1638 {
1639 vl_set_memory_gid (gid);
1640 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001641 clib_warning ("[%d] configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001642 }
Dave Wallace8af20542017-10-26 03:29:30 -04001643 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001644 &vcl_cfg->segment_baseva))
1645 {
1646 if (VPPCOM_DEBUG > 0)
Dave Wallace8af20542017-10-26 03:29:30 -04001647 clib_warning ("[%d] configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001648 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001649 }
1650 else if (unformat (line_input, "segment-size 0x%lx",
1651 &vcl_cfg->segment_size))
1652 {
1653 if (VPPCOM_DEBUG > 0)
1654 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001655 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001656 vcl_cfg->segment_size);
1657 }
1658 else if (unformat (line_input, "segment-size %ld",
1659 &vcl_cfg->segment_size))
1660 {
1661 if (VPPCOM_DEBUG > 0)
1662 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001663 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001664 vcl_cfg->segment_size);
1665 }
1666 else if (unformat (line_input, "add-segment-size 0x%lx",
1667 &vcl_cfg->add_segment_size))
1668 {
1669 if (VPPCOM_DEBUG > 0)
1670 clib_warning
1671 ("[%d] configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001672 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001673 vcl_cfg->add_segment_size);
1674 }
1675 else if (unformat (line_input, "add-segment-size %ld",
1676 &vcl_cfg->add_segment_size))
1677 {
1678 if (VPPCOM_DEBUG > 0)
1679 clib_warning
1680 ("[%d] configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001681 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001682 vcl_cfg->add_segment_size);
1683 }
1684 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1685 &vcl_cfg->preallocated_fifo_pairs))
1686 {
1687 if (VPPCOM_DEBUG > 0)
1688 clib_warning ("[%d] configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001689 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001690 vcl_cfg->preallocated_fifo_pairs,
1691 vcl_cfg->preallocated_fifo_pairs);
1692 }
1693 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1694 &vcl_cfg->rx_fifo_size))
1695 {
1696 if (VPPCOM_DEBUG > 0)
1697 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001698 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001699 vcl_cfg->rx_fifo_size);
1700 }
1701 else if (unformat (line_input, "rx-fifo-size %ld",
1702 &vcl_cfg->rx_fifo_size))
1703 {
1704 if (VPPCOM_DEBUG > 0)
1705 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001706 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001707 vcl_cfg->rx_fifo_size);
1708 }
1709 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1710 &vcl_cfg->tx_fifo_size))
1711 {
1712 if (VPPCOM_DEBUG > 0)
1713 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001714 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001715 vcl_cfg->tx_fifo_size);
1716 }
1717 else if (unformat (line_input, "tx-fifo-size %ld",
1718 &vcl_cfg->tx_fifo_size))
1719 {
1720 if (VPPCOM_DEBUG > 0)
1721 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001722 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001723 vcl_cfg->tx_fifo_size);
1724 }
1725 else if (unformat (line_input, "event-queue-size 0x%lx",
1726 &vcl_cfg->event_queue_size))
1727 {
1728 if (VPPCOM_DEBUG > 0)
1729 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001730 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001731 vcl_cfg->event_queue_size);
1732 }
1733 else if (unformat (line_input, "event-queue-size %ld",
1734 &vcl_cfg->event_queue_size))
1735 {
1736 if (VPPCOM_DEBUG > 0)
1737 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001738 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001739 vcl_cfg->event_queue_size);
1740 }
1741 else if (unformat (line_input, "listen-queue-size 0x%lx",
1742 &vcl_cfg->listen_queue_size))
1743 {
1744 if (VPPCOM_DEBUG > 0)
1745 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001746 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001747 vcl_cfg->listen_queue_size);
1748 }
1749 else if (unformat (line_input, "listen-queue-size %ld",
1750 &vcl_cfg->listen_queue_size))
1751 {
1752 if (VPPCOM_DEBUG > 0)
1753 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001754 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001755 vcl_cfg->listen_queue_size);
1756 }
1757 else if (unformat (line_input, "app-timeout %f",
1758 &vcl_cfg->app_timeout))
1759 {
1760 if (VPPCOM_DEBUG > 0)
1761 clib_warning ("[%d] configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001762 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001763 }
1764 else if (unformat (line_input, "session-timeout %f",
1765 &vcl_cfg->session_timeout))
1766 {
1767 if (VPPCOM_DEBUG > 0)
1768 clib_warning ("[%d] configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001769 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001770 }
1771 else if (unformat (line_input, "accept-timeout %f",
1772 &vcl_cfg->accept_timeout))
1773 {
1774 if (VPPCOM_DEBUG > 0)
1775 clib_warning ("[%d] configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001776 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001777 }
Dave Wallace774169b2017-11-01 20:07:40 -04001778 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001779 {
Dave Wallace774169b2017-11-01 20:07:40 -04001780 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001781 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001782 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001783 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001784 }
Dave Wallace774169b2017-11-01 20:07:40 -04001785 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001786 {
Dave Wallace774169b2017-11-01 20:07:40 -04001787 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001788 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001789 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001790 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001791 }
Dave Wallace774169b2017-11-01 20:07:40 -04001792 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001793 {
Dave Wallace774169b2017-11-01 20:07:40 -04001794 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001795 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001796 clib_warning ("[%d] configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001797 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001798 }
1799 else if (unformat (line_input, "app-scope-global"))
1800 {
1801 vcl_cfg->app_scope_global = 1;
1802 if (VPPCOM_DEBUG > 0)
1803 clib_warning ("[%d] configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001804 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001805 }
1806 else if (unformat (line_input, "namespace-secret %lu",
1807 &vcl_cfg->namespace_secret))
1808 {
1809 if (VPPCOM_DEBUG > 0)
1810 clib_warning
1811 ("[%d] configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001812 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04001813 vcl_cfg->namespace_secret);
1814 }
1815 else if (unformat (line_input, "namespace-id %v",
1816 &vcl_cfg->namespace_id))
1817 {
1818 vl_api_application_attach_t *mp;
1819 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1820 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1821 if (nsid_vec_len > max_nsid_vec_len)
1822 {
1823 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1824 if (VPPCOM_DEBUG > 0)
1825 clib_warning ("[%d] configured namespace_id is too long,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05001826 " truncated to %d characters!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001827 max_nsid_vec_len);
1828 }
1829
1830 if (VPPCOM_DEBUG > 0)
1831 clib_warning ("[%d] configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001832 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001833 }
Dave Wallace543852a2017-08-03 02:11:34 -04001834 else if (unformat (line_input, "}"))
1835 {
1836 vc_cfg_input = 0;
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("[%d] completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001839 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001840 goto input_done;
1841 }
1842 else
1843 {
1844 if (line_input->buffer[line_input->index] != '#')
1845 {
1846 clib_warning ("[%d] Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001847 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001848 &line_input->buffer[line_input->index]);
1849 }
1850 }
1851 }
1852 }
1853
1854input_done:
1855 unformat_free (input);
1856
1857file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001858 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001859 close (fd);
1860}
1861
1862/*
1863 * VPPCOM Public API functions
1864 */
1865int
1866vppcom_app_create (char *app_name)
1867{
Dave Wallace543852a2017-08-03 02:11:34 -04001868 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1869 u8 *heap;
1870 mheap_t *h;
1871 int rv;
1872
1873 if (!vcm->init)
1874 {
1875 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001876 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001877
1878 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001879 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001880 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1881 if (env_var_str)
1882 {
1883 u32 tmp;
1884 if (sscanf (env_var_str, "%u", &tmp) != 1)
1885 clib_warning ("[%d] Invalid debug level specified in "
1886 "the environment variable "
1887 VPPCOM_ENV_DEBUG
1888 " (%s)!\n", getpid (), env_var_str);
1889 else
1890 {
1891 vcm->debug = tmp;
1892 clib_warning ("[%d] configured debug level (%u) from "
1893 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1894 }
1895 }
Dave Wallace8af20542017-10-26 03:29:30 -04001896 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001897 if (!conf_fname)
1898 {
1899 conf_fname = VPPCOM_CONF_DEFAULT;
1900 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001901 clib_warning ("[%d] getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001902 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001903 }
1904 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001905 clib_fifo_validate (vcm->client_session_index_fifo,
1906 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001907 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04001908 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1909 if (env_var_str)
1910 {
1911 u32 ns_id_vec_len = strlen (env_var_str);
1912
1913 vec_reset_length (vcm->cfg.namespace_id);
1914 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1915 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1916
1917 if (VPPCOM_DEBUG > 0)
1918 clib_warning ("[%d] configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001919 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001920 vcm->cfg.namespace_id);
1921 }
1922 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1923 if (env_var_str)
1924 {
1925 u64 tmp;
1926 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1927 clib_warning ("[%d] Invalid namespace secret specified in "
1928 "the environment variable "
1929 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001930 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001931 else
1932 {
1933 vcm->cfg.namespace_secret = tmp;
1934 if (VPPCOM_DEBUG > 0)
1935 clib_warning ("[%d] configured namespace secret (%lu) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001936 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001937 vcm->cfg.namespace_secret);
1938 }
1939 }
Dave Wallace774169b2017-11-01 20:07:40 -04001940 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001941 {
Dave Wallace774169b2017-11-01 20:07:40 -04001942 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001943 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001944 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001945 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001946 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001947 }
Dave Wallace774169b2017-11-01 20:07:40 -04001948 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001949 {
Dave Wallace774169b2017-11-01 20:07:40 -04001950 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001951 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001952 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001953 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001954 vcm->cfg.app_proxy_transport_udp);
1955 }
1956 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1957 {
1958 vcm->cfg.app_scope_local = 1;
1959 if (VPPCOM_DEBUG > 0)
1960 clib_warning ("[%d] configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001961 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001962 vcm->cfg.app_scope_local);
1963 }
1964 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1965 {
1966 vcm->cfg.app_scope_global = 1;
1967 if (VPPCOM_DEBUG > 0)
1968 clib_warning ("[%d] configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001969 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001970 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001971 }
1972
Dave Wallace543852a2017-08-03 02:11:34 -04001973 vcm->bind_session_index = ~0;
1974 vcm->main_cpu = os_get_thread_index ();
1975 heap = clib_mem_get_per_cpu_heap ();
1976 h = mheap_header (heap);
1977
1978 /* make the main heap thread-safe */
1979 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1980
1981 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1982
1983 clib_time_init (&vcm->clib_time);
1984 vppcom_init_error_string_table ();
1985 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1986 20 /* timeout in secs */ );
1987 clib_spinlock_init (&vcm->sessions_lockp);
1988 vppcom_api_hookup ();
1989 }
1990
1991 if (vcm->my_client_index == ~0)
1992 {
1993 vcm->app_state = STATE_APP_START;
1994 rv = vppcom_connect_to_vpp (app_name);
1995 if (rv)
1996 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001997 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001998 return rv;
1999 }
2000
2001 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002002 clib_warning ("[%d] sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002003
2004 rv = vppcom_app_session_enable ();
2005 if (rv)
2006 {
2007 clib_warning ("[%d] vppcom_app_session_enable() failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002008 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002009 return rv;
2010 }
2011
2012 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002013 clib_warning ("[%d] sending app attach", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002014
2015 rv = vppcom_app_attach ();
2016 if (rv)
2017 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002018 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002019 return rv;
2020 }
Dave Wallace543852a2017-08-03 02:11:34 -04002021
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002022 if (VPPCOM_DEBUG > 0)
2023 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002024 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002025 vcm->my_client_index);
2026 }
Dave Wallace543852a2017-08-03 02:11:34 -04002027
2028 return VPPCOM_OK;
2029}
2030
2031void
2032vppcom_app_destroy (void)
2033{
Dave Wallace543852a2017-08-03 02:11:34 -04002034 int rv;
2035
2036 if (vcm->my_client_index == ~0)
2037 return;
2038
2039 if (VPPCOM_DEBUG > 0)
2040 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002041 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002042
2043 vppcom_app_detach ();
2044 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2045 if (PREDICT_FALSE (rv))
2046 {
2047 if (VPPCOM_DEBUG > 0)
2048 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002049 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002050 }
2051 vl_client_disconnect_from_vlib ();
2052 vcm->my_client_index = ~0;
2053 vcm->app_state = STATE_APP_START;
2054}
2055
2056int
2057vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2058{
Dave Wallace543852a2017-08-03 02:11:34 -04002059 session_t *session;
2060 u32 session_index;
2061
2062 clib_spinlock_lock (&vcm->sessions_lockp);
2063 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002064 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002065 session_index = session - vcm->sessions;
2066
2067 session->vrf = vrf;
2068 session->proto = proto;
2069 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002070 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002071 clib_spinlock_unlock (&vcm->sessions_lockp);
2072
2073 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002074 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002075
2076 return (int) session_index;
2077}
2078
2079int
2080vppcom_session_close (uint32_t session_index)
2081{
Dave Wallace543852a2017-08-03 02:11:34 -04002082 session_t *session = 0;
2083 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002084 u8 is_server;
2085 u8 is_listen;
2086 u8 is_cut_thru;
2087 u8 is_vep;
2088 u8 is_vep_session;
2089 u32 next_sid;
2090 u32 vep_idx;
2091 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002092
2093 clib_spinlock_lock (&vcm->sessions_lockp);
2094 rv = vppcom_session_at_index (session_index, &session);
2095 if (PREDICT_FALSE (rv))
2096 {
Dave Wallace543852a2017-08-03 02:11:34 -04002097 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002098 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002099 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002100 clib_spinlock_unlock (&vcm->sessions_lockp);
2101 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002102 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002103 is_server = session->is_server;
2104 is_listen = session->is_listen;
2105 is_cut_thru = session->is_cut_thru;
2106 is_vep = session->is_vep;
2107 is_vep_session = session->is_vep_session;
2108 next_sid = session->vep.next_sid;
2109 vep_idx = session->vep.vep_idx;
2110 state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04002111 clib_spinlock_unlock (&vcm->sessions_lockp);
2112
2113 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002114 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002115
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002116 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002117 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002118 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002119 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002120 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallace19481612017-09-15 18:47:44 -04002121 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002122 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002123 "rv = %s (%d)", getpid (), vep_idx, next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002124 vppcom_retval_str (rv), rv);
2125
2126 clib_spinlock_lock (&vcm->sessions_lockp);
2127 rv = vppcom_session_at_index (session_index, &session);
2128 if (PREDICT_FALSE (rv))
2129 {
2130 if (VPPCOM_DEBUG > 0)
2131 clib_warning
2132 ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002133 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002134 clib_spinlock_unlock (&vcm->sessions_lockp);
2135 goto done;
2136 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002137 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002138 clib_spinlock_unlock (&vcm->sessions_lockp);
2139 }
2140 }
2141 else
2142 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002143 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002144 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002145 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2146 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2147 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002148 "rv = %s (%d)", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002149 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150 }
2151
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002152 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002153 {
Dave Wallace60caa062017-11-10 17:07:13 -05002154 rv = vppcom_session_unbind_cut_thru (session, session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002155 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2156 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2157 "rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002158 getpid (), session_index,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002159 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002160 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002161 else if (is_server && is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002162 {
2163 rv = vppcom_session_unbind (session_index);
2164 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2165 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002166 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002167 vppcom_retval_str (rv), rv);
2168 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002169 else if (state == STATE_CONNECT)
2170 if (vppcom_session_disconnect (session_index))
2171 goto done;
Dave Wallace19481612017-09-15 18:47:44 -04002172 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002173 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002174 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002175 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002176done:
Dave Wallace543852a2017-08-03 02:11:34 -04002177 return rv;
2178}
2179
2180int
2181vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2182{
Dave Wallace543852a2017-08-03 02:11:34 -04002183 session_t *session = 0;
2184 int rv;
2185
2186 if (!ep || !ep->ip)
2187 return VPPCOM_EINVAL;
2188
2189 clib_spinlock_lock (&vcm->sessions_lockp);
2190 rv = vppcom_session_at_index (session_index, &session);
2191 if (PREDICT_FALSE (rv))
2192 {
2193 clib_spinlock_unlock (&vcm->sessions_lockp);
2194 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002195 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002196 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002197 return rv;
2198 }
2199
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002200 if (session->is_vep)
2201 {
2202 clib_spinlock_unlock (&vcm->sessions_lockp);
2203 if (VPPCOM_DEBUG > 0)
2204 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002205 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002206 return VPPCOM_EBADFD;
2207 }
2208
Dave Wallace543852a2017-08-03 02:11:34 -04002209 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002210 session->lcl_addr.is_ip4 = ep->is_ip4;
2211 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002212 session->lcl_port = ep->port;
2213
2214 if (VPPCOM_DEBUG > 0)
2215 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002216 getpid (), session_index, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07002217 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2218 clib_net_to_host_u16 (session->lcl_port));
Dave Wallace543852a2017-08-03 02:11:34 -04002219
2220 clib_spinlock_unlock (&vcm->sessions_lockp);
2221 return VPPCOM_OK;
2222}
2223
2224int
Dave Wallace33e002b2017-09-06 01:20:02 -04002225vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002226{
Dave Wallace33e002b2017-09-06 01:20:02 -04002227 session_t *listen_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002228 int rv;
2229
2230 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002231 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002232 if (PREDICT_FALSE (rv))
2233 {
2234 clib_spinlock_unlock (&vcm->sessions_lockp);
2235 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002236 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002237 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002238 return rv;
2239 }
2240
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002241 if (listen_session->is_vep)
2242 {
2243 clib_spinlock_unlock (&vcm->sessions_lockp);
2244 if (VPPCOM_DEBUG > 0)
2245 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002246 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002247 return VPPCOM_EBADFD;
2248 }
2249
Dave Wallacee695cb42017-11-02 22:04:42 -04002250 if (listen_session->is_listen)
2251 {
2252 clib_spinlock_unlock (&vcm->sessions_lockp);
2253 if (VPPCOM_DEBUG > 0)
2254 clib_warning ("[%d] sid (%u) is already in listen state!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002255 getpid (), listen_session_index);
Dave Wallacee695cb42017-11-02 22:04:42 -04002256 return VPPCOM_OK;
2257 }
2258
Dave Wallace543852a2017-08-03 02:11:34 -04002259 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002260 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002261
2262 ASSERT (vcm->bind_session_index == ~0);
Dave Wallace33e002b2017-09-06 01:20:02 -04002263 vcm->bind_session_index = listen_session_index;
2264 vppcom_send_bind_sock (listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002265 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002266 rv =
2267 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2268 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002269 if (PREDICT_FALSE (rv))
2270 {
2271 vcm->bind_session_index = ~0;
2272 if (VPPCOM_DEBUG > 0)
2273 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002274 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002275 return rv;
2276 }
2277
2278 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002279 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002280 if (PREDICT_FALSE (rv))
2281 {
2282 clib_spinlock_unlock (&vcm->sessions_lockp);
2283 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002284 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002285 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002286 return rv;
2287 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002288 listen_session->is_listen = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002289 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002290 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002291
2292 return VPPCOM_OK;
2293}
2294
2295int
2296vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace227867f2017-11-13 21:21:53 -05002297 uint32_t flags, double wait_for_time)
Dave Wallace543852a2017-08-03 02:11:34 -04002298{
Dave Wallace33e002b2017-09-06 01:20:02 -04002299 session_t *listen_session = 0;
2300 session_t *client_session = 0;
Dave Wallace227867f2017-11-13 21:21:53 -05002301 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002302 int rv;
2303 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002304 char *cut_thru_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002305
2306 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002307 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002308 if (PREDICT_FALSE (rv))
2309 {
2310 clib_spinlock_unlock (&vcm->sessions_lockp);
2311 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002312 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002313 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002314 return rv;
2315 }
2316
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002317 if (listen_session->is_vep)
2318 {
2319 clib_spinlock_unlock (&vcm->sessions_lockp);
2320 if (VPPCOM_DEBUG > 0)
2321 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002322 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002323 return VPPCOM_EBADFD;
2324 }
2325
Dave Wallace33e002b2017-09-06 01:20:02 -04002326 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002327 {
2328 clib_spinlock_unlock (&vcm->sessions_lockp);
2329 if (VPPCOM_DEBUG > 0)
2330 clib_warning ("[%d] session not in listen state, state = %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002331 getpid (),
Dave Wallace33e002b2017-09-06 01:20:02 -04002332 vppcom_session_state_str (listen_session->state));
Dave Wallace543852a2017-08-03 02:11:34 -04002333 return VPPCOM_EBADFD;
2334 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002335 wait_for = listen_session->is_nonblocking ? 0 :
Dave Wallace543852a2017-08-03 02:11:34 -04002336 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2337
2338 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002339 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002340 listen_session_index,
Dave Wallace33e002b2017-09-06 01:20:02 -04002341 vppcom_session_state_str (listen_session->state),
2342 listen_session->state);
Dave Wallace543852a2017-08-03 02:11:34 -04002343 clib_spinlock_unlock (&vcm->sessions_lockp);
2344
2345 while (1)
2346 {
2347 rv = vppcom_wait_for_client_session_index (wait_for);
2348 if (rv)
2349 {
2350 if ((VPPCOM_DEBUG > 0))
2351 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002352 getpid (), listen_session_index,
Dave Wallace543852a2017-08-03 02:11:34 -04002353 vppcom_retval_str (rv), rv);
2354 if ((wait_for == 0) || (wait_for_time > 0))
2355 return rv;
2356 }
2357 else
2358 break;
2359 }
2360
Dave Wallace543852a2017-08-03 02:11:34 -04002361 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002362 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002363 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002364 ASSERT (rv == VPPCOM_OK);
Dave Wallace35830af2017-10-09 01:43:42 -04002365 ASSERT (client_session->peer_addr.is_ip4 ==
2366 listen_session->lcl_addr.is_ip4);
Dave Wallace543852a2017-08-03 02:11:34 -04002367
Dave Wallace227867f2017-11-13 21:21:53 -05002368 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002369 if (VPPCOM_DEBUG > 0)
Dave Wallace227867f2017-11-13 21:21:53 -05002370 clib_warning ("[%d] Got a request: client sid %d, flags %d, "
2371 " is_nonblocking %u", getpid (), client_session_index,
2372 flags, client_session->is_nonblocking);
Dave Wallace543852a2017-08-03 02:11:34 -04002373
Dave Wallace33e002b2017-09-06 01:20:02 -04002374 ep->vrf = client_session->vrf;
2375 ep->is_cut_thru = client_session->is_cut_thru;
Dave Wallace35830af2017-10-09 01:43:42 -04002376 ep->is_ip4 = client_session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07002377 ep->port = client_session->peer_port;
Dave Wallace35830af2017-10-09 01:43:42 -04002378 if (client_session->peer_addr.is_ip4)
2379 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2380 sizeof (ip4_address_t));
Dave Wallace33e002b2017-09-06 01:20:02 -04002381 else
Dave Wallace35830af2017-10-09 01:43:42 -04002382 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2383 sizeof (ip6_address_t));
Dave Wallace60caa062017-11-10 17:07:13 -05002384
2385 if (client_session->is_server && client_session->is_cut_thru)
2386 {
2387 static svm_fifo_segment_create_args_t _a;
2388 svm_fifo_segment_create_args_t *a = &_a;
2389 svm_fifo_segment_private_t *seg;
2390
2391 cut_thru_str = "cut-thru ";
2392
2393 /* Create the segment */
2394 memset (a, 0, sizeof (*a));
2395 a->segment_name = (char *)
2396 format ((u8 *) a->segment_name, "%d:segment%d%c",
2397 getpid (), vcm->unique_segment_index++, 0);
2398 a->segment_size = vcm->cfg.segment_size;
2399 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2400 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2401 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2402
2403 rv = svm_fifo_segment_create (a);
2404 if (PREDICT_FALSE (rv))
2405 {
2406 if (VPPCOM_DEBUG > 1)
2407 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2408 getpid (), a->segment_name);
2409 vec_reset_length (a->new_segment_indices);
2410 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002411 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002412 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 return VPPCOM_ENOMEM;
2414 }
2415
2416 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2417 client_session->sm_seg_index = a->new_segment_indices[0];
2418 vec_free (a->new_segment_indices);
2419
2420 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2421 client_session->server_rx_fifo =
2422 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2423 FIFO_SEGMENT_RX_FREELIST);
2424 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2425 {
2426 svm_fifo_segment_delete (seg);
2427 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2428 getpid (), vcm->cfg.rx_fifo_size,
2429 vcm->cfg.rx_fifo_size);
2430 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002431 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002432 clib_spinlock_unlock (&vcm->sessions_lockp);
2433 return VPPCOM_ENOMEM;
2434 }
2435 client_session->server_rx_fifo->master_session_index =
2436 client_session_index;
2437
2438 client_session->server_tx_fifo =
2439 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2440 FIFO_SEGMENT_TX_FREELIST);
2441 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2442 {
2443 svm_fifo_segment_delete (seg);
2444 if (VPPCOM_DEBUG > 1)
2445 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2446 getpid (), vcm->cfg.tx_fifo_size,
2447 vcm->cfg.tx_fifo_size);
2448 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002449 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002450 clib_spinlock_unlock (&vcm->sessions_lockp);
2451 return VPPCOM_ENOMEM;
2452 }
2453 client_session->server_tx_fifo->master_session_index =
2454 client_session_index;
2455
2456 if (VPPCOM_DEBUG > 1)
2457 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2458 getpid (), client_session->segment_name,
2459 client_session->server_rx_fifo,
2460 client_session->server_tx_fifo);
2461
2462#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2463 {
2464 void *oldheap;
2465 ssvm_shared_header_t *sh = seg->ssvm.sh;
2466
2467 ssvm_lock_non_recursive (sh, 1);
2468 oldheap = ssvm_push_heap (sh);
2469 event_q = client_session->vpp_event_queue =
2470 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2471 sizeof (session_fifo_event_t),
2472 getpid (), 0 /* signal not sent */ );
2473 ssvm_pop_heap (oldheap);
2474 ssvm_unlock_non_recursive (sh);
2475 }
2476#endif
Dave Wallace227867f2017-11-13 21:21:53 -05002477 vppcom_send_connect_session_reply (client_session, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05002478 }
2479 else
2480 {
2481 cut_thru_str = " ";
Florin Corasdcf55ce2017-11-16 15:32:50 -08002482 vppcom_send_accept_session_reply (client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05002483 }
2484
Stevenac1f96d2017-10-24 16:03:58 -07002485 if (VPPCOM_DEBUG > 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002486 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2487 "%U peer port %u",
2488 getpid (), client_session_index, cut_thru_str,
2489 format_ip46_address, &client_session->peer_addr.ip46,
Stevenac1f96d2017-10-24 16:03:58 -07002490 client_session->peer_addr.is_ip4,
2491 clib_net_to_host_u16 (client_session->peer_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002492
Dave Wallace543852a2017-08-03 02:11:34 -04002493 clib_spinlock_unlock (&vcm->sessions_lockp);
2494 return (int) client_session_index;
2495}
2496
2497int
2498vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2499{
Dave Wallace543852a2017-08-03 02:11:34 -04002500 session_t *session = 0;
2501 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002502
2503 clib_spinlock_lock (&vcm->sessions_lockp);
2504 rv = vppcom_session_at_index (session_index, &session);
2505 if (PREDICT_FALSE (rv))
2506 {
2507 clib_spinlock_unlock (&vcm->sessions_lockp);
2508 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002509 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002510 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002511 return rv;
2512 }
2513
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002514 if (session->is_vep)
2515 {
2516 clib_spinlock_unlock (&vcm->sessions_lockp);
2517 if (VPPCOM_DEBUG > 0)
2518 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002519 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002520 return VPPCOM_EBADFD;
2521 }
2522
Dave Wallace543852a2017-08-03 02:11:34 -04002523 if (session->state == STATE_CONNECT)
2524 {
2525 clib_spinlock_unlock (&vcm->sessions_lockp);
2526 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002527 clib_warning ("[%d] session, sid (%u) already connected!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002528 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002529 return VPPCOM_OK;
2530 }
2531
2532 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002533 session->peer_addr.is_ip4 = server_ep->is_ip4;
2534 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002535 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002536
2537 if (VPPCOM_DEBUG > 0)
2538 {
2539 u8 *ip_str = format (0, "%U", format_ip46_address,
Dave Wallace35830af2017-10-09 01:43:42 -04002540 &session->peer_addr.ip46,
2541 session->peer_addr.is_ip4);
Dave Wallace7876d392017-10-19 03:53:57 -04002542 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002543 getpid (), session_index, ip_str,
Stevenac1f96d2017-10-24 16:03:58 -07002544 clib_net_to_host_u16 (session->peer_port),
Dave Wallace7876d392017-10-19 03:53:57 -04002545 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002546 vec_free (ip_str);
2547 }
2548
2549 vppcom_send_connect_sock (session, session_index);
2550 clib_spinlock_unlock (&vcm->sessions_lockp);
2551 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2552 vcm->cfg.session_timeout);
2553 if (PREDICT_FALSE (rv))
2554 {
2555 if (VPPCOM_DEBUG > 0)
2556 clib_warning ("[%d] connect timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002557 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002558 return rv;
2559 }
Dave Wallace7876d392017-10-19 03:53:57 -04002560 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002561 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04002562
Dave Wallace543852a2017-08-03 02:11:34 -04002563 return VPPCOM_OK;
2564}
2565
Steven58f464e2017-10-25 12:33:12 -07002566static inline int
2567vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2568 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002569{
Dave Wallace543852a2017-08-03 02:11:34 -04002570 session_t *session = 0;
2571 svm_fifo_t *rx_fifo;
2572 int n_read = 0;
2573 int rv;
2574 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002575 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002576
2577 ASSERT (buf);
2578
2579 clib_spinlock_lock (&vcm->sessions_lockp);
2580 rv = vppcom_session_at_index (session_index, &session);
2581 if (PREDICT_FALSE (rv))
2582 {
2583 clib_spinlock_unlock (&vcm->sessions_lockp);
2584 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002586 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002587 return rv;
2588 }
2589
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002590 if (session->is_vep)
2591 {
2592 clib_spinlock_unlock (&vcm->sessions_lockp);
2593 if (VPPCOM_DEBUG > 0)
2594 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002595 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002596 return VPPCOM_EBADFD;
2597 }
2598
Dave Wallace33e002b2017-09-06 01:20:02 -04002599 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002600 {
Dave Wallace543852a2017-08-03 02:11:34 -04002601 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002602 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002603 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002604 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002605 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002606 }
Dave Wallace543852a2017-08-03 02:11:34 -04002607
Dave Wallace33e002b2017-09-06 01:20:02 -04002608 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2609 session->server_rx_fifo : session->server_tx_fifo);
2610 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2611 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace60caa062017-11-10 17:07:13 -05002612 poll_et =
2613 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002614 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002615
2616 do
2617 {
Steven58f464e2017-10-25 12:33:12 -07002618 if (peek)
2619 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2620 else
2621 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002622 }
2623 while (!session->is_nonblocking && (n_read <= 0));
2624
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002625 if (poll_et && (n_read <= 0))
2626 {
2627 clib_spinlock_lock (&vcm->sessions_lockp);
2628 session->vep.et_mask |= EPOLLIN;
2629 clib_spinlock_unlock (&vcm->sessions_lockp);
2630 }
Dave Wallace543852a2017-08-03 02:11:34 -04002631
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
Dave Wallace2e005bb2017-11-07 01:21:39 -05002633 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002634 session_index, n_read, fifo_str, rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002635
2636 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002637}
2638
Steven58f464e2017-10-25 12:33:12 -07002639int
2640vppcom_session_read (uint32_t session_index, void *buf, int n)
2641{
2642 return (vppcom_session_read_internal (session_index, buf, n, 0));
2643}
2644
2645static int
2646vppcom_session_peek (uint32_t session_index, void *buf, int n)
2647{
2648 return (vppcom_session_read_internal (session_index, buf, n, 1));
2649}
2650
Dave Wallace543852a2017-08-03 02:11:34 -04002651static inline int
2652vppcom_session_read_ready (session_t * session, u32 session_index)
2653{
Dave Wallace498b3a52017-11-09 13:00:34 -05002654 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002655 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002656 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002657
2658 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002659 if (session->is_vep)
2660 {
2661 clib_spinlock_unlock (&vcm->sessions_lockp);
2662 if (VPPCOM_DEBUG > 0)
2663 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002664 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002665 return VPPCOM_EBADFD;
2666 }
2667
Dave Wallace33e002b2017-09-06 01:20:02 -04002668 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002669 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002670 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002672 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002673 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002674 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002675
2676 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04002677 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2678 else
2679 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002680 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2681 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002682
Dave Wallace33e002b2017-09-06 01:20:02 -04002683 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002684 }
2685
Dave Wallace60caa062017-11-10 17:07:13 -05002686 poll_et =
2687 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2688 if (poll_et && (ready == 0))
2689 {
2690 if (VPPCOM_DEBUG > 11)
2691 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2692 session_index, session->vep.et_mask);
2693 session->vep.et_mask |= EPOLLIN;
2694 if (VPPCOM_DEBUG > 11)
2695 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2696 session_index, session->vep.et_mask);
2697 }
2698
2699 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2700 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2701 "et_mask 0x%x",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002702 getpid (), session_index, session->is_listen,
Dave Wallace543852a2017-08-03 02:11:34 -04002703 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
Dave Wallace60caa062017-11-10 17:07:13 -05002704 rx_fifo, ready, session->vep.et_mask);
Dave Wallace543852a2017-08-03 02:11:34 -04002705 return ready;
2706}
2707
2708int
2709vppcom_session_write (uint32_t session_index, void *buf, int n)
2710{
Dave Wallace543852a2017-08-03 02:11:34 -04002711 session_t *session = 0;
2712 svm_fifo_t *tx_fifo;
2713 unix_shared_memory_queue_t *q;
2714 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002716 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002717 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002718
2719 ASSERT (buf);
2720
2721 clib_spinlock_lock (&vcm->sessions_lockp);
2722 rv = vppcom_session_at_index (session_index, &session);
2723 if (PREDICT_FALSE (rv))
2724 {
2725 clib_spinlock_unlock (&vcm->sessions_lockp);
2726 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002728 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002729 return rv;
2730 }
2731
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002732 if (session->is_vep)
2733 {
2734 clib_spinlock_unlock (&vcm->sessions_lockp);
2735 if (VPPCOM_DEBUG > 0)
2736 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002737 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002738 return VPPCOM_EBADFD;
2739 }
2740
Dave Wallace33e002b2017-09-06 01:20:02 -04002741 if (session->state == STATE_DISCONNECT)
2742 {
2743 clib_spinlock_unlock (&vcm->sessions_lockp);
2744 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002745 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002746 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002747 return VPPCOM_ECONNRESET;
2748 }
2749
Dave Wallace543852a2017-08-03 02:11:34 -04002750 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2751 session->server_tx_fifo : session->server_rx_fifo);
2752 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2753 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002754 q = session->vpp_event_queue;
Dave Wallace60caa062017-11-10 17:07:13 -05002755 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2756 (EPOLLET | EPOLLOUT));
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002757 clib_spinlock_unlock (&vcm->sessions_lockp);
2758
Dave Wallace543852a2017-08-03 02:11:34 -04002759 do
2760 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002761 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002762 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763 while (!session->is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002764
2765 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002767 {
2768 int rval;
2769
2770 /* Fabricate TX event, send to vpp */
2771 evt.fifo = tx_fifo;
2772 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002773
Dave Wallace543852a2017-08-03 02:11:34 -04002774 rval = vppcom_session_at_index (session_index, &session);
2775 if (PREDICT_FALSE (rval))
2776 {
Dave Wallace543852a2017-08-03 02:11:34 -04002777 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002779 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002780 return rval;
2781 }
Dave Wallace543852a2017-08-03 02:11:34 -04002782 ASSERT (q);
2783 unix_shared_memory_queue_add (q, (u8 *) & evt,
2784 0 /* do wait for mutex */ );
2785 }
2786
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002787 if (poll_et && (n_write <= 0))
2788 {
2789 clib_spinlock_lock (&vcm->sessions_lockp);
2790 session->vep.et_mask |= EPOLLOUT;
2791 clib_spinlock_unlock (&vcm->sessions_lockp);
2792 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002793
Dave Wallace543852a2017-08-03 02:11:34 -04002794 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002795 {
2796 if (n_write == -2)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002797 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002798 session_index, fifo_str, tx_fifo);
2799 else
Dave Wallace2e005bb2017-11-07 01:21:39 -05002800 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002801 session_index, n_write, fifo_str, tx_fifo);
2802 }
2803 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002804}
2805
2806static inline int
2807vppcom_session_write_ready (session_t * session, u32 session_index)
2808{
Dave Wallace543852a2017-08-03 02:11:34 -04002809 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04002810 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002812 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002813
2814 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002815 if (session->is_vep)
2816 {
2817 clib_spinlock_unlock (&vcm->sessions_lockp);
2818 if (VPPCOM_DEBUG > 0)
2819 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002820 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002821 return VPPCOM_EBADFD;
2822 }
2823
Dave Wallace33e002b2017-09-06 01:20:02 -04002824 if (session->state == STATE_DISCONNECT)
2825 {
2826 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002827 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002828 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002829 return VPPCOM_ECONNRESET;
2830 }
2831
Dave Wallace543852a2017-08-03 02:11:34 -04002832 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2833 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002834 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2835 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04002836
Dave Wallacef7f809c2017-10-03 01:48:42 -04002837 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002838
Dave Wallace33e002b2017-09-06 01:20:02 -04002839 if (VPPCOM_DEBUG > 3)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002840 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002841 session_index, fifo_str, tx_fifo, ready);
Dave Wallace60caa062017-11-10 17:07:13 -05002842 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2843 (EPOLLET | EPOLLOUT));
2844 if (poll_et && (ready == 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002845 session->vep.et_mask |= EPOLLOUT;
2846
2847 return ready;
Dave Wallace543852a2017-08-03 02:11:34 -04002848}
2849
2850int
2851vppcom_select (unsigned long n_bits, unsigned long *read_map,
2852 unsigned long *write_map, unsigned long *except_map,
2853 double time_to_wait)
2854{
Dave Wallace543852a2017-08-03 02:11:34 -04002855 u32 session_index;
2856 session_t *session = 0;
2857 int rv, bits_set = 0;
2858 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2859 u32 minbits = clib_max (n_bits, BITS (uword));
2860
2861 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2862
Dave Wallace7876d392017-10-19 03:53:57 -04002863 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002864 {
2865 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2866 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2867 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2868 }
Dave Wallace7876d392017-10-19 03:53:57 -04002869 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002870 {
2871 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2872 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2873 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2874 }
Dave Wallace7876d392017-10-19 03:53:57 -04002875 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002876 {
2877 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2878 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2879 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2880 }
2881
2882 do
2883 {
2884 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002885 if (n_bits)
2886 {
2887 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002888 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002889 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2890 ({
2891 clib_spinlock_lock (&vcm->sessions_lockp);
2892 rv = vppcom_session_at_index (session_index, &session);
2893 if (rv < 0)
2894 {
2895 clib_spinlock_unlock (&vcm->sessions_lockp);
2896 if (VPPCOM_DEBUG > 1)
2897 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002898 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002899 session_index);
2900 bits_set = VPPCOM_EBADFD;
2901 goto select_done;
2902 }
2903
2904 rv = vppcom_session_read_ready (session, session_index);
2905 clib_spinlock_unlock (&vcm->sessions_lockp);
2906 if (except_map && vcm->ex_bitmap &&
2907 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2908 (rv < 0))
2909 {
2910 // TBD: clib_warning
2911 clib_bitmap_set_no_check (except_map, session_index, 1);
2912 bits_set++;
2913 }
2914 else if (rv > 0)
2915 {
2916 // TBD: clib_warning
2917 clib_bitmap_set_no_check (read_map, session_index, 1);
2918 bits_set++;
2919 }
2920 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002921 }
2922
Dave Wallacee22aa742017-10-20 12:30:38 -04002923 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002924 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002925 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2926 ({
2927 clib_spinlock_lock (&vcm->sessions_lockp);
2928 rv = vppcom_session_at_index (session_index, &session);
2929 if (rv < 0)
2930 {
2931 clib_spinlock_unlock (&vcm->sessions_lockp);
2932 if (VPPCOM_DEBUG > 0)
2933 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002934 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002935 session_index);
2936 bits_set = VPPCOM_EBADFD;
2937 goto select_done;
2938 }
Dave Wallace543852a2017-08-03 02:11:34 -04002939
Dave Wallacee22aa742017-10-20 12:30:38 -04002940 rv = vppcom_session_write_ready (session, session_index);
2941 clib_spinlock_unlock (&vcm->sessions_lockp);
2942 if (write_map && (rv > 0))
2943 {
2944 // TBD: clib_warning
2945 clib_bitmap_set_no_check (write_map, session_index, 1);
2946 bits_set++;
2947 }
2948 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002949 }
2950
Dave Wallacee22aa742017-10-20 12:30:38 -04002951 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002952 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002953 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2954 ({
2955 clib_spinlock_lock (&vcm->sessions_lockp);
2956 rv = vppcom_session_at_index (session_index, &session);
2957 if (rv < 0)
2958 {
2959 clib_spinlock_unlock (&vcm->sessions_lockp);
2960 if (VPPCOM_DEBUG > 1)
2961 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002962 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002963 session_index);
2964 bits_set = VPPCOM_EBADFD;
2965 goto select_done;
2966 }
Dave Wallace543852a2017-08-03 02:11:34 -04002967
Dave Wallacee22aa742017-10-20 12:30:38 -04002968 rv = vppcom_session_read_ready (session, session_index);
2969 clib_spinlock_unlock (&vcm->sessions_lockp);
2970 if (rv < 0)
2971 {
2972 // TBD: clib_warning
2973 clib_bitmap_set_no_check (except_map, session_index, 1);
2974 bits_set++;
2975 }
2976 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002977 }
Dave Wallacee22aa742017-10-20 12:30:38 -04002978 }
Dave Wallace543852a2017-08-03 02:11:34 -04002979 /* *INDENT-ON* */
2980 }
2981 while (clib_time_now (&vcm->clib_time) < timeout);
2982
2983select_done:
2984 return (bits_set);
2985}
2986
Dave Wallacef7f809c2017-10-03 01:48:42 -04002987static inline void
2988vep_verify_epoll_chain (u32 vep_idx)
2989{
2990 session_t *session;
2991 vppcom_epoll_t *vep;
2992 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05002993 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002994
Dave Wallace498b3a52017-11-09 13:00:34 -05002995 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002996 return;
2997
2998 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2999 rv = vppcom_session_at_index (vep_idx, &session);
3000 if (PREDICT_FALSE (rv))
3001 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003002 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003003 goto done;
3004 }
3005 if (PREDICT_FALSE (!session->is_vep))
3006 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003007 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003008 vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003009 goto done;
3010 }
Dave Wallace498b3a52017-11-09 13:00:34 -05003011 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3012 "{\n"
3013 " is_vep = %u\n"
3014 " is_vep_session = %u\n"
3015 " wait_cont_idx = 0x%x (%u)\n"
3016 "}\n", getpid (),
3017 vep_idx, session->is_vep, session->is_vep_session,
3018 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003019 do
3020 {
3021 vep = &session->vep;
Dave Wallacee695cb42017-11-02 22:04:42 -04003022 sid = vep->next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003023 if (sid != ~0)
3024 {
3025 rv = vppcom_session_at_index (sid, &session);
3026 if (PREDICT_FALSE (rv))
3027 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003028 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003029 goto done;
3030 }
3031 if (PREDICT_FALSE (session->is_vep))
Dave Wallace774169b2017-11-01 20:07:40 -04003032 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003033 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003034 else if (PREDICT_FALSE (!session->is_vep_session))
3035 {
Dave Wallace774169b2017-11-01 20:07:40 -04003036 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003037 getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003038 goto done;
3039 }
3040 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
Dave Wallace774169b2017-11-01 20:07:40 -04003041 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003042 "vep_idx (%u)!", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04003043 sid, session->vep.vep_idx, vep_idx);
Dave Wallace498b3a52017-11-09 13:00:34 -05003044 if (session->is_vep_session)
3045 {
3046 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3047 "{\n"
3048 " next_sid = 0x%x (%u)\n"
3049 " prev_sid = 0x%x (%u)\n"
3050 " vep_idx = 0x%x (%u)\n"
3051 " ev.events = 0x%x\n"
3052 " ev.data.u64 = 0x%llx\n"
3053 " et_mask = 0x%x\n"
3054 "}\n",
3055 vep_idx, sid, sid,
3056 vep->next_sid, vep->next_sid,
3057 vep->prev_sid, vep->prev_sid,
3058 vep->vep_idx, vep->vep_idx,
3059 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3060 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003061 }
3062 }
3063 while (sid != ~0);
3064
3065done:
Dave Wallace498b3a52017-11-09 13:00:34 -05003066 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003067}
3068
3069int
3070vppcom_epoll_create (void)
3071{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003072 session_t *vep_session;
3073 u32 vep_idx;
3074
3075 clib_spinlock_lock (&vcm->sessions_lockp);
3076 pool_get (vcm->sessions, vep_session);
3077 memset (vep_session, 0, sizeof (*vep_session));
3078 vep_idx = vep_session - vcm->sessions;
3079
3080 vep_session->is_vep = 1;
3081 vep_session->vep.vep_idx = ~0;
3082 vep_session->vep.next_sid = ~0;
3083 vep_session->vep.prev_sid = ~0;
3084 vep_session->wait_cont_idx = ~0;
3085 clib_spinlock_unlock (&vcm->sessions_lockp);
3086
3087 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05003088 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003089
3090 return (vep_idx);
3091}
3092
3093int
3094vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3095 struct epoll_event *event)
3096{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003097 session_t *vep_session;
3098 session_t *session;
3099 int rv;
3100
3101 if (vep_idx == session_index)
3102 {
3103 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003104 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003105 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003106 return VPPCOM_EINVAL;
3107 }
3108
3109 clib_spinlock_lock (&vcm->sessions_lockp);
3110 rv = vppcom_session_at_index (vep_idx, &vep_session);
3111 if (PREDICT_FALSE (rv))
3112 {
3113 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003114 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003115 goto done;
3116 }
3117 if (PREDICT_FALSE (!vep_session->is_vep))
3118 {
3119 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003120 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003121 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003122 rv = VPPCOM_EINVAL;
3123 goto done;
3124 }
3125
3126 ASSERT (vep_session->vep.vep_idx == ~0);
3127 ASSERT (vep_session->vep.prev_sid == ~0);
3128
3129 rv = vppcom_session_at_index (session_index, &session);
3130 if (PREDICT_FALSE (rv))
3131 {
3132 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003133 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003134 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003135 goto done;
3136 }
3137 if (PREDICT_FALSE (session->is_vep))
3138 {
3139 if (VPPCOM_DEBUG > 0)
3140 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3141 rv = VPPCOM_EINVAL;
3142 goto done;
3143 }
3144
3145 switch (op)
3146 {
3147 case EPOLL_CTL_ADD:
3148 if (PREDICT_FALSE (!event))
3149 {
Dave Wallace774169b2017-11-01 20:07:40 -04003150 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003151 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003152 rv = VPPCOM_EINVAL;
3153 goto done;
3154 }
3155 if (vep_session->vep.next_sid != ~0)
3156 {
3157 session_t *next_session;
3158 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3159 &next_session);
3160 if (PREDICT_FALSE (rv))
3161 {
3162 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003163 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3164 "vep.next_sid (%u) on vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003165 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003166 goto done;
3167 }
3168 ASSERT (next_session->vep.prev_sid == vep_idx);
3169 next_session->vep.prev_sid = session_index;
3170 }
3171 session->vep.next_sid = vep_session->vep.next_sid;
3172 session->vep.prev_sid = vep_idx;
3173 session->vep.vep_idx = vep_idx;
3174 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3175 session->vep.ev = *event;
3176 session->is_vep_session = 1;
3177 vep_session->vep.next_sid = session_index;
3178 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003179 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003180 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003181 event->events, event->data.u64);
3182 break;
3183
3184 case EPOLL_CTL_MOD:
3185 if (PREDICT_FALSE (!event))
3186 {
Dave Wallace774169b2017-11-01 20:07:40 -04003187 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003188 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003189 rv = VPPCOM_EINVAL;
3190 goto done;
3191 }
3192 if (PREDICT_FALSE (!session->is_vep_session &&
3193 (session->vep.vep_idx != vep_idx)))
3194 {
3195 if (VPPCOM_DEBUG > 0)
3196 {
3197 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003198 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3199 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003200 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003201 else
Dave Wallace774169b2017-11-01 20:07:40 -04003202 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3203 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003204 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003205 session->vep.vep_idx, vep_idx);
3206 }
3207 rv = VPPCOM_EINVAL;
3208 goto done;
3209 }
3210 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3211 session->vep.ev = *event;
3212 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003213 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003214 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003215 event->events, event->data.u64);
3216 break;
3217
3218 case EPOLL_CTL_DEL:
3219 if (PREDICT_FALSE (!session->is_vep_session &&
3220 (session->vep.vep_idx != vep_idx)))
3221 {
3222 if (VPPCOM_DEBUG > 0)
3223 {
3224 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003225 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3226 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003227 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003228 else
Dave Wallace774169b2017-11-01 20:07:40 -04003229 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3230 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003231 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003232 session->vep.vep_idx, vep_idx);
3233 }
3234 rv = VPPCOM_EINVAL;
3235 goto done;
3236 }
3237
3238 vep_session->wait_cont_idx =
3239 (vep_session->wait_cont_idx == session_index) ?
3240 session->vep.next_sid : vep_session->wait_cont_idx;
3241
3242 if (session->vep.prev_sid == vep_idx)
3243 vep_session->vep.next_sid = session->vep.next_sid;
3244 else
3245 {
3246 session_t *prev_session;
3247 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3248 if (PREDICT_FALSE (rv))
3249 {
3250 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003251 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3252 "vep.prev_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003253 getpid (), session->vep.prev_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003254 session_index);
3255 goto done;
3256 }
3257 ASSERT (prev_session->vep.next_sid == session_index);
3258 prev_session->vep.next_sid = session->vep.next_sid;
3259 }
3260 if (session->vep.next_sid != ~0)
3261 {
3262 session_t *next_session;
3263 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3264 if (PREDICT_FALSE (rv))
3265 {
3266 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003267 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3268 "vep.next_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003269 getpid (), session->vep.next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003270 session_index);
3271 goto done;
3272 }
3273 ASSERT (next_session->vep.prev_sid == session_index);
3274 next_session->vep.prev_sid = session->vep.prev_sid;
3275 }
3276
3277 memset (&session->vep, 0, sizeof (session->vep));
3278 session->vep.next_sid = ~0;
3279 session->vep.prev_sid = ~0;
3280 session->vep.vep_idx = ~0;
3281 session->is_vep_session = 0;
3282 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003283 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003284 getpid (), vep_idx, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003285 break;
3286
3287 default:
Dave Wallace2e005bb2017-11-07 01:21:39 -05003288 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003289 rv = VPPCOM_EINVAL;
3290 }
3291
3292 vep_verify_epoll_chain (vep_idx);
3293
3294done:
3295 clib_spinlock_unlock (&vcm->sessions_lockp);
3296 return rv;
3297}
3298
Dave Wallacef7f809c2017-10-03 01:48:42 -04003299int
3300vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3301 int maxevents, double wait_for_time)
3302{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003303 session_t *vep_session;
3304 int rv;
3305 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003306 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003307 int num_ev = 0;
3308 u32 vep_next_sid, wait_cont_idx;
3309 u8 is_vep;
3310
3311 if (PREDICT_FALSE (maxevents <= 0))
3312 {
3313 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003314 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003315 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003316 return VPPCOM_EINVAL;
3317 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003318 memset (events, 0, sizeof (*events) * maxevents);
3319
3320 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3321 vep_next_sid = vep_session->vep.next_sid;
3322 is_vep = vep_session->is_vep;
3323 wait_cont_idx = vep_session->wait_cont_idx;
3324 clib_spinlock_unlock (&vcm->sessions_lockp);
3325
3326 if (PREDICT_FALSE (!is_vep))
3327 {
3328 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003329 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003330 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003331 rv = VPPCOM_EINVAL;
3332 goto done;
3333 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003334 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003335 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003336 if (VPPCOM_DEBUG > 0)
3337 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003338 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003339 goto done;
3340 }
3341
3342 do
3343 {
3344 u32 sid;
3345 u32 next_sid = ~0;
3346 session_t *session;
3347
3348 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3349 sid != ~0; sid = next_sid)
3350 {
3351 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3352 u8 add_event, is_vep_session;
3353 int ready;
3354 u64 session_ev_data;
3355
3356 VCL_LOCK_AND_GET_SESSION (sid, &session);
3357 next_sid = session->vep.next_sid;
3358 session_events = session->vep.ev.events;
3359 et_mask = session->vep.et_mask;
3360 is_vep = session->is_vep;
3361 is_vep_session = session->is_vep_session;
3362 session_vep_idx = session->vep.vep_idx;
3363 session_ev_data = session->vep.ev.data.u64;
3364 clib_spinlock_unlock (&vcm->sessions_lockp);
3365
3366 if (PREDICT_FALSE (is_vep))
3367 {
3368 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003369 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003370 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003371 rv = VPPCOM_EINVAL;
3372 goto done;
3373 }
3374 if (PREDICT_FALSE (!is_vep_session))
3375 {
3376 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003377 clib_warning ("[%d] ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003378 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003379 rv = VPPCOM_EINVAL;
3380 goto done;
3381 }
3382 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3383 {
Dave Wallace774169b2017-11-01 20:07:40 -04003384 clib_warning ("[%d] ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003385 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003386 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003387 rv = VPPCOM_EINVAL;
3388 goto done;
3389 }
3390
3391 add_event = clear_et_mask = 0;
3392
Dave Wallace60caa062017-11-10 17:07:13 -05003393 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003394 {
3395 VCL_LOCK_AND_GET_SESSION (sid, &session);
3396 ready = vppcom_session_read_ready (session, sid);
3397 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003398 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003399 {
3400 add_event = 1;
3401 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003402 if (((EPOLLET | EPOLLIN) & session_events) ==
3403 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003404 clear_et_mask |= EPOLLIN;
3405 }
3406 else if (ready < 0)
3407 {
3408 add_event = 1;
3409 switch (ready)
3410 {
3411 case VPPCOM_ECONNRESET:
3412 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3413 break;
3414
3415 default:
3416 events[num_ev].events |= EPOLLERR;
3417 break;
3418 }
3419 }
3420 }
3421
Dave Wallace60caa062017-11-10 17:07:13 -05003422 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003423 {
3424 VCL_LOCK_AND_GET_SESSION (sid, &session);
3425 ready = vppcom_session_write_ready (session, sid);
3426 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003427 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003428 {
3429 add_event = 1;
3430 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003431 if (((EPOLLET | EPOLLOUT) & session_events) ==
3432 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003433 clear_et_mask |= EPOLLOUT;
3434 }
3435 else if (ready < 0)
3436 {
3437 add_event = 1;
3438 switch (ready)
3439 {
3440 case VPPCOM_ECONNRESET:
3441 events[num_ev].events |= EPOLLHUP;
3442 break;
3443
3444 default:
3445 events[num_ev].events |= EPOLLERR;
3446 break;
3447 }
3448 }
3449 }
3450
3451 if (add_event)
3452 {
3453 events[num_ev].data.u64 = session_ev_data;
3454 if (EPOLLONESHOT & session_events)
3455 {
3456 VCL_LOCK_AND_GET_SESSION (sid, &session);
3457 session->vep.ev.events = 0;
3458 clib_spinlock_unlock (&vcm->sessions_lockp);
3459 }
3460 num_ev++;
3461 if (num_ev == maxevents)
3462 {
3463 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3464 vep_session->wait_cont_idx = next_sid;
3465 clib_spinlock_unlock (&vcm->sessions_lockp);
3466 goto done;
3467 }
3468 }
3469 if (wait_cont_idx != ~0)
3470 {
3471 if (next_sid == ~0)
3472 next_sid = vep_next_sid;
3473 else if (next_sid == wait_cont_idx)
3474 next_sid = ~0;
3475 }
3476 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003477 if (wait_for_time != -1)
3478 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003479 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003480 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003481
3482 if (wait_cont_idx != ~0)
3483 {
3484 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3485 vep_session->wait_cont_idx = ~0;
3486 clib_spinlock_unlock (&vcm->sessions_lockp);
3487 }
3488done:
3489 return (rv != VPPCOM_OK) ? rv : num_ev;
3490}
3491
Dave Wallace35830af2017-10-09 01:43:42 -04003492int
3493vppcom_session_attr (uint32_t session_index, uint32_t op,
3494 void *buffer, uint32_t * buflen)
3495{
Dave Wallace35830af2017-10-09 01:43:42 -04003496 session_t *session;
3497 int rv = VPPCOM_OK;
3498 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003499 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003500
3501 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3502 switch (op)
3503 {
3504 case VPPCOM_ATTR_GET_NREAD:
3505 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05003506 if (VPPCOM_DEBUG > 2)
3507 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003508 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003509
3510 break;
3511
Dave Wallace227867f2017-11-13 21:21:53 -05003512 case VPPCOM_ATTR_GET_NWRITE:
3513 rv = vppcom_session_write_ready (session, session_index);
3514 if (VPPCOM_DEBUG > 2)
3515 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3516 getpid (), session_index, rv);
3517
Dave Wallace35830af2017-10-09 01:43:42 -04003518 break;
3519
3520 case VPPCOM_ATTR_GET_FLAGS:
3521 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3522 {
3523 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3524 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05003525 if (VPPCOM_DEBUG > 2)
3526 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3527 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3528 session_index, *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003529 }
3530 else
3531 rv = VPPCOM_EINVAL;
3532 break;
3533
3534 case VPPCOM_ATTR_SET_FLAGS:
3535 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3536 {
3537 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace227867f2017-11-13 21:21:53 -05003538 if (VPPCOM_DEBUG > 2)
3539 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3540 "flags = 0x%08x, is_nonblocking = %u",
3541 getpid (), *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003542 }
3543 else
3544 rv = VPPCOM_EINVAL;
3545 break;
3546
3547 case VPPCOM_ATTR_GET_PEER_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003548 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003549 {
Steven2199aab2017-10-15 20:18:47 -07003550 ep->vrf = session->vrf;
3551 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003552 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07003553 if (session->peer_addr.is_ip4)
3554 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3555 sizeof (ip4_address_t));
3556 else
3557 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3558 sizeof (ip6_address_t));
3559 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003560 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003561 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003562 "%u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003563 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003564 &session->peer_addr.ip46, ep->is_ip4,
3565 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003566 }
3567 else
3568 rv = VPPCOM_EINVAL;
3569 break;
3570
3571 case VPPCOM_ATTR_GET_LCL_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003572 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003573 {
Steven2199aab2017-10-15 20:18:47 -07003574 ep->vrf = session->vrf;
3575 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003576 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07003577 if (session->lcl_addr.is_ip4)
3578 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3579 sizeof (ip4_address_t));
3580 else
3581 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3582 sizeof (ip6_address_t));
3583 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003584 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003585 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003586 "%u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003587 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003588 &session->lcl_addr.ip46, ep->is_ip4,
3589 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003590 }
3591 else
3592 rv = VPPCOM_EINVAL;
3593 break;
Stevenb5a11602017-10-11 09:59:30 -07003594
3595 case VPPCOM_ATTR_SET_REUSEADDR:
3596 break;
3597
3598 case VPPCOM_ATTR_SET_BROADCAST:
3599 break;
3600
3601 case VPPCOM_ATTR_SET_V6ONLY:
3602 break;
Stevenbccd3392017-10-12 20:42:21 -07003603
3604 case VPPCOM_ATTR_SET_KEEPALIVE:
3605 break;
3606
3607 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3608 break;
3609
3610 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3611 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003612
3613 default:
3614 rv = VPPCOM_EINVAL;
3615 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003616 }
3617
3618done:
3619 clib_spinlock_unlock (&vcm->sessions_lockp);
3620 return rv;
3621}
3622
Stevenac1f96d2017-10-24 16:03:58 -07003623int
3624vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3625 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3626{
Stevenac1f96d2017-10-24 16:03:58 -07003627 int rv = VPPCOM_OK;
3628 session_t *session = 0;
3629
3630 if (ep)
3631 {
3632 clib_spinlock_lock (&vcm->sessions_lockp);
3633 rv = vppcom_session_at_index (session_index, &session);
3634 if (PREDICT_FALSE (rv))
3635 {
3636 clib_spinlock_unlock (&vcm->sessions_lockp);
3637 if (VPPCOM_DEBUG > 0)
3638 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003639 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003640 rv = VPPCOM_EBADFD;
3641 clib_spinlock_unlock (&vcm->sessions_lockp);
3642 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07003643 }
3644 ep->vrf = session->vrf;
3645 ep->is_ip4 = session->peer_addr.is_ip4;
3646 ep->port = session->peer_port;
3647 if (session->peer_addr.is_ip4)
3648 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3649 sizeof (ip4_address_t));
3650 else
3651 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3652 sizeof (ip6_address_t));
3653 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07003654 }
Steven58f464e2017-10-25 12:33:12 -07003655
3656 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003657 rv = vppcom_session_read (session_index, buffer, buflen);
3658 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003659 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003660 else
3661 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003662 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07003663 rv = VPPCOM_EAFNOSUPPORT;
3664 }
3665
Dave Wallacefaf9d772017-10-26 16:12:04 -04003666done:
Stevenac1f96d2017-10-24 16:03:58 -07003667 return rv;
3668}
3669
3670int
3671vppcom_session_sendto (uint32_t session_index, void *buffer,
3672 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3673{
Dave Wallace617dffa2017-10-26 14:47:06 -04003674 if (!buffer)
3675 return VPPCOM_EINVAL;
3676
3677 if (ep)
3678 {
3679 // TBD
3680 return VPPCOM_EINVAL;
3681 }
3682
3683 if (flags)
3684 {
3685 // TBD check the flags and do the right thing
3686 if (VPPCOM_DEBUG > 2)
3687 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003688 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003689 }
3690
3691 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003692}
3693
Dave Wallacee22aa742017-10-20 12:30:38 -04003694/*
3695 * fd.io coding-style-patch-verification: ON
3696 *
3697 * Local Variables:
3698 * eval: (c-set-style "gnu")
3699 * End:
3700 */