blob: 2382f29a2e15c8daa90475b586b762851cd68848 [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;
602
603 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
604 if (p)
605 {
606 session_t *session = 0;
607 int rv;
608 clib_spinlock_lock (&vcm->sessions_lockp);
609 rv = vppcom_session_at_index (p[0], &session);
610 if (PREDICT_FALSE (rv))
611 {
612 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400613 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500614 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400615 }
616 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
617 session->state = STATE_DISCONNECT;
618 clib_spinlock_unlock (&vcm->sessions_lockp);
619 }
620 else
621 {
622 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500623 clib_warning ("[%d] couldn't find session key %llx", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400624 mp->handle);
625 }
626
627 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500628 clib_warning ("[%d] disconnect_session failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400629 format_api_error, ntohl (mp->retval));
630}
631
632static void
633vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
634{
Dave Wallace543852a2017-08-03 02:11:34 -0400635 static svm_fifo_segment_create_args_t _a;
636 svm_fifo_segment_create_args_t *a = &_a;
637 int rv;
638
639 memset (a, 0, sizeof (*a));
640 a->segment_name = (char *) mp->segment_name;
641 a->segment_size = mp->segment_size;
642 /* Attach to the segment vpp created */
643 rv = svm_fifo_segment_attach (a);
644 vec_reset_length (a->new_segment_indices);
645 if (PREDICT_FALSE (rv))
646 {
647 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500648 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400649 return;
650 }
651 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500652 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400653 mp->segment_name, mp->segment_size);
654}
655
656static void
657vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
658{
Dave Wallace543852a2017-08-03 02:11:34 -0400659 session_t *session = 0;
660 vl_api_disconnect_session_reply_t *rmp;
661 uword *p;
662 int rv = 0;
663
664 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
665 if (p)
666 {
667 int rval;
668 clib_spinlock_lock (&vcm->sessions_lockp);
669 rval = vppcom_session_at_index (p[0], &session);
670 if (PREDICT_FALSE (rval))
671 {
672 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400673 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500674 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400675 }
676 else
677 pool_put (vcm->sessions, session);
678 clib_spinlock_unlock (&vcm->sessions_lockp);
679 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
680 }
681 else
682 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500683 clib_warning ("[%d] couldn't find session key %llx", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400684 mp->handle);
685 rv = -11;
686 }
687
688 rmp = vl_msg_api_alloc (sizeof (*rmp));
689 memset (rmp, 0, sizeof (*rmp));
690
691 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
692 rmp->retval = htonl (rv);
693 rmp->handle = mp->handle;
694 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
695}
696
697static void
698vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
699{
Dave Wallace543852a2017-08-03 02:11:34 -0400700 session_t *session = 0;
701 vl_api_reset_session_reply_t *rmp;
702 uword *p;
703 int rv = 0;
704
705 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
706 if (p)
707 {
708 int rval;
709 clib_spinlock_lock (&vcm->sessions_lockp);
710 rval = vppcom_session_at_index (p[0], &session);
711 if (PREDICT_FALSE (rval))
712 {
713 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400714 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500715 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400716 }
717 else
718 pool_put (vcm->sessions, session);
719 clib_spinlock_unlock (&vcm->sessions_lockp);
720 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
721 }
722 else
723 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500724 clib_warning ("[%d] couldn't find session key %llx", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400725 mp->handle);
726 rv = -11;
727 }
728
729 rmp = vl_msg_api_alloc (sizeof (*rmp));
730 memset (rmp, 0, sizeof (*rmp));
731 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
732 rmp->retval = htonl (rv);
733 rmp->handle = mp->handle;
734 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
735}
736
737static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400738vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400739{
Dave Wallace543852a2017-08-03 02:11:34 -0400740 session_t *session;
741 u32 session_index;
742 svm_fifo_t *rx_fifo, *tx_fifo;
743 u8 is_cut_thru = 0;
744 int rv;
745
746 if (mp->retval)
747 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500748 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400749 ntohl (mp->retval));
750 return;
751 }
752
Dave Wallace33e002b2017-09-06 01:20:02 -0400753 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -0400754 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500755 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400756 session_index, session_index);
757
758 clib_spinlock_lock (&vcm->sessions_lockp);
759 if (pool_is_free_index (vcm->sessions, session_index))
760 {
761 clib_spinlock_unlock (&vcm->sessions_lockp);
762 if (VPPCOM_DEBUG > 1)
763 clib_warning ("[%d] invalid session, sid %d is closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500764 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400765 return;
766 }
767
768 /* We've been redirected */
769 if (mp->segment_name_length > 0)
770 {
771 static svm_fifo_segment_create_args_t _a;
772 svm_fifo_segment_create_args_t *a = &_a;
773
774 is_cut_thru = 1;
775 memset (a, 0, sizeof (*a));
776 a->segment_name = (char *) mp->segment_name;
777 if (VPPCOM_DEBUG > 1)
Dave Wallace60caa062017-11-10 17:07:13 -0500778 clib_warning ("[%d] cut-thru segment: %s\n",
779 getpid (), a->segment_name);
780
Dave Wallace543852a2017-08-03 02:11:34 -0400781 rv = svm_fifo_segment_attach (a);
782 vec_reset_length (a->new_segment_indices);
783 if (PREDICT_FALSE (rv))
784 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400785 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400786 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500787 getpid (), a->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400788 return;
789 }
790 }
791
792 /*
793 * Setup session
794 */
Dave Wallace543852a2017-08-03 02:11:34 -0400795 session = pool_elt_at_index (vcm->sessions, session_index);
796 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -0400797 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
798 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400799
800 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
801 rx_fifo->client_session_index = session_index;
802 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
803 tx_fifo->client_session_index = session_index;
804
805 session->server_rx_fifo = rx_fifo;
806 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500807 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400808 session->state = STATE_CONNECT;
809
810 /* Add it to lookup table */
811 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500812
813 if (VPPCOM_DEBUG > 1)
814 clib_warning ("[%d] client sid %d\n"
815 " session_rx_fifo %p, refcnt %d\n"
816 " session_tx_fifo %p, refcnt %d",
817 getpid (), session_index,
818 session->server_rx_fifo,
819 session->server_rx_fifo->refcnt,
820 session->server_tx_fifo, session->server_tx_fifo->refcnt);
821
Dave Wallace543852a2017-08-03 02:11:34 -0400822 clib_spinlock_unlock (&vcm->sessions_lockp);
823}
824
825static void
826vppcom_send_connect_sock (session_t * session, u32 session_index)
827{
Dave Wallace543852a2017-08-03 02:11:34 -0400828 vl_api_connect_sock_t *cmp;
829
830 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
831 session->is_server = 0;
832 cmp = vl_msg_api_alloc (sizeof (*cmp));
833 memset (cmp, 0, sizeof (*cmp));
834 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
835 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400836 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400837
838 if (VPPCOM_DEBUG > 1)
Dave Wallace33e002b2017-09-06 01:20:02 -0400839 clib_warning ("[%d] session_index = %d 0x%08x",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500840 getpid (), session_index, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400841
842 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400843 cmp->is_ip4 = session->peer_addr.is_ip4;
844 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -0700845 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400846 cmp->proto = session->proto;
847 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
848 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
849}
850
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400851static inline void
852vppcom_send_disconnect (session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -0400853{
Dave Wallace543852a2017-08-03 02:11:34 -0400854 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400855
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400856 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400857 dmp = vl_msg_api_alloc (sizeof (*dmp));
858 memset (dmp, 0, sizeof (*dmp));
859 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
860 dmp->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500861 dmp->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400862 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400863}
864
865static void
866vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
867{
Dave Wallace543852a2017-08-03 02:11:34 -0400868 session_t *session = 0;
869 int rv;
870
871 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500872 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400873 ntohl (mp->retval));
874
875 ASSERT (vcm->bind_session_index != ~0);
876
877 clib_spinlock_lock (&vcm->sessions_lockp);
878 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
879 if (rv == VPPCOM_OK)
880 {
Dave Wallace60caa062017-11-10 17:07:13 -0500881 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400882 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
883 vcm->bind_session_index);
884 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
885 vcm->bind_session_index = ~0;
886 }
887 clib_spinlock_unlock (&vcm->sessions_lockp);
888}
889
890static void
891vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
892{
Dave Wallace543852a2017-08-03 02:11:34 -0400893 session_t *session = 0;
894 int rv;
895
896 clib_spinlock_lock (&vcm->sessions_lockp);
897 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
Dave Wallace19481612017-09-15 18:47:44 -0400898 if (rv == VPPCOM_OK)
Dave Wallace543852a2017-08-03 02:11:34 -0400899 {
Dave Wallace19481612017-09-15 18:47:44 -0400900 if ((VPPCOM_DEBUG > 1) && (mp->retval))
Dave Wallace2e005bb2017-11-07 01:21:39 -0500901 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
Dave Wallace19481612017-09-15 18:47:44 -0400902 ntohl (mp->retval));
903
904 vcm->bind_session_index = ~0;
905 session->state = STATE_START;
Dave Wallace543852a2017-08-03 02:11:34 -0400906 }
Dave Wallace543852a2017-08-03 02:11:34 -0400907 clib_spinlock_unlock (&vcm->sessions_lockp);
908}
909
910u8 *
911format_ip4_address (u8 * s, va_list * args)
912{
913 u8 *a = va_arg (*args, u8 *);
914 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
915}
916
917u8 *
918format_ip6_address (u8 * s, va_list * args)
919{
920 ip6_address_t *a = va_arg (*args, ip6_address_t *);
921 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
922
923 i_max_n_zero = ARRAY_LEN (a->as_u16);
924 max_n_zeros = 0;
925 i_first_zero = i_max_n_zero;
926 n_zeros = 0;
927 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
928 {
929 u32 is_zero = a->as_u16[i] == 0;
930 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
931 {
932 i_first_zero = i;
933 n_zeros = 0;
934 }
935 n_zeros += is_zero;
936 if ((!is_zero && n_zeros > max_n_zeros)
937 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
938 {
939 i_max_n_zero = i_first_zero;
940 max_n_zeros = n_zeros;
941 i_first_zero = ARRAY_LEN (a->as_u16);
942 n_zeros = 0;
943 }
944 }
945
946 last_double_colon = 0;
947 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
948 {
949 if (i == i_max_n_zero && max_n_zeros > 1)
950 {
951 s = format (s, "::");
952 i += max_n_zeros - 1;
953 last_double_colon = 1;
954 }
955 else
956 {
957 s = format (s, "%s%x",
958 (last_double_colon || i == 0) ? "" : ":",
959 clib_net_to_host_u16 (a->as_u16[i]));
960 last_double_colon = 0;
961 }
962 }
963
964 return s;
965}
966
967/* Format an IP46 address. */
968u8 *
969format_ip46_address (u8 * s, va_list * args)
970{
971 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
972 ip46_type_t type = va_arg (*args, ip46_type_t);
973 int is_ip4 = 1;
974
975 switch (type)
976 {
977 case IP46_TYPE_ANY:
978 is_ip4 = ip46_address_is_ip4 (ip46);
979 break;
980 case IP46_TYPE_IP4:
981 is_ip4 = 1;
982 break;
983 case IP46_TYPE_IP6:
984 is_ip4 = 0;
985 break;
986 }
987
988 return is_ip4 ?
989 format (s, "%U", format_ip4_address, &ip46->ip4) :
990 format (s, "%U", format_ip6_address, &ip46->ip6);
991}
992
Dave Wallace60caa062017-11-10 17:07:13 -0500993static inline void
994vppcom_send_accept_session_reply (u32 handle, int retval)
995{
996 vl_api_accept_session_reply_t *rmp;
997
998 rmp = vl_msg_api_alloc (sizeof (*rmp));
999 memset (rmp, 0, sizeof (*rmp));
1000 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1001 rmp->retval = htonl (retval);
1002 rmp->handle = handle;
1003 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1004}
1005
Dave Wallace543852a2017-08-03 02:11:34 -04001006static void
1007vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1008{
Dave Wallace543852a2017-08-03 02:11:34 -04001009 svm_fifo_t *rx_fifo, *tx_fifo;
1010 session_t *session;
1011 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001012
Dave Wallace60caa062017-11-10 17:07:13 -05001013 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001014 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1015 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001016 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallace60caa062017-11-10 17:07:13 -05001017 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL,
1018 mp->handle);
1019 clib_spinlock_unlock (&vcm->sessions_lockp);
1020 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001021 }
1022
Dave Wallace543852a2017-08-03 02:11:34 -04001023 /* Allocate local session and set it up */
1024 pool_get (vcm->sessions, session);
1025 memset (session, 0, sizeof (*session));
1026 session_index = session - vcm->sessions;
1027
1028 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1029 rx_fifo->client_session_index = session_index;
1030 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1031 tx_fifo->client_session_index = session_index;
1032
Dave Wallace60caa062017-11-10 17:07:13 -05001033 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001034 session->server_rx_fifo = rx_fifo;
1035 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001036 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1037 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001038 session->state = STATE_ACCEPT;
1039 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001040 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001041 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001042 session->peer_addr.is_ip4 = mp->is_ip4;
1043 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1044 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001045
1046 /* Add it to lookup table */
1047 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1048
1049 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001050 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001051
Dave Wallace60caa062017-11-10 17:07:13 -05001052 if (VPPCOM_DEBUG > 1)
1053 {
1054 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1055 clib_warning ("[%d] received request to accept session (sid %d) "
1056 "from %s:%d", getpid (), session_index, ip_str,
1057 clib_net_to_host_u16 (mp->port));
1058 vec_free (ip_str);
1059 }
1060}
1061
1062static void
1063vppcom_send_connect_session_reply (session_t * session, u32 context,
1064 int retval, int handle)
1065{
1066 vl_api_connect_session_reply_t *rmp;
1067 u32 len;
1068 unix_shared_memory_queue_t *client_q;
1069
Dave Wallace543852a2017-08-03 02:11:34 -04001070 rmp = vl_msg_api_alloc (sizeof (*rmp));
1071 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001072
1073 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1074 rmp->context = session ? session->client_context : context;
1075 rmp->retval = htonl (retval);
1076 rmp->handle = session ? session->vpp_handle : handle;
1077
1078 if (session)
1079 {
1080 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1081 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1082 rmp->vpp_event_queue_address =
1083 pointer_to_uword (session->vpp_event_queue);
1084 rmp->segment_size = vcm->cfg.segment_size;
1085 len = vec_len (session->segment_name);
1086 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1087 clib_memcpy (rmp->segment_name, session->segment_name,
1088 rmp->segment_name_length - 1);
1089 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1090 sizeof (rmp->lcl_ip));
1091 rmp->is_ip4 = session->lcl_addr.is_ip4;
1092 }
1093
1094 client_q = uword_to_pointer (session->client_queue_address,
1095 unix_shared_memory_queue_t *);
1096 ASSERT (client_q);
1097 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001098}
1099
1100/*
1101 * Acting as server for redirected connect requests
1102 */
1103static void
1104vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1105{
Dave Wallace543852a2017-08-03 02:11:34 -04001106 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001107 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001108
Dave Wallacef7f809c2017-10-03 01:48:42 -04001109 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001110 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1111 {
1112 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001113 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001114 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05001115 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL, 0);
1116 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001117 }
1118
Dave Wallace543852a2017-08-03 02:11:34 -04001119 pool_get (vcm->sessions, session);
1120 memset (session, 0, sizeof (*session));
1121 session_index = session - vcm->sessions;
1122
Dave Wallace60caa062017-11-10 17:07:13 -05001123 session->client_context = mp->context;
1124 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001125 session->client_queue_address = mp->client_queue_address;
1126 session->is_cut_thru = 1;
1127 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001128 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001129 session->peer_addr.is_ip4 = mp->is_ip4;
1130 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1131 sizeof (session->peer_addr.ip46));
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001132
Dave Wallace543852a2017-08-03 02:11:34 -04001133 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001134 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001135 if (VPPCOM_DEBUG > 1)
1136 clib_warning
Dave Wallace60caa062017-11-10 17:07:13 -05001137 ("[%d] Got a cut-thru connect request to client: "
1138 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
Dave Wallace2e005bb2017-11-07 01:21:39 -05001139 clib_fifo_elts (vcm->client_session_index_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001140 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001141}
1142
1143static void
1144vppcom_send_bind_sock (session_t * session)
1145{
Dave Wallace543852a2017-08-03 02:11:34 -04001146 vl_api_bind_sock_t *bmp;
1147
1148 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1149 session->is_server = 1;
1150 bmp = vl_msg_api_alloc (sizeof (*bmp));
1151 memset (bmp, 0, sizeof (*bmp));
1152
1153 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1154 bmp->client_index = vcm->my_client_index;
1155 bmp->context = htonl (0xfeedface);
1156 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001157 bmp->is_ip4 = session->lcl_addr.is_ip4;
1158 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001159 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001160 bmp->proto = session->proto;
1161 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1162 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1163}
1164
1165static void
1166vppcom_send_unbind_sock (u32 session_index)
1167{
Dave Wallace543852a2017-08-03 02:11:34 -04001168 vl_api_unbind_sock_t *ump;
1169 session_t *session = 0;
1170 int rv;
1171
1172 clib_spinlock_lock (&vcm->sessions_lockp);
1173 rv = vppcom_session_at_index (session_index, &session);
1174 if (PREDICT_FALSE (rv))
1175 {
1176 clib_spinlock_unlock (&vcm->sessions_lockp);
1177 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001178 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001179 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001180 return;
1181 }
1182
1183 ump = vl_msg_api_alloc (sizeof (*ump));
1184 memset (ump, 0, sizeof (*ump));
1185
1186 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1187 ump->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -05001188 ump->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001189 clib_spinlock_unlock (&vcm->sessions_lockp);
1190 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1191}
1192
1193static int
Dave Wallace60caa062017-11-10 17:07:13 -05001194vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001195{
1196 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1197 svm_fifo_segment_private_t *seg;
1198 int rv = VPPCOM_OK;
1199
Dave Wallace60caa062017-11-10 17:07:13 -05001200 if (VPPCOM_DEBUG > 1)
1201 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1202 " server_rx_fifo %p, refcnt = %d\n"
1203 " server_tx_fifo %p, refcnt = %d",
1204 getpid (), session_index, session->sm_seg_index,
1205 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1206 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1207
Dave Wallace543852a2017-08-03 02:11:34 -04001208 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1209 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1210 FIFO_SEGMENT_RX_FREELIST);
1211 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1212 FIFO_SEGMENT_TX_FREELIST);
1213 svm_fifo_segment_delete (seg);
1214
1215 return rv;
1216}
1217
1218static int
1219vppcom_session_unbind (u32 session_index)
1220{
Dave Wallace543852a2017-08-03 02:11:34 -04001221 int rv;
1222
1223 clib_spinlock_lock (&vcm->sessions_lockp);
1224 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1225 {
1226 clib_spinlock_unlock (&vcm->sessions_lockp);
1227 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001228 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001229 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001230 return VPPCOM_EBADFD;
1231 }
1232 clib_spinlock_unlock (&vcm->sessions_lockp);
1233
1234 vcm->bind_session_index = session_index;
1235 vppcom_send_unbind_sock (session_index);
1236 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1237 vcm->cfg.session_timeout);
1238 if (PREDICT_FALSE (rv))
1239 {
1240 vcm->bind_session_index = ~0;
1241 if (VPPCOM_DEBUG > 0)
1242 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001243 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001244 return rv;
1245 }
1246 return VPPCOM_OK;
1247}
1248
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001249static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001250vppcom_session_disconnect (u32 session_index)
1251{
Dave Wallace543852a2017-08-03 02:11:34 -04001252 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001253 session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001254
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001255 clib_spinlock_lock (&vcm->sessions_lockp);
1256 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04001257 if (PREDICT_FALSE (rv))
1258 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001259 clib_spinlock_unlock (&vcm->sessions_lockp);
1260 if (VPPCOM_DEBUG > 1)
1261 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001262 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001263 return rv;
1264 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001265
1266 if (!session->is_cut_thru)
1267 {
1268 vppcom_send_disconnect (session);
1269 clib_spinlock_unlock (&vcm->sessions_lockp);
1270
1271 rv = vppcom_wait_for_session_state_change (session_index,
1272 STATE_DISCONNECT, 1.0);
1273 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1274 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001275 getpid (), session_index, vppcom_retval_str (rv), rv);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001276 }
1277 else
1278 clib_spinlock_unlock (&vcm->sessions_lockp);
1279
Dave Wallace543852a2017-08-03 02:11:34 -04001280 return VPPCOM_OK;
1281}
1282
1283#define foreach_sock_msg \
1284_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1285_(BIND_SOCK_REPLY, bind_sock_reply) \
1286_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1287_(ACCEPT_SESSION, accept_session) \
1288_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001289_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001290_(DISCONNECT_SESSION, disconnect_session) \
1291_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1292_(RESET_SESSION, reset_session) \
1293_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1294_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1295_(MAP_ANOTHER_SEGMENT, map_another_segment)
1296
1297static void
1298vppcom_api_hookup (void)
1299{
1300#define _(N,n) \
1301 vl_msg_api_set_handlers(VL_API_##N, #n, \
1302 vl_api_##n##_t_handler, \
1303 vl_noop_handler, \
1304 vl_api_##n##_t_endian, \
1305 vl_api_##n##_t_print, \
1306 sizeof(vl_api_##n##_t), 1);
1307 foreach_sock_msg;
1308#undef _
1309}
1310
1311static void
1312vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1313{
1314 ASSERT (vcl_cfg);
1315
1316 vcl_cfg->heapsize = (256ULL << 20);
1317 vcl_cfg->segment_baseva = 0x200000000ULL;
1318 vcl_cfg->segment_size = (256 << 20);
1319 vcl_cfg->add_segment_size = (128 << 20);
1320 vcl_cfg->preallocated_fifo_pairs = 8;
1321 vcl_cfg->rx_fifo_size = (1 << 20);
1322 vcl_cfg->tx_fifo_size = (1 << 20);
1323 vcl_cfg->event_queue_size = 2048;
1324 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1325 vcl_cfg->app_timeout = 10 * 60.0;
1326 vcl_cfg->session_timeout = 10 * 60.0;
1327 vcl_cfg->accept_timeout = 60.0;
1328}
1329
1330static void
1331vppcom_cfg_heapsize (char *conf_fname)
1332{
Dave Wallace543852a2017-08-03 02:11:34 -04001333 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1334 FILE *fp;
1335 char inbuf[4096];
1336 int argc = 1;
1337 char **argv = NULL;
1338 char *arg = NULL;
1339 char *p;
1340 int i;
1341 u8 *sizep;
1342 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001343 void *vcl_mem;
1344 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001345
1346 fp = fopen (conf_fname, "r");
1347 if (fp == NULL)
1348 {
1349 if (VPPCOM_DEBUG > 0)
1350 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1351 goto defaulted;
1352 }
1353 argv = calloc (1, sizeof (char *));
1354 if (argv == NULL)
1355 goto defaulted;
1356
1357 while (1)
1358 {
1359 if (fgets (inbuf, 4096, fp) == 0)
1360 break;
1361 p = strtok (inbuf, " \t\n");
1362 while (p != NULL)
1363 {
1364 if (*p == '#')
1365 break;
1366 argc++;
1367 char **tmp = realloc (argv, argc * sizeof (char *));
1368 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001369 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001370 argv = tmp;
1371 arg = strndup (p, 1024);
1372 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001373 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001374 argv[argc - 1] = arg;
1375 p = strtok (NULL, " \t\n");
1376 }
1377 }
1378
1379 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001380 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001381
1382 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1383 if (tmp == NULL)
1384 goto defaulted;
1385 argv = tmp;
1386 argv[argc] = NULL;
1387
1388 /*
1389 * Look for and parse the "heapsize" config parameter.
1390 * Manual since none of the clib infra has been bootstrapped yet.
1391 *
1392 * Format: heapsize <nn>[mM][gG]
1393 */
1394
1395 for (i = 1; i < (argc - 1); i++)
1396 {
1397 if (!strncmp (argv[i], "heapsize", 8))
1398 {
1399 sizep = (u8 *) argv[i + 1];
1400 size = 0;
1401 while (*sizep >= '0' && *sizep <= '9')
1402 {
1403 size *= 10;
1404 size += *sizep++ - '0';
1405 }
1406 if (size == 0)
1407 {
1408 if (VPPCOM_DEBUG > 0)
1409 clib_warning ("[%d] parse error '%s %s', "
1410 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001411 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001412 vcl_cfg->heapsize, vcl_cfg->heapsize);
1413 goto defaulted;
1414 }
1415
1416 if (*sizep == 'g' || *sizep == 'G')
1417 vcl_cfg->heapsize = size << 30;
1418 else if (*sizep == 'm' || *sizep == 'M')
1419 vcl_cfg->heapsize = size << 20;
1420 else
1421 {
1422 if (VPPCOM_DEBUG > 0)
1423 clib_warning ("[%d] parse error '%s %s', "
1424 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001425 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001426 vcl_cfg->heapsize, vcl_cfg->heapsize);
1427 goto defaulted;
1428 }
1429 }
1430 }
1431
1432defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001433 if (fp != NULL)
1434 fclose (fp);
1435 if (argv != NULL)
1436 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001437
Dave Wallace2e005bb2017-11-07 01:21:39 -05001438 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1439 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001440 if (vcl_mem <= 0)
1441 {
1442 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1443 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1444 "-1, 0) failed!",
1445 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1446 return;
1447 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001448 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1449 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001450 {
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001451 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1452 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001453 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001454 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1455 if (!vcl_mem)
1456 {
1457 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1458 return;
1459 }
1460
1461 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1462 vcm = vcl_mem;
1463
1464 if (VPPCOM_DEBUG > 0)
1465 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1466 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001467}
1468
1469static void
1470vppcom_cfg_read (char *conf_fname)
1471{
Dave Wallace543852a2017-08-03 02:11:34 -04001472 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1473 int fd;
1474 unformat_input_t _input, *input = &_input;
1475 unformat_input_t _line_input, *line_input = &_line_input;
1476 u8 vc_cfg_input = 0;
1477 u8 *chroot_path;
1478 struct stat s;
1479 u32 uid, gid;
1480
1481 fd = open (conf_fname, O_RDONLY);
1482 if (fd < 0)
1483 {
1484 if (VPPCOM_DEBUG > 0)
1485 clib_warning ("[%d] open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001486 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001487 goto file_done;
1488 }
1489
1490 if (fstat (fd, &s) < 0)
1491 {
1492 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001493 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001494 goto file_done;
1495 }
1496
1497 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1498 {
1499 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001500 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001501 goto file_done;
1502 }
1503
Dave Barach59b25652017-09-10 15:04:27 -04001504 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001505
1506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1507 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001508 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001509 unformat_skip_white_space (line_input);
1510
Dave Wallace8af20542017-10-26 03:29:30 -04001511 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001512 {
1513 vc_cfg_input = 1;
1514 continue;
1515 }
1516
1517 if (vc_cfg_input)
1518 {
1519 if (unformat (line_input, "heapsize %s", &chroot_path))
1520 {
1521 vec_terminate_c_string (chroot_path);
1522 if (VPPCOM_DEBUG > 0)
1523 clib_warning ("[%d] configured heapsize %s, "
1524 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001525 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001526 vcl_cfg->heapsize);
1527 vec_free (chroot_path);
1528 }
1529 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1530 {
1531 vec_terminate_c_string (chroot_path);
1532 vl_set_memory_root_path ((char *) chroot_path);
1533 if (VPPCOM_DEBUG > 0)
1534 clib_warning ("[%d] configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001535 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001536 chroot_path = 0; /* Don't vec_free() it! */
1537 }
1538 else if (unformat (line_input, "uid %d", &uid))
1539 {
1540 vl_set_memory_uid (uid);
1541 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001542 clib_warning ("[%d] configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001543 }
1544 else if (unformat (line_input, "gid %d", &gid))
1545 {
1546 vl_set_memory_gid (gid);
1547 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001548 clib_warning ("[%d] configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001549 }
Dave Wallace8af20542017-10-26 03:29:30 -04001550 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001551 &vcl_cfg->segment_baseva))
1552 {
1553 if (VPPCOM_DEBUG > 0)
Dave Wallace8af20542017-10-26 03:29:30 -04001554 clib_warning ("[%d] configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001555 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001556 }
1557 else if (unformat (line_input, "segment-size 0x%lx",
1558 &vcl_cfg->segment_size))
1559 {
1560 if (VPPCOM_DEBUG > 0)
1561 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001562 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001563 vcl_cfg->segment_size);
1564 }
1565 else if (unformat (line_input, "segment-size %ld",
1566 &vcl_cfg->segment_size))
1567 {
1568 if (VPPCOM_DEBUG > 0)
1569 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001570 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001571 vcl_cfg->segment_size);
1572 }
1573 else if (unformat (line_input, "add-segment-size 0x%lx",
1574 &vcl_cfg->add_segment_size))
1575 {
1576 if (VPPCOM_DEBUG > 0)
1577 clib_warning
1578 ("[%d] configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001579 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001580 vcl_cfg->add_segment_size);
1581 }
1582 else if (unformat (line_input, "add-segment-size %ld",
1583 &vcl_cfg->add_segment_size))
1584 {
1585 if (VPPCOM_DEBUG > 0)
1586 clib_warning
1587 ("[%d] configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001588 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001589 vcl_cfg->add_segment_size);
1590 }
1591 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1592 &vcl_cfg->preallocated_fifo_pairs))
1593 {
1594 if (VPPCOM_DEBUG > 0)
1595 clib_warning ("[%d] configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001596 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001597 vcl_cfg->preallocated_fifo_pairs,
1598 vcl_cfg->preallocated_fifo_pairs);
1599 }
1600 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1601 &vcl_cfg->rx_fifo_size))
1602 {
1603 if (VPPCOM_DEBUG > 0)
1604 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001605 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001606 vcl_cfg->rx_fifo_size);
1607 }
1608 else if (unformat (line_input, "rx-fifo-size %ld",
1609 &vcl_cfg->rx_fifo_size))
1610 {
1611 if (VPPCOM_DEBUG > 0)
1612 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001613 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001614 vcl_cfg->rx_fifo_size);
1615 }
1616 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1617 &vcl_cfg->tx_fifo_size))
1618 {
1619 if (VPPCOM_DEBUG > 0)
1620 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001621 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001622 vcl_cfg->tx_fifo_size);
1623 }
1624 else if (unformat (line_input, "tx-fifo-size %ld",
1625 &vcl_cfg->tx_fifo_size))
1626 {
1627 if (VPPCOM_DEBUG > 0)
1628 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001629 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001630 vcl_cfg->tx_fifo_size);
1631 }
1632 else if (unformat (line_input, "event-queue-size 0x%lx",
1633 &vcl_cfg->event_queue_size))
1634 {
1635 if (VPPCOM_DEBUG > 0)
1636 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001637 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001638 vcl_cfg->event_queue_size);
1639 }
1640 else if (unformat (line_input, "event-queue-size %ld",
1641 &vcl_cfg->event_queue_size))
1642 {
1643 if (VPPCOM_DEBUG > 0)
1644 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001645 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001646 vcl_cfg->event_queue_size);
1647 }
1648 else if (unformat (line_input, "listen-queue-size 0x%lx",
1649 &vcl_cfg->listen_queue_size))
1650 {
1651 if (VPPCOM_DEBUG > 0)
1652 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001653 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001654 vcl_cfg->listen_queue_size);
1655 }
1656 else if (unformat (line_input, "listen-queue-size %ld",
1657 &vcl_cfg->listen_queue_size))
1658 {
1659 if (VPPCOM_DEBUG > 0)
1660 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001661 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001662 vcl_cfg->listen_queue_size);
1663 }
1664 else if (unformat (line_input, "app-timeout %f",
1665 &vcl_cfg->app_timeout))
1666 {
1667 if (VPPCOM_DEBUG > 0)
1668 clib_warning ("[%d] configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001669 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001670 }
1671 else if (unformat (line_input, "session-timeout %f",
1672 &vcl_cfg->session_timeout))
1673 {
1674 if (VPPCOM_DEBUG > 0)
1675 clib_warning ("[%d] configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001676 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001677 }
1678 else if (unformat (line_input, "accept-timeout %f",
1679 &vcl_cfg->accept_timeout))
1680 {
1681 if (VPPCOM_DEBUG > 0)
1682 clib_warning ("[%d] configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001683 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001684 }
Dave Wallace774169b2017-11-01 20:07:40 -04001685 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001686 {
Dave Wallace774169b2017-11-01 20:07:40 -04001687 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001688 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001689 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001690 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001691 }
Dave Wallace774169b2017-11-01 20:07:40 -04001692 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001693 {
Dave Wallace774169b2017-11-01 20:07:40 -04001694 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001695 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001696 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001697 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001698 }
Dave Wallace774169b2017-11-01 20:07:40 -04001699 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001700 {
Dave Wallace774169b2017-11-01 20:07:40 -04001701 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001702 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001703 clib_warning ("[%d] configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001704 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001705 }
1706 else if (unformat (line_input, "app-scope-global"))
1707 {
1708 vcl_cfg->app_scope_global = 1;
1709 if (VPPCOM_DEBUG > 0)
1710 clib_warning ("[%d] configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001711 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001712 }
1713 else if (unformat (line_input, "namespace-secret %lu",
1714 &vcl_cfg->namespace_secret))
1715 {
1716 if (VPPCOM_DEBUG > 0)
1717 clib_warning
1718 ("[%d] configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001719 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04001720 vcl_cfg->namespace_secret);
1721 }
1722 else if (unformat (line_input, "namespace-id %v",
1723 &vcl_cfg->namespace_id))
1724 {
1725 vl_api_application_attach_t *mp;
1726 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1727 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1728 if (nsid_vec_len > max_nsid_vec_len)
1729 {
1730 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1731 if (VPPCOM_DEBUG > 0)
1732 clib_warning ("[%d] configured namespace_id is too long,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05001733 " truncated to %d characters!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001734 max_nsid_vec_len);
1735 }
1736
1737 if (VPPCOM_DEBUG > 0)
1738 clib_warning ("[%d] configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001739 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001740 }
Dave Wallace543852a2017-08-03 02:11:34 -04001741 else if (unformat (line_input, "}"))
1742 {
1743 vc_cfg_input = 0;
1744 if (VPPCOM_DEBUG > 0)
1745 clib_warning ("[%d] completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001746 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001747 goto input_done;
1748 }
1749 else
1750 {
1751 if (line_input->buffer[line_input->index] != '#')
1752 {
1753 clib_warning ("[%d] Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001754 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001755 &line_input->buffer[line_input->index]);
1756 }
1757 }
1758 }
1759 }
1760
1761input_done:
1762 unformat_free (input);
1763
1764file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001765 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001766 close (fd);
1767}
1768
1769/*
1770 * VPPCOM Public API functions
1771 */
1772int
1773vppcom_app_create (char *app_name)
1774{
Dave Wallace543852a2017-08-03 02:11:34 -04001775 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1776 u8 *heap;
1777 mheap_t *h;
1778 int rv;
1779
1780 if (!vcm->init)
1781 {
1782 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001783 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001784
1785 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001786 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001787 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1788 if (env_var_str)
1789 {
1790 u32 tmp;
1791 if (sscanf (env_var_str, "%u", &tmp) != 1)
1792 clib_warning ("[%d] Invalid debug level specified in "
1793 "the environment variable "
1794 VPPCOM_ENV_DEBUG
1795 " (%s)!\n", getpid (), env_var_str);
1796 else
1797 {
1798 vcm->debug = tmp;
1799 clib_warning ("[%d] configured debug level (%u) from "
1800 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1801 }
1802 }
Dave Wallace8af20542017-10-26 03:29:30 -04001803 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001804 if (!conf_fname)
1805 {
1806 conf_fname = VPPCOM_CONF_DEFAULT;
1807 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001808 clib_warning ("[%d] getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001809 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001810 }
1811 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001812 clib_fifo_validate (vcm->client_session_index_fifo,
1813 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001814 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04001815 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1816 if (env_var_str)
1817 {
1818 u32 ns_id_vec_len = strlen (env_var_str);
1819
1820 vec_reset_length (vcm->cfg.namespace_id);
1821 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1822 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1823
1824 if (VPPCOM_DEBUG > 0)
1825 clib_warning ("[%d] configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001826 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001827 vcm->cfg.namespace_id);
1828 }
1829 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1830 if (env_var_str)
1831 {
1832 u64 tmp;
1833 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1834 clib_warning ("[%d] Invalid namespace secret specified in "
1835 "the environment variable "
1836 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001837 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001838 else
1839 {
1840 vcm->cfg.namespace_secret = tmp;
1841 if (VPPCOM_DEBUG > 0)
1842 clib_warning ("[%d] configured namespace secret (%lu) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001843 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001844 vcm->cfg.namespace_secret);
1845 }
1846 }
Dave Wallace774169b2017-11-01 20:07:40 -04001847 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001848 {
Dave Wallace774169b2017-11-01 20:07:40 -04001849 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001850 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001851 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001852 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001853 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001854 }
Dave Wallace774169b2017-11-01 20:07:40 -04001855 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001856 {
Dave Wallace774169b2017-11-01 20:07:40 -04001857 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001858 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001859 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001860 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001861 vcm->cfg.app_proxy_transport_udp);
1862 }
1863 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1864 {
1865 vcm->cfg.app_scope_local = 1;
1866 if (VPPCOM_DEBUG > 0)
1867 clib_warning ("[%d] configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001868 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001869 vcm->cfg.app_scope_local);
1870 }
1871 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1872 {
1873 vcm->cfg.app_scope_global = 1;
1874 if (VPPCOM_DEBUG > 0)
1875 clib_warning ("[%d] configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001876 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001877 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001878 }
1879
Dave Wallace543852a2017-08-03 02:11:34 -04001880 vcm->bind_session_index = ~0;
1881 vcm->main_cpu = os_get_thread_index ();
1882 heap = clib_mem_get_per_cpu_heap ();
1883 h = mheap_header (heap);
1884
1885 /* make the main heap thread-safe */
1886 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1887
1888 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1889
1890 clib_time_init (&vcm->clib_time);
1891 vppcom_init_error_string_table ();
1892 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1893 20 /* timeout in secs */ );
1894 clib_spinlock_init (&vcm->sessions_lockp);
1895 vppcom_api_hookup ();
1896 }
1897
1898 if (vcm->my_client_index == ~0)
1899 {
1900 vcm->app_state = STATE_APP_START;
1901 rv = vppcom_connect_to_vpp (app_name);
1902 if (rv)
1903 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001904 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001905 return rv;
1906 }
1907
1908 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001909 clib_warning ("[%d] sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001910
1911 rv = vppcom_app_session_enable ();
1912 if (rv)
1913 {
1914 clib_warning ("[%d] vppcom_app_session_enable() failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001915 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 app attach", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001921
1922 rv = vppcom_app_attach ();
1923 if (rv)
1924 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001925 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001926 return rv;
1927 }
Dave Wallace543852a2017-08-03 02:11:34 -04001928
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001929 if (VPPCOM_DEBUG > 0)
1930 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001931 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001932 vcm->my_client_index);
1933 }
Dave Wallace543852a2017-08-03 02:11:34 -04001934
1935 return VPPCOM_OK;
1936}
1937
1938void
1939vppcom_app_destroy (void)
1940{
Dave Wallace543852a2017-08-03 02:11:34 -04001941 int rv;
1942
1943 if (vcm->my_client_index == ~0)
1944 return;
1945
1946 if (VPPCOM_DEBUG > 0)
1947 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001948 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001949
1950 vppcom_app_detach ();
1951 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1952 if (PREDICT_FALSE (rv))
1953 {
1954 if (VPPCOM_DEBUG > 0)
1955 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001956 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001957 }
1958 vl_client_disconnect_from_vlib ();
1959 vcm->my_client_index = ~0;
1960 vcm->app_state = STATE_APP_START;
1961}
1962
1963int
1964vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1965{
Dave Wallace543852a2017-08-03 02:11:34 -04001966 session_t *session;
1967 u32 session_index;
1968
1969 clib_spinlock_lock (&vcm->sessions_lockp);
1970 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001971 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04001972 session_index = session - vcm->sessions;
1973
1974 session->vrf = vrf;
1975 session->proto = proto;
1976 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001977 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001978 clib_spinlock_unlock (&vcm->sessions_lockp);
1979
1980 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001981 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001982
1983 return (int) session_index;
1984}
1985
1986int
1987vppcom_session_close (uint32_t session_index)
1988{
Dave Wallace543852a2017-08-03 02:11:34 -04001989 session_t *session = 0;
1990 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001991 u8 is_server;
1992 u8 is_listen;
1993 u8 is_cut_thru;
1994 u8 is_vep;
1995 u8 is_vep_session;
1996 u32 next_sid;
1997 u32 vep_idx;
1998 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04001999
2000 clib_spinlock_lock (&vcm->sessions_lockp);
2001 rv = vppcom_session_at_index (session_index, &session);
2002 if (PREDICT_FALSE (rv))
2003 {
Dave Wallace543852a2017-08-03 02:11:34 -04002004 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002005 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002006 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002007 clib_spinlock_unlock (&vcm->sessions_lockp);
2008 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002009 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002010 is_server = session->is_server;
2011 is_listen = session->is_listen;
2012 is_cut_thru = session->is_cut_thru;
2013 is_vep = session->is_vep;
2014 is_vep_session = session->is_vep_session;
2015 next_sid = session->vep.next_sid;
2016 vep_idx = session->vep.vep_idx;
2017 state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04002018 clib_spinlock_unlock (&vcm->sessions_lockp);
2019
2020 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002021 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002022
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002023 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002024 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002025 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002026 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002027 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallace19481612017-09-15 18:47:44 -04002028 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002029 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002030 "rv = %s (%d)", getpid (), vep_idx, next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002031 vppcom_retval_str (rv), rv);
2032
2033 clib_spinlock_lock (&vcm->sessions_lockp);
2034 rv = vppcom_session_at_index (session_index, &session);
2035 if (PREDICT_FALSE (rv))
2036 {
2037 if (VPPCOM_DEBUG > 0)
2038 clib_warning
2039 ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002040 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002041 clib_spinlock_unlock (&vcm->sessions_lockp);
2042 goto done;
2043 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002044 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002045 clib_spinlock_unlock (&vcm->sessions_lockp);
2046 }
2047 }
2048 else
2049 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002050 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002051 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002052 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2053 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2054 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002055 "rv = %s (%d)", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002056 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002057 }
2058
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002059 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002060 {
Dave Wallace60caa062017-11-10 17:07:13 -05002061 rv = vppcom_session_unbind_cut_thru (session, session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002062 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2063 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2064 "rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002065 getpid (), session_index,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002066 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002067 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002068 else if (is_server && is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069 {
2070 rv = vppcom_session_unbind (session_index);
2071 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2072 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002073 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002074 vppcom_retval_str (rv), rv);
2075 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002076 else if (state == STATE_CONNECT)
2077 if (vppcom_session_disconnect (session_index))
2078 goto done;
Dave Wallace19481612017-09-15 18:47:44 -04002079 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002080 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002081 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002082 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002083done:
Dave Wallace543852a2017-08-03 02:11:34 -04002084 return rv;
2085}
2086
2087int
2088vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2089{
Dave Wallace543852a2017-08-03 02:11:34 -04002090 session_t *session = 0;
2091 int rv;
2092
2093 if (!ep || !ep->ip)
2094 return VPPCOM_EINVAL;
2095
2096 clib_spinlock_lock (&vcm->sessions_lockp);
2097 rv = vppcom_session_at_index (session_index, &session);
2098 if (PREDICT_FALSE (rv))
2099 {
2100 clib_spinlock_unlock (&vcm->sessions_lockp);
2101 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002102 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002103 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002104 return rv;
2105 }
2106
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002107 if (session->is_vep)
2108 {
2109 clib_spinlock_unlock (&vcm->sessions_lockp);
2110 if (VPPCOM_DEBUG > 0)
2111 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002112 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002113 return VPPCOM_EBADFD;
2114 }
2115
Dave Wallace543852a2017-08-03 02:11:34 -04002116 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002117 session->lcl_addr.is_ip4 = ep->is_ip4;
2118 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002119 session->lcl_port = ep->port;
2120
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002123 getpid (), session_index, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07002124 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2125 clib_net_to_host_u16 (session->lcl_port));
Dave Wallace543852a2017-08-03 02:11:34 -04002126
2127 clib_spinlock_unlock (&vcm->sessions_lockp);
2128 return VPPCOM_OK;
2129}
2130
2131int
Dave Wallace33e002b2017-09-06 01:20:02 -04002132vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002133{
Dave Wallace33e002b2017-09-06 01:20:02 -04002134 session_t *listen_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002135 int rv;
2136
2137 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002138 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002139 if (PREDICT_FALSE (rv))
2140 {
2141 clib_spinlock_unlock (&vcm->sessions_lockp);
2142 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002143 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002144 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002145 return rv;
2146 }
2147
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002148 if (listen_session->is_vep)
2149 {
2150 clib_spinlock_unlock (&vcm->sessions_lockp);
2151 if (VPPCOM_DEBUG > 0)
2152 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002153 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002154 return VPPCOM_EBADFD;
2155 }
2156
Dave Wallacee695cb42017-11-02 22:04:42 -04002157 if (listen_session->is_listen)
2158 {
2159 clib_spinlock_unlock (&vcm->sessions_lockp);
2160 if (VPPCOM_DEBUG > 0)
2161 clib_warning ("[%d] sid (%u) is already in listen state!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002162 getpid (), listen_session_index);
Dave Wallacee695cb42017-11-02 22:04:42 -04002163 return VPPCOM_OK;
2164 }
2165
Dave Wallace543852a2017-08-03 02:11:34 -04002166 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002167 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002168
2169 ASSERT (vcm->bind_session_index == ~0);
Dave Wallace33e002b2017-09-06 01:20:02 -04002170 vcm->bind_session_index = listen_session_index;
2171 vppcom_send_bind_sock (listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002172 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002173 rv =
2174 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2175 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002176 if (PREDICT_FALSE (rv))
2177 {
2178 vcm->bind_session_index = ~0;
2179 if (VPPCOM_DEBUG > 0)
2180 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002181 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002182 return rv;
2183 }
2184
2185 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002186 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002187 if (PREDICT_FALSE (rv))
2188 {
2189 clib_spinlock_unlock (&vcm->sessions_lockp);
2190 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002191 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002192 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002193 return rv;
2194 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002195 listen_session->is_listen = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002196 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002198
2199 return VPPCOM_OK;
2200}
2201
2202int
2203vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2204 double wait_for_time)
2205{
Dave Wallace33e002b2017-09-06 01:20:02 -04002206 session_t *listen_session = 0;
2207 session_t *client_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002208 u32 client_session_index;
2209 int rv;
2210 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002211 char *cut_thru_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002212
2213 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002214 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002215 if (PREDICT_FALSE (rv))
2216 {
2217 clib_spinlock_unlock (&vcm->sessions_lockp);
2218 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002219 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002220 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002221 return rv;
2222 }
2223
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002224 if (listen_session->is_vep)
2225 {
2226 clib_spinlock_unlock (&vcm->sessions_lockp);
2227 if (VPPCOM_DEBUG > 0)
2228 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002229 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002230 return VPPCOM_EBADFD;
2231 }
2232
Dave Wallace33e002b2017-09-06 01:20:02 -04002233 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002234 {
2235 clib_spinlock_unlock (&vcm->sessions_lockp);
2236 if (VPPCOM_DEBUG > 0)
2237 clib_warning ("[%d] session not in listen state, state = %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002238 getpid (),
Dave Wallace33e002b2017-09-06 01:20:02 -04002239 vppcom_session_state_str (listen_session->state));
Dave Wallace543852a2017-08-03 02:11:34 -04002240 return VPPCOM_EBADFD;
2241 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002242 wait_for = listen_session->is_nonblocking ? 0 :
Dave Wallace543852a2017-08-03 02:11:34 -04002243 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2244
2245 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002246 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002247 listen_session_index,
Dave Wallace33e002b2017-09-06 01:20:02 -04002248 vppcom_session_state_str (listen_session->state),
2249 listen_session->state);
Dave Wallace543852a2017-08-03 02:11:34 -04002250 clib_spinlock_unlock (&vcm->sessions_lockp);
2251
2252 while (1)
2253 {
2254 rv = vppcom_wait_for_client_session_index (wait_for);
2255 if (rv)
2256 {
2257 if ((VPPCOM_DEBUG > 0))
2258 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002259 getpid (), listen_session_index,
Dave Wallace543852a2017-08-03 02:11:34 -04002260 vppcom_retval_str (rv), rv);
2261 if ((wait_for == 0) || (wait_for_time > 0))
2262 return rv;
2263 }
2264 else
2265 break;
2266 }
2267
Dave Wallace543852a2017-08-03 02:11:34 -04002268 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002269 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002270 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002271 ASSERT (rv == VPPCOM_OK);
Dave Wallace35830af2017-10-09 01:43:42 -04002272 ASSERT (client_session->peer_addr.is_ip4 ==
2273 listen_session->lcl_addr.is_ip4);
Dave Wallace543852a2017-08-03 02:11:34 -04002274
2275 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002276 clib_warning ("[%d] Got a request: client sid %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002277 client_session_index);
2278
Dave Wallace33e002b2017-09-06 01:20:02 -04002279 ep->vrf = client_session->vrf;
2280 ep->is_cut_thru = client_session->is_cut_thru;
Dave Wallace35830af2017-10-09 01:43:42 -04002281 ep->is_ip4 = client_session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07002282 ep->port = client_session->peer_port;
Dave Wallace35830af2017-10-09 01:43:42 -04002283 if (client_session->peer_addr.is_ip4)
2284 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2285 sizeof (ip4_address_t));
Dave Wallace33e002b2017-09-06 01:20:02 -04002286 else
Dave Wallace35830af2017-10-09 01:43:42 -04002287 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2288 sizeof (ip6_address_t));
Dave Wallace60caa062017-11-10 17:07:13 -05002289
2290 if (client_session->is_server && client_session->is_cut_thru)
2291 {
2292 static svm_fifo_segment_create_args_t _a;
2293 svm_fifo_segment_create_args_t *a = &_a;
2294 svm_fifo_segment_private_t *seg;
2295
2296 cut_thru_str = "cut-thru ";
2297
2298 /* Create the segment */
2299 memset (a, 0, sizeof (*a));
2300 a->segment_name = (char *)
2301 format ((u8 *) a->segment_name, "%d:segment%d%c",
2302 getpid (), vcm->unique_segment_index++, 0);
2303 a->segment_size = vcm->cfg.segment_size;
2304 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2305 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2306 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2307
2308 rv = svm_fifo_segment_create (a);
2309 if (PREDICT_FALSE (rv))
2310 {
2311 if (VPPCOM_DEBUG > 1)
2312 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2313 getpid (), a->segment_name);
2314 vec_reset_length (a->new_segment_indices);
2315 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2316 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2317 clib_spinlock_unlock (&vcm->sessions_lockp);
2318 return VPPCOM_ENOMEM;
2319 }
2320
2321 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2322 client_session->sm_seg_index = a->new_segment_indices[0];
2323 vec_free (a->new_segment_indices);
2324
2325 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2326 client_session->server_rx_fifo =
2327 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2328 FIFO_SEGMENT_RX_FREELIST);
2329 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2330 {
2331 svm_fifo_segment_delete (seg);
2332 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2333 getpid (), vcm->cfg.rx_fifo_size,
2334 vcm->cfg.rx_fifo_size);
2335 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2336 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2337 clib_spinlock_unlock (&vcm->sessions_lockp);
2338 return VPPCOM_ENOMEM;
2339 }
2340 client_session->server_rx_fifo->master_session_index =
2341 client_session_index;
2342
2343 client_session->server_tx_fifo =
2344 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2345 FIFO_SEGMENT_TX_FREELIST);
2346 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2347 {
2348 svm_fifo_segment_delete (seg);
2349 if (VPPCOM_DEBUG > 1)
2350 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2351 getpid (), vcm->cfg.tx_fifo_size,
2352 vcm->cfg.tx_fifo_size);
2353 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2354 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2355 clib_spinlock_unlock (&vcm->sessions_lockp);
2356 return VPPCOM_ENOMEM;
2357 }
2358 client_session->server_tx_fifo->master_session_index =
2359 client_session_index;
2360
2361 if (VPPCOM_DEBUG > 1)
2362 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2363 getpid (), client_session->segment_name,
2364 client_session->server_rx_fifo,
2365 client_session->server_tx_fifo);
2366
2367#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2368 {
2369 void *oldheap;
2370 ssvm_shared_header_t *sh = seg->ssvm.sh;
2371
2372 ssvm_lock_non_recursive (sh, 1);
2373 oldheap = ssvm_push_heap (sh);
2374 event_q = client_session->vpp_event_queue =
2375 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2376 sizeof (session_fifo_event_t),
2377 getpid (), 0 /* signal not sent */ );
2378 ssvm_pop_heap (oldheap);
2379 ssvm_unlock_non_recursive (sh);
2380 }
2381#endif
2382 vppcom_send_connect_session_reply (client_session, 0, 0, 0);
2383 }
2384 else
2385 {
2386 cut_thru_str = " ";
2387 vppcom_send_accept_session_reply (0, client_session->vpp_handle);
2388 }
2389
Stevenac1f96d2017-10-24 16:03:58 -07002390 if (VPPCOM_DEBUG > 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002391 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2392 "%U peer port %u",
2393 getpid (), client_session_index, cut_thru_str,
2394 format_ip46_address, &client_session->peer_addr.ip46,
Stevenac1f96d2017-10-24 16:03:58 -07002395 client_session->peer_addr.is_ip4,
2396 clib_net_to_host_u16 (client_session->peer_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002397
Dave Wallace543852a2017-08-03 02:11:34 -04002398 clib_spinlock_unlock (&vcm->sessions_lockp);
2399 return (int) client_session_index;
2400}
2401
2402int
2403vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2404{
Dave Wallace543852a2017-08-03 02:11:34 -04002405 session_t *session = 0;
2406 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002407
2408 clib_spinlock_lock (&vcm->sessions_lockp);
2409 rv = vppcom_session_at_index (session_index, &session);
2410 if (PREDICT_FALSE (rv))
2411 {
2412 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002414 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002415 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002416 return rv;
2417 }
2418
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002419 if (session->is_vep)
2420 {
2421 clib_spinlock_unlock (&vcm->sessions_lockp);
2422 if (VPPCOM_DEBUG > 0)
2423 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002424 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002425 return VPPCOM_EBADFD;
2426 }
2427
Dave Wallace543852a2017-08-03 02:11:34 -04002428 if (session->state == STATE_CONNECT)
2429 {
2430 clib_spinlock_unlock (&vcm->sessions_lockp);
2431 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002432 clib_warning ("[%d] session, sid (%u) already connected!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002433 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002434 return VPPCOM_OK;
2435 }
2436
2437 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002438 session->peer_addr.is_ip4 = server_ep->is_ip4;
2439 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002440 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002441
2442 if (VPPCOM_DEBUG > 0)
2443 {
2444 u8 *ip_str = format (0, "%U", format_ip46_address,
Dave Wallace35830af2017-10-09 01:43:42 -04002445 &session->peer_addr.ip46,
2446 session->peer_addr.is_ip4);
Dave Wallace7876d392017-10-19 03:53:57 -04002447 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002448 getpid (), session_index, ip_str,
Stevenac1f96d2017-10-24 16:03:58 -07002449 clib_net_to_host_u16 (session->peer_port),
Dave Wallace7876d392017-10-19 03:53:57 -04002450 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002451 vec_free (ip_str);
2452 }
2453
2454 vppcom_send_connect_sock (session, session_index);
2455 clib_spinlock_unlock (&vcm->sessions_lockp);
2456 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2457 vcm->cfg.session_timeout);
2458 if (PREDICT_FALSE (rv))
2459 {
2460 if (VPPCOM_DEBUG > 0)
2461 clib_warning ("[%d] connect timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002462 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002463 return rv;
2464 }
Dave Wallace7876d392017-10-19 03:53:57 -04002465 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002466 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04002467
Dave Wallace543852a2017-08-03 02:11:34 -04002468 return VPPCOM_OK;
2469}
2470
Steven58f464e2017-10-25 12:33:12 -07002471static inline int
2472vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2473 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002474{
Dave Wallace543852a2017-08-03 02:11:34 -04002475 session_t *session = 0;
2476 svm_fifo_t *rx_fifo;
2477 int n_read = 0;
2478 int rv;
2479 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002480 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002481
2482 ASSERT (buf);
2483
2484 clib_spinlock_lock (&vcm->sessions_lockp);
2485 rv = vppcom_session_at_index (session_index, &session);
2486 if (PREDICT_FALSE (rv))
2487 {
2488 clib_spinlock_unlock (&vcm->sessions_lockp);
2489 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002490 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002491 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002492 return rv;
2493 }
2494
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002495 if (session->is_vep)
2496 {
2497 clib_spinlock_unlock (&vcm->sessions_lockp);
2498 if (VPPCOM_DEBUG > 0)
2499 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002500 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002501 return VPPCOM_EBADFD;
2502 }
2503
Dave Wallace33e002b2017-09-06 01:20:02 -04002504 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002505 {
Dave Wallace543852a2017-08-03 02:11:34 -04002506 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002507 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002508 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002509 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002510 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002511 }
Dave Wallace543852a2017-08-03 02:11:34 -04002512
Dave Wallace33e002b2017-09-06 01:20:02 -04002513 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2514 session->server_rx_fifo : session->server_tx_fifo);
2515 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2516 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace60caa062017-11-10 17:07:13 -05002517 poll_et =
2518 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002519 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002520
2521 do
2522 {
Steven58f464e2017-10-25 12:33:12 -07002523 if (peek)
2524 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2525 else
2526 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002527 }
2528 while (!session->is_nonblocking && (n_read <= 0));
2529
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002530 if (poll_et && (n_read <= 0))
2531 {
2532 clib_spinlock_lock (&vcm->sessions_lockp);
2533 session->vep.et_mask |= EPOLLIN;
2534 clib_spinlock_unlock (&vcm->sessions_lockp);
2535 }
Dave Wallace543852a2017-08-03 02:11:34 -04002536
Dave Wallacef7f809c2017-10-03 01:48:42 -04002537 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
Dave Wallace2e005bb2017-11-07 01:21:39 -05002538 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002539 session_index, n_read, fifo_str, rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002540
2541 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002542}
2543
Steven58f464e2017-10-25 12:33:12 -07002544int
2545vppcom_session_read (uint32_t session_index, void *buf, int n)
2546{
2547 return (vppcom_session_read_internal (session_index, buf, n, 0));
2548}
2549
2550static int
2551vppcom_session_peek (uint32_t session_index, void *buf, int n)
2552{
2553 return (vppcom_session_read_internal (session_index, buf, n, 1));
2554}
2555
Dave Wallace543852a2017-08-03 02:11:34 -04002556static inline int
2557vppcom_session_read_ready (session_t * session, u32 session_index)
2558{
Dave Wallace498b3a52017-11-09 13:00:34 -05002559 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002560 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002561 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002562
2563 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002564 if (session->is_vep)
2565 {
2566 clib_spinlock_unlock (&vcm->sessions_lockp);
2567 if (VPPCOM_DEBUG > 0)
2568 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002569 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002570 return VPPCOM_EBADFD;
2571 }
2572
Dave Wallace33e002b2017-09-06 01:20:02 -04002573 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002574 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002575 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002576 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002577 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002578 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002579 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002580
2581 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04002582 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2583 else
2584 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002585 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2586 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002587
Dave Wallace33e002b2017-09-06 01:20:02 -04002588 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002589 }
2590
Dave Wallace60caa062017-11-10 17:07:13 -05002591 poll_et =
2592 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2593 if (poll_et && (ready == 0))
2594 {
2595 if (VPPCOM_DEBUG > 11)
2596 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2597 session_index, session->vep.et_mask);
2598 session->vep.et_mask |= EPOLLIN;
2599 if (VPPCOM_DEBUG > 11)
2600 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2601 session_index, session->vep.et_mask);
2602 }
2603
2604 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2605 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2606 "et_mask 0x%x",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002607 getpid (), session_index, session->is_listen,
Dave Wallace543852a2017-08-03 02:11:34 -04002608 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
Dave Wallace60caa062017-11-10 17:07:13 -05002609 rx_fifo, ready, session->vep.et_mask);
Dave Wallace543852a2017-08-03 02:11:34 -04002610 return ready;
2611}
2612
2613int
2614vppcom_session_write (uint32_t session_index, void *buf, int n)
2615{
Dave Wallace543852a2017-08-03 02:11:34 -04002616 session_t *session = 0;
2617 svm_fifo_t *tx_fifo;
2618 unix_shared_memory_queue_t *q;
2619 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002620 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002621 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002622 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002623
2624 ASSERT (buf);
2625
2626 clib_spinlock_lock (&vcm->sessions_lockp);
2627 rv = vppcom_session_at_index (session_index, &session);
2628 if (PREDICT_FALSE (rv))
2629 {
2630 clib_spinlock_unlock (&vcm->sessions_lockp);
2631 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002633 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002634 return rv;
2635 }
2636
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002637 if (session->is_vep)
2638 {
2639 clib_spinlock_unlock (&vcm->sessions_lockp);
2640 if (VPPCOM_DEBUG > 0)
2641 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002642 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002643 return VPPCOM_EBADFD;
2644 }
2645
Dave Wallace33e002b2017-09-06 01:20:02 -04002646 if (session->state == STATE_DISCONNECT)
2647 {
2648 clib_spinlock_unlock (&vcm->sessions_lockp);
2649 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002650 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002651 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002652 return VPPCOM_ECONNRESET;
2653 }
2654
Dave Wallace543852a2017-08-03 02:11:34 -04002655 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2656 session->server_tx_fifo : session->server_rx_fifo);
2657 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2658 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002659 q = session->vpp_event_queue;
Dave Wallace60caa062017-11-10 17:07:13 -05002660 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2661 (EPOLLET | EPOLLOUT));
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002662 clib_spinlock_unlock (&vcm->sessions_lockp);
2663
Dave Wallace543852a2017-08-03 02:11:34 -04002664 do
2665 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002666 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002667 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 while (!session->is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002669
2670 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002672 {
2673 int rval;
2674
2675 /* Fabricate TX event, send to vpp */
2676 evt.fifo = tx_fifo;
2677 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002678
Dave Wallace543852a2017-08-03 02:11:34 -04002679 rval = vppcom_session_at_index (session_index, &session);
2680 if (PREDICT_FALSE (rval))
2681 {
Dave Wallace543852a2017-08-03 02:11:34 -04002682 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002683 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002684 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002685 return rval;
2686 }
Dave Wallace543852a2017-08-03 02:11:34 -04002687 ASSERT (q);
2688 unix_shared_memory_queue_add (q, (u8 *) & evt,
2689 0 /* do wait for mutex */ );
2690 }
2691
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002692 if (poll_et && (n_write <= 0))
2693 {
2694 clib_spinlock_lock (&vcm->sessions_lockp);
2695 session->vep.et_mask |= EPOLLOUT;
2696 clib_spinlock_unlock (&vcm->sessions_lockp);
2697 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698
Dave Wallace543852a2017-08-03 02:11:34 -04002699 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002700 {
2701 if (n_write == -2)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002702 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002703 session_index, fifo_str, tx_fifo);
2704 else
Dave Wallace2e005bb2017-11-07 01:21:39 -05002705 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002706 session_index, n_write, fifo_str, tx_fifo);
2707 }
2708 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002709}
2710
2711static inline int
2712vppcom_session_write_ready (session_t * session, u32 session_index)
2713{
Dave Wallace543852a2017-08-03 02:11:34 -04002714 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04002715 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002716 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002717 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002718
2719 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002720 if (session->is_vep)
2721 {
2722 clib_spinlock_unlock (&vcm->sessions_lockp);
2723 if (VPPCOM_DEBUG > 0)
2724 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002725 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002726 return VPPCOM_EBADFD;
2727 }
2728
Dave Wallace33e002b2017-09-06 01:20:02 -04002729 if (session->state == STATE_DISCONNECT)
2730 {
2731 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002733 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002734 return VPPCOM_ECONNRESET;
2735 }
2736
Dave Wallace543852a2017-08-03 02:11:34 -04002737 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2738 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002739 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2740 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04002741
Dave Wallacef7f809c2017-10-03 01:48:42 -04002742 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002743
Dave Wallace33e002b2017-09-06 01:20:02 -04002744 if (VPPCOM_DEBUG > 3)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002745 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 session_index, fifo_str, tx_fifo, ready);
Dave Wallace60caa062017-11-10 17:07:13 -05002747 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2748 (EPOLLET | EPOLLOUT));
2749 if (poll_et && (ready == 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750 session->vep.et_mask |= EPOLLOUT;
2751
2752 return ready;
Dave Wallace543852a2017-08-03 02:11:34 -04002753}
2754
2755int
2756vppcom_select (unsigned long n_bits, unsigned long *read_map,
2757 unsigned long *write_map, unsigned long *except_map,
2758 double time_to_wait)
2759{
Dave Wallace543852a2017-08-03 02:11:34 -04002760 u32 session_index;
2761 session_t *session = 0;
2762 int rv, bits_set = 0;
2763 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2764 u32 minbits = clib_max (n_bits, BITS (uword));
2765
2766 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2767
Dave Wallace7876d392017-10-19 03:53:57 -04002768 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002769 {
2770 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2771 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2772 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2773 }
Dave Wallace7876d392017-10-19 03:53:57 -04002774 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002775 {
2776 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2777 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2778 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2779 }
Dave Wallace7876d392017-10-19 03:53:57 -04002780 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002781 {
2782 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2783 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2784 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2785 }
2786
2787 do
2788 {
2789 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002790 if (n_bits)
2791 {
2792 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002793 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002794 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2795 ({
2796 clib_spinlock_lock (&vcm->sessions_lockp);
2797 rv = vppcom_session_at_index (session_index, &session);
2798 if (rv < 0)
2799 {
2800 clib_spinlock_unlock (&vcm->sessions_lockp);
2801 if (VPPCOM_DEBUG > 1)
2802 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002803 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002804 session_index);
2805 bits_set = VPPCOM_EBADFD;
2806 goto select_done;
2807 }
2808
2809 rv = vppcom_session_read_ready (session, session_index);
2810 clib_spinlock_unlock (&vcm->sessions_lockp);
2811 if (except_map && vcm->ex_bitmap &&
2812 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2813 (rv < 0))
2814 {
2815 // TBD: clib_warning
2816 clib_bitmap_set_no_check (except_map, session_index, 1);
2817 bits_set++;
2818 }
2819 else if (rv > 0)
2820 {
2821 // TBD: clib_warning
2822 clib_bitmap_set_no_check (read_map, session_index, 1);
2823 bits_set++;
2824 }
2825 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002826 }
2827
Dave Wallacee22aa742017-10-20 12:30:38 -04002828 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002829 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002830 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2831 ({
2832 clib_spinlock_lock (&vcm->sessions_lockp);
2833 rv = vppcom_session_at_index (session_index, &session);
2834 if (rv < 0)
2835 {
2836 clib_spinlock_unlock (&vcm->sessions_lockp);
2837 if (VPPCOM_DEBUG > 0)
2838 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002839 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002840 session_index);
2841 bits_set = VPPCOM_EBADFD;
2842 goto select_done;
2843 }
Dave Wallace543852a2017-08-03 02:11:34 -04002844
Dave Wallacee22aa742017-10-20 12:30:38 -04002845 rv = vppcom_session_write_ready (session, session_index);
2846 clib_spinlock_unlock (&vcm->sessions_lockp);
2847 if (write_map && (rv > 0))
2848 {
2849 // TBD: clib_warning
2850 clib_bitmap_set_no_check (write_map, session_index, 1);
2851 bits_set++;
2852 }
2853 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002854 }
2855
Dave Wallacee22aa742017-10-20 12:30:38 -04002856 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002857 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002858 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2859 ({
2860 clib_spinlock_lock (&vcm->sessions_lockp);
2861 rv = vppcom_session_at_index (session_index, &session);
2862 if (rv < 0)
2863 {
2864 clib_spinlock_unlock (&vcm->sessions_lockp);
2865 if (VPPCOM_DEBUG > 1)
2866 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002867 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002868 session_index);
2869 bits_set = VPPCOM_EBADFD;
2870 goto select_done;
2871 }
Dave Wallace543852a2017-08-03 02:11:34 -04002872
Dave Wallacee22aa742017-10-20 12:30:38 -04002873 rv = vppcom_session_read_ready (session, session_index);
2874 clib_spinlock_unlock (&vcm->sessions_lockp);
2875 if (rv < 0)
2876 {
2877 // TBD: clib_warning
2878 clib_bitmap_set_no_check (except_map, session_index, 1);
2879 bits_set++;
2880 }
2881 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002882 }
Dave Wallacee22aa742017-10-20 12:30:38 -04002883 }
Dave Wallace543852a2017-08-03 02:11:34 -04002884 /* *INDENT-ON* */
2885 }
2886 while (clib_time_now (&vcm->clib_time) < timeout);
2887
2888select_done:
2889 return (bits_set);
2890}
2891
Dave Wallacef7f809c2017-10-03 01:48:42 -04002892static inline void
2893vep_verify_epoll_chain (u32 vep_idx)
2894{
2895 session_t *session;
2896 vppcom_epoll_t *vep;
2897 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05002898 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002899
Dave Wallace498b3a52017-11-09 13:00:34 -05002900 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002901 return;
2902
2903 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2904 rv = vppcom_session_at_index (vep_idx, &session);
2905 if (PREDICT_FALSE (rv))
2906 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002907 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908 goto done;
2909 }
2910 if (PREDICT_FALSE (!session->is_vep))
2911 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002912 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002913 vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002914 goto done;
2915 }
Dave Wallace498b3a52017-11-09 13:00:34 -05002916 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2917 "{\n"
2918 " is_vep = %u\n"
2919 " is_vep_session = %u\n"
2920 " wait_cont_idx = 0x%x (%u)\n"
2921 "}\n", getpid (),
2922 vep_idx, session->is_vep, session->is_vep_session,
2923 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002924 do
2925 {
2926 vep = &session->vep;
Dave Wallacee695cb42017-11-02 22:04:42 -04002927 sid = vep->next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002928 if (sid != ~0)
2929 {
2930 rv = vppcom_session_at_index (sid, &session);
2931 if (PREDICT_FALSE (rv))
2932 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002933 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002934 goto done;
2935 }
2936 if (PREDICT_FALSE (session->is_vep))
Dave Wallace774169b2017-11-01 20:07:40 -04002937 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002938 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002939 else if (PREDICT_FALSE (!session->is_vep_session))
2940 {
Dave Wallace774169b2017-11-01 20:07:40 -04002941 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002942 getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002943 goto done;
2944 }
2945 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
Dave Wallace774169b2017-11-01 20:07:40 -04002946 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002947 "vep_idx (%u)!", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002948 sid, session->vep.vep_idx, vep_idx);
Dave Wallace498b3a52017-11-09 13:00:34 -05002949 if (session->is_vep_session)
2950 {
2951 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2952 "{\n"
2953 " next_sid = 0x%x (%u)\n"
2954 " prev_sid = 0x%x (%u)\n"
2955 " vep_idx = 0x%x (%u)\n"
2956 " ev.events = 0x%x\n"
2957 " ev.data.u64 = 0x%llx\n"
2958 " et_mask = 0x%x\n"
2959 "}\n",
2960 vep_idx, sid, sid,
2961 vep->next_sid, vep->next_sid,
2962 vep->prev_sid, vep->prev_sid,
2963 vep->vep_idx, vep->vep_idx,
2964 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2965 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002966 }
2967 }
2968 while (sid != ~0);
2969
2970done:
Dave Wallace498b3a52017-11-09 13:00:34 -05002971 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002972}
2973
2974int
2975vppcom_epoll_create (void)
2976{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002977 session_t *vep_session;
2978 u32 vep_idx;
2979
2980 clib_spinlock_lock (&vcm->sessions_lockp);
2981 pool_get (vcm->sessions, vep_session);
2982 memset (vep_session, 0, sizeof (*vep_session));
2983 vep_idx = vep_session - vcm->sessions;
2984
2985 vep_session->is_vep = 1;
2986 vep_session->vep.vep_idx = ~0;
2987 vep_session->vep.next_sid = ~0;
2988 vep_session->vep.prev_sid = ~0;
2989 vep_session->wait_cont_idx = ~0;
2990 clib_spinlock_unlock (&vcm->sessions_lockp);
2991
2992 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002993 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002994
2995 return (vep_idx);
2996}
2997
2998int
2999vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3000 struct epoll_event *event)
3001{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003002 session_t *vep_session;
3003 session_t *session;
3004 int rv;
3005
3006 if (vep_idx == session_index)
3007 {
3008 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003009 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003010 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003011 return VPPCOM_EINVAL;
3012 }
3013
3014 clib_spinlock_lock (&vcm->sessions_lockp);
3015 rv = vppcom_session_at_index (vep_idx, &vep_session);
3016 if (PREDICT_FALSE (rv))
3017 {
3018 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003019 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003020 goto done;
3021 }
3022 if (PREDICT_FALSE (!vep_session->is_vep))
3023 {
3024 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003025 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003026 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003027 rv = VPPCOM_EINVAL;
3028 goto done;
3029 }
3030
3031 ASSERT (vep_session->vep.vep_idx == ~0);
3032 ASSERT (vep_session->vep.prev_sid == ~0);
3033
3034 rv = vppcom_session_at_index (session_index, &session);
3035 if (PREDICT_FALSE (rv))
3036 {
3037 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003038 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003039 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003040 goto done;
3041 }
3042 if (PREDICT_FALSE (session->is_vep))
3043 {
3044 if (VPPCOM_DEBUG > 0)
3045 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3046 rv = VPPCOM_EINVAL;
3047 goto done;
3048 }
3049
3050 switch (op)
3051 {
3052 case EPOLL_CTL_ADD:
3053 if (PREDICT_FALSE (!event))
3054 {
Dave Wallace774169b2017-11-01 20:07:40 -04003055 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003056 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003057 rv = VPPCOM_EINVAL;
3058 goto done;
3059 }
3060 if (vep_session->vep.next_sid != ~0)
3061 {
3062 session_t *next_session;
3063 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3064 &next_session);
3065 if (PREDICT_FALSE (rv))
3066 {
3067 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003068 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3069 "vep.next_sid (%u) on vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003070 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003071 goto done;
3072 }
3073 ASSERT (next_session->vep.prev_sid == vep_idx);
3074 next_session->vep.prev_sid = session_index;
3075 }
3076 session->vep.next_sid = vep_session->vep.next_sid;
3077 session->vep.prev_sid = vep_idx;
3078 session->vep.vep_idx = vep_idx;
3079 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3080 session->vep.ev = *event;
3081 session->is_vep_session = 1;
3082 vep_session->vep.next_sid = session_index;
3083 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003084 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003085 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003086 event->events, event->data.u64);
3087 break;
3088
3089 case EPOLL_CTL_MOD:
3090 if (PREDICT_FALSE (!event))
3091 {
Dave Wallace774169b2017-11-01 20:07:40 -04003092 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003093 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003094 rv = VPPCOM_EINVAL;
3095 goto done;
3096 }
3097 if (PREDICT_FALSE (!session->is_vep_session &&
3098 (session->vep.vep_idx != vep_idx)))
3099 {
3100 if (VPPCOM_DEBUG > 0)
3101 {
3102 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003103 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3104 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003105 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003106 else
Dave Wallace774169b2017-11-01 20:07:40 -04003107 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3108 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003109 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003110 session->vep.vep_idx, vep_idx);
3111 }
3112 rv = VPPCOM_EINVAL;
3113 goto done;
3114 }
3115 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3116 session->vep.ev = *event;
3117 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003118 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003119 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003120 event->events, event->data.u64);
3121 break;
3122
3123 case EPOLL_CTL_DEL:
3124 if (PREDICT_FALSE (!session->is_vep_session &&
3125 (session->vep.vep_idx != vep_idx)))
3126 {
3127 if (VPPCOM_DEBUG > 0)
3128 {
3129 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003130 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3131 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003132 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003133 else
Dave Wallace774169b2017-11-01 20:07:40 -04003134 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3135 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003136 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003137 session->vep.vep_idx, vep_idx);
3138 }
3139 rv = VPPCOM_EINVAL;
3140 goto done;
3141 }
3142
3143 vep_session->wait_cont_idx =
3144 (vep_session->wait_cont_idx == session_index) ?
3145 session->vep.next_sid : vep_session->wait_cont_idx;
3146
3147 if (session->vep.prev_sid == vep_idx)
3148 vep_session->vep.next_sid = session->vep.next_sid;
3149 else
3150 {
3151 session_t *prev_session;
3152 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3153 if (PREDICT_FALSE (rv))
3154 {
3155 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003156 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3157 "vep.prev_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003158 getpid (), session->vep.prev_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003159 session_index);
3160 goto done;
3161 }
3162 ASSERT (prev_session->vep.next_sid == session_index);
3163 prev_session->vep.next_sid = session->vep.next_sid;
3164 }
3165 if (session->vep.next_sid != ~0)
3166 {
3167 session_t *next_session;
3168 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3169 if (PREDICT_FALSE (rv))
3170 {
3171 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003172 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3173 "vep.next_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003174 getpid (), session->vep.next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003175 session_index);
3176 goto done;
3177 }
3178 ASSERT (next_session->vep.prev_sid == session_index);
3179 next_session->vep.prev_sid = session->vep.prev_sid;
3180 }
3181
3182 memset (&session->vep, 0, sizeof (session->vep));
3183 session->vep.next_sid = ~0;
3184 session->vep.prev_sid = ~0;
3185 session->vep.vep_idx = ~0;
3186 session->is_vep_session = 0;
3187 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003188 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003189 getpid (), vep_idx, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003190 break;
3191
3192 default:
Dave Wallace2e005bb2017-11-07 01:21:39 -05003193 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003194 rv = VPPCOM_EINVAL;
3195 }
3196
3197 vep_verify_epoll_chain (vep_idx);
3198
3199done:
3200 clib_spinlock_unlock (&vcm->sessions_lockp);
3201 return rv;
3202}
3203
Dave Wallacef7f809c2017-10-03 01:48:42 -04003204int
3205vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3206 int maxevents, double wait_for_time)
3207{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003208 session_t *vep_session;
3209 int rv;
3210 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003211 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003212 int num_ev = 0;
3213 u32 vep_next_sid, wait_cont_idx;
3214 u8 is_vep;
3215
3216 if (PREDICT_FALSE (maxevents <= 0))
3217 {
3218 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003219 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003220 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003221 return VPPCOM_EINVAL;
3222 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003223 memset (events, 0, sizeof (*events) * maxevents);
3224
3225 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3226 vep_next_sid = vep_session->vep.next_sid;
3227 is_vep = vep_session->is_vep;
3228 wait_cont_idx = vep_session->wait_cont_idx;
3229 clib_spinlock_unlock (&vcm->sessions_lockp);
3230
3231 if (PREDICT_FALSE (!is_vep))
3232 {
3233 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003234 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003235 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003236 rv = VPPCOM_EINVAL;
3237 goto done;
3238 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003239 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003240 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003241 if (VPPCOM_DEBUG > 0)
3242 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003243 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003244 goto done;
3245 }
3246
3247 do
3248 {
3249 u32 sid;
3250 u32 next_sid = ~0;
3251 session_t *session;
3252
3253 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3254 sid != ~0; sid = next_sid)
3255 {
3256 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3257 u8 add_event, is_vep_session;
3258 int ready;
3259 u64 session_ev_data;
3260
3261 VCL_LOCK_AND_GET_SESSION (sid, &session);
3262 next_sid = session->vep.next_sid;
3263 session_events = session->vep.ev.events;
3264 et_mask = session->vep.et_mask;
3265 is_vep = session->is_vep;
3266 is_vep_session = session->is_vep_session;
3267 session_vep_idx = session->vep.vep_idx;
3268 session_ev_data = session->vep.ev.data.u64;
3269 clib_spinlock_unlock (&vcm->sessions_lockp);
3270
3271 if (PREDICT_FALSE (is_vep))
3272 {
3273 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003274 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003275 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003276 rv = VPPCOM_EINVAL;
3277 goto done;
3278 }
3279 if (PREDICT_FALSE (!is_vep_session))
3280 {
3281 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003282 clib_warning ("[%d] ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003283 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003284 rv = VPPCOM_EINVAL;
3285 goto done;
3286 }
3287 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3288 {
Dave Wallace774169b2017-11-01 20:07:40 -04003289 clib_warning ("[%d] ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003290 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003291 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003292 rv = VPPCOM_EINVAL;
3293 goto done;
3294 }
3295
3296 add_event = clear_et_mask = 0;
3297
Dave Wallace60caa062017-11-10 17:07:13 -05003298 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003299 {
3300 VCL_LOCK_AND_GET_SESSION (sid, &session);
3301 ready = vppcom_session_read_ready (session, sid);
3302 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003303 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003304 {
3305 add_event = 1;
3306 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003307 if (((EPOLLET | EPOLLIN) & session_events) ==
3308 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003309 clear_et_mask |= EPOLLIN;
3310 }
3311 else if (ready < 0)
3312 {
3313 add_event = 1;
3314 switch (ready)
3315 {
3316 case VPPCOM_ECONNRESET:
3317 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3318 break;
3319
3320 default:
3321 events[num_ev].events |= EPOLLERR;
3322 break;
3323 }
3324 }
3325 }
3326
Dave Wallace60caa062017-11-10 17:07:13 -05003327 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003328 {
3329 VCL_LOCK_AND_GET_SESSION (sid, &session);
3330 ready = vppcom_session_write_ready (session, sid);
3331 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003332 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003333 {
3334 add_event = 1;
3335 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003336 if (((EPOLLET | EPOLLOUT) & session_events) ==
3337 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003338 clear_et_mask |= EPOLLOUT;
3339 }
3340 else if (ready < 0)
3341 {
3342 add_event = 1;
3343 switch (ready)
3344 {
3345 case VPPCOM_ECONNRESET:
3346 events[num_ev].events |= EPOLLHUP;
3347 break;
3348
3349 default:
3350 events[num_ev].events |= EPOLLERR;
3351 break;
3352 }
3353 }
3354 }
3355
3356 if (add_event)
3357 {
3358 events[num_ev].data.u64 = session_ev_data;
3359 if (EPOLLONESHOT & session_events)
3360 {
3361 VCL_LOCK_AND_GET_SESSION (sid, &session);
3362 session->vep.ev.events = 0;
3363 clib_spinlock_unlock (&vcm->sessions_lockp);
3364 }
3365 num_ev++;
3366 if (num_ev == maxevents)
3367 {
3368 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3369 vep_session->wait_cont_idx = next_sid;
3370 clib_spinlock_unlock (&vcm->sessions_lockp);
3371 goto done;
3372 }
3373 }
3374 if (wait_cont_idx != ~0)
3375 {
3376 if (next_sid == ~0)
3377 next_sid = vep_next_sid;
3378 else if (next_sid == wait_cont_idx)
3379 next_sid = ~0;
3380 }
3381 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003382 if (wait_for_time != -1)
3383 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003384 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003385 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003386
3387 if (wait_cont_idx != ~0)
3388 {
3389 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3390 vep_session->wait_cont_idx = ~0;
3391 clib_spinlock_unlock (&vcm->sessions_lockp);
3392 }
3393done:
3394 return (rv != VPPCOM_OK) ? rv : num_ev;
3395}
3396
Dave Wallace35830af2017-10-09 01:43:42 -04003397int
3398vppcom_session_attr (uint32_t session_index, uint32_t op,
3399 void *buffer, uint32_t * buflen)
3400{
Dave Wallace35830af2017-10-09 01:43:42 -04003401 session_t *session;
3402 int rv = VPPCOM_OK;
3403 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003404 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003405
3406 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3407 switch (op)
3408 {
3409 case VPPCOM_ATTR_GET_NREAD:
3410 rv = vppcom_session_read_ready (session, session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003411 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003412 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003413 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003414
3415 break;
3416
3417 case VPPCOM_ATTR_PEEK_NREAD:
3418 /* TBD */
3419 break;
3420
3421 case VPPCOM_ATTR_GET_FLAGS:
3422 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3423 {
3424 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3425 *buflen = sizeof (*flags);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003426 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003427 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003428 "is_nonblocking = %u", getpid (), *flags,
Dave Wallace35830af2017-10-09 01:43:42 -04003429 session->is_nonblocking);
3430 }
3431 else
3432 rv = VPPCOM_EINVAL;
3433 break;
3434
3435 case VPPCOM_ATTR_SET_FLAGS:
3436 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3437 {
3438 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallacefaf9d772017-10-26 16:12:04 -04003439 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003440 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003441 "is_nonblocking = %u", getpid (), *flags,
Dave Wallace35830af2017-10-09 01:43:42 -04003442 session->is_nonblocking);
3443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_PEER_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003449 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003450 {
Steven2199aab2017-10-15 20:18:47 -07003451 ep->vrf = session->vrf;
3452 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003453 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07003454 if (session->peer_addr.is_ip4)
3455 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3456 sizeof (ip4_address_t));
3457 else
3458 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3459 sizeof (ip6_address_t));
3460 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003461 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003462 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003463 "%u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003464 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003465 &session->peer_addr.ip46, ep->is_ip4,
3466 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003467 }
3468 else
3469 rv = VPPCOM_EINVAL;
3470 break;
3471
3472 case VPPCOM_ATTR_GET_LCL_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003473 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003474 {
Steven2199aab2017-10-15 20:18:47 -07003475 ep->vrf = session->vrf;
3476 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003477 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07003478 if (session->lcl_addr.is_ip4)
3479 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3480 sizeof (ip4_address_t));
3481 else
3482 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3483 sizeof (ip6_address_t));
3484 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003485 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003486 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003487 "%u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003488 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003489 &session->lcl_addr.ip46, ep->is_ip4,
3490 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003491 }
3492 else
3493 rv = VPPCOM_EINVAL;
3494 break;
Stevenb5a11602017-10-11 09:59:30 -07003495
3496 case VPPCOM_ATTR_SET_REUSEADDR:
3497 break;
3498
3499 case VPPCOM_ATTR_SET_BROADCAST:
3500 break;
3501
3502 case VPPCOM_ATTR_SET_V6ONLY:
3503 break;
Stevenbccd3392017-10-12 20:42:21 -07003504
3505 case VPPCOM_ATTR_SET_KEEPALIVE:
3506 break;
3507
3508 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3509 break;
3510
3511 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3512 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003513
3514 default:
3515 rv = VPPCOM_EINVAL;
3516 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003517 }
3518
3519done:
3520 clib_spinlock_unlock (&vcm->sessions_lockp);
3521 return rv;
3522}
3523
Stevenac1f96d2017-10-24 16:03:58 -07003524int
3525vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3526 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3527{
Stevenac1f96d2017-10-24 16:03:58 -07003528 int rv = VPPCOM_OK;
3529 session_t *session = 0;
3530
3531 if (ep)
3532 {
3533 clib_spinlock_lock (&vcm->sessions_lockp);
3534 rv = vppcom_session_at_index (session_index, &session);
3535 if (PREDICT_FALSE (rv))
3536 {
3537 clib_spinlock_unlock (&vcm->sessions_lockp);
3538 if (VPPCOM_DEBUG > 0)
3539 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003540 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003541 rv = VPPCOM_EBADFD;
3542 clib_spinlock_unlock (&vcm->sessions_lockp);
3543 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07003544 }
3545 ep->vrf = session->vrf;
3546 ep->is_ip4 = session->peer_addr.is_ip4;
3547 ep->port = session->peer_port;
3548 if (session->peer_addr.is_ip4)
3549 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3550 sizeof (ip4_address_t));
3551 else
3552 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3553 sizeof (ip6_address_t));
3554 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07003555 }
Steven58f464e2017-10-25 12:33:12 -07003556
3557 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003558 rv = vppcom_session_read (session_index, buffer, buflen);
3559 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003560 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003561 else
3562 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003563 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07003564 rv = VPPCOM_EAFNOSUPPORT;
3565 }
3566
Dave Wallacefaf9d772017-10-26 16:12:04 -04003567done:
Stevenac1f96d2017-10-24 16:03:58 -07003568 return rv;
3569}
3570
3571int
3572vppcom_session_sendto (uint32_t session_index, void *buffer,
3573 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3574{
Dave Wallace617dffa2017-10-26 14:47:06 -04003575 if (!buffer)
3576 return VPPCOM_EINVAL;
3577
3578 if (ep)
3579 {
3580 // TBD
3581 return VPPCOM_EINVAL;
3582 }
3583
3584 if (flags)
3585 {
3586 // TBD check the flags and do the right thing
3587 if (VPPCOM_DEBUG > 2)
3588 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003589 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003590 }
3591
3592 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003593}
3594
Dave Wallacee22aa742017-10-20 12:30:38 -04003595/*
3596 * fd.io coding-style-patch-verification: ON
3597 *
3598 * Local Variables:
3599 * eval: (c-set-style "gnu")
3600 * End:
3601 */