blob: 09ebe26813f98bfad3eff6b063d89771284e9804 [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
309static int
310vppcom_connect_to_vpp (char *app_name)
311{
312 api_main_t *am = &api_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400313
314 if (VPPCOM_DEBUG > 0)
315 printf ("\nConnecting to VPP api...");
316 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
317 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500318 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400319 return VPPCOM_ECONNREFUSED;
320 }
321
322 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
323 vcm->my_client_index = am->my_client_index;
324 if (VPPCOM_DEBUG > 0)
325 printf (" connected!\n");
326
327 vcm->app_state = STATE_APP_CONN_VPP;
328 return VPPCOM_OK;
329}
330
331static u8 *
332format_api_error (u8 * s, va_list * args)
333{
Dave Wallace543852a2017-08-03 02:11:34 -0400334 i32 error = va_arg (*args, u32);
335 uword *p;
336
337 p = hash_get (vcm->error_string_by_error_number, -error);
338
339 if (p)
340 s = format (s, "%s (%d)", p[0], error);
341 else
342 s = format (s, "%d", error);
343 return s;
344}
345
346static void
347vppcom_init_error_string_table (void)
348{
Dave Wallace543852a2017-08-03 02:11:34 -0400349 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
350
351#define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
352 foreach_vnet_api_error;
353#undef _
354
355 hash_set (vcm->error_string_by_error_number, 99, "Misc");
356}
357
358static inline int
359vppcom_wait_for_app_state_change (app_state_t app_state)
360{
Dave Wallace543852a2017-08-03 02:11:34 -0400361 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
362
363 while (clib_time_now (&vcm->clib_time) < timeout)
364 {
365 if (vcm->app_state == app_state)
366 return VPPCOM_OK;
367 }
368 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500369 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400370 vppcom_app_state_str (app_state), app_state);
371 return VPPCOM_ETIMEDOUT;
372}
373
374static inline int
375vppcom_wait_for_session_state_change (u32 session_index,
376 session_state_t state,
377 f64 wait_for_time)
378{
Dave Wallace543852a2017-08-03 02:11:34 -0400379 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
380 session_t *volatile session;
381 int rv;
382
383 do
384 {
385 clib_spinlock_lock (&vcm->sessions_lockp);
386 rv = vppcom_session_at_index (session_index, &session);
387 if (PREDICT_FALSE (rv))
388 {
389 clib_spinlock_unlock (&vcm->sessions_lockp);
390 return rv;
391 }
392 if (session->state == state)
393 {
394 clib_spinlock_unlock (&vcm->sessions_lockp);
395 return VPPCOM_OK;
396 }
397 clib_spinlock_unlock (&vcm->sessions_lockp);
398 }
399 while (clib_time_now (&vcm->clib_time) < timeout);
400
401 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500402 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400403 vppcom_session_state_str (state), state);
404 return VPPCOM_ETIMEDOUT;
405}
406
407static inline int
408vppcom_wait_for_client_session_index (f64 wait_for_time)
409{
Dave Wallace543852a2017-08-03 02:11:34 -0400410 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
411
412 do
413 {
414 if (clib_fifo_elts (vcm->client_session_index_fifo))
415 return VPPCOM_OK;
416 }
417 while (clib_time_now (&vcm->clib_time) < timeout);
418
419 if (wait_for_time == 0)
420 return VPPCOM_EAGAIN;
421
422 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500423 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400424 return VPPCOM_ETIMEDOUT;
425}
426
427/*
428 * VPP-API message functions
429 */
430static void
431vppcom_send_session_enable_disable (u8 is_enable)
432{
Dave Wallace543852a2017-08-03 02:11:34 -0400433 vl_api_session_enable_disable_t *bmp;
434 bmp = vl_msg_api_alloc (sizeof (*bmp));
435 memset (bmp, 0, sizeof (*bmp));
436
437 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
438 bmp->client_index = vcm->my_client_index;
439 bmp->context = htonl (0xfeedface);
440 bmp->is_enable = is_enable;
441 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
442}
443
444static int
445vppcom_app_session_enable (void)
446{
Dave Wallace543852a2017-08-03 02:11:34 -0400447 int rv;
448
449 if (vcm->app_state != STATE_APP_ENABLED)
450 {
451 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
452 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
453 if (PREDICT_FALSE (rv))
454 {
455 if (VPPCOM_DEBUG > 0)
456 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500457 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400458 return rv;
459 }
460 }
461 return VPPCOM_OK;
462}
463
464static void
465 vl_api_session_enable_disable_reply_t_handler
466 (vl_api_session_enable_disable_reply_t * mp)
467{
Dave Wallace543852a2017-08-03 02:11:34 -0400468 if (mp->retval)
469 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500470 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400471 format_api_error, ntohl (mp->retval));
472 }
473 else
474 vcm->app_state = STATE_APP_ENABLED;
475}
476
477static void
478vppcom_app_send_attach (void)
479{
Dave Wallace543852a2017-08-03 02:11:34 -0400480 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400481 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400482 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
483 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400484
Dave Wallace543852a2017-08-03 02:11:34 -0400485 bmp = vl_msg_api_alloc (sizeof (*bmp));
486 memset (bmp, 0, sizeof (*bmp));
487
488 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
489 bmp->client_index = vcm->my_client_index;
490 bmp->context = htonl (0xfeedface);
491 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700492 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400493 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
494 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
495 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
496 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
497 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
498 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Dave Wallace543852a2017-08-03 02:11:34 -0400499 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
500 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
501 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
502 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400503 if (nsid_len)
504 {
505 bmp->namespace_id_len = nsid_len;
506 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
507 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
508 }
Dave Wallace543852a2017-08-03 02:11:34 -0400509 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
510}
511
512static int
513vppcom_app_attach (void)
514{
Dave Wallace543852a2017-08-03 02:11:34 -0400515 int rv;
516
517 vppcom_app_send_attach ();
518 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
519 if (PREDICT_FALSE (rv))
520 {
521 if (VPPCOM_DEBUG > 0)
522 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500523 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400524 return rv;
525 }
526 return VPPCOM_OK;
527}
528
529static void
530vppcom_app_detach (void)
531{
Dave Wallace543852a2017-08-03 02:11:34 -0400532 vl_api_application_detach_t *bmp;
533 bmp = vl_msg_api_alloc (sizeof (*bmp));
534 memset (bmp, 0, sizeof (*bmp));
535
536 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
537 bmp->client_index = vcm->my_client_index;
538 bmp->context = htonl (0xfeedface);
539 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
540}
541
542static void
543vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
544 mp)
545{
Dave Wallace543852a2017-08-03 02:11:34 -0400546 static svm_fifo_segment_create_args_t _a;
547 svm_fifo_segment_create_args_t *a = &_a;
548 int rv;
549
550 memset (a, 0, sizeof (*a));
551 if (mp->retval)
552 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500553 clib_warning ("[%d] attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400554 format_api_error, ntohl (mp->retval));
555 return;
556 }
557
558 if (mp->segment_name_length == 0)
559 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500560 clib_warning ("[%d] segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400561 return;
562 }
563
564 a->segment_name = (char *) mp->segment_name;
565 a->segment_size = mp->segment_size;
566
567 ASSERT (mp->app_event_queue_address);
568
569 /* Attach to the segment vpp created */
570 rv = svm_fifo_segment_attach (a);
571 vec_reset_length (a->new_segment_indices);
572 if (PREDICT_FALSE (rv))
573 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500574 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400575 mp->segment_name);
576 return;
577 }
578
579 vcm->app_event_queue =
580 uword_to_pointer (mp->app_event_queue_address,
581 unix_shared_memory_queue_t *);
582
583 vcm->app_state = STATE_APP_ATTACHED;
584}
585
586static void
587vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
588 mp)
589{
Dave Wallace543852a2017-08-03 02:11:34 -0400590 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500591 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400592 ntohl (mp->retval));
593
594 vcm->app_state = STATE_APP_ENABLED;
595}
596
597static void
598vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
599 mp)
600{
Dave Wallace543852a2017-08-03 02:11:34 -0400601 uword *p;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500602 u32 session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -0400603
604 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
605 if (p)
606 {
607 session_t *session = 0;
608 int rv;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500609
610 session_index = p[0];
611 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400612 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace3bd43b82017-11-11 22:45:38 -0500613 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400614 if (PREDICT_FALSE (rv))
615 {
616 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500617 clib_warning ("[%d] invalid session, sid %u has been closed!",
618 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400619 }
Dave Wallace3bd43b82017-11-11 22:45:38 -0500620 else
621 {
622 if (VPPCOM_DEBUG > 1)
623 clib_warning ("[%d] vpp handle 0x%llx, sid %u disconnected!",
624 getpid (), mp->handle, session_index);
625
626 session->state = STATE_DISCONNECT;
627 }
Dave Wallace543852a2017-08-03 02:11:34 -0400628 clib_spinlock_unlock (&vcm->sessions_lockp);
629 }
630 else
631 {
632 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500633 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
634 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400635 }
636
637 if (mp->retval)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500638 clib_warning ("[%d] disconnect_session vpp handle 0x%llx, sid %u "
639 "failed: %U", getpid (), mp->handle, session_index,
Dave Wallace543852a2017-08-03 02:11:34 -0400640 format_api_error, ntohl (mp->retval));
641}
642
643static void
644vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
645{
Dave Wallace543852a2017-08-03 02:11:34 -0400646 static svm_fifo_segment_create_args_t _a;
647 svm_fifo_segment_create_args_t *a = &_a;
648 int rv;
649
650 memset (a, 0, sizeof (*a));
651 a->segment_name = (char *) mp->segment_name;
652 a->segment_size = mp->segment_size;
653 /* Attach to the segment vpp created */
654 rv = svm_fifo_segment_attach (a);
655 vec_reset_length (a->new_segment_indices);
656 if (PREDICT_FALSE (rv))
657 {
658 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500659 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400660 return;
661 }
662 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500663 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400664 mp->segment_name, mp->segment_size);
665}
666
667static void
668vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
669{
Dave Wallace543852a2017-08-03 02:11:34 -0400670 session_t *session = 0;
671 vl_api_disconnect_session_reply_t *rmp;
672 uword *p;
673 int rv = 0;
674
675 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
676 if (p)
677 {
678 int rval;
679 clib_spinlock_lock (&vcm->sessions_lockp);
680 rval = vppcom_session_at_index (p[0], &session);
681 if (PREDICT_FALSE (rval))
682 {
683 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400684 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500685 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400686 }
687 else
688 pool_put (vcm->sessions, session);
689 clib_spinlock_unlock (&vcm->sessions_lockp);
690 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
691 }
692 else
693 {
Dave Wallace3bd43b82017-11-11 22:45:38 -0500694 clib_warning ("[%d] couldn't find session: unknonwn vpp handle 0x%llx",
695 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400696 rv = -11;
697 }
698
699 rmp = vl_msg_api_alloc (sizeof (*rmp));
700 memset (rmp, 0, sizeof (*rmp));
701
702 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
703 rmp->retval = htonl (rv);
704 rmp->handle = mp->handle;
705 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
706}
707
708static void
709vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
710{
Dave Wallace543852a2017-08-03 02:11:34 -0400711 session_t *session = 0;
712 vl_api_reset_session_reply_t *rmp;
713 uword *p;
714 int rv = 0;
715
716 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
717 if (p)
718 {
719 int rval;
720 clib_spinlock_lock (&vcm->sessions_lockp);
721 rval = vppcom_session_at_index (p[0], &session);
722 if (PREDICT_FALSE (rval))
723 {
724 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400725 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500726 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400727 }
728 else
729 pool_put (vcm->sessions, session);
730 clib_spinlock_unlock (&vcm->sessions_lockp);
731 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
732 }
733 else
734 {
Dave Wallace3bd43b82017-11-11 22:45:38 -0500735 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
736 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400737 rv = -11;
738 }
739
740 rmp = vl_msg_api_alloc (sizeof (*rmp));
741 memset (rmp, 0, sizeof (*rmp));
742 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
743 rmp->retval = htonl (rv);
744 rmp->handle = mp->handle;
745 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
746}
747
748static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400749vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400750{
Dave Wallace543852a2017-08-03 02:11:34 -0400751 session_t *session;
752 u32 session_index;
753 svm_fifo_t *rx_fifo, *tx_fifo;
754 u8 is_cut_thru = 0;
755 int rv;
756
757 if (mp->retval)
758 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500759 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400760 ntohl (mp->retval));
761 return;
762 }
763
Dave Wallace33e002b2017-09-06 01:20:02 -0400764 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -0400765 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500766 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400767 session_index, session_index);
768
769 clib_spinlock_lock (&vcm->sessions_lockp);
770 if (pool_is_free_index (vcm->sessions, session_index))
771 {
772 clib_spinlock_unlock (&vcm->sessions_lockp);
773 if (VPPCOM_DEBUG > 1)
774 clib_warning ("[%d] invalid session, sid %d is closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500775 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400776 return;
777 }
778
779 /* We've been redirected */
780 if (mp->segment_name_length > 0)
781 {
782 static svm_fifo_segment_create_args_t _a;
783 svm_fifo_segment_create_args_t *a = &_a;
784
785 is_cut_thru = 1;
786 memset (a, 0, sizeof (*a));
787 a->segment_name = (char *) mp->segment_name;
788 if (VPPCOM_DEBUG > 1)
Dave Wallace60caa062017-11-10 17:07:13 -0500789 clib_warning ("[%d] cut-thru segment: %s\n",
790 getpid (), a->segment_name);
791
Dave Wallace543852a2017-08-03 02:11:34 -0400792 rv = svm_fifo_segment_attach (a);
793 vec_reset_length (a->new_segment_indices);
794 if (PREDICT_FALSE (rv))
795 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400796 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400797 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500798 getpid (), a->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400799 return;
800 }
801 }
802
803 /*
804 * Setup session
805 */
Dave Wallace543852a2017-08-03 02:11:34 -0400806 session = pool_elt_at_index (vcm->sessions, session_index);
807 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -0400808 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
809 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400810
811 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
812 rx_fifo->client_session_index = session_index;
813 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
814 tx_fifo->client_session_index = session_index;
815
816 session->server_rx_fifo = rx_fifo;
817 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500818 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400819 session->state = STATE_CONNECT;
820
821 /* Add it to lookup table */
822 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500823
824 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500825 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
Dave Wallace60caa062017-11-10 17:07:13 -0500826 " session_rx_fifo %p, refcnt %d\n"
827 " session_tx_fifo %p, refcnt %d",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500828 getpid (), session_index, mp->handle,
Dave Wallace60caa062017-11-10 17:07:13 -0500829 session->server_rx_fifo,
830 session->server_rx_fifo->refcnt,
831 session->server_tx_fifo, session->server_tx_fifo->refcnt);
832
Dave Wallace543852a2017-08-03 02:11:34 -0400833 clib_spinlock_unlock (&vcm->sessions_lockp);
834}
835
836static void
837vppcom_send_connect_sock (session_t * session, u32 session_index)
838{
Dave Wallace543852a2017-08-03 02:11:34 -0400839 vl_api_connect_sock_t *cmp;
840
841 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
842 session->is_server = 0;
843 cmp = vl_msg_api_alloc (sizeof (*cmp));
844 memset (cmp, 0, sizeof (*cmp));
845 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
846 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400847 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400848
849 if (VPPCOM_DEBUG > 1)
Dave Wallace33e002b2017-09-06 01:20:02 -0400850 clib_warning ("[%d] session_index = %d 0x%08x",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500851 getpid (), session_index, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400852
853 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400854 cmp->is_ip4 = session->peer_addr.is_ip4;
855 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -0700856 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400857 cmp->proto = session->proto;
858 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
859 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
860}
861
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400862static inline void
863vppcom_send_disconnect (session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -0400864{
Dave Wallace543852a2017-08-03 02:11:34 -0400865 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400866
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400867 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400868 dmp = vl_msg_api_alloc (sizeof (*dmp));
869 memset (dmp, 0, sizeof (*dmp));
870 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
871 dmp->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500872 dmp->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400873 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400874}
875
876static void
877vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
878{
Dave Wallace543852a2017-08-03 02:11:34 -0400879 session_t *session = 0;
880 int rv;
881
882 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500883 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400884 ntohl (mp->retval));
885
886 ASSERT (vcm->bind_session_index != ~0);
887
888 clib_spinlock_lock (&vcm->sessions_lockp);
889 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
890 if (rv == VPPCOM_OK)
891 {
Dave Wallace60caa062017-11-10 17:07:13 -0500892 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400893 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
894 vcm->bind_session_index);
895 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
896 vcm->bind_session_index = ~0;
897 }
898 clib_spinlock_unlock (&vcm->sessions_lockp);
899}
900
901static void
902vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
903{
Dave Wallace543852a2017-08-03 02:11:34 -0400904 session_t *session = 0;
905 int rv;
906
907 clib_spinlock_lock (&vcm->sessions_lockp);
908 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
Dave Wallace19481612017-09-15 18:47:44 -0400909 if (rv == VPPCOM_OK)
Dave Wallace543852a2017-08-03 02:11:34 -0400910 {
Dave Wallace19481612017-09-15 18:47:44 -0400911 if ((VPPCOM_DEBUG > 1) && (mp->retval))
Dave Wallace2e005bb2017-11-07 01:21:39 -0500912 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
Dave Wallace19481612017-09-15 18:47:44 -0400913 ntohl (mp->retval));
914
915 vcm->bind_session_index = ~0;
916 session->state = STATE_START;
Dave Wallace543852a2017-08-03 02:11:34 -0400917 }
Dave Wallace543852a2017-08-03 02:11:34 -0400918 clib_spinlock_unlock (&vcm->sessions_lockp);
919}
920
921u8 *
922format_ip4_address (u8 * s, va_list * args)
923{
924 u8 *a = va_arg (*args, u8 *);
925 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
926}
927
928u8 *
929format_ip6_address (u8 * s, va_list * args)
930{
931 ip6_address_t *a = va_arg (*args, ip6_address_t *);
932 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
933
934 i_max_n_zero = ARRAY_LEN (a->as_u16);
935 max_n_zeros = 0;
936 i_first_zero = i_max_n_zero;
937 n_zeros = 0;
938 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
939 {
940 u32 is_zero = a->as_u16[i] == 0;
941 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
942 {
943 i_first_zero = i;
944 n_zeros = 0;
945 }
946 n_zeros += is_zero;
947 if ((!is_zero && n_zeros > max_n_zeros)
948 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
949 {
950 i_max_n_zero = i_first_zero;
951 max_n_zeros = n_zeros;
952 i_first_zero = ARRAY_LEN (a->as_u16);
953 n_zeros = 0;
954 }
955 }
956
957 last_double_colon = 0;
958 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
959 {
960 if (i == i_max_n_zero && max_n_zeros > 1)
961 {
962 s = format (s, "::");
963 i += max_n_zeros - 1;
964 last_double_colon = 1;
965 }
966 else
967 {
968 s = format (s, "%s%x",
969 (last_double_colon || i == 0) ? "" : ":",
970 clib_net_to_host_u16 (a->as_u16[i]));
971 last_double_colon = 0;
972 }
973 }
974
975 return s;
976}
977
978/* Format an IP46 address. */
979u8 *
980format_ip46_address (u8 * s, va_list * args)
981{
982 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
983 ip46_type_t type = va_arg (*args, ip46_type_t);
984 int is_ip4 = 1;
985
986 switch (type)
987 {
988 case IP46_TYPE_ANY:
989 is_ip4 = ip46_address_is_ip4 (ip46);
990 break;
991 case IP46_TYPE_IP4:
992 is_ip4 = 1;
993 break;
994 case IP46_TYPE_IP6:
995 is_ip4 = 0;
996 break;
997 }
998
999 return is_ip4 ?
1000 format (s, "%U", format_ip4_address, &ip46->ip4) :
1001 format (s, "%U", format_ip6_address, &ip46->ip6);
1002}
1003
Dave Wallace60caa062017-11-10 17:07:13 -05001004static inline void
1005vppcom_send_accept_session_reply (u32 handle, int retval)
1006{
1007 vl_api_accept_session_reply_t *rmp;
1008
1009 rmp = vl_msg_api_alloc (sizeof (*rmp));
1010 memset (rmp, 0, sizeof (*rmp));
1011 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1012 rmp->retval = htonl (retval);
1013 rmp->handle = handle;
1014 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1015}
1016
Dave Wallace543852a2017-08-03 02:11:34 -04001017static void
1018vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1019{
Dave Wallace543852a2017-08-03 02:11:34 -04001020 svm_fifo_t *rx_fifo, *tx_fifo;
1021 session_t *session;
1022 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001023
Dave Wallace60caa062017-11-10 17:07:13 -05001024 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001025 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1026 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001027 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallace60caa062017-11-10 17:07:13 -05001028 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL,
1029 mp->handle);
1030 clib_spinlock_unlock (&vcm->sessions_lockp);
1031 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001032 }
1033
Dave Wallace543852a2017-08-03 02:11:34 -04001034 /* Allocate local session and set it up */
1035 pool_get (vcm->sessions, session);
1036 memset (session, 0, sizeof (*session));
1037 session_index = session - vcm->sessions;
1038
1039 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1040 rx_fifo->client_session_index = session_index;
1041 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1042 tx_fifo->client_session_index = session_index;
1043
Dave Wallace60caa062017-11-10 17:07:13 -05001044 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001045 session->server_rx_fifo = rx_fifo;
1046 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001047 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1048 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001049 session->state = STATE_ACCEPT;
1050 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001051 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001052 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001053 session->peer_addr.is_ip4 = mp->is_ip4;
1054 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1055 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001056
1057 /* Add it to lookup table */
1058 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1059
1060 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001061 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001062
Dave Wallace60caa062017-11-10 17:07:13 -05001063 if (VPPCOM_DEBUG > 1)
1064 {
1065 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1066 clib_warning ("[%d] received request to accept session (sid %d) "
1067 "from %s:%d", getpid (), session_index, ip_str,
1068 clib_net_to_host_u16 (mp->port));
1069 vec_free (ip_str);
1070 }
1071}
1072
1073static void
1074vppcom_send_connect_session_reply (session_t * session, u32 context,
1075 int retval, int handle)
1076{
1077 vl_api_connect_session_reply_t *rmp;
1078 u32 len;
1079 unix_shared_memory_queue_t *client_q;
1080
Dave Wallace543852a2017-08-03 02:11:34 -04001081 rmp = vl_msg_api_alloc (sizeof (*rmp));
1082 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001083
1084 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1085 rmp->context = session ? session->client_context : context;
1086 rmp->retval = htonl (retval);
1087 rmp->handle = session ? session->vpp_handle : handle;
1088
1089 if (session)
1090 {
1091 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1092 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1093 rmp->vpp_event_queue_address =
1094 pointer_to_uword (session->vpp_event_queue);
1095 rmp->segment_size = vcm->cfg.segment_size;
1096 len = vec_len (session->segment_name);
1097 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1098 clib_memcpy (rmp->segment_name, session->segment_name,
1099 rmp->segment_name_length - 1);
1100 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1101 sizeof (rmp->lcl_ip));
1102 rmp->is_ip4 = session->lcl_addr.is_ip4;
1103 }
1104
1105 client_q = uword_to_pointer (session->client_queue_address,
1106 unix_shared_memory_queue_t *);
1107 ASSERT (client_q);
1108 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001109}
1110
1111/*
1112 * Acting as server for redirected connect requests
1113 */
1114static void
1115vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1116{
Dave Wallace543852a2017-08-03 02:11:34 -04001117 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001118 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001119
Dave Wallacef7f809c2017-10-03 01:48:42 -04001120 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001121 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1122 {
1123 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001124 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001125 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05001126 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL, 0);
1127 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001128 }
1129
Dave Wallace543852a2017-08-03 02:11:34 -04001130 pool_get (vcm->sessions, session);
1131 memset (session, 0, sizeof (*session));
1132 session_index = session - vcm->sessions;
1133
Dave Wallace60caa062017-11-10 17:07:13 -05001134 session->client_context = mp->context;
1135 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001136 session->client_queue_address = mp->client_queue_address;
1137 session->is_cut_thru = 1;
1138 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001139 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001140 session->peer_addr.is_ip4 = mp->is_ip4;
1141 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1142 sizeof (session->peer_addr.ip46));
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001143
Dave Wallace543852a2017-08-03 02:11:34 -04001144 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001145 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001146 if (VPPCOM_DEBUG > 1)
1147 clib_warning
Dave Wallace60caa062017-11-10 17:07:13 -05001148 ("[%d] Got a cut-thru connect request to client: "
1149 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
Dave Wallace2e005bb2017-11-07 01:21:39 -05001150 clib_fifo_elts (vcm->client_session_index_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001151 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001152}
1153
1154static void
1155vppcom_send_bind_sock (session_t * session)
1156{
Dave Wallace543852a2017-08-03 02:11:34 -04001157 vl_api_bind_sock_t *bmp;
1158
1159 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1160 session->is_server = 1;
1161 bmp = vl_msg_api_alloc (sizeof (*bmp));
1162 memset (bmp, 0, sizeof (*bmp));
1163
1164 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1165 bmp->client_index = vcm->my_client_index;
1166 bmp->context = htonl (0xfeedface);
1167 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001168 bmp->is_ip4 = session->lcl_addr.is_ip4;
1169 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001170 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001171 bmp->proto = session->proto;
1172 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1173 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1174}
1175
1176static void
1177vppcom_send_unbind_sock (u32 session_index)
1178{
Dave Wallace543852a2017-08-03 02:11:34 -04001179 vl_api_unbind_sock_t *ump;
1180 session_t *session = 0;
1181 int rv;
1182
1183 clib_spinlock_lock (&vcm->sessions_lockp);
1184 rv = vppcom_session_at_index (session_index, &session);
1185 if (PREDICT_FALSE (rv))
1186 {
1187 clib_spinlock_unlock (&vcm->sessions_lockp);
1188 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001189 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001190 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001191 return;
1192 }
1193
1194 ump = vl_msg_api_alloc (sizeof (*ump));
1195 memset (ump, 0, sizeof (*ump));
1196
1197 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1198 ump->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -05001199 ump->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001200 clib_spinlock_unlock (&vcm->sessions_lockp);
1201 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1202}
1203
1204static int
Dave Wallace60caa062017-11-10 17:07:13 -05001205vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001206{
1207 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1208 svm_fifo_segment_private_t *seg;
1209 int rv = VPPCOM_OK;
1210
Dave Wallace60caa062017-11-10 17:07:13 -05001211 if (VPPCOM_DEBUG > 1)
1212 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1213 " server_rx_fifo %p, refcnt = %d\n"
1214 " server_tx_fifo %p, refcnt = %d",
1215 getpid (), session_index, session->sm_seg_index,
1216 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1217 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1218
Dave Wallace543852a2017-08-03 02:11:34 -04001219 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1220 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1221 FIFO_SEGMENT_RX_FREELIST);
1222 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1223 FIFO_SEGMENT_TX_FREELIST);
1224 svm_fifo_segment_delete (seg);
1225
1226 return rv;
1227}
1228
1229static int
1230vppcom_session_unbind (u32 session_index)
1231{
Dave Wallace543852a2017-08-03 02:11:34 -04001232 int rv;
1233
1234 clib_spinlock_lock (&vcm->sessions_lockp);
1235 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1236 {
1237 clib_spinlock_unlock (&vcm->sessions_lockp);
1238 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001239 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001240 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001241 return VPPCOM_EBADFD;
1242 }
1243 clib_spinlock_unlock (&vcm->sessions_lockp);
1244
1245 vcm->bind_session_index = session_index;
1246 vppcom_send_unbind_sock (session_index);
1247 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1248 vcm->cfg.session_timeout);
1249 if (PREDICT_FALSE (rv))
1250 {
1251 vcm->bind_session_index = ~0;
1252 if (VPPCOM_DEBUG > 0)
1253 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001254 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001255 return rv;
1256 }
1257 return VPPCOM_OK;
1258}
1259
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001260static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001261vppcom_session_disconnect (u32 session_index)
1262{
Dave Wallace543852a2017-08-03 02:11:34 -04001263 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001264 session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001265
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001266 clib_spinlock_lock (&vcm->sessions_lockp);
1267 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04001268 if (PREDICT_FALSE (rv))
1269 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001270 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 if (VPPCOM_DEBUG > 1)
1272 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001273 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001274 return rv;
1275 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001276
1277 if (!session->is_cut_thru)
1278 {
1279 vppcom_send_disconnect (session);
1280 clib_spinlock_unlock (&vcm->sessions_lockp);
1281
1282 rv = vppcom_wait_for_session_state_change (session_index,
1283 STATE_DISCONNECT, 1.0);
1284 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1285 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001286 getpid (), session_index, vppcom_retval_str (rv), rv);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001287 }
1288 else
1289 clib_spinlock_unlock (&vcm->sessions_lockp);
1290
Dave Wallace543852a2017-08-03 02:11:34 -04001291 return VPPCOM_OK;
1292}
1293
1294#define foreach_sock_msg \
1295_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1296_(BIND_SOCK_REPLY, bind_sock_reply) \
1297_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1298_(ACCEPT_SESSION, accept_session) \
1299_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001300_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001301_(DISCONNECT_SESSION, disconnect_session) \
1302_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1303_(RESET_SESSION, reset_session) \
1304_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1305_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1306_(MAP_ANOTHER_SEGMENT, map_another_segment)
1307
1308static void
1309vppcom_api_hookup (void)
1310{
1311#define _(N,n) \
1312 vl_msg_api_set_handlers(VL_API_##N, #n, \
1313 vl_api_##n##_t_handler, \
1314 vl_noop_handler, \
1315 vl_api_##n##_t_endian, \
1316 vl_api_##n##_t_print, \
1317 sizeof(vl_api_##n##_t), 1);
1318 foreach_sock_msg;
1319#undef _
1320}
1321
1322static void
1323vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1324{
1325 ASSERT (vcl_cfg);
1326
1327 vcl_cfg->heapsize = (256ULL << 20);
1328 vcl_cfg->segment_baseva = 0x200000000ULL;
1329 vcl_cfg->segment_size = (256 << 20);
1330 vcl_cfg->add_segment_size = (128 << 20);
1331 vcl_cfg->preallocated_fifo_pairs = 8;
1332 vcl_cfg->rx_fifo_size = (1 << 20);
1333 vcl_cfg->tx_fifo_size = (1 << 20);
1334 vcl_cfg->event_queue_size = 2048;
1335 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1336 vcl_cfg->app_timeout = 10 * 60.0;
1337 vcl_cfg->session_timeout = 10 * 60.0;
1338 vcl_cfg->accept_timeout = 60.0;
1339}
1340
1341static void
1342vppcom_cfg_heapsize (char *conf_fname)
1343{
Dave Wallace543852a2017-08-03 02:11:34 -04001344 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1345 FILE *fp;
1346 char inbuf[4096];
1347 int argc = 1;
1348 char **argv = NULL;
1349 char *arg = NULL;
1350 char *p;
1351 int i;
1352 u8 *sizep;
1353 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001354 void *vcl_mem;
1355 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001356
1357 fp = fopen (conf_fname, "r");
1358 if (fp == NULL)
1359 {
1360 if (VPPCOM_DEBUG > 0)
1361 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1362 goto defaulted;
1363 }
1364 argv = calloc (1, sizeof (char *));
1365 if (argv == NULL)
1366 goto defaulted;
1367
1368 while (1)
1369 {
1370 if (fgets (inbuf, 4096, fp) == 0)
1371 break;
1372 p = strtok (inbuf, " \t\n");
1373 while (p != NULL)
1374 {
1375 if (*p == '#')
1376 break;
1377 argc++;
1378 char **tmp = realloc (argv, argc * sizeof (char *));
1379 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001380 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001381 argv = tmp;
1382 arg = strndup (p, 1024);
1383 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001384 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001385 argv[argc - 1] = arg;
1386 p = strtok (NULL, " \t\n");
1387 }
1388 }
1389
1390 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001391 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001392
1393 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1394 if (tmp == NULL)
1395 goto defaulted;
1396 argv = tmp;
1397 argv[argc] = NULL;
1398
1399 /*
1400 * Look for and parse the "heapsize" config parameter.
1401 * Manual since none of the clib infra has been bootstrapped yet.
1402 *
1403 * Format: heapsize <nn>[mM][gG]
1404 */
1405
1406 for (i = 1; i < (argc - 1); i++)
1407 {
1408 if (!strncmp (argv[i], "heapsize", 8))
1409 {
1410 sizep = (u8 *) argv[i + 1];
1411 size = 0;
1412 while (*sizep >= '0' && *sizep <= '9')
1413 {
1414 size *= 10;
1415 size += *sizep++ - '0';
1416 }
1417 if (size == 0)
1418 {
1419 if (VPPCOM_DEBUG > 0)
1420 clib_warning ("[%d] parse error '%s %s', "
1421 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001422 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001423 vcl_cfg->heapsize, vcl_cfg->heapsize);
1424 goto defaulted;
1425 }
1426
1427 if (*sizep == 'g' || *sizep == 'G')
1428 vcl_cfg->heapsize = size << 30;
1429 else if (*sizep == 'm' || *sizep == 'M')
1430 vcl_cfg->heapsize = size << 20;
1431 else
1432 {
1433 if (VPPCOM_DEBUG > 0)
1434 clib_warning ("[%d] parse error '%s %s', "
1435 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001436 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001437 vcl_cfg->heapsize, vcl_cfg->heapsize);
1438 goto defaulted;
1439 }
1440 }
1441 }
1442
1443defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001444 if (fp != NULL)
1445 fclose (fp);
1446 if (argv != NULL)
1447 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001448
Dave Wallace2e005bb2017-11-07 01:21:39 -05001449 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1450 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001451 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001452 {
1453 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1454 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1455 "-1, 0) failed!",
1456 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1457 return;
1458 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001459 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1460 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001461 {
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001462 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1463 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001464 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001465 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1466 if (!vcl_mem)
1467 {
1468 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1469 return;
1470 }
1471
1472 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1473 vcm = vcl_mem;
1474
1475 if (VPPCOM_DEBUG > 0)
1476 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1477 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001478}
1479
1480static void
1481vppcom_cfg_read (char *conf_fname)
1482{
Dave Wallace543852a2017-08-03 02:11:34 -04001483 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1484 int fd;
1485 unformat_input_t _input, *input = &_input;
1486 unformat_input_t _line_input, *line_input = &_line_input;
1487 u8 vc_cfg_input = 0;
1488 u8 *chroot_path;
1489 struct stat s;
1490 u32 uid, gid;
1491
1492 fd = open (conf_fname, O_RDONLY);
1493 if (fd < 0)
1494 {
1495 if (VPPCOM_DEBUG > 0)
1496 clib_warning ("[%d] open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001497 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001498 goto file_done;
1499 }
1500
1501 if (fstat (fd, &s) < 0)
1502 {
1503 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001504 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001505 goto file_done;
1506 }
1507
1508 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1509 {
1510 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001511 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001512 goto file_done;
1513 }
1514
Dave Barach59b25652017-09-10 15:04:27 -04001515 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001516
1517 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1518 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001519 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001520 unformat_skip_white_space (line_input);
1521
Dave Wallace8af20542017-10-26 03:29:30 -04001522 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001523 {
1524 vc_cfg_input = 1;
1525 continue;
1526 }
1527
1528 if (vc_cfg_input)
1529 {
1530 if (unformat (line_input, "heapsize %s", &chroot_path))
1531 {
1532 vec_terminate_c_string (chroot_path);
1533 if (VPPCOM_DEBUG > 0)
1534 clib_warning ("[%d] configured heapsize %s, "
1535 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001536 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001537 vcl_cfg->heapsize);
1538 vec_free (chroot_path);
1539 }
1540 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1541 {
1542 vec_terminate_c_string (chroot_path);
1543 vl_set_memory_root_path ((char *) chroot_path);
1544 if (VPPCOM_DEBUG > 0)
1545 clib_warning ("[%d] configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001546 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001547 chroot_path = 0; /* Don't vec_free() it! */
1548 }
1549 else if (unformat (line_input, "uid %d", &uid))
1550 {
1551 vl_set_memory_uid (uid);
1552 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001553 clib_warning ("[%d] configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001554 }
1555 else if (unformat (line_input, "gid %d", &gid))
1556 {
1557 vl_set_memory_gid (gid);
1558 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001559 clib_warning ("[%d] configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001560 }
Dave Wallace8af20542017-10-26 03:29:30 -04001561 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001562 &vcl_cfg->segment_baseva))
1563 {
1564 if (VPPCOM_DEBUG > 0)
Dave Wallace8af20542017-10-26 03:29:30 -04001565 clib_warning ("[%d] configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001566 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001567 }
1568 else if (unformat (line_input, "segment-size 0x%lx",
1569 &vcl_cfg->segment_size))
1570 {
1571 if (VPPCOM_DEBUG > 0)
1572 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001573 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001574 vcl_cfg->segment_size);
1575 }
1576 else if (unformat (line_input, "segment-size %ld",
1577 &vcl_cfg->segment_size))
1578 {
1579 if (VPPCOM_DEBUG > 0)
1580 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001581 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001582 vcl_cfg->segment_size);
1583 }
1584 else if (unformat (line_input, "add-segment-size 0x%lx",
1585 &vcl_cfg->add_segment_size))
1586 {
1587 if (VPPCOM_DEBUG > 0)
1588 clib_warning
1589 ("[%d] configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001590 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001591 vcl_cfg->add_segment_size);
1592 }
1593 else if (unformat (line_input, "add-segment-size %ld",
1594 &vcl_cfg->add_segment_size))
1595 {
1596 if (VPPCOM_DEBUG > 0)
1597 clib_warning
1598 ("[%d] configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001599 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001600 vcl_cfg->add_segment_size);
1601 }
1602 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1603 &vcl_cfg->preallocated_fifo_pairs))
1604 {
1605 if (VPPCOM_DEBUG > 0)
1606 clib_warning ("[%d] configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001607 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001608 vcl_cfg->preallocated_fifo_pairs,
1609 vcl_cfg->preallocated_fifo_pairs);
1610 }
1611 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1612 &vcl_cfg->rx_fifo_size))
1613 {
1614 if (VPPCOM_DEBUG > 0)
1615 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001616 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001617 vcl_cfg->rx_fifo_size);
1618 }
1619 else if (unformat (line_input, "rx-fifo-size %ld",
1620 &vcl_cfg->rx_fifo_size))
1621 {
1622 if (VPPCOM_DEBUG > 0)
1623 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001624 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001625 vcl_cfg->rx_fifo_size);
1626 }
1627 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1628 &vcl_cfg->tx_fifo_size))
1629 {
1630 if (VPPCOM_DEBUG > 0)
1631 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001632 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001633 vcl_cfg->tx_fifo_size);
1634 }
1635 else if (unformat (line_input, "tx-fifo-size %ld",
1636 &vcl_cfg->tx_fifo_size))
1637 {
1638 if (VPPCOM_DEBUG > 0)
1639 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001640 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001641 vcl_cfg->tx_fifo_size);
1642 }
1643 else if (unformat (line_input, "event-queue-size 0x%lx",
1644 &vcl_cfg->event_queue_size))
1645 {
1646 if (VPPCOM_DEBUG > 0)
1647 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001648 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001649 vcl_cfg->event_queue_size);
1650 }
1651 else if (unformat (line_input, "event-queue-size %ld",
1652 &vcl_cfg->event_queue_size))
1653 {
1654 if (VPPCOM_DEBUG > 0)
1655 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001656 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001657 vcl_cfg->event_queue_size);
1658 }
1659 else if (unformat (line_input, "listen-queue-size 0x%lx",
1660 &vcl_cfg->listen_queue_size))
1661 {
1662 if (VPPCOM_DEBUG > 0)
1663 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001664 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001665 vcl_cfg->listen_queue_size);
1666 }
1667 else if (unformat (line_input, "listen-queue-size %ld",
1668 &vcl_cfg->listen_queue_size))
1669 {
1670 if (VPPCOM_DEBUG > 0)
1671 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001672 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001673 vcl_cfg->listen_queue_size);
1674 }
1675 else if (unformat (line_input, "app-timeout %f",
1676 &vcl_cfg->app_timeout))
1677 {
1678 if (VPPCOM_DEBUG > 0)
1679 clib_warning ("[%d] configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001680 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001681 }
1682 else if (unformat (line_input, "session-timeout %f",
1683 &vcl_cfg->session_timeout))
1684 {
1685 if (VPPCOM_DEBUG > 0)
1686 clib_warning ("[%d] configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001687 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001688 }
1689 else if (unformat (line_input, "accept-timeout %f",
1690 &vcl_cfg->accept_timeout))
1691 {
1692 if (VPPCOM_DEBUG > 0)
1693 clib_warning ("[%d] configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001694 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001695 }
Dave Wallace774169b2017-11-01 20:07:40 -04001696 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001697 {
Dave Wallace774169b2017-11-01 20:07:40 -04001698 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001699 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001700 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001701 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001702 }
Dave Wallace774169b2017-11-01 20:07:40 -04001703 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001704 {
Dave Wallace774169b2017-11-01 20:07:40 -04001705 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001706 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001707 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001708 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001709 }
Dave Wallace774169b2017-11-01 20:07:40 -04001710 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001711 {
Dave Wallace774169b2017-11-01 20:07:40 -04001712 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001713 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001714 clib_warning ("[%d] configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001715 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001716 }
1717 else if (unformat (line_input, "app-scope-global"))
1718 {
1719 vcl_cfg->app_scope_global = 1;
1720 if (VPPCOM_DEBUG > 0)
1721 clib_warning ("[%d] configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001722 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001723 }
1724 else if (unformat (line_input, "namespace-secret %lu",
1725 &vcl_cfg->namespace_secret))
1726 {
1727 if (VPPCOM_DEBUG > 0)
1728 clib_warning
1729 ("[%d] configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001730 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04001731 vcl_cfg->namespace_secret);
1732 }
1733 else if (unformat (line_input, "namespace-id %v",
1734 &vcl_cfg->namespace_id))
1735 {
1736 vl_api_application_attach_t *mp;
1737 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1738 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1739 if (nsid_vec_len > max_nsid_vec_len)
1740 {
1741 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1742 if (VPPCOM_DEBUG > 0)
1743 clib_warning ("[%d] configured namespace_id is too long,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05001744 " truncated to %d characters!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001745 max_nsid_vec_len);
1746 }
1747
1748 if (VPPCOM_DEBUG > 0)
1749 clib_warning ("[%d] configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001750 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001751 }
Dave Wallace543852a2017-08-03 02:11:34 -04001752 else if (unformat (line_input, "}"))
1753 {
1754 vc_cfg_input = 0;
1755 if (VPPCOM_DEBUG > 0)
1756 clib_warning ("[%d] completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001757 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001758 goto input_done;
1759 }
1760 else
1761 {
1762 if (line_input->buffer[line_input->index] != '#')
1763 {
1764 clib_warning ("[%d] Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001765 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001766 &line_input->buffer[line_input->index]);
1767 }
1768 }
1769 }
1770 }
1771
1772input_done:
1773 unformat_free (input);
1774
1775file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001776 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001777 close (fd);
1778}
1779
1780/*
1781 * VPPCOM Public API functions
1782 */
1783int
1784vppcom_app_create (char *app_name)
1785{
Dave Wallace543852a2017-08-03 02:11:34 -04001786 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1787 u8 *heap;
1788 mheap_t *h;
1789 int rv;
1790
1791 if (!vcm->init)
1792 {
1793 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001794 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001795
1796 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001797 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001798 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1799 if (env_var_str)
1800 {
1801 u32 tmp;
1802 if (sscanf (env_var_str, "%u", &tmp) != 1)
1803 clib_warning ("[%d] Invalid debug level specified in "
1804 "the environment variable "
1805 VPPCOM_ENV_DEBUG
1806 " (%s)!\n", getpid (), env_var_str);
1807 else
1808 {
1809 vcm->debug = tmp;
1810 clib_warning ("[%d] configured debug level (%u) from "
1811 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1812 }
1813 }
Dave Wallace8af20542017-10-26 03:29:30 -04001814 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001815 if (!conf_fname)
1816 {
1817 conf_fname = VPPCOM_CONF_DEFAULT;
1818 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001819 clib_warning ("[%d] getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001820 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001821 }
1822 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001823 clib_fifo_validate (vcm->client_session_index_fifo,
1824 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001825 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04001826 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1827 if (env_var_str)
1828 {
1829 u32 ns_id_vec_len = strlen (env_var_str);
1830
1831 vec_reset_length (vcm->cfg.namespace_id);
1832 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1833 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1834
1835 if (VPPCOM_DEBUG > 0)
1836 clib_warning ("[%d] configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001837 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001838 vcm->cfg.namespace_id);
1839 }
1840 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1841 if (env_var_str)
1842 {
1843 u64 tmp;
1844 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1845 clib_warning ("[%d] Invalid namespace secret specified in "
1846 "the environment variable "
1847 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001848 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001849 else
1850 {
1851 vcm->cfg.namespace_secret = tmp;
1852 if (VPPCOM_DEBUG > 0)
1853 clib_warning ("[%d] configured namespace secret (%lu) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001854 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001855 vcm->cfg.namespace_secret);
1856 }
1857 }
Dave Wallace774169b2017-11-01 20:07:40 -04001858 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001859 {
Dave Wallace774169b2017-11-01 20:07:40 -04001860 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001861 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001862 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001863 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001864 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001865 }
Dave Wallace774169b2017-11-01 20:07:40 -04001866 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001867 {
Dave Wallace774169b2017-11-01 20:07:40 -04001868 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001869 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001870 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001871 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001872 vcm->cfg.app_proxy_transport_udp);
1873 }
1874 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1875 {
1876 vcm->cfg.app_scope_local = 1;
1877 if (VPPCOM_DEBUG > 0)
1878 clib_warning ("[%d] configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001879 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001880 vcm->cfg.app_scope_local);
1881 }
1882 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1883 {
1884 vcm->cfg.app_scope_global = 1;
1885 if (VPPCOM_DEBUG > 0)
1886 clib_warning ("[%d] configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001887 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001888 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001889 }
1890
Dave Wallace543852a2017-08-03 02:11:34 -04001891 vcm->bind_session_index = ~0;
1892 vcm->main_cpu = os_get_thread_index ();
1893 heap = clib_mem_get_per_cpu_heap ();
1894 h = mheap_header (heap);
1895
1896 /* make the main heap thread-safe */
1897 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1898
1899 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1900
1901 clib_time_init (&vcm->clib_time);
1902 vppcom_init_error_string_table ();
1903 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1904 20 /* timeout in secs */ );
1905 clib_spinlock_init (&vcm->sessions_lockp);
1906 vppcom_api_hookup ();
1907 }
1908
1909 if (vcm->my_client_index == ~0)
1910 {
1911 vcm->app_state = STATE_APP_START;
1912 rv = vppcom_connect_to_vpp (app_name);
1913 if (rv)
1914 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001915 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001916 return rv;
1917 }
1918
1919 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001920 clib_warning ("[%d] sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001921
1922 rv = vppcom_app_session_enable ();
1923 if (rv)
1924 {
1925 clib_warning ("[%d] vppcom_app_session_enable() failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001926 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001927 return rv;
1928 }
1929
1930 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001931 clib_warning ("[%d] sending app attach", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001932
1933 rv = vppcom_app_attach ();
1934 if (rv)
1935 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001936 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001937 return rv;
1938 }
Dave Wallace543852a2017-08-03 02:11:34 -04001939
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001940 if (VPPCOM_DEBUG > 0)
1941 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001942 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001943 vcm->my_client_index);
1944 }
Dave Wallace543852a2017-08-03 02:11:34 -04001945
1946 return VPPCOM_OK;
1947}
1948
1949void
1950vppcom_app_destroy (void)
1951{
Dave Wallace543852a2017-08-03 02:11:34 -04001952 int rv;
1953
1954 if (vcm->my_client_index == ~0)
1955 return;
1956
1957 if (VPPCOM_DEBUG > 0)
1958 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001959 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001960
1961 vppcom_app_detach ();
1962 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1963 if (PREDICT_FALSE (rv))
1964 {
1965 if (VPPCOM_DEBUG > 0)
1966 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001967 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001968 }
1969 vl_client_disconnect_from_vlib ();
1970 vcm->my_client_index = ~0;
1971 vcm->app_state = STATE_APP_START;
1972}
1973
1974int
1975vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1976{
Dave Wallace543852a2017-08-03 02:11:34 -04001977 session_t *session;
1978 u32 session_index;
1979
1980 clib_spinlock_lock (&vcm->sessions_lockp);
1981 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001982 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04001983 session_index = session - vcm->sessions;
1984
1985 session->vrf = vrf;
1986 session->proto = proto;
1987 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001988 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001989 clib_spinlock_unlock (&vcm->sessions_lockp);
1990
1991 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001992 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001993
1994 return (int) session_index;
1995}
1996
1997int
1998vppcom_session_close (uint32_t session_index)
1999{
Dave Wallace543852a2017-08-03 02:11:34 -04002000 session_t *session = 0;
2001 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002002 u8 is_server;
2003 u8 is_listen;
2004 u8 is_cut_thru;
2005 u8 is_vep;
2006 u8 is_vep_session;
2007 u32 next_sid;
2008 u32 vep_idx;
2009 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002010
2011 clib_spinlock_lock (&vcm->sessions_lockp);
2012 rv = vppcom_session_at_index (session_index, &session);
2013 if (PREDICT_FALSE (rv))
2014 {
Dave Wallace543852a2017-08-03 02:11:34 -04002015 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002016 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002017 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002018 clib_spinlock_unlock (&vcm->sessions_lockp);
2019 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002020 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002021 is_server = session->is_server;
2022 is_listen = session->is_listen;
2023 is_cut_thru = session->is_cut_thru;
2024 is_vep = session->is_vep;
2025 is_vep_session = session->is_vep_session;
2026 next_sid = session->vep.next_sid;
2027 vep_idx = session->vep.vep_idx;
2028 state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04002029 clib_spinlock_unlock (&vcm->sessions_lockp);
2030
2031 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002032 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002033
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002034 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002035 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002036 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002037 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002038 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallace19481612017-09-15 18:47:44 -04002039 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002040 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002041 "rv = %s (%d)", getpid (), vep_idx, next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002042 vppcom_retval_str (rv), rv);
2043
2044 clib_spinlock_lock (&vcm->sessions_lockp);
2045 rv = vppcom_session_at_index (session_index, &session);
2046 if (PREDICT_FALSE (rv))
2047 {
2048 if (VPPCOM_DEBUG > 0)
2049 clib_warning
2050 ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002051 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002052 clib_spinlock_unlock (&vcm->sessions_lockp);
2053 goto done;
2054 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002055 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002056 clib_spinlock_unlock (&vcm->sessions_lockp);
2057 }
2058 }
2059 else
2060 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002061 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002062 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002063 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2064 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2065 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002066 "rv = %s (%d)", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002067 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002068 }
2069
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002070 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002071 {
Dave Wallace60caa062017-11-10 17:07:13 -05002072 rv = vppcom_session_unbind_cut_thru (session, session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002073 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2074 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2075 "rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002076 getpid (), session_index,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002077 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002078 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002079 else if (is_server && is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002080 {
2081 rv = vppcom_session_unbind (session_index);
2082 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2083 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002084 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002085 vppcom_retval_str (rv), rv);
2086 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002087 else if (state == STATE_CONNECT)
2088 if (vppcom_session_disconnect (session_index))
2089 goto done;
Dave Wallace19481612017-09-15 18:47:44 -04002090 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002091 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002092 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002093 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002094done:
Dave Wallace543852a2017-08-03 02:11:34 -04002095 return rv;
2096}
2097
2098int
2099vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2100{
Dave Wallace543852a2017-08-03 02:11:34 -04002101 session_t *session = 0;
2102 int rv;
2103
2104 if (!ep || !ep->ip)
2105 return VPPCOM_EINVAL;
2106
2107 clib_spinlock_lock (&vcm->sessions_lockp);
2108 rv = vppcom_session_at_index (session_index, &session);
2109 if (PREDICT_FALSE (rv))
2110 {
2111 clib_spinlock_unlock (&vcm->sessions_lockp);
2112 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002113 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002114 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002115 return rv;
2116 }
2117
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002118 if (session->is_vep)
2119 {
2120 clib_spinlock_unlock (&vcm->sessions_lockp);
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002123 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002124 return VPPCOM_EBADFD;
2125 }
2126
Dave Wallace543852a2017-08-03 02:11:34 -04002127 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002128 session->lcl_addr.is_ip4 = ep->is_ip4;
2129 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002130 session->lcl_port = ep->port;
2131
2132 if (VPPCOM_DEBUG > 0)
2133 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002134 getpid (), session_index, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07002135 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2136 clib_net_to_host_u16 (session->lcl_port));
Dave Wallace543852a2017-08-03 02:11:34 -04002137
2138 clib_spinlock_unlock (&vcm->sessions_lockp);
2139 return VPPCOM_OK;
2140}
2141
2142int
Dave Wallace33e002b2017-09-06 01:20:02 -04002143vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002144{
Dave Wallace33e002b2017-09-06 01:20:02 -04002145 session_t *listen_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002146 int rv;
2147
2148 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002149 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002150 if (PREDICT_FALSE (rv))
2151 {
2152 clib_spinlock_unlock (&vcm->sessions_lockp);
2153 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002154 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002155 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002156 return rv;
2157 }
2158
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002159 if (listen_session->is_vep)
2160 {
2161 clib_spinlock_unlock (&vcm->sessions_lockp);
2162 if (VPPCOM_DEBUG > 0)
2163 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002164 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002165 return VPPCOM_EBADFD;
2166 }
2167
Dave Wallacee695cb42017-11-02 22:04:42 -04002168 if (listen_session->is_listen)
2169 {
2170 clib_spinlock_unlock (&vcm->sessions_lockp);
2171 if (VPPCOM_DEBUG > 0)
2172 clib_warning ("[%d] sid (%u) is already in listen state!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002173 getpid (), listen_session_index);
Dave Wallacee695cb42017-11-02 22:04:42 -04002174 return VPPCOM_OK;
2175 }
2176
Dave Wallace543852a2017-08-03 02:11:34 -04002177 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002178 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002179
2180 ASSERT (vcm->bind_session_index == ~0);
Dave Wallace33e002b2017-09-06 01:20:02 -04002181 vcm->bind_session_index = listen_session_index;
2182 vppcom_send_bind_sock (listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002183 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002184 rv =
2185 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2186 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002187 if (PREDICT_FALSE (rv))
2188 {
2189 vcm->bind_session_index = ~0;
2190 if (VPPCOM_DEBUG > 0)
2191 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002192 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002193 return rv;
2194 }
2195
2196 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002197 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002198 if (PREDICT_FALSE (rv))
2199 {
2200 clib_spinlock_unlock (&vcm->sessions_lockp);
2201 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002202 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002203 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002204 return rv;
2205 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002206 listen_session->is_listen = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002207 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002209
2210 return VPPCOM_OK;
2211}
2212
2213int
2214vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2215 double wait_for_time)
2216{
Dave Wallace33e002b2017-09-06 01:20:02 -04002217 session_t *listen_session = 0;
2218 session_t *client_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002219 u32 client_session_index;
2220 int rv;
2221 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002222 char *cut_thru_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002223
2224 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002225 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002226 if (PREDICT_FALSE (rv))
2227 {
2228 clib_spinlock_unlock (&vcm->sessions_lockp);
2229 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002230 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002231 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002232 return rv;
2233 }
2234
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002235 if (listen_session->is_vep)
2236 {
2237 clib_spinlock_unlock (&vcm->sessions_lockp);
2238 if (VPPCOM_DEBUG > 0)
2239 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002240 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002241 return VPPCOM_EBADFD;
2242 }
2243
Dave Wallace33e002b2017-09-06 01:20:02 -04002244 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002245 {
2246 clib_spinlock_unlock (&vcm->sessions_lockp);
2247 if (VPPCOM_DEBUG > 0)
2248 clib_warning ("[%d] session not in listen state, state = %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002249 getpid (),
Dave Wallace33e002b2017-09-06 01:20:02 -04002250 vppcom_session_state_str (listen_session->state));
Dave Wallace543852a2017-08-03 02:11:34 -04002251 return VPPCOM_EBADFD;
2252 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002253 wait_for = listen_session->is_nonblocking ? 0 :
Dave Wallace543852a2017-08-03 02:11:34 -04002254 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2255
2256 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002257 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002258 listen_session_index,
Dave Wallace33e002b2017-09-06 01:20:02 -04002259 vppcom_session_state_str (listen_session->state),
2260 listen_session->state);
Dave Wallace543852a2017-08-03 02:11:34 -04002261 clib_spinlock_unlock (&vcm->sessions_lockp);
2262
2263 while (1)
2264 {
2265 rv = vppcom_wait_for_client_session_index (wait_for);
2266 if (rv)
2267 {
2268 if ((VPPCOM_DEBUG > 0))
2269 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002270 getpid (), listen_session_index,
Dave Wallace543852a2017-08-03 02:11:34 -04002271 vppcom_retval_str (rv), rv);
2272 if ((wait_for == 0) || (wait_for_time > 0))
2273 return rv;
2274 }
2275 else
2276 break;
2277 }
2278
Dave Wallace543852a2017-08-03 02:11:34 -04002279 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002280 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002281 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002282 ASSERT (rv == VPPCOM_OK);
Dave Wallace35830af2017-10-09 01:43:42 -04002283 ASSERT (client_session->peer_addr.is_ip4 ==
2284 listen_session->lcl_addr.is_ip4);
Dave Wallace543852a2017-08-03 02:11:34 -04002285
2286 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002287 clib_warning ("[%d] Got a request: client sid %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002288 client_session_index);
2289
Dave Wallace33e002b2017-09-06 01:20:02 -04002290 ep->vrf = client_session->vrf;
2291 ep->is_cut_thru = client_session->is_cut_thru;
Dave Wallace35830af2017-10-09 01:43:42 -04002292 ep->is_ip4 = client_session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07002293 ep->port = client_session->peer_port;
Dave Wallace35830af2017-10-09 01:43:42 -04002294 if (client_session->peer_addr.is_ip4)
2295 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2296 sizeof (ip4_address_t));
Dave Wallace33e002b2017-09-06 01:20:02 -04002297 else
Dave Wallace35830af2017-10-09 01:43:42 -04002298 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2299 sizeof (ip6_address_t));
Dave Wallace60caa062017-11-10 17:07:13 -05002300
2301 if (client_session->is_server && client_session->is_cut_thru)
2302 {
2303 static svm_fifo_segment_create_args_t _a;
2304 svm_fifo_segment_create_args_t *a = &_a;
2305 svm_fifo_segment_private_t *seg;
2306
2307 cut_thru_str = "cut-thru ";
2308
2309 /* Create the segment */
2310 memset (a, 0, sizeof (*a));
2311 a->segment_name = (char *)
2312 format ((u8 *) a->segment_name, "%d:segment%d%c",
2313 getpid (), vcm->unique_segment_index++, 0);
2314 a->segment_size = vcm->cfg.segment_size;
2315 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2316 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2317 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2318
2319 rv = svm_fifo_segment_create (a);
2320 if (PREDICT_FALSE (rv))
2321 {
2322 if (VPPCOM_DEBUG > 1)
2323 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2324 getpid (), a->segment_name);
2325 vec_reset_length (a->new_segment_indices);
2326 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2327 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2328 clib_spinlock_unlock (&vcm->sessions_lockp);
2329 return VPPCOM_ENOMEM;
2330 }
2331
2332 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2333 client_session->sm_seg_index = a->new_segment_indices[0];
2334 vec_free (a->new_segment_indices);
2335
2336 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2337 client_session->server_rx_fifo =
2338 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2339 FIFO_SEGMENT_RX_FREELIST);
2340 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2341 {
2342 svm_fifo_segment_delete (seg);
2343 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2344 getpid (), vcm->cfg.rx_fifo_size,
2345 vcm->cfg.rx_fifo_size);
2346 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2347 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2348 clib_spinlock_unlock (&vcm->sessions_lockp);
2349 return VPPCOM_ENOMEM;
2350 }
2351 client_session->server_rx_fifo->master_session_index =
2352 client_session_index;
2353
2354 client_session->server_tx_fifo =
2355 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2356 FIFO_SEGMENT_TX_FREELIST);
2357 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2358 {
2359 svm_fifo_segment_delete (seg);
2360 if (VPPCOM_DEBUG > 1)
2361 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2362 getpid (), vcm->cfg.tx_fifo_size,
2363 vcm->cfg.tx_fifo_size);
2364 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2365 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2366 clib_spinlock_unlock (&vcm->sessions_lockp);
2367 return VPPCOM_ENOMEM;
2368 }
2369 client_session->server_tx_fifo->master_session_index =
2370 client_session_index;
2371
2372 if (VPPCOM_DEBUG > 1)
2373 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2374 getpid (), client_session->segment_name,
2375 client_session->server_rx_fifo,
2376 client_session->server_tx_fifo);
2377
2378#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2379 {
2380 void *oldheap;
2381 ssvm_shared_header_t *sh = seg->ssvm.sh;
2382
2383 ssvm_lock_non_recursive (sh, 1);
2384 oldheap = ssvm_push_heap (sh);
2385 event_q = client_session->vpp_event_queue =
2386 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2387 sizeof (session_fifo_event_t),
2388 getpid (), 0 /* signal not sent */ );
2389 ssvm_pop_heap (oldheap);
2390 ssvm_unlock_non_recursive (sh);
2391 }
2392#endif
2393 vppcom_send_connect_session_reply (client_session, 0, 0, 0);
2394 }
2395 else
2396 {
2397 cut_thru_str = " ";
2398 vppcom_send_accept_session_reply (0, client_session->vpp_handle);
2399 }
2400
Stevenac1f96d2017-10-24 16:03:58 -07002401 if (VPPCOM_DEBUG > 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002402 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2403 "%U peer port %u",
2404 getpid (), client_session_index, cut_thru_str,
2405 format_ip46_address, &client_session->peer_addr.ip46,
Stevenac1f96d2017-10-24 16:03:58 -07002406 client_session->peer_addr.is_ip4,
2407 clib_net_to_host_u16 (client_session->peer_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002408
Dave Wallace543852a2017-08-03 02:11:34 -04002409 clib_spinlock_unlock (&vcm->sessions_lockp);
2410 return (int) client_session_index;
2411}
2412
2413int
2414vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2415{
Dave Wallace543852a2017-08-03 02:11:34 -04002416 session_t *session = 0;
2417 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002418
2419 clib_spinlock_lock (&vcm->sessions_lockp);
2420 rv = vppcom_session_at_index (session_index, &session);
2421 if (PREDICT_FALSE (rv))
2422 {
2423 clib_spinlock_unlock (&vcm->sessions_lockp);
2424 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002425 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002426 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002427 return rv;
2428 }
2429
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002430 if (session->is_vep)
2431 {
2432 clib_spinlock_unlock (&vcm->sessions_lockp);
2433 if (VPPCOM_DEBUG > 0)
2434 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002435 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002436 return VPPCOM_EBADFD;
2437 }
2438
Dave Wallace543852a2017-08-03 02:11:34 -04002439 if (session->state == STATE_CONNECT)
2440 {
2441 clib_spinlock_unlock (&vcm->sessions_lockp);
2442 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002443 clib_warning ("[%d] session, sid (%u) already connected!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002444 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002445 return VPPCOM_OK;
2446 }
2447
2448 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002449 session->peer_addr.is_ip4 = server_ep->is_ip4;
2450 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002451 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002452
2453 if (VPPCOM_DEBUG > 0)
2454 {
2455 u8 *ip_str = format (0, "%U", format_ip46_address,
Dave Wallace35830af2017-10-09 01:43:42 -04002456 &session->peer_addr.ip46,
2457 session->peer_addr.is_ip4);
Dave Wallace7876d392017-10-19 03:53:57 -04002458 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002459 getpid (), session_index, ip_str,
Stevenac1f96d2017-10-24 16:03:58 -07002460 clib_net_to_host_u16 (session->peer_port),
Dave Wallace7876d392017-10-19 03:53:57 -04002461 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002462 vec_free (ip_str);
2463 }
2464
2465 vppcom_send_connect_sock (session, session_index);
2466 clib_spinlock_unlock (&vcm->sessions_lockp);
2467 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2468 vcm->cfg.session_timeout);
2469 if (PREDICT_FALSE (rv))
2470 {
2471 if (VPPCOM_DEBUG > 0)
2472 clib_warning ("[%d] connect timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002473 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002474 return rv;
2475 }
Dave Wallace7876d392017-10-19 03:53:57 -04002476 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002477 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04002478
Dave Wallace543852a2017-08-03 02:11:34 -04002479 return VPPCOM_OK;
2480}
2481
Steven58f464e2017-10-25 12:33:12 -07002482static inline int
2483vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2484 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002485{
Dave Wallace543852a2017-08-03 02:11:34 -04002486 session_t *session = 0;
2487 svm_fifo_t *rx_fifo;
2488 int n_read = 0;
2489 int rv;
2490 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002491 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002492
2493 ASSERT (buf);
2494
2495 clib_spinlock_lock (&vcm->sessions_lockp);
2496 rv = vppcom_session_at_index (session_index, &session);
2497 if (PREDICT_FALSE (rv))
2498 {
2499 clib_spinlock_unlock (&vcm->sessions_lockp);
2500 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002501 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002502 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002503 return rv;
2504 }
2505
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002506 if (session->is_vep)
2507 {
2508 clib_spinlock_unlock (&vcm->sessions_lockp);
2509 if (VPPCOM_DEBUG > 0)
2510 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002511 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002512 return VPPCOM_EBADFD;
2513 }
2514
Dave Wallace33e002b2017-09-06 01:20:02 -04002515 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002516 {
Dave Wallace543852a2017-08-03 02:11:34 -04002517 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002518 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002519 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002520 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002521 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002522 }
Dave Wallace543852a2017-08-03 02:11:34 -04002523
Dave Wallace33e002b2017-09-06 01:20:02 -04002524 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2525 session->server_rx_fifo : session->server_tx_fifo);
2526 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2527 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace60caa062017-11-10 17:07:13 -05002528 poll_et =
2529 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002530 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002531
2532 do
2533 {
Steven58f464e2017-10-25 12:33:12 -07002534 if (peek)
2535 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2536 else
2537 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002538 }
2539 while (!session->is_nonblocking && (n_read <= 0));
2540
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002541 if (poll_et && (n_read <= 0))
2542 {
2543 clib_spinlock_lock (&vcm->sessions_lockp);
2544 session->vep.et_mask |= EPOLLIN;
2545 clib_spinlock_unlock (&vcm->sessions_lockp);
2546 }
Dave Wallace543852a2017-08-03 02:11:34 -04002547
Dave Wallacef7f809c2017-10-03 01:48:42 -04002548 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
Dave Wallace2e005bb2017-11-07 01:21:39 -05002549 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002550 session_index, n_read, fifo_str, rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002551
2552 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002553}
2554
Steven58f464e2017-10-25 12:33:12 -07002555int
2556vppcom_session_read (uint32_t session_index, void *buf, int n)
2557{
2558 return (vppcom_session_read_internal (session_index, buf, n, 0));
2559}
2560
2561static int
2562vppcom_session_peek (uint32_t session_index, void *buf, int n)
2563{
2564 return (vppcom_session_read_internal (session_index, buf, n, 1));
2565}
2566
Dave Wallace543852a2017-08-03 02:11:34 -04002567static inline int
2568vppcom_session_read_ready (session_t * session, u32 session_index)
2569{
Dave Wallace498b3a52017-11-09 13:00:34 -05002570 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002571 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002572 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002573
2574 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002575 if (session->is_vep)
2576 {
2577 clib_spinlock_unlock (&vcm->sessions_lockp);
2578 if (VPPCOM_DEBUG > 0)
2579 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002580 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002581 return VPPCOM_EBADFD;
2582 }
2583
Dave Wallace33e002b2017-09-06 01:20:02 -04002584 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002585 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002586 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002587 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002588 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002589 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002590 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002591
2592 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04002593 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2594 else
2595 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002596 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2597 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002598
Dave Wallace33e002b2017-09-06 01:20:02 -04002599 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002600 }
2601
Dave Wallace60caa062017-11-10 17:07:13 -05002602 poll_et =
2603 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2604 if (poll_et && (ready == 0))
2605 {
2606 if (VPPCOM_DEBUG > 11)
2607 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2608 session_index, session->vep.et_mask);
2609 session->vep.et_mask |= EPOLLIN;
2610 if (VPPCOM_DEBUG > 11)
2611 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2612 session_index, session->vep.et_mask);
2613 }
2614
2615 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2616 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2617 "et_mask 0x%x",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002618 getpid (), session_index, session->is_listen,
Dave Wallace543852a2017-08-03 02:11:34 -04002619 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
Dave Wallace60caa062017-11-10 17:07:13 -05002620 rx_fifo, ready, session->vep.et_mask);
Dave Wallace543852a2017-08-03 02:11:34 -04002621 return ready;
2622}
2623
2624int
2625vppcom_session_write (uint32_t session_index, void *buf, int n)
2626{
Dave Wallace543852a2017-08-03 02:11:34 -04002627 session_t *session = 0;
2628 svm_fifo_t *tx_fifo;
2629 unix_shared_memory_queue_t *q;
2630 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002631 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002632 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002633 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002634
2635 ASSERT (buf);
2636
2637 clib_spinlock_lock (&vcm->sessions_lockp);
2638 rv = vppcom_session_at_index (session_index, &session);
2639 if (PREDICT_FALSE (rv))
2640 {
2641 clib_spinlock_unlock (&vcm->sessions_lockp);
2642 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002643 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002644 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002645 return rv;
2646 }
2647
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002648 if (session->is_vep)
2649 {
2650 clib_spinlock_unlock (&vcm->sessions_lockp);
2651 if (VPPCOM_DEBUG > 0)
2652 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002653 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002654 return VPPCOM_EBADFD;
2655 }
2656
Dave Wallace33e002b2017-09-06 01:20:02 -04002657 if (session->state == STATE_DISCONNECT)
2658 {
2659 clib_spinlock_unlock (&vcm->sessions_lockp);
2660 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002661 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002662 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002663 return VPPCOM_ECONNRESET;
2664 }
2665
Dave Wallace543852a2017-08-03 02:11:34 -04002666 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2667 session->server_tx_fifo : session->server_rx_fifo);
2668 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2669 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002670 q = session->vpp_event_queue;
Dave Wallace60caa062017-11-10 17:07:13 -05002671 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2672 (EPOLLET | EPOLLOUT));
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002673 clib_spinlock_unlock (&vcm->sessions_lockp);
2674
Dave Wallace543852a2017-08-03 02:11:34 -04002675 do
2676 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002677 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002678 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 while (!session->is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002680
2681 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002683 {
2684 int rval;
2685
2686 /* Fabricate TX event, send to vpp */
2687 evt.fifo = tx_fifo;
2688 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002689
Dave Wallace543852a2017-08-03 02:11:34 -04002690 rval = vppcom_session_at_index (session_index, &session);
2691 if (PREDICT_FALSE (rval))
2692 {
Dave Wallace543852a2017-08-03 02:11:34 -04002693 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002695 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002696 return rval;
2697 }
Dave Wallace543852a2017-08-03 02:11:34 -04002698 ASSERT (q);
2699 unix_shared_memory_queue_add (q, (u8 *) & evt,
2700 0 /* do wait for mutex */ );
2701 }
2702
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002703 if (poll_et && (n_write <= 0))
2704 {
2705 clib_spinlock_lock (&vcm->sessions_lockp);
2706 session->vep.et_mask |= EPOLLOUT;
2707 clib_spinlock_unlock (&vcm->sessions_lockp);
2708 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002709
Dave Wallace543852a2017-08-03 02:11:34 -04002710 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002711 {
2712 if (n_write == -2)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002713 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002714 session_index, fifo_str, tx_fifo);
2715 else
Dave Wallace2e005bb2017-11-07 01:21:39 -05002716 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002717 session_index, n_write, fifo_str, tx_fifo);
2718 }
2719 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002720}
2721
2722static inline int
2723vppcom_session_write_ready (session_t * session, u32 session_index)
2724{
Dave Wallace543852a2017-08-03 02:11:34 -04002725 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04002726 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002728 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002729
2730 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002731 if (session->is_vep)
2732 {
2733 clib_spinlock_unlock (&vcm->sessions_lockp);
2734 if (VPPCOM_DEBUG > 0)
2735 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002736 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002737 return VPPCOM_EBADFD;
2738 }
2739
Dave Wallace33e002b2017-09-06 01:20:02 -04002740 if (session->state == STATE_DISCONNECT)
2741 {
2742 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002744 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002745 return VPPCOM_ECONNRESET;
2746 }
2747
Dave Wallace543852a2017-08-03 02:11:34 -04002748 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2749 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002750 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2751 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04002752
Dave Wallacef7f809c2017-10-03 01:48:42 -04002753 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002754
Dave Wallace33e002b2017-09-06 01:20:02 -04002755 if (VPPCOM_DEBUG > 3)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002756 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 session_index, fifo_str, tx_fifo, ready);
Dave Wallace60caa062017-11-10 17:07:13 -05002758 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2759 (EPOLLET | EPOLLOUT));
2760 if (poll_et && (ready == 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002761 session->vep.et_mask |= EPOLLOUT;
2762
2763 return ready;
Dave Wallace543852a2017-08-03 02:11:34 -04002764}
2765
2766int
2767vppcom_select (unsigned long n_bits, unsigned long *read_map,
2768 unsigned long *write_map, unsigned long *except_map,
2769 double time_to_wait)
2770{
Dave Wallace543852a2017-08-03 02:11:34 -04002771 u32 session_index;
2772 session_t *session = 0;
2773 int rv, bits_set = 0;
2774 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2775 u32 minbits = clib_max (n_bits, BITS (uword));
2776
2777 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2778
Dave Wallace7876d392017-10-19 03:53:57 -04002779 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002780 {
2781 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2782 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2783 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2784 }
Dave Wallace7876d392017-10-19 03:53:57 -04002785 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002786 {
2787 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2788 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2789 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2790 }
Dave Wallace7876d392017-10-19 03:53:57 -04002791 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002792 {
2793 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2794 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2795 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2796 }
2797
2798 do
2799 {
2800 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002801 if (n_bits)
2802 {
2803 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002804 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002805 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2806 ({
2807 clib_spinlock_lock (&vcm->sessions_lockp);
2808 rv = vppcom_session_at_index (session_index, &session);
2809 if (rv < 0)
2810 {
2811 clib_spinlock_unlock (&vcm->sessions_lockp);
2812 if (VPPCOM_DEBUG > 1)
2813 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002814 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002815 session_index);
2816 bits_set = VPPCOM_EBADFD;
2817 goto select_done;
2818 }
2819
2820 rv = vppcom_session_read_ready (session, session_index);
2821 clib_spinlock_unlock (&vcm->sessions_lockp);
2822 if (except_map && vcm->ex_bitmap &&
2823 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2824 (rv < 0))
2825 {
2826 // TBD: clib_warning
2827 clib_bitmap_set_no_check (except_map, session_index, 1);
2828 bits_set++;
2829 }
2830 else if (rv > 0)
2831 {
2832 // TBD: clib_warning
2833 clib_bitmap_set_no_check (read_map, session_index, 1);
2834 bits_set++;
2835 }
2836 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002837 }
2838
Dave Wallacee22aa742017-10-20 12:30:38 -04002839 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002840 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002841 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2842 ({
2843 clib_spinlock_lock (&vcm->sessions_lockp);
2844 rv = vppcom_session_at_index (session_index, &session);
2845 if (rv < 0)
2846 {
2847 clib_spinlock_unlock (&vcm->sessions_lockp);
2848 if (VPPCOM_DEBUG > 0)
2849 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002850 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002851 session_index);
2852 bits_set = VPPCOM_EBADFD;
2853 goto select_done;
2854 }
Dave Wallace543852a2017-08-03 02:11:34 -04002855
Dave Wallacee22aa742017-10-20 12:30:38 -04002856 rv = vppcom_session_write_ready (session, session_index);
2857 clib_spinlock_unlock (&vcm->sessions_lockp);
2858 if (write_map && (rv > 0))
2859 {
2860 // TBD: clib_warning
2861 clib_bitmap_set_no_check (write_map, session_index, 1);
2862 bits_set++;
2863 }
2864 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002865 }
2866
Dave Wallacee22aa742017-10-20 12:30:38 -04002867 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002868 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002869 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2870 ({
2871 clib_spinlock_lock (&vcm->sessions_lockp);
2872 rv = vppcom_session_at_index (session_index, &session);
2873 if (rv < 0)
2874 {
2875 clib_spinlock_unlock (&vcm->sessions_lockp);
2876 if (VPPCOM_DEBUG > 1)
2877 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002878 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002879 session_index);
2880 bits_set = VPPCOM_EBADFD;
2881 goto select_done;
2882 }
Dave Wallace543852a2017-08-03 02:11:34 -04002883
Dave Wallacee22aa742017-10-20 12:30:38 -04002884 rv = vppcom_session_read_ready (session, session_index);
2885 clib_spinlock_unlock (&vcm->sessions_lockp);
2886 if (rv < 0)
2887 {
2888 // TBD: clib_warning
2889 clib_bitmap_set_no_check (except_map, session_index, 1);
2890 bits_set++;
2891 }
2892 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002893 }
Dave Wallacee22aa742017-10-20 12:30:38 -04002894 }
Dave Wallace543852a2017-08-03 02:11:34 -04002895 /* *INDENT-ON* */
2896 }
2897 while (clib_time_now (&vcm->clib_time) < timeout);
2898
2899select_done:
2900 return (bits_set);
2901}
2902
Dave Wallacef7f809c2017-10-03 01:48:42 -04002903static inline void
2904vep_verify_epoll_chain (u32 vep_idx)
2905{
2906 session_t *session;
2907 vppcom_epoll_t *vep;
2908 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05002909 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002910
Dave Wallace498b3a52017-11-09 13:00:34 -05002911 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002912 return;
2913
2914 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2915 rv = vppcom_session_at_index (vep_idx, &session);
2916 if (PREDICT_FALSE (rv))
2917 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002918 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002919 goto done;
2920 }
2921 if (PREDICT_FALSE (!session->is_vep))
2922 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002923 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002924 vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002925 goto done;
2926 }
Dave Wallace498b3a52017-11-09 13:00:34 -05002927 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2928 "{\n"
2929 " is_vep = %u\n"
2930 " is_vep_session = %u\n"
2931 " wait_cont_idx = 0x%x (%u)\n"
2932 "}\n", getpid (),
2933 vep_idx, session->is_vep, session->is_vep_session,
2934 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002935 do
2936 {
2937 vep = &session->vep;
Dave Wallacee695cb42017-11-02 22:04:42 -04002938 sid = vep->next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002939 if (sid != ~0)
2940 {
2941 rv = vppcom_session_at_index (sid, &session);
2942 if (PREDICT_FALSE (rv))
2943 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002944 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002945 goto done;
2946 }
2947 if (PREDICT_FALSE (session->is_vep))
Dave Wallace774169b2017-11-01 20:07:40 -04002948 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002949 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002950 else if (PREDICT_FALSE (!session->is_vep_session))
2951 {
Dave Wallace774169b2017-11-01 20:07:40 -04002952 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002953 getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002954 goto done;
2955 }
2956 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
Dave Wallace774169b2017-11-01 20:07:40 -04002957 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002958 "vep_idx (%u)!", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002959 sid, session->vep.vep_idx, vep_idx);
Dave Wallace498b3a52017-11-09 13:00:34 -05002960 if (session->is_vep_session)
2961 {
2962 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2963 "{\n"
2964 " next_sid = 0x%x (%u)\n"
2965 " prev_sid = 0x%x (%u)\n"
2966 " vep_idx = 0x%x (%u)\n"
2967 " ev.events = 0x%x\n"
2968 " ev.data.u64 = 0x%llx\n"
2969 " et_mask = 0x%x\n"
2970 "}\n",
2971 vep_idx, sid, sid,
2972 vep->next_sid, vep->next_sid,
2973 vep->prev_sid, vep->prev_sid,
2974 vep->vep_idx, vep->vep_idx,
2975 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2976 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002977 }
2978 }
2979 while (sid != ~0);
2980
2981done:
Dave Wallace498b3a52017-11-09 13:00:34 -05002982 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002983}
2984
2985int
2986vppcom_epoll_create (void)
2987{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002988 session_t *vep_session;
2989 u32 vep_idx;
2990
2991 clib_spinlock_lock (&vcm->sessions_lockp);
2992 pool_get (vcm->sessions, vep_session);
2993 memset (vep_session, 0, sizeof (*vep_session));
2994 vep_idx = vep_session - vcm->sessions;
2995
2996 vep_session->is_vep = 1;
2997 vep_session->vep.vep_idx = ~0;
2998 vep_session->vep.next_sid = ~0;
2999 vep_session->vep.prev_sid = ~0;
3000 vep_session->wait_cont_idx = ~0;
3001 clib_spinlock_unlock (&vcm->sessions_lockp);
3002
3003 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05003004 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003005
3006 return (vep_idx);
3007}
3008
3009int
3010vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3011 struct epoll_event *event)
3012{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003013 session_t *vep_session;
3014 session_t *session;
3015 int rv;
3016
3017 if (vep_idx == session_index)
3018 {
3019 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003020 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003021 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003022 return VPPCOM_EINVAL;
3023 }
3024
3025 clib_spinlock_lock (&vcm->sessions_lockp);
3026 rv = vppcom_session_at_index (vep_idx, &vep_session);
3027 if (PREDICT_FALSE (rv))
3028 {
3029 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003030 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003031 goto done;
3032 }
3033 if (PREDICT_FALSE (!vep_session->is_vep))
3034 {
3035 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003036 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003037 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003038 rv = VPPCOM_EINVAL;
3039 goto done;
3040 }
3041
3042 ASSERT (vep_session->vep.vep_idx == ~0);
3043 ASSERT (vep_session->vep.prev_sid == ~0);
3044
3045 rv = vppcom_session_at_index (session_index, &session);
3046 if (PREDICT_FALSE (rv))
3047 {
3048 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003049 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003050 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003051 goto done;
3052 }
3053 if (PREDICT_FALSE (session->is_vep))
3054 {
3055 if (VPPCOM_DEBUG > 0)
3056 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3057 rv = VPPCOM_EINVAL;
3058 goto done;
3059 }
3060
3061 switch (op)
3062 {
3063 case EPOLL_CTL_ADD:
3064 if (PREDICT_FALSE (!event))
3065 {
Dave Wallace774169b2017-11-01 20:07:40 -04003066 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003067 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003068 rv = VPPCOM_EINVAL;
3069 goto done;
3070 }
3071 if (vep_session->vep.next_sid != ~0)
3072 {
3073 session_t *next_session;
3074 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3075 &next_session);
3076 if (PREDICT_FALSE (rv))
3077 {
3078 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003079 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3080 "vep.next_sid (%u) on vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003081 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003082 goto done;
3083 }
3084 ASSERT (next_session->vep.prev_sid == vep_idx);
3085 next_session->vep.prev_sid = session_index;
3086 }
3087 session->vep.next_sid = vep_session->vep.next_sid;
3088 session->vep.prev_sid = vep_idx;
3089 session->vep.vep_idx = vep_idx;
3090 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3091 session->vep.ev = *event;
3092 session->is_vep_session = 1;
3093 vep_session->vep.next_sid = session_index;
3094 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003095 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003096 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003097 event->events, event->data.u64);
3098 break;
3099
3100 case EPOLL_CTL_MOD:
3101 if (PREDICT_FALSE (!event))
3102 {
Dave Wallace774169b2017-11-01 20:07:40 -04003103 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003104 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003105 rv = VPPCOM_EINVAL;
3106 goto done;
3107 }
3108 if (PREDICT_FALSE (!session->is_vep_session &&
3109 (session->vep.vep_idx != vep_idx)))
3110 {
3111 if (VPPCOM_DEBUG > 0)
3112 {
3113 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003114 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3115 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003116 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003117 else
Dave Wallace774169b2017-11-01 20:07:40 -04003118 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3119 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003120 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003121 session->vep.vep_idx, vep_idx);
3122 }
3123 rv = VPPCOM_EINVAL;
3124 goto done;
3125 }
3126 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3127 session->vep.ev = *event;
3128 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003129 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003130 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003131 event->events, event->data.u64);
3132 break;
3133
3134 case EPOLL_CTL_DEL:
3135 if (PREDICT_FALSE (!session->is_vep_session &&
3136 (session->vep.vep_idx != vep_idx)))
3137 {
3138 if (VPPCOM_DEBUG > 0)
3139 {
3140 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003141 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3142 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003143 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003144 else
Dave Wallace774169b2017-11-01 20:07:40 -04003145 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3146 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003147 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003148 session->vep.vep_idx, vep_idx);
3149 }
3150 rv = VPPCOM_EINVAL;
3151 goto done;
3152 }
3153
3154 vep_session->wait_cont_idx =
3155 (vep_session->wait_cont_idx == session_index) ?
3156 session->vep.next_sid : vep_session->wait_cont_idx;
3157
3158 if (session->vep.prev_sid == vep_idx)
3159 vep_session->vep.next_sid = session->vep.next_sid;
3160 else
3161 {
3162 session_t *prev_session;
3163 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3164 if (PREDICT_FALSE (rv))
3165 {
3166 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003167 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3168 "vep.prev_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003169 getpid (), session->vep.prev_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003170 session_index);
3171 goto done;
3172 }
3173 ASSERT (prev_session->vep.next_sid == session_index);
3174 prev_session->vep.next_sid = session->vep.next_sid;
3175 }
3176 if (session->vep.next_sid != ~0)
3177 {
3178 session_t *next_session;
3179 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3180 if (PREDICT_FALSE (rv))
3181 {
3182 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003183 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3184 "vep.next_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003185 getpid (), session->vep.next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003186 session_index);
3187 goto done;
3188 }
3189 ASSERT (next_session->vep.prev_sid == session_index);
3190 next_session->vep.prev_sid = session->vep.prev_sid;
3191 }
3192
3193 memset (&session->vep, 0, sizeof (session->vep));
3194 session->vep.next_sid = ~0;
3195 session->vep.prev_sid = ~0;
3196 session->vep.vep_idx = ~0;
3197 session->is_vep_session = 0;
3198 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003199 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003200 getpid (), vep_idx, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003201 break;
3202
3203 default:
Dave Wallace2e005bb2017-11-07 01:21:39 -05003204 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003205 rv = VPPCOM_EINVAL;
3206 }
3207
3208 vep_verify_epoll_chain (vep_idx);
3209
3210done:
3211 clib_spinlock_unlock (&vcm->sessions_lockp);
3212 return rv;
3213}
3214
Dave Wallacef7f809c2017-10-03 01:48:42 -04003215int
3216vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3217 int maxevents, double wait_for_time)
3218{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003219 session_t *vep_session;
3220 int rv;
3221 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003222 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003223 int num_ev = 0;
3224 u32 vep_next_sid, wait_cont_idx;
3225 u8 is_vep;
3226
3227 if (PREDICT_FALSE (maxevents <= 0))
3228 {
3229 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003230 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003231 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003232 return VPPCOM_EINVAL;
3233 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003234 memset (events, 0, sizeof (*events) * maxevents);
3235
3236 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3237 vep_next_sid = vep_session->vep.next_sid;
3238 is_vep = vep_session->is_vep;
3239 wait_cont_idx = vep_session->wait_cont_idx;
3240 clib_spinlock_unlock (&vcm->sessions_lockp);
3241
3242 if (PREDICT_FALSE (!is_vep))
3243 {
3244 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003245 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003246 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003247 rv = VPPCOM_EINVAL;
3248 goto done;
3249 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003250 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003251 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003252 if (VPPCOM_DEBUG > 0)
3253 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003254 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003255 goto done;
3256 }
3257
3258 do
3259 {
3260 u32 sid;
3261 u32 next_sid = ~0;
3262 session_t *session;
3263
3264 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3265 sid != ~0; sid = next_sid)
3266 {
3267 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3268 u8 add_event, is_vep_session;
3269 int ready;
3270 u64 session_ev_data;
3271
3272 VCL_LOCK_AND_GET_SESSION (sid, &session);
3273 next_sid = session->vep.next_sid;
3274 session_events = session->vep.ev.events;
3275 et_mask = session->vep.et_mask;
3276 is_vep = session->is_vep;
3277 is_vep_session = session->is_vep_session;
3278 session_vep_idx = session->vep.vep_idx;
3279 session_ev_data = session->vep.ev.data.u64;
3280 clib_spinlock_unlock (&vcm->sessions_lockp);
3281
3282 if (PREDICT_FALSE (is_vep))
3283 {
3284 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003285 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003286 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003287 rv = VPPCOM_EINVAL;
3288 goto done;
3289 }
3290 if (PREDICT_FALSE (!is_vep_session))
3291 {
3292 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003293 clib_warning ("[%d] ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003294 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003295 rv = VPPCOM_EINVAL;
3296 goto done;
3297 }
3298 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3299 {
Dave Wallace774169b2017-11-01 20:07:40 -04003300 clib_warning ("[%d] ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003301 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003302 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003303 rv = VPPCOM_EINVAL;
3304 goto done;
3305 }
3306
3307 add_event = clear_et_mask = 0;
3308
Dave Wallace60caa062017-11-10 17:07:13 -05003309 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003310 {
3311 VCL_LOCK_AND_GET_SESSION (sid, &session);
3312 ready = vppcom_session_read_ready (session, sid);
3313 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003314 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003315 {
3316 add_event = 1;
3317 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003318 if (((EPOLLET | EPOLLIN) & session_events) ==
3319 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003320 clear_et_mask |= EPOLLIN;
3321 }
3322 else if (ready < 0)
3323 {
3324 add_event = 1;
3325 switch (ready)
3326 {
3327 case VPPCOM_ECONNRESET:
3328 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3329 break;
3330
3331 default:
3332 events[num_ev].events |= EPOLLERR;
3333 break;
3334 }
3335 }
3336 }
3337
Dave Wallace60caa062017-11-10 17:07:13 -05003338 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003339 {
3340 VCL_LOCK_AND_GET_SESSION (sid, &session);
3341 ready = vppcom_session_write_ready (session, sid);
3342 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003343 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003344 {
3345 add_event = 1;
3346 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003347 if (((EPOLLET | EPOLLOUT) & session_events) ==
3348 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003349 clear_et_mask |= EPOLLOUT;
3350 }
3351 else if (ready < 0)
3352 {
3353 add_event = 1;
3354 switch (ready)
3355 {
3356 case VPPCOM_ECONNRESET:
3357 events[num_ev].events |= EPOLLHUP;
3358 break;
3359
3360 default:
3361 events[num_ev].events |= EPOLLERR;
3362 break;
3363 }
3364 }
3365 }
3366
3367 if (add_event)
3368 {
3369 events[num_ev].data.u64 = session_ev_data;
3370 if (EPOLLONESHOT & session_events)
3371 {
3372 VCL_LOCK_AND_GET_SESSION (sid, &session);
3373 session->vep.ev.events = 0;
3374 clib_spinlock_unlock (&vcm->sessions_lockp);
3375 }
3376 num_ev++;
3377 if (num_ev == maxevents)
3378 {
3379 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3380 vep_session->wait_cont_idx = next_sid;
3381 clib_spinlock_unlock (&vcm->sessions_lockp);
3382 goto done;
3383 }
3384 }
3385 if (wait_cont_idx != ~0)
3386 {
3387 if (next_sid == ~0)
3388 next_sid = vep_next_sid;
3389 else if (next_sid == wait_cont_idx)
3390 next_sid = ~0;
3391 }
3392 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003393 if (wait_for_time != -1)
3394 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003395 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003396 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003397
3398 if (wait_cont_idx != ~0)
3399 {
3400 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3401 vep_session->wait_cont_idx = ~0;
3402 clib_spinlock_unlock (&vcm->sessions_lockp);
3403 }
3404done:
3405 return (rv != VPPCOM_OK) ? rv : num_ev;
3406}
3407
Dave Wallace35830af2017-10-09 01:43:42 -04003408int
3409vppcom_session_attr (uint32_t session_index, uint32_t op,
3410 void *buffer, uint32_t * buflen)
3411{
Dave Wallace35830af2017-10-09 01:43:42 -04003412 session_t *session;
3413 int rv = VPPCOM_OK;
3414 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003415 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003416
3417 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3418 switch (op)
3419 {
3420 case VPPCOM_ATTR_GET_NREAD:
3421 rv = vppcom_session_read_ready (session, session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003422 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003423 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003424 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003425
3426 break;
3427
3428 case VPPCOM_ATTR_PEEK_NREAD:
3429 /* TBD */
3430 break;
3431
3432 case VPPCOM_ATTR_GET_FLAGS:
3433 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3434 {
3435 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3436 *buflen = sizeof (*flags);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003437 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003438 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003439 "is_nonblocking = %u", getpid (), *flags,
Dave Wallace35830af2017-10-09 01:43:42 -04003440 session->is_nonblocking);
3441 }
3442 else
3443 rv = VPPCOM_EINVAL;
3444 break;
3445
3446 case VPPCOM_ATTR_SET_FLAGS:
3447 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3448 {
3449 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallacefaf9d772017-10-26 16:12:04 -04003450 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003451 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003452 "is_nonblocking = %u", getpid (), *flags,
Dave Wallace35830af2017-10-09 01:43:42 -04003453 session->is_nonblocking);
3454 }
3455 else
3456 rv = VPPCOM_EINVAL;
3457 break;
3458
3459 case VPPCOM_ATTR_GET_PEER_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003460 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003461 {
Steven2199aab2017-10-15 20:18:47 -07003462 ep->vrf = session->vrf;
3463 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003464 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07003465 if (session->peer_addr.is_ip4)
3466 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3467 sizeof (ip4_address_t));
3468 else
3469 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3470 sizeof (ip6_address_t));
3471 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003472 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003473 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003474 "%u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003475 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003476 &session->peer_addr.ip46, ep->is_ip4,
3477 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003478 }
3479 else
3480 rv = VPPCOM_EINVAL;
3481 break;
3482
3483 case VPPCOM_ATTR_GET_LCL_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003484 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003485 {
Steven2199aab2017-10-15 20:18:47 -07003486 ep->vrf = session->vrf;
3487 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003488 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07003489 if (session->lcl_addr.is_ip4)
3490 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3491 sizeof (ip4_address_t));
3492 else
3493 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3494 sizeof (ip6_address_t));
3495 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003496 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003497 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003498 "%u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003499 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003500 &session->lcl_addr.ip46, ep->is_ip4,
3501 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003502 }
3503 else
3504 rv = VPPCOM_EINVAL;
3505 break;
Stevenb5a11602017-10-11 09:59:30 -07003506
3507 case VPPCOM_ATTR_SET_REUSEADDR:
3508 break;
3509
3510 case VPPCOM_ATTR_SET_BROADCAST:
3511 break;
3512
3513 case VPPCOM_ATTR_SET_V6ONLY:
3514 break;
Stevenbccd3392017-10-12 20:42:21 -07003515
3516 case VPPCOM_ATTR_SET_KEEPALIVE:
3517 break;
3518
3519 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3520 break;
3521
3522 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3523 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003524
3525 default:
3526 rv = VPPCOM_EINVAL;
3527 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003528 }
3529
3530done:
3531 clib_spinlock_unlock (&vcm->sessions_lockp);
3532 return rv;
3533}
3534
Stevenac1f96d2017-10-24 16:03:58 -07003535int
3536vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3537 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3538{
Stevenac1f96d2017-10-24 16:03:58 -07003539 int rv = VPPCOM_OK;
3540 session_t *session = 0;
3541
3542 if (ep)
3543 {
3544 clib_spinlock_lock (&vcm->sessions_lockp);
3545 rv = vppcom_session_at_index (session_index, &session);
3546 if (PREDICT_FALSE (rv))
3547 {
3548 clib_spinlock_unlock (&vcm->sessions_lockp);
3549 if (VPPCOM_DEBUG > 0)
3550 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003551 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003552 rv = VPPCOM_EBADFD;
3553 clib_spinlock_unlock (&vcm->sessions_lockp);
3554 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07003555 }
3556 ep->vrf = session->vrf;
3557 ep->is_ip4 = session->peer_addr.is_ip4;
3558 ep->port = session->peer_port;
3559 if (session->peer_addr.is_ip4)
3560 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3561 sizeof (ip4_address_t));
3562 else
3563 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3564 sizeof (ip6_address_t));
3565 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07003566 }
Steven58f464e2017-10-25 12:33:12 -07003567
3568 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003569 rv = vppcom_session_read (session_index, buffer, buflen);
3570 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003571 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003572 else
3573 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003574 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07003575 rv = VPPCOM_EAFNOSUPPORT;
3576 }
3577
Dave Wallacefaf9d772017-10-26 16:12:04 -04003578done:
Stevenac1f96d2017-10-24 16:03:58 -07003579 return rv;
3580}
3581
3582int
3583vppcom_session_sendto (uint32_t session_index, void *buffer,
3584 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3585{
Dave Wallace617dffa2017-10-26 14:47:06 -04003586 if (!buffer)
3587 return VPPCOM_EINVAL;
3588
3589 if (ep)
3590 {
3591 // TBD
3592 return VPPCOM_EINVAL;
3593 }
3594
3595 if (flags)
3596 {
3597 // TBD check the flags and do the right thing
3598 if (VPPCOM_DEBUG > 2)
3599 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003600 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003601 }
3602
3603 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003604}
3605
Dave Wallacee22aa742017-10-20 12:30:38 -04003606/*
3607 * fd.io coding-style-patch-verification: ON
3608 *
3609 * Local Variables:
3610 * eval: (c-set-style "gnu")
3611 * End:
3612 */