blob: 2f62b81d864383e91ebd9b42002cd850590e92e1 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <signal.h>
19#include <svm/svm_fifo_segment.h>
20#include <vlibmemory/api.h>
21#include <vpp/api/vpe_msg_enum.h>
22#include <vnet/session/application_interface.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040023#include <vcl/vppcom.h>
Dave Wallace543852a2017-08-03 02:11:34 -040024#include <vlib/unix/unix.h>
25#include <vppinfra/vec_bootstrap.h>
26
27#define vl_typedefs /* define message structures */
28#include <vpp/api/vpe_all_api_h.h>
29#undef vl_typedefs
30
31/* declare message handlers for each api */
32
33#define vl_endianfun /* define message structures */
34#include <vpp/api/vpe_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...)
39#define vl_printfun
40#include <vpp/api/vpe_all_api_h.h>
41#undef vl_printfun
42
43#if (CLIB_DEBUG > 0)
Dave Wallace498b3a52017-11-09 13:00:34 -050044/* Set VPPCOM_DEBUG_INIT 2 for connection debug,
45 * 3 for read/write debug output
46 * or
47 * export VCL_DEBUG=<#> to set dynamically.
48 */
49#define VPPCOM_DEBUG_INIT 1
Dave Wallace543852a2017-08-03 02:11:34 -040050#else
Dave Wallace498b3a52017-11-09 13:00:34 -050051#define VPPCOM_DEBUG_INIT 0
Dave Wallace543852a2017-08-03 02:11:34 -040052#endif
53
Dave Wallace498b3a52017-11-09 13:00:34 -050054#define VPPCOM_DEBUG vcm->debug
55
Dave Wallace543852a2017-08-03 02:11:34 -040056/*
57 * VPPCOM Private definitions and functions.
58 */
59typedef enum
60{
61 STATE_APP_START,
62 STATE_APP_CONN_VPP,
63 STATE_APP_ENABLED,
64 STATE_APP_ATTACHED,
65} app_state_t;
66
67typedef enum
68{
69 STATE_START,
70 STATE_CONNECT,
71 STATE_LISTEN,
72 STATE_ACCEPT,
73 STATE_DISCONNECT,
74 STATE_FAILED
75} session_state_t;
76
Dave Wallacef7f809c2017-10-03 01:48:42 -040077typedef struct epoll_event vppcom_epoll_event_t;
78
79typedef struct
80{
81 u32 next_sid;
82 u32 prev_sid;
83 u32 vep_idx;
84 vppcom_epoll_event_t ev;
85#define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
Dave Wallace60caa062017-11-10 17:07:13 -050086#define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
Dave Wallacef7f809c2017-10-03 01:48:42 -040087 u32 et_mask;
88} vppcom_epoll_t;
89
Dave Wallace543852a2017-08-03 02:11:34 -040090typedef struct
91{
Dave Wallace35830af2017-10-09 01:43:42 -040092 u8 is_ip4;
93 ip46_address_t ip46;
94} vppcom_ip46_t;
95
96typedef struct
97{
Dave Wallace543852a2017-08-03 02:11:34 -040098 volatile session_state_t state;
99
100 svm_fifo_t *server_rx_fifo;
101 svm_fifo_t *server_tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500102 u8 *segment_name;
Dave Wallace543852a2017-08-03 02:11:34 -0400103 u32 sm_seg_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500104 u32 client_context;
105 u64 vpp_handle;
Dave Wallace33e002b2017-09-06 01:20:02 -0400106 unix_shared_memory_queue_t *vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400107
108 /* Socket configuration state */
Dave Wallacef7f809c2017-10-03 01:48:42 -0400109 /* TBD: covert 'is_*' vars to bit in u8 flags; */
Dave Wallace543852a2017-08-03 02:11:34 -0400110 u8 is_server;
111 u8 is_listen;
112 u8 is_cut_thru;
113 u8 is_nonblocking;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400114 u8 is_vep;
115 u8 is_vep_session;
116 u32 wait_cont_idx;
117 vppcom_epoll_t vep;
Dave Wallace543852a2017-08-03 02:11:34 -0400118 u32 vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400119 vppcom_ip46_t lcl_addr;
120 vppcom_ip46_t peer_addr;
Stevenac1f96d2017-10-24 16:03:58 -0700121 u16 lcl_port; // network order
122 u16 peer_port; // network order
Dave Wallace543852a2017-08-03 02:11:34 -0400123 u8 proto;
124 u64 client_queue_address;
125 u64 options[16];
126} session_t;
127
128typedef struct vppcom_cfg_t_
129{
130 u64 heapsize;
131 u64 segment_baseva;
132 u32 segment_size;
133 u32 add_segment_size;
134 u32 preallocated_fifo_pairs;
135 u32 rx_fifo_size;
136 u32 tx_fifo_size;
137 u32 event_queue_size;
138 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400139 u8 app_proxy_transport_tcp;
140 u8 app_proxy_transport_udp;
141 u8 app_scope_local;
142 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400143 u8 *namespace_id;
144 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400145 f64 app_timeout;
146 f64 session_timeout;
147 f64 accept_timeout;
148} vppcom_cfg_t;
149
150typedef struct vppcom_main_t_
151{
152 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500153 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400154 u32 *client_session_index_fifo;
155 volatile u32 bind_session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400156 int main_cpu;
157
158 /* vpe input queue */
159 unix_shared_memory_queue_t *vl_input_queue;
160
161 /* API client handle */
162 u32 my_client_index;
163
164 /* Session pool */
165 clib_spinlock_t sessions_lockp;
166 session_t *sessions;
167
168 /* Hash table for disconnect processing */
169 uword *session_index_by_vpp_handles;
170
171 /* Select bitmaps */
172 clib_bitmap_t *rd_bitmap;
173 clib_bitmap_t *wr_bitmap;
174 clib_bitmap_t *ex_bitmap;
175
176 /* Our event queue */
177 unix_shared_memory_queue_t *app_event_queue;
178
179 /* unique segment name counter */
180 u32 unique_segment_index;
181
Dave Wallace543852a2017-08-03 02:11:34 -0400182 /* For deadman timers */
183 clib_time_t clib_time;
184
185 /* State of the connection, shared between msg RX thread and main thread */
186 volatile app_state_t app_state;
187
188 vppcom_cfg_t cfg;
189
190 /* VNET_API_ERROR_FOO -> "Foo" hash table */
191 uword *error_string_by_error_number;
192} vppcom_main_t;
193
Dave Wallace2e005bb2017-11-07 01:21:39 -0500194/* NOTE: _vppcom_main is only used until the heap is allocated.
195 * Do not access it directly -- use vcm which will point to
196 * the heap allocated copy after init.
197 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500198static vppcom_main_t _vppcom_main = {
199 .debug = VPPCOM_DEBUG_INIT,
200 .my_client_index = ~0
201};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500202
203static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400204
Dave Wallace60caa062017-11-10 17:07:13 -0500205#define VCL_LOCK_AND_GET_SESSION(I, S) \
206do { \
207 clib_spinlock_lock (&vcm->sessions_lockp); \
208 rv = vppcom_session_at_index (I, S); \
209 if (PREDICT_FALSE (rv)) \
210 { \
211 clib_spinlock_unlock (&vcm->sessions_lockp); \
212 \
213 if (VPPCOM_DEBUG > 0) \
214 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
215 getpid (), I); \
216 \
217 goto done; \
218 } \
219} while (0)
220
Dave Wallace543852a2017-08-03 02:11:34 -0400221static const char *
222vppcom_app_state_str (app_state_t state)
223{
224 char *st;
225
226 switch (state)
227 {
228 case STATE_APP_START:
229 st = "STATE_APP_START";
230 break;
231
232 case STATE_APP_CONN_VPP:
233 st = "STATE_APP_CONN_VPP";
234 break;
235
236 case STATE_APP_ENABLED:
237 st = "STATE_APP_ENABLED";
238 break;
239
240 case STATE_APP_ATTACHED:
241 st = "STATE_APP_ATTACHED";
242 break;
243
244 default:
245 st = "UNKNOWN_APP_STATE";
246 break;
247 }
248
249 return st;
250}
251
252static const char *
253vppcom_session_state_str (session_state_t state)
254{
255 char *st;
256
257 switch (state)
258 {
259 case STATE_START:
260 st = "STATE_START";
261 break;
262
263 case STATE_CONNECT:
264 st = "STATE_CONNECT";
265 break;
266
267 case STATE_LISTEN:
268 st = "STATE_LISTEN";
269 break;
270
271 case STATE_ACCEPT:
272 st = "STATE_ACCEPT";
273 break;
274
275 case STATE_DISCONNECT:
276 st = "STATE_DISCONNECT";
277 break;
278
279 case STATE_FAILED:
280 st = "STATE_FAILED";
281 break;
282
283 default:
284 st = "UNKNOWN_STATE";
285 break;
286 }
287
288 return st;
289}
290
291/*
292 * VPPCOM Utility Functions
293 */
294static inline int
295vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
296{
Dave Wallace543852a2017-08-03 02:11:34 -0400297 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
298 if (PREDICT_FALSE ((session_index == ~0) ||
299 pool_is_free_index (vcm->sessions, session_index)))
300 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400301 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500302 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400303 return VPPCOM_EBADFD;
304 }
305 *sess = pool_elt_at_index (vcm->sessions, session_index);
306 return VPPCOM_OK;
307}
308
309static int
310vppcom_connect_to_vpp (char *app_name)
311{
312 api_main_t *am = &api_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400313
314 if (VPPCOM_DEBUG > 0)
315 printf ("\nConnecting to VPP api...");
316 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
317 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500318 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400319 return VPPCOM_ECONNREFUSED;
320 }
321
322 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
323 vcm->my_client_index = am->my_client_index;
324 if (VPPCOM_DEBUG > 0)
325 printf (" connected!\n");
326
327 vcm->app_state = STATE_APP_CONN_VPP;
328 return VPPCOM_OK;
329}
330
331static u8 *
332format_api_error (u8 * s, va_list * args)
333{
Dave Wallace543852a2017-08-03 02:11:34 -0400334 i32 error = va_arg (*args, u32);
335 uword *p;
336
337 p = hash_get (vcm->error_string_by_error_number, -error);
338
339 if (p)
340 s = format (s, "%s (%d)", p[0], error);
341 else
342 s = format (s, "%d", error);
343 return s;
344}
345
346static void
347vppcom_init_error_string_table (void)
348{
Dave Wallace543852a2017-08-03 02:11:34 -0400349 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
350
351#define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
352 foreach_vnet_api_error;
353#undef _
354
355 hash_set (vcm->error_string_by_error_number, 99, "Misc");
356}
357
358static inline int
359vppcom_wait_for_app_state_change (app_state_t app_state)
360{
Dave Wallace543852a2017-08-03 02:11:34 -0400361 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
362
363 while (clib_time_now (&vcm->clib_time) < timeout)
364 {
365 if (vcm->app_state == app_state)
366 return VPPCOM_OK;
367 }
368 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500369 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400370 vppcom_app_state_str (app_state), app_state);
371 return VPPCOM_ETIMEDOUT;
372}
373
374static inline int
375vppcom_wait_for_session_state_change (u32 session_index,
376 session_state_t state,
377 f64 wait_for_time)
378{
Dave Wallace543852a2017-08-03 02:11:34 -0400379 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
380 session_t *volatile session;
381 int rv;
382
383 do
384 {
385 clib_spinlock_lock (&vcm->sessions_lockp);
386 rv = vppcom_session_at_index (session_index, &session);
387 if (PREDICT_FALSE (rv))
388 {
389 clib_spinlock_unlock (&vcm->sessions_lockp);
390 return rv;
391 }
392 if (session->state == state)
393 {
394 clib_spinlock_unlock (&vcm->sessions_lockp);
395 return VPPCOM_OK;
396 }
397 clib_spinlock_unlock (&vcm->sessions_lockp);
398 }
399 while (clib_time_now (&vcm->clib_time) < timeout);
400
401 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500402 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400403 vppcom_session_state_str (state), state);
404 return VPPCOM_ETIMEDOUT;
405}
406
407static inline int
408vppcom_wait_for_client_session_index (f64 wait_for_time)
409{
Dave Wallace543852a2017-08-03 02:11:34 -0400410 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
411
412 do
413 {
414 if (clib_fifo_elts (vcm->client_session_index_fifo))
415 return VPPCOM_OK;
416 }
417 while (clib_time_now (&vcm->clib_time) < timeout);
418
419 if (wait_for_time == 0)
420 return VPPCOM_EAGAIN;
421
422 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500423 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400424 return VPPCOM_ETIMEDOUT;
425}
426
427/*
428 * VPP-API message functions
429 */
430static void
431vppcom_send_session_enable_disable (u8 is_enable)
432{
Dave Wallace543852a2017-08-03 02:11:34 -0400433 vl_api_session_enable_disable_t *bmp;
434 bmp = vl_msg_api_alloc (sizeof (*bmp));
435 memset (bmp, 0, sizeof (*bmp));
436
437 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
438 bmp->client_index = vcm->my_client_index;
439 bmp->context = htonl (0xfeedface);
440 bmp->is_enable = is_enable;
441 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
442}
443
444static int
445vppcom_app_session_enable (void)
446{
Dave Wallace543852a2017-08-03 02:11:34 -0400447 int rv;
448
449 if (vcm->app_state != STATE_APP_ENABLED)
450 {
451 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
452 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
453 if (PREDICT_FALSE (rv))
454 {
455 if (VPPCOM_DEBUG > 0)
456 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500457 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400458 return rv;
459 }
460 }
461 return VPPCOM_OK;
462}
463
464static void
465 vl_api_session_enable_disable_reply_t_handler
466 (vl_api_session_enable_disable_reply_t * mp)
467{
Dave Wallace543852a2017-08-03 02:11:34 -0400468 if (mp->retval)
469 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500470 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400471 format_api_error, ntohl (mp->retval));
472 }
473 else
474 vcm->app_state = STATE_APP_ENABLED;
475}
476
477static void
478vppcom_app_send_attach (void)
479{
Dave Wallace543852a2017-08-03 02:11:34 -0400480 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400481 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400482 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
483 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400484
Dave Wallace543852a2017-08-03 02:11:34 -0400485 bmp = vl_msg_api_alloc (sizeof (*bmp));
486 memset (bmp, 0, sizeof (*bmp));
487
488 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
489 bmp->client_index = vcm->my_client_index;
490 bmp->context = htonl (0xfeedface);
491 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700492 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400493 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
494 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
495 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
496 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
497 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
498 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Dave Wallace543852a2017-08-03 02:11:34 -0400499 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
500 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
501 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
502 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400503 if (nsid_len)
504 {
505 bmp->namespace_id_len = nsid_len;
506 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
507 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
508 }
Dave Wallace543852a2017-08-03 02:11:34 -0400509 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
510}
511
512static int
513vppcom_app_attach (void)
514{
Dave Wallace543852a2017-08-03 02:11:34 -0400515 int rv;
516
517 vppcom_app_send_attach ();
518 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
519 if (PREDICT_FALSE (rv))
520 {
521 if (VPPCOM_DEBUG > 0)
522 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500523 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400524 return rv;
525 }
526 return VPPCOM_OK;
527}
528
529static void
530vppcom_app_detach (void)
531{
Dave Wallace543852a2017-08-03 02:11:34 -0400532 vl_api_application_detach_t *bmp;
533 bmp = vl_msg_api_alloc (sizeof (*bmp));
534 memset (bmp, 0, sizeof (*bmp));
535
536 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
537 bmp->client_index = vcm->my_client_index;
538 bmp->context = htonl (0xfeedface);
539 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
540}
541
542static void
543vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
544 mp)
545{
Dave Wallace543852a2017-08-03 02:11:34 -0400546 static svm_fifo_segment_create_args_t _a;
547 svm_fifo_segment_create_args_t *a = &_a;
548 int rv;
549
550 memset (a, 0, sizeof (*a));
551 if (mp->retval)
552 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500553 clib_warning ("[%d] attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400554 format_api_error, ntohl (mp->retval));
555 return;
556 }
557
558 if (mp->segment_name_length == 0)
559 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500560 clib_warning ("[%d] segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400561 return;
562 }
563
564 a->segment_name = (char *) mp->segment_name;
565 a->segment_size = mp->segment_size;
566
567 ASSERT (mp->app_event_queue_address);
568
569 /* Attach to the segment vpp created */
570 rv = svm_fifo_segment_attach (a);
571 vec_reset_length (a->new_segment_indices);
572 if (PREDICT_FALSE (rv))
573 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500574 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400575 mp->segment_name);
576 return;
577 }
578
579 vcm->app_event_queue =
580 uword_to_pointer (mp->app_event_queue_address,
581 unix_shared_memory_queue_t *);
582
583 vcm->app_state = STATE_APP_ATTACHED;
584}
585
586static void
587vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
588 mp)
589{
Dave Wallace543852a2017-08-03 02:11:34 -0400590 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500591 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400592 ntohl (mp->retval));
593
594 vcm->app_state = STATE_APP_ENABLED;
595}
596
597static void
598vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
599 mp)
600{
Dave Wallace543852a2017-08-03 02:11:34 -0400601 uword *p;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500602 u32 session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -0400603
604 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
605 if (p)
606 {
607 session_t *session = 0;
608 int rv;
Dave Wallace3bd43b82017-11-11 22:45:38 -0500609
610 session_index = p[0];
611 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400612 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace3bd43b82017-11-11 22:45:38 -0500613 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400614 if (PREDICT_FALSE (rv))
615 {
616 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500617 clib_warning ("[%d] invalid session, sid %u has been closed!",
618 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400619 }
Dave Wallace3bd43b82017-11-11 22:45:38 -0500620 else
621 {
622 if (VPPCOM_DEBUG > 1)
623 clib_warning ("[%d] vpp handle 0x%llx, sid %u disconnected!",
624 getpid (), mp->handle, session_index);
625
626 session->state = STATE_DISCONNECT;
627 }
Dave Wallace543852a2017-08-03 02:11:34 -0400628 clib_spinlock_unlock (&vcm->sessions_lockp);
629 }
630 else
631 {
632 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500633 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
634 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400635 }
636
637 if (mp->retval)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500638 clib_warning ("[%d] disconnect_session vpp handle 0x%llx, sid %u "
639 "failed: %U", getpid (), mp->handle, session_index,
Dave Wallace543852a2017-08-03 02:11:34 -0400640 format_api_error, ntohl (mp->retval));
641}
642
643static void
644vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
645{
Dave Wallace543852a2017-08-03 02:11:34 -0400646 static svm_fifo_segment_create_args_t _a;
647 svm_fifo_segment_create_args_t *a = &_a;
648 int rv;
649
650 memset (a, 0, sizeof (*a));
651 a->segment_name = (char *) mp->segment_name;
652 a->segment_size = mp->segment_size;
653 /* Attach to the segment vpp created */
654 rv = svm_fifo_segment_attach (a);
655 vec_reset_length (a->new_segment_indices);
656 if (PREDICT_FALSE (rv))
657 {
658 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500659 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400660 return;
661 }
662 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500663 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400664 mp->segment_name, mp->segment_size);
665}
666
667static void
668vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
669{
Dave Wallace543852a2017-08-03 02:11:34 -0400670 session_t *session = 0;
671 vl_api_disconnect_session_reply_t *rmp;
672 uword *p;
673 int rv = 0;
674
675 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
676 if (p)
677 {
678 int rval;
679 clib_spinlock_lock (&vcm->sessions_lockp);
680 rval = vppcom_session_at_index (p[0], &session);
681 if (PREDICT_FALSE (rval))
682 {
683 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400684 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500685 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400686 }
687 else
688 pool_put (vcm->sessions, session);
689 clib_spinlock_unlock (&vcm->sessions_lockp);
690 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
691 }
692 else
693 {
Dave Wallace227867f2017-11-13 21:21:53 -0500694 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500695 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400696 rv = -11;
697 }
698
699 rmp = vl_msg_api_alloc (sizeof (*rmp));
700 memset (rmp, 0, sizeof (*rmp));
701
702 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
703 rmp->retval = htonl (rv);
704 rmp->handle = mp->handle;
705 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
706}
707
708static void
709vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
710{
Dave Wallace543852a2017-08-03 02:11:34 -0400711 session_t *session = 0;
712 vl_api_reset_session_reply_t *rmp;
713 uword *p;
714 int rv = 0;
715
716 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
717 if (p)
718 {
719 int rval;
720 clib_spinlock_lock (&vcm->sessions_lockp);
721 rval = vppcom_session_at_index (p[0], &session);
722 if (PREDICT_FALSE (rval))
723 {
724 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400725 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500726 getpid (), p[0]);
Dave Wallace543852a2017-08-03 02:11:34 -0400727 }
728 else
729 pool_put (vcm->sessions, session);
730 clib_spinlock_unlock (&vcm->sessions_lockp);
731 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
732 }
733 else
734 {
Dave Wallace3bd43b82017-11-11 22:45:38 -0500735 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
736 getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400737 rv = -11;
738 }
739
740 rmp = vl_msg_api_alloc (sizeof (*rmp));
741 memset (rmp, 0, sizeof (*rmp));
742 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
743 rmp->retval = htonl (rv);
744 rmp->handle = mp->handle;
745 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
746}
747
748static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400749vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400750{
Dave Wallace543852a2017-08-03 02:11:34 -0400751 session_t *session;
752 u32 session_index;
753 svm_fifo_t *rx_fifo, *tx_fifo;
754 u8 is_cut_thru = 0;
755 int rv;
756
757 if (mp->retval)
758 {
Dave Wallace2e005bb2017-11-07 01:21:39 -0500759 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400760 ntohl (mp->retval));
761 return;
762 }
763
Dave Wallace33e002b2017-09-06 01:20:02 -0400764 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -0400765 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500766 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400767 session_index, session_index);
768
769 clib_spinlock_lock (&vcm->sessions_lockp);
770 if (pool_is_free_index (vcm->sessions, session_index))
771 {
772 clib_spinlock_unlock (&vcm->sessions_lockp);
773 if (VPPCOM_DEBUG > 1)
774 clib_warning ("[%d] invalid session, sid %d is closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500775 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400776 return;
777 }
778
779 /* We've been redirected */
780 if (mp->segment_name_length > 0)
781 {
782 static svm_fifo_segment_create_args_t _a;
783 svm_fifo_segment_create_args_t *a = &_a;
784
785 is_cut_thru = 1;
786 memset (a, 0, sizeof (*a));
787 a->segment_name = (char *) mp->segment_name;
788 if (VPPCOM_DEBUG > 1)
Dave Wallace60caa062017-11-10 17:07:13 -0500789 clib_warning ("[%d] cut-thru segment: %s\n",
790 getpid (), a->segment_name);
791
Dave Wallace543852a2017-08-03 02:11:34 -0400792 rv = svm_fifo_segment_attach (a);
793 vec_reset_length (a->new_segment_indices);
794 if (PREDICT_FALSE (rv))
795 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400796 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400797 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500798 getpid (), a->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400799 return;
800 }
801 }
802
803 /*
804 * Setup session
805 */
Dave Wallace543852a2017-08-03 02:11:34 -0400806 session = pool_elt_at_index (vcm->sessions, session_index);
807 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -0400808 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
809 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400810
811 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
812 rx_fifo->client_session_index = session_index;
813 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
814 tx_fifo->client_session_index = session_index;
815
816 session->server_rx_fifo = rx_fifo;
817 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500818 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400819 session->state = STATE_CONNECT;
820
821 /* Add it to lookup table */
822 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500823
824 if (VPPCOM_DEBUG > 1)
Dave Wallace3bd43b82017-11-11 22:45:38 -0500825 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
Dave Wallace60caa062017-11-10 17:07:13 -0500826 " session_rx_fifo %p, refcnt %d\n"
827 " session_tx_fifo %p, refcnt %d",
Dave Wallace3bd43b82017-11-11 22:45:38 -0500828 getpid (), session_index, mp->handle,
Dave Wallace60caa062017-11-10 17:07:13 -0500829 session->server_rx_fifo,
830 session->server_rx_fifo->refcnt,
831 session->server_tx_fifo, session->server_tx_fifo->refcnt);
832
Dave Wallace543852a2017-08-03 02:11:34 -0400833 clib_spinlock_unlock (&vcm->sessions_lockp);
834}
835
836static void
837vppcom_send_connect_sock (session_t * session, u32 session_index)
838{
Dave Wallace543852a2017-08-03 02:11:34 -0400839 vl_api_connect_sock_t *cmp;
840
841 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
842 session->is_server = 0;
843 cmp = vl_msg_api_alloc (sizeof (*cmp));
844 memset (cmp, 0, sizeof (*cmp));
845 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
846 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400847 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400848
849 if (VPPCOM_DEBUG > 1)
Dave Wallace33e002b2017-09-06 01:20:02 -0400850 clib_warning ("[%d] session_index = %d 0x%08x",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500851 getpid (), session_index, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400852
853 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400854 cmp->is_ip4 = session->peer_addr.is_ip4;
855 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -0700856 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -0400857 cmp->proto = session->proto;
858 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
859 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
860}
861
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400862static inline void
863vppcom_send_disconnect (session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -0400864{
Dave Wallace543852a2017-08-03 02:11:34 -0400865 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400866
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400867 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400868 dmp = vl_msg_api_alloc (sizeof (*dmp));
869 memset (dmp, 0, sizeof (*dmp));
870 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
871 dmp->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500872 dmp->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400873 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400874}
875
876static void
877vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
878{
Dave Wallace543852a2017-08-03 02:11:34 -0400879 session_t *session = 0;
880 int rv;
881
882 if (mp->retval)
Dave Wallace2e005bb2017-11-07 01:21:39 -0500883 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400884 ntohl (mp->retval));
885
886 ASSERT (vcm->bind_session_index != ~0);
887
888 clib_spinlock_lock (&vcm->sessions_lockp);
889 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
890 if (rv == VPPCOM_OK)
891 {
Dave Wallace60caa062017-11-10 17:07:13 -0500892 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400893 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
894 vcm->bind_session_index);
895 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
896 vcm->bind_session_index = ~0;
897 }
898 clib_spinlock_unlock (&vcm->sessions_lockp);
899}
900
901static void
902vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
903{
Dave Wallace543852a2017-08-03 02:11:34 -0400904 session_t *session = 0;
905 int rv;
906
907 clib_spinlock_lock (&vcm->sessions_lockp);
908 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
Dave Wallace19481612017-09-15 18:47:44 -0400909 if (rv == VPPCOM_OK)
Dave Wallace543852a2017-08-03 02:11:34 -0400910 {
Dave Wallace19481612017-09-15 18:47:44 -0400911 if ((VPPCOM_DEBUG > 1) && (mp->retval))
Dave Wallace2e005bb2017-11-07 01:21:39 -0500912 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
Dave Wallace19481612017-09-15 18:47:44 -0400913 ntohl (mp->retval));
914
915 vcm->bind_session_index = ~0;
916 session->state = STATE_START;
Dave Wallace543852a2017-08-03 02:11:34 -0400917 }
Dave Wallace543852a2017-08-03 02:11:34 -0400918 clib_spinlock_unlock (&vcm->sessions_lockp);
919}
920
921u8 *
922format_ip4_address (u8 * s, va_list * args)
923{
924 u8 *a = va_arg (*args, u8 *);
925 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
926}
927
928u8 *
929format_ip6_address (u8 * s, va_list * args)
930{
931 ip6_address_t *a = va_arg (*args, ip6_address_t *);
932 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
933
934 i_max_n_zero = ARRAY_LEN (a->as_u16);
935 max_n_zeros = 0;
936 i_first_zero = i_max_n_zero;
937 n_zeros = 0;
938 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
939 {
940 u32 is_zero = a->as_u16[i] == 0;
941 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
942 {
943 i_first_zero = i;
944 n_zeros = 0;
945 }
946 n_zeros += is_zero;
947 if ((!is_zero && n_zeros > max_n_zeros)
948 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
949 {
950 i_max_n_zero = i_first_zero;
951 max_n_zeros = n_zeros;
952 i_first_zero = ARRAY_LEN (a->as_u16);
953 n_zeros = 0;
954 }
955 }
956
957 last_double_colon = 0;
958 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
959 {
960 if (i == i_max_n_zero && max_n_zeros > 1)
961 {
962 s = format (s, "::");
963 i += max_n_zeros - 1;
964 last_double_colon = 1;
965 }
966 else
967 {
968 s = format (s, "%s%x",
969 (last_double_colon || i == 0) ? "" : ":",
970 clib_net_to_host_u16 (a->as_u16[i]));
971 last_double_colon = 0;
972 }
973 }
974
975 return s;
976}
977
978/* Format an IP46 address. */
979u8 *
980format_ip46_address (u8 * s, va_list * args)
981{
982 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
983 ip46_type_t type = va_arg (*args, ip46_type_t);
984 int is_ip4 = 1;
985
986 switch (type)
987 {
988 case IP46_TYPE_ANY:
989 is_ip4 = ip46_address_is_ip4 (ip46);
990 break;
991 case IP46_TYPE_IP4:
992 is_ip4 = 1;
993 break;
994 case IP46_TYPE_IP6:
995 is_ip4 = 0;
996 break;
997 }
998
999 return is_ip4 ?
1000 format (s, "%U", format_ip4_address, &ip46->ip4) :
1001 format (s, "%U", format_ip6_address, &ip46->ip6);
1002}
1003
Dave Wallace60caa062017-11-10 17:07:13 -05001004static inline void
1005vppcom_send_accept_session_reply (u32 handle, int retval)
1006{
1007 vl_api_accept_session_reply_t *rmp;
1008
1009 rmp = vl_msg_api_alloc (sizeof (*rmp));
1010 memset (rmp, 0, sizeof (*rmp));
1011 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1012 rmp->retval = htonl (retval);
1013 rmp->handle = handle;
1014 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1015}
1016
Dave Wallace543852a2017-08-03 02:11:34 -04001017static void
1018vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1019{
Dave Wallace543852a2017-08-03 02:11:34 -04001020 svm_fifo_t *rx_fifo, *tx_fifo;
1021 session_t *session;
1022 u32 session_index;
Dave Wallace227867f2017-11-13 21:21:53 -05001023 uword *p;
Dave Wallace543852a2017-08-03 02:11:34 -04001024
Dave Wallace60caa062017-11-10 17:07:13 -05001025 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001026 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1027 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001028 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallace60caa062017-11-10 17:07:13 -05001029 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL,
1030 mp->handle);
1031 clib_spinlock_unlock (&vcm->sessions_lockp);
1032 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001033 }
1034
Dave Wallace543852a2017-08-03 02:11:34 -04001035 /* Allocate local session and set it up */
1036 pool_get (vcm->sessions, session);
1037 memset (session, 0, sizeof (*session));
1038 session_index = session - vcm->sessions;
1039
1040 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1041 rx_fifo->client_session_index = session_index;
1042 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1043 tx_fifo->client_session_index = session_index;
1044
Dave Wallace60caa062017-11-10 17:07:13 -05001045 session->vpp_handle = mp->handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001046 session->server_rx_fifo = rx_fifo;
1047 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001048 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1049 unix_shared_memory_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001050 session->state = STATE_ACCEPT;
1051 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001052 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001053 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001054 session->peer_addr.is_ip4 = mp->is_ip4;
1055 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1056 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001057
1058 /* Add it to lookup table */
1059 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1060
Dave Wallace227867f2017-11-13 21:21:53 -05001061 p = hash_get (vcm->session_index_by_vpp_handles, mp->listener_handle);
1062 if (p)
1063 {
1064 int rval;
1065 session_t *listen_session;
1066
1067 rval = vppcom_session_at_index (p[0], &listen_session);
1068 if (PREDICT_FALSE (rval))
1069 {
1070 if (VPPCOM_DEBUG > 1)
1071 clib_warning ("[%d] invalid listen session, sid (%u) "
1072 "has been closed!", getpid (), p[0]);
1073 }
1074 else
1075 {
1076 session->lcl_port = listen_session->lcl_port;
1077 session->lcl_addr = listen_session->lcl_addr;
1078 }
1079 clib_spinlock_unlock (&vcm->sessions_lockp);
1080 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
1081 }
1082 else
1083 {
1084 clib_warning ("[%d] couldn't find listen session: unknown vpp "
1085 "listener handle %llx", getpid (), mp->listener_handle);
1086 }
1087
1088 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001089 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001090 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001091
Dave Wallace60caa062017-11-10 17:07:13 -05001092 if (VPPCOM_DEBUG > 1)
1093 {
1094 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1095 clib_warning ("[%d] received request to accept session (sid %d) "
1096 "from %s:%d", getpid (), session_index, ip_str,
1097 clib_net_to_host_u16 (mp->port));
1098 vec_free (ip_str);
1099 }
1100}
1101
1102static void
Dave Wallace227867f2017-11-13 21:21:53 -05001103vppcom_send_connect_session_reply (session_t * session, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001104{
1105 vl_api_connect_session_reply_t *rmp;
1106 u32 len;
1107 unix_shared_memory_queue_t *client_q;
1108
Dave Wallace543852a2017-08-03 02:11:34 -04001109 rmp = vl_msg_api_alloc (sizeof (*rmp));
1110 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001111
1112 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallace227867f2017-11-13 21:21:53 -05001113 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001114 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001115 rmp->handle = session->vpp_handle;
1116 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1117 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1118 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1119 rmp->segment_size = vcm->cfg.segment_size;
1120 len = vec_len (session->segment_name);
1121 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1122 clib_memcpy (rmp->segment_name, session->segment_name,
1123 rmp->segment_name_length - 1);
1124 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1125 sizeof (rmp->lcl_ip));
1126 rmp->is_ip4 = session->lcl_addr.is_ip4;
Dave Wallace60caa062017-11-10 17:07:13 -05001127 client_q = uword_to_pointer (session->client_queue_address,
1128 unix_shared_memory_queue_t *);
1129 ASSERT (client_q);
1130 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001131}
1132
1133/*
1134 * Acting as server for redirected connect requests
1135 */
1136static void
1137vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1138{
Dave Wallace543852a2017-08-03 02:11:34 -04001139 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001140 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001141
Dave Wallacef7f809c2017-10-03 01:48:42 -04001142 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001143 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1144 {
1145 if (VPPCOM_DEBUG > 1)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001146 clib_warning ("[%d] client session queue is full!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001147 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05001148 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL, 0);
1149 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001150 }
1151
Dave Wallace543852a2017-08-03 02:11:34 -04001152 pool_get (vcm->sessions, session);
1153 memset (session, 0, sizeof (*session));
1154 session_index = session - vcm->sessions;
1155
Dave Wallace60caa062017-11-10 17:07:13 -05001156 session->client_context = mp->context;
1157 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001158 session->client_queue_address = mp->client_queue_address;
1159 session->is_cut_thru = 1;
1160 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001161 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001162 session->peer_addr.is_ip4 = mp->is_ip4;
1163 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1164 sizeof (session->peer_addr.ip46));
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001165
Dave Wallace543852a2017-08-03 02:11:34 -04001166 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001167 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001168 if (VPPCOM_DEBUG > 1)
1169 clib_warning
Dave Wallace60caa062017-11-10 17:07:13 -05001170 ("[%d] Got a cut-thru connect request to client: "
1171 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
Dave Wallace2e005bb2017-11-07 01:21:39 -05001172 clib_fifo_elts (vcm->client_session_index_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001173 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001174}
1175
1176static void
1177vppcom_send_bind_sock (session_t * session)
1178{
Dave Wallace543852a2017-08-03 02:11:34 -04001179 vl_api_bind_sock_t *bmp;
1180
1181 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1182 session->is_server = 1;
1183 bmp = vl_msg_api_alloc (sizeof (*bmp));
1184 memset (bmp, 0, sizeof (*bmp));
1185
1186 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1187 bmp->client_index = vcm->my_client_index;
1188 bmp->context = htonl (0xfeedface);
1189 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001190 bmp->is_ip4 = session->lcl_addr.is_ip4;
1191 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001192 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001193 bmp->proto = session->proto;
1194 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1195 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1196}
1197
1198static void
1199vppcom_send_unbind_sock (u32 session_index)
1200{
Dave Wallace543852a2017-08-03 02:11:34 -04001201 vl_api_unbind_sock_t *ump;
1202 session_t *session = 0;
1203 int rv;
1204
1205 clib_spinlock_lock (&vcm->sessions_lockp);
1206 rv = vppcom_session_at_index (session_index, &session);
1207 if (PREDICT_FALSE (rv))
1208 {
1209 clib_spinlock_unlock (&vcm->sessions_lockp);
1210 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001211 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001212 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001213 return;
1214 }
1215
1216 ump = vl_msg_api_alloc (sizeof (*ump));
1217 memset (ump, 0, sizeof (*ump));
1218
1219 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1220 ump->client_index = vcm->my_client_index;
Dave Wallace60caa062017-11-10 17:07:13 -05001221 ump->handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001222 clib_spinlock_unlock (&vcm->sessions_lockp);
1223 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1224}
1225
1226static int
Dave Wallace60caa062017-11-10 17:07:13 -05001227vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001228{
1229 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1230 svm_fifo_segment_private_t *seg;
1231 int rv = VPPCOM_OK;
1232
Dave Wallace60caa062017-11-10 17:07:13 -05001233 if (VPPCOM_DEBUG > 1)
1234 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1235 " server_rx_fifo %p, refcnt = %d\n"
1236 " server_tx_fifo %p, refcnt = %d",
1237 getpid (), session_index, session->sm_seg_index,
1238 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1239 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1240
Dave Wallace543852a2017-08-03 02:11:34 -04001241 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1242 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1243 FIFO_SEGMENT_RX_FREELIST);
1244 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1245 FIFO_SEGMENT_TX_FREELIST);
1246 svm_fifo_segment_delete (seg);
1247
1248 return rv;
1249}
1250
1251static int
1252vppcom_session_unbind (u32 session_index)
1253{
Dave Wallace543852a2017-08-03 02:11:34 -04001254 int rv;
1255
1256 clib_spinlock_lock (&vcm->sessions_lockp);
1257 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1258 {
1259 clib_spinlock_unlock (&vcm->sessions_lockp);
1260 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001261 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 VPPCOM_EBADFD;
1264 }
1265 clib_spinlock_unlock (&vcm->sessions_lockp);
1266
1267 vcm->bind_session_index = session_index;
1268 vppcom_send_unbind_sock (session_index);
1269 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1270 vcm->cfg.session_timeout);
1271 if (PREDICT_FALSE (rv))
1272 {
1273 vcm->bind_session_index = ~0;
1274 if (VPPCOM_DEBUG > 0)
1275 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001276 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001277 return rv;
1278 }
1279 return VPPCOM_OK;
1280}
1281
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001282static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001283vppcom_session_disconnect (u32 session_index)
1284{
Dave Wallace543852a2017-08-03 02:11:34 -04001285 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001286 session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001287
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001288 clib_spinlock_lock (&vcm->sessions_lockp);
1289 rv = vppcom_session_at_index (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04001290 if (PREDICT_FALSE (rv))
1291 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001292 clib_spinlock_unlock (&vcm->sessions_lockp);
1293 if (VPPCOM_DEBUG > 1)
1294 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001295 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001296 return rv;
1297 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001298
1299 if (!session->is_cut_thru)
1300 {
1301 vppcom_send_disconnect (session);
1302 clib_spinlock_unlock (&vcm->sessions_lockp);
1303
1304 rv = vppcom_wait_for_session_state_change (session_index,
1305 STATE_DISCONNECT, 1.0);
Dave Wallace227867f2017-11-13 21:21:53 -05001306 /* TBD: Force clean up on error/timeout since there is no other
1307 * way to recover from a failed disconnect.
1308 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001309 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1310 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001311 getpid (), session_index, vppcom_retval_str (rv), rv);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001312 }
1313 else
Dave Wallace227867f2017-11-13 21:21:53 -05001314 {
1315 /* TBD: Handle cut-thru disconnect */
1316 clib_spinlock_unlock (&vcm->sessions_lockp);
1317 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001318
Dave Wallace543852a2017-08-03 02:11:34 -04001319 return VPPCOM_OK;
1320}
1321
1322#define foreach_sock_msg \
1323_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1324_(BIND_SOCK_REPLY, bind_sock_reply) \
1325_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1326_(ACCEPT_SESSION, accept_session) \
1327_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001328_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001329_(DISCONNECT_SESSION, disconnect_session) \
1330_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1331_(RESET_SESSION, reset_session) \
1332_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1333_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1334_(MAP_ANOTHER_SEGMENT, map_another_segment)
1335
1336static void
1337vppcom_api_hookup (void)
1338{
1339#define _(N,n) \
1340 vl_msg_api_set_handlers(VL_API_##N, #n, \
1341 vl_api_##n##_t_handler, \
1342 vl_noop_handler, \
1343 vl_api_##n##_t_endian, \
1344 vl_api_##n##_t_print, \
1345 sizeof(vl_api_##n##_t), 1);
1346 foreach_sock_msg;
1347#undef _
1348}
1349
1350static void
1351vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1352{
1353 ASSERT (vcl_cfg);
1354
1355 vcl_cfg->heapsize = (256ULL << 20);
1356 vcl_cfg->segment_baseva = 0x200000000ULL;
1357 vcl_cfg->segment_size = (256 << 20);
1358 vcl_cfg->add_segment_size = (128 << 20);
1359 vcl_cfg->preallocated_fifo_pairs = 8;
1360 vcl_cfg->rx_fifo_size = (1 << 20);
1361 vcl_cfg->tx_fifo_size = (1 << 20);
1362 vcl_cfg->event_queue_size = 2048;
1363 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1364 vcl_cfg->app_timeout = 10 * 60.0;
1365 vcl_cfg->session_timeout = 10 * 60.0;
1366 vcl_cfg->accept_timeout = 60.0;
1367}
1368
1369static void
1370vppcom_cfg_heapsize (char *conf_fname)
1371{
Dave Wallace543852a2017-08-03 02:11:34 -04001372 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1373 FILE *fp;
1374 char inbuf[4096];
1375 int argc = 1;
1376 char **argv = NULL;
1377 char *arg = NULL;
1378 char *p;
1379 int i;
1380 u8 *sizep;
1381 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001382 void *vcl_mem;
1383 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001384
1385 fp = fopen (conf_fname, "r");
1386 if (fp == NULL)
1387 {
1388 if (VPPCOM_DEBUG > 0)
1389 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1390 goto defaulted;
1391 }
1392 argv = calloc (1, sizeof (char *));
1393 if (argv == NULL)
1394 goto defaulted;
1395
1396 while (1)
1397 {
1398 if (fgets (inbuf, 4096, fp) == 0)
1399 break;
1400 p = strtok (inbuf, " \t\n");
1401 while (p != NULL)
1402 {
1403 if (*p == '#')
1404 break;
1405 argc++;
1406 char **tmp = realloc (argv, argc * sizeof (char *));
1407 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001408 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001409 argv = tmp;
1410 arg = strndup (p, 1024);
1411 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001412 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001413 argv[argc - 1] = arg;
1414 p = strtok (NULL, " \t\n");
1415 }
1416 }
1417
1418 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001419 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001420
1421 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1422 if (tmp == NULL)
1423 goto defaulted;
1424 argv = tmp;
1425 argv[argc] = NULL;
1426
1427 /*
1428 * Look for and parse the "heapsize" config parameter.
1429 * Manual since none of the clib infra has been bootstrapped yet.
1430 *
1431 * Format: heapsize <nn>[mM][gG]
1432 */
1433
1434 for (i = 1; i < (argc - 1); i++)
1435 {
1436 if (!strncmp (argv[i], "heapsize", 8))
1437 {
1438 sizep = (u8 *) argv[i + 1];
1439 size = 0;
1440 while (*sizep >= '0' && *sizep <= '9')
1441 {
1442 size *= 10;
1443 size += *sizep++ - '0';
1444 }
1445 if (size == 0)
1446 {
1447 if (VPPCOM_DEBUG > 0)
1448 clib_warning ("[%d] parse error '%s %s', "
1449 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001450 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001451 vcl_cfg->heapsize, vcl_cfg->heapsize);
1452 goto defaulted;
1453 }
1454
1455 if (*sizep == 'g' || *sizep == 'G')
1456 vcl_cfg->heapsize = size << 30;
1457 else if (*sizep == 'm' || *sizep == 'M')
1458 vcl_cfg->heapsize = size << 20;
1459 else
1460 {
1461 if (VPPCOM_DEBUG > 0)
1462 clib_warning ("[%d] parse error '%s %s', "
1463 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001464 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001465 vcl_cfg->heapsize, vcl_cfg->heapsize);
1466 goto defaulted;
1467 }
1468 }
1469 }
1470
1471defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001472 if (fp != NULL)
1473 fclose (fp);
1474 if (argv != NULL)
1475 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001476
Dave Wallace2e005bb2017-11-07 01:21:39 -05001477 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1478 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001479 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001480 {
1481 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1482 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1483 "-1, 0) failed!",
1484 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1485 return;
1486 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001487 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1488 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001489 {
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001490 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1491 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001492 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001493 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1494 if (!vcl_mem)
1495 {
1496 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1497 return;
1498 }
1499
1500 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1501 vcm = vcl_mem;
1502
1503 if (VPPCOM_DEBUG > 0)
1504 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1505 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001506}
1507
1508static void
1509vppcom_cfg_read (char *conf_fname)
1510{
Dave Wallace543852a2017-08-03 02:11:34 -04001511 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1512 int fd;
1513 unformat_input_t _input, *input = &_input;
1514 unformat_input_t _line_input, *line_input = &_line_input;
1515 u8 vc_cfg_input = 0;
1516 u8 *chroot_path;
1517 struct stat s;
1518 u32 uid, gid;
1519
1520 fd = open (conf_fname, O_RDONLY);
1521 if (fd < 0)
1522 {
1523 if (VPPCOM_DEBUG > 0)
1524 clib_warning ("[%d] open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001525 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001526 goto file_done;
1527 }
1528
1529 if (fstat (fd, &s) < 0)
1530 {
1531 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001532 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001533 goto file_done;
1534 }
1535
1536 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1537 {
1538 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001539 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001540 goto file_done;
1541 }
1542
Dave Barach59b25652017-09-10 15:04:27 -04001543 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001544
1545 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1546 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001547 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001548 unformat_skip_white_space (line_input);
1549
Dave Wallace8af20542017-10-26 03:29:30 -04001550 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001551 {
1552 vc_cfg_input = 1;
1553 continue;
1554 }
1555
1556 if (vc_cfg_input)
1557 {
1558 if (unformat (line_input, "heapsize %s", &chroot_path))
1559 {
1560 vec_terminate_c_string (chroot_path);
1561 if (VPPCOM_DEBUG > 0)
1562 clib_warning ("[%d] configured heapsize %s, "
1563 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001564 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001565 vcl_cfg->heapsize);
1566 vec_free (chroot_path);
1567 }
1568 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1569 {
1570 vec_terminate_c_string (chroot_path);
1571 vl_set_memory_root_path ((char *) chroot_path);
1572 if (VPPCOM_DEBUG > 0)
1573 clib_warning ("[%d] configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001574 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001575 chroot_path = 0; /* Don't vec_free() it! */
1576 }
1577 else if (unformat (line_input, "uid %d", &uid))
1578 {
1579 vl_set_memory_uid (uid);
1580 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001581 clib_warning ("[%d] configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001582 }
1583 else if (unformat (line_input, "gid %d", &gid))
1584 {
1585 vl_set_memory_gid (gid);
1586 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001587 clib_warning ("[%d] configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001588 }
Dave Wallace8af20542017-10-26 03:29:30 -04001589 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001590 &vcl_cfg->segment_baseva))
1591 {
1592 if (VPPCOM_DEBUG > 0)
Dave Wallace8af20542017-10-26 03:29:30 -04001593 clib_warning ("[%d] configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001594 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001595 }
1596 else if (unformat (line_input, "segment-size 0x%lx",
1597 &vcl_cfg->segment_size))
1598 {
1599 if (VPPCOM_DEBUG > 0)
1600 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001601 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001602 vcl_cfg->segment_size);
1603 }
1604 else if (unformat (line_input, "segment-size %ld",
1605 &vcl_cfg->segment_size))
1606 {
1607 if (VPPCOM_DEBUG > 0)
1608 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001609 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001610 vcl_cfg->segment_size);
1611 }
1612 else if (unformat (line_input, "add-segment-size 0x%lx",
1613 &vcl_cfg->add_segment_size))
1614 {
1615 if (VPPCOM_DEBUG > 0)
1616 clib_warning
1617 ("[%d] configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001618 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001619 vcl_cfg->add_segment_size);
1620 }
1621 else if (unformat (line_input, "add-segment-size %ld",
1622 &vcl_cfg->add_segment_size))
1623 {
1624 if (VPPCOM_DEBUG > 0)
1625 clib_warning
1626 ("[%d] configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001627 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001628 vcl_cfg->add_segment_size);
1629 }
1630 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1631 &vcl_cfg->preallocated_fifo_pairs))
1632 {
1633 if (VPPCOM_DEBUG > 0)
1634 clib_warning ("[%d] configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001635 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001636 vcl_cfg->preallocated_fifo_pairs,
1637 vcl_cfg->preallocated_fifo_pairs);
1638 }
1639 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1640 &vcl_cfg->rx_fifo_size))
1641 {
1642 if (VPPCOM_DEBUG > 0)
1643 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001644 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001645 vcl_cfg->rx_fifo_size);
1646 }
1647 else if (unformat (line_input, "rx-fifo-size %ld",
1648 &vcl_cfg->rx_fifo_size))
1649 {
1650 if (VPPCOM_DEBUG > 0)
1651 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001652 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001653 vcl_cfg->rx_fifo_size);
1654 }
1655 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1656 &vcl_cfg->tx_fifo_size))
1657 {
1658 if (VPPCOM_DEBUG > 0)
1659 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001660 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001661 vcl_cfg->tx_fifo_size);
1662 }
1663 else if (unformat (line_input, "tx-fifo-size %ld",
1664 &vcl_cfg->tx_fifo_size))
1665 {
1666 if (VPPCOM_DEBUG > 0)
1667 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001668 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001669 vcl_cfg->tx_fifo_size);
1670 }
1671 else if (unformat (line_input, "event-queue-size 0x%lx",
1672 &vcl_cfg->event_queue_size))
1673 {
1674 if (VPPCOM_DEBUG > 0)
1675 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001676 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001677 vcl_cfg->event_queue_size);
1678 }
1679 else if (unformat (line_input, "event-queue-size %ld",
1680 &vcl_cfg->event_queue_size))
1681 {
1682 if (VPPCOM_DEBUG > 0)
1683 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001684 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001685 vcl_cfg->event_queue_size);
1686 }
1687 else if (unformat (line_input, "listen-queue-size 0x%lx",
1688 &vcl_cfg->listen_queue_size))
1689 {
1690 if (VPPCOM_DEBUG > 0)
1691 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001692 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001693 vcl_cfg->listen_queue_size);
1694 }
1695 else if (unformat (line_input, "listen-queue-size %ld",
1696 &vcl_cfg->listen_queue_size))
1697 {
1698 if (VPPCOM_DEBUG > 0)
1699 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001700 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001701 vcl_cfg->listen_queue_size);
1702 }
1703 else if (unformat (line_input, "app-timeout %f",
1704 &vcl_cfg->app_timeout))
1705 {
1706 if (VPPCOM_DEBUG > 0)
1707 clib_warning ("[%d] configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001708 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001709 }
1710 else if (unformat (line_input, "session-timeout %f",
1711 &vcl_cfg->session_timeout))
1712 {
1713 if (VPPCOM_DEBUG > 0)
1714 clib_warning ("[%d] configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001715 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001716 }
1717 else if (unformat (line_input, "accept-timeout %f",
1718 &vcl_cfg->accept_timeout))
1719 {
1720 if (VPPCOM_DEBUG > 0)
1721 clib_warning ("[%d] configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001722 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001723 }
Dave Wallace774169b2017-11-01 20:07:40 -04001724 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001725 {
Dave Wallace774169b2017-11-01 20:07:40 -04001726 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001727 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001728 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001729 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001730 }
Dave Wallace774169b2017-11-01 20:07:40 -04001731 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001732 {
Dave Wallace774169b2017-11-01 20:07:40 -04001733 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001734 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001735 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001736 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001737 }
Dave Wallace774169b2017-11-01 20:07:40 -04001738 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001739 {
Dave Wallace774169b2017-11-01 20:07:40 -04001740 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001741 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001742 clib_warning ("[%d] configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001743 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001744 }
1745 else if (unformat (line_input, "app-scope-global"))
1746 {
1747 vcl_cfg->app_scope_global = 1;
1748 if (VPPCOM_DEBUG > 0)
1749 clib_warning ("[%d] configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001750 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001751 }
1752 else if (unformat (line_input, "namespace-secret %lu",
1753 &vcl_cfg->namespace_secret))
1754 {
1755 if (VPPCOM_DEBUG > 0)
1756 clib_warning
1757 ("[%d] configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001758 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04001759 vcl_cfg->namespace_secret);
1760 }
1761 else if (unformat (line_input, "namespace-id %v",
1762 &vcl_cfg->namespace_id))
1763 {
1764 vl_api_application_attach_t *mp;
1765 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1766 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1767 if (nsid_vec_len > max_nsid_vec_len)
1768 {
1769 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1770 if (VPPCOM_DEBUG > 0)
1771 clib_warning ("[%d] configured namespace_id is too long,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05001772 " truncated to %d characters!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001773 max_nsid_vec_len);
1774 }
1775
1776 if (VPPCOM_DEBUG > 0)
1777 clib_warning ("[%d] configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001778 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001779 }
Dave Wallace543852a2017-08-03 02:11:34 -04001780 else if (unformat (line_input, "}"))
1781 {
1782 vc_cfg_input = 0;
1783 if (VPPCOM_DEBUG > 0)
1784 clib_warning ("[%d] completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001785 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001786 goto input_done;
1787 }
1788 else
1789 {
1790 if (line_input->buffer[line_input->index] != '#')
1791 {
1792 clib_warning ("[%d] Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001793 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001794 &line_input->buffer[line_input->index]);
1795 }
1796 }
1797 }
1798 }
1799
1800input_done:
1801 unformat_free (input);
1802
1803file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001804 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001805 close (fd);
1806}
1807
1808/*
1809 * VPPCOM Public API functions
1810 */
1811int
1812vppcom_app_create (char *app_name)
1813{
Dave Wallace543852a2017-08-03 02:11:34 -04001814 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1815 u8 *heap;
1816 mheap_t *h;
1817 int rv;
1818
1819 if (!vcm->init)
1820 {
1821 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001822 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001823
1824 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001825 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001826 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1827 if (env_var_str)
1828 {
1829 u32 tmp;
1830 if (sscanf (env_var_str, "%u", &tmp) != 1)
1831 clib_warning ("[%d] Invalid debug level specified in "
1832 "the environment variable "
1833 VPPCOM_ENV_DEBUG
1834 " (%s)!\n", getpid (), env_var_str);
1835 else
1836 {
1837 vcm->debug = tmp;
1838 clib_warning ("[%d] configured debug level (%u) from "
1839 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1840 }
1841 }
Dave Wallace8af20542017-10-26 03:29:30 -04001842 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001843 if (!conf_fname)
1844 {
1845 conf_fname = VPPCOM_CONF_DEFAULT;
1846 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001847 clib_warning ("[%d] getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001848 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001849 }
1850 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001851 clib_fifo_validate (vcm->client_session_index_fifo,
1852 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001853 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04001854 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1855 if (env_var_str)
1856 {
1857 u32 ns_id_vec_len = strlen (env_var_str);
1858
1859 vec_reset_length (vcm->cfg.namespace_id);
1860 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1861 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1862
1863 if (VPPCOM_DEBUG > 0)
1864 clib_warning ("[%d] configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001865 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001866 vcm->cfg.namespace_id);
1867 }
1868 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1869 if (env_var_str)
1870 {
1871 u64 tmp;
1872 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1873 clib_warning ("[%d] Invalid namespace secret specified in "
1874 "the environment variable "
1875 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001876 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001877 else
1878 {
1879 vcm->cfg.namespace_secret = tmp;
1880 if (VPPCOM_DEBUG > 0)
1881 clib_warning ("[%d] configured namespace secret (%lu) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001882 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04001883 vcm->cfg.namespace_secret);
1884 }
1885 }
Dave Wallace774169b2017-11-01 20:07:40 -04001886 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001887 {
Dave Wallace774169b2017-11-01 20:07:40 -04001888 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001889 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001890 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001891 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001892 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001893 }
Dave Wallace774169b2017-11-01 20:07:40 -04001894 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001895 {
Dave Wallace774169b2017-11-01 20:07:40 -04001896 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04001897 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04001898 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001899 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001900 vcm->cfg.app_proxy_transport_udp);
1901 }
1902 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1903 {
1904 vcm->cfg.app_scope_local = 1;
1905 if (VPPCOM_DEBUG > 0)
1906 clib_warning ("[%d] configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001907 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001908 vcm->cfg.app_scope_local);
1909 }
1910 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1911 {
1912 vcm->cfg.app_scope_global = 1;
1913 if (VPPCOM_DEBUG > 0)
1914 clib_warning ("[%d] configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001915 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04001916 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001917 }
1918
Dave Wallace543852a2017-08-03 02:11:34 -04001919 vcm->bind_session_index = ~0;
1920 vcm->main_cpu = os_get_thread_index ();
1921 heap = clib_mem_get_per_cpu_heap ();
1922 h = mheap_header (heap);
1923
1924 /* make the main heap thread-safe */
1925 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1926
1927 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1928
1929 clib_time_init (&vcm->clib_time);
1930 vppcom_init_error_string_table ();
1931 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1932 20 /* timeout in secs */ );
1933 clib_spinlock_init (&vcm->sessions_lockp);
1934 vppcom_api_hookup ();
1935 }
1936
1937 if (vcm->my_client_index == ~0)
1938 {
1939 vcm->app_state = STATE_APP_START;
1940 rv = vppcom_connect_to_vpp (app_name);
1941 if (rv)
1942 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001943 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001944 return rv;
1945 }
1946
1947 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001948 clib_warning ("[%d] sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001949
1950 rv = vppcom_app_session_enable ();
1951 if (rv)
1952 {
1953 clib_warning ("[%d] vppcom_app_session_enable() failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001954 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001955 return rv;
1956 }
1957
1958 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001959 clib_warning ("[%d] sending app attach", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001960
1961 rv = vppcom_app_attach ();
1962 if (rv)
1963 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05001964 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001965 return rv;
1966 }
Dave Wallace543852a2017-08-03 02:11:34 -04001967
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001968 if (VPPCOM_DEBUG > 0)
1969 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001970 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001971 vcm->my_client_index);
1972 }
Dave Wallace543852a2017-08-03 02:11:34 -04001973
1974 return VPPCOM_OK;
1975}
1976
1977void
1978vppcom_app_destroy (void)
1979{
Dave Wallace543852a2017-08-03 02:11:34 -04001980 int rv;
1981
1982 if (vcm->my_client_index == ~0)
1983 return;
1984
1985 if (VPPCOM_DEBUG > 0)
1986 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001987 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001988
1989 vppcom_app_detach ();
1990 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1991 if (PREDICT_FALSE (rv))
1992 {
1993 if (VPPCOM_DEBUG > 0)
1994 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001995 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04001996 }
1997 vl_client_disconnect_from_vlib ();
1998 vcm->my_client_index = ~0;
1999 vcm->app_state = STATE_APP_START;
2000}
2001
2002int
2003vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2004{
Dave Wallace543852a2017-08-03 02:11:34 -04002005 session_t *session;
2006 u32 session_index;
2007
2008 clib_spinlock_lock (&vcm->sessions_lockp);
2009 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002010 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002011 session_index = session - vcm->sessions;
2012
2013 session->vrf = vrf;
2014 session->proto = proto;
2015 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002016 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002017 clib_spinlock_unlock (&vcm->sessions_lockp);
2018
2019 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002020 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002021
2022 return (int) session_index;
2023}
2024
2025int
2026vppcom_session_close (uint32_t session_index)
2027{
Dave Wallace543852a2017-08-03 02:11:34 -04002028 session_t *session = 0;
2029 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002030 u8 is_server;
2031 u8 is_listen;
2032 u8 is_cut_thru;
2033 u8 is_vep;
2034 u8 is_vep_session;
2035 u32 next_sid;
2036 u32 vep_idx;
2037 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002038
2039 clib_spinlock_lock (&vcm->sessions_lockp);
2040 rv = vppcom_session_at_index (session_index, &session);
2041 if (PREDICT_FALSE (rv))
2042 {
Dave Wallace543852a2017-08-03 02:11:34 -04002043 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002044 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002045 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002046 clib_spinlock_unlock (&vcm->sessions_lockp);
2047 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002048 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002049 is_server = session->is_server;
2050 is_listen = session->is_listen;
2051 is_cut_thru = session->is_cut_thru;
2052 is_vep = session->is_vep;
2053 is_vep_session = session->is_vep_session;
2054 next_sid = session->vep.next_sid;
2055 vep_idx = session->vep.vep_idx;
2056 state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04002057 clib_spinlock_unlock (&vcm->sessions_lockp);
2058
2059 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002060 clib_warning ("[%d] sid %d", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002061
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002062 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002063 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002064 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002065 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002066 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallace19481612017-09-15 18:47:44 -04002067 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002068 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002069 "rv = %s (%d)", getpid (), vep_idx, next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002070 vppcom_retval_str (rv), rv);
2071
2072 clib_spinlock_lock (&vcm->sessions_lockp);
2073 rv = vppcom_session_at_index (session_index, &session);
2074 if (PREDICT_FALSE (rv))
2075 {
2076 if (VPPCOM_DEBUG > 0)
2077 clib_warning
2078 ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002079 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002080 clib_spinlock_unlock (&vcm->sessions_lockp);
2081 goto done;
2082 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002083 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002084 clib_spinlock_unlock (&vcm->sessions_lockp);
2085 }
2086 }
2087 else
2088 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002089 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002090 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002091 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2092 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2093 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002094 "rv = %s (%d)", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002095 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002096 }
2097
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002098 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002099 {
Dave Wallace60caa062017-11-10 17:07:13 -05002100 rv = vppcom_session_unbind_cut_thru (session, session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002101 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2102 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2103 "rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002104 getpid (), session_index,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002105 vppcom_retval_str (rv), rv);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002106 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002107 else if (is_server && is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002108 {
2109 rv = vppcom_session_unbind (session_index);
2110 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2111 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002112 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002113 vppcom_retval_str (rv), rv);
2114 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002115 else if (state == STATE_CONNECT)
2116 if (vppcom_session_disconnect (session_index))
2117 goto done;
Dave Wallace19481612017-09-15 18:47:44 -04002118 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002119 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002120 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002121 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002122done:
Dave Wallace543852a2017-08-03 02:11:34 -04002123 return rv;
2124}
2125
2126int
2127vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2128{
Dave Wallace543852a2017-08-03 02:11:34 -04002129 session_t *session = 0;
2130 int rv;
2131
2132 if (!ep || !ep->ip)
2133 return VPPCOM_EINVAL;
2134
2135 clib_spinlock_lock (&vcm->sessions_lockp);
2136 rv = vppcom_session_at_index (session_index, &session);
2137 if (PREDICT_FALSE (rv))
2138 {
2139 clib_spinlock_unlock (&vcm->sessions_lockp);
2140 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002141 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002142 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002143 return rv;
2144 }
2145
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002146 if (session->is_vep)
2147 {
2148 clib_spinlock_unlock (&vcm->sessions_lockp);
2149 if (VPPCOM_DEBUG > 0)
2150 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002151 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002152 return VPPCOM_EBADFD;
2153 }
2154
Dave Wallace543852a2017-08-03 02:11:34 -04002155 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002156 session->lcl_addr.is_ip4 = ep->is_ip4;
2157 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002158 session->lcl_port = ep->port;
2159
2160 if (VPPCOM_DEBUG > 0)
2161 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002162 getpid (), session_index, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07002163 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2164 clib_net_to_host_u16 (session->lcl_port));
Dave Wallace543852a2017-08-03 02:11:34 -04002165
2166 clib_spinlock_unlock (&vcm->sessions_lockp);
2167 return VPPCOM_OK;
2168}
2169
2170int
Dave Wallace33e002b2017-09-06 01:20:02 -04002171vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002172{
Dave Wallace33e002b2017-09-06 01:20:02 -04002173 session_t *listen_session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002174 int rv;
2175
2176 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002177 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002178 if (PREDICT_FALSE (rv))
2179 {
2180 clib_spinlock_unlock (&vcm->sessions_lockp);
2181 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002182 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002183 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002184 return rv;
2185 }
2186
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002187 if (listen_session->is_vep)
2188 {
2189 clib_spinlock_unlock (&vcm->sessions_lockp);
2190 if (VPPCOM_DEBUG > 0)
2191 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002192 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002193 return VPPCOM_EBADFD;
2194 }
2195
Dave Wallacee695cb42017-11-02 22:04:42 -04002196 if (listen_session->is_listen)
2197 {
2198 clib_spinlock_unlock (&vcm->sessions_lockp);
2199 if (VPPCOM_DEBUG > 0)
2200 clib_warning ("[%d] sid (%u) is already in listen state!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002201 getpid (), listen_session_index);
Dave Wallacee695cb42017-11-02 22:04:42 -04002202 return VPPCOM_OK;
2203 }
2204
Dave Wallace543852a2017-08-03 02:11:34 -04002205 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002206 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002207
2208 ASSERT (vcm->bind_session_index == ~0);
Dave Wallace33e002b2017-09-06 01:20:02 -04002209 vcm->bind_session_index = listen_session_index;
2210 vppcom_send_bind_sock (listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002211 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002212 rv =
2213 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2214 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002215 if (PREDICT_FALSE (rv))
2216 {
2217 vcm->bind_session_index = ~0;
2218 if (VPPCOM_DEBUG > 0)
2219 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002220 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002221 return rv;
2222 }
2223
2224 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002225 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002226 if (PREDICT_FALSE (rv))
2227 {
2228 clib_spinlock_unlock (&vcm->sessions_lockp);
2229 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002230 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002231 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002232 return rv;
2233 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002234 listen_session->is_listen = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002235 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002236 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002237
2238 return VPPCOM_OK;
2239}
2240
2241int
2242vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace227867f2017-11-13 21:21:53 -05002243 uint32_t flags, double wait_for_time)
Dave Wallace543852a2017-08-03 02:11:34 -04002244{
Dave Wallace33e002b2017-09-06 01:20:02 -04002245 session_t *listen_session = 0;
2246 session_t *client_session = 0;
Dave Wallace227867f2017-11-13 21:21:53 -05002247 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002248 int rv;
2249 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002250 char *cut_thru_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002251
2252 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002253 rv = vppcom_session_at_index (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002254 if (PREDICT_FALSE (rv))
2255 {
2256 clib_spinlock_unlock (&vcm->sessions_lockp);
2257 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002258 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002259 getpid (), listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002260 return rv;
2261 }
2262
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002263 if (listen_session->is_vep)
2264 {
2265 clib_spinlock_unlock (&vcm->sessions_lockp);
2266 if (VPPCOM_DEBUG > 0)
2267 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002268 getpid (), listen_session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002269 return VPPCOM_EBADFD;
2270 }
2271
Dave Wallace33e002b2017-09-06 01:20:02 -04002272 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002273 {
2274 clib_spinlock_unlock (&vcm->sessions_lockp);
2275 if (VPPCOM_DEBUG > 0)
2276 clib_warning ("[%d] session not in listen state, state = %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002277 getpid (),
Dave Wallace33e002b2017-09-06 01:20:02 -04002278 vppcom_session_state_str (listen_session->state));
Dave Wallace543852a2017-08-03 02:11:34 -04002279 return VPPCOM_EBADFD;
2280 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002281 wait_for = listen_session->is_nonblocking ? 0 :
Dave Wallace543852a2017-08-03 02:11:34 -04002282 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2283
2284 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002285 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002286 listen_session_index,
Dave Wallace33e002b2017-09-06 01:20:02 -04002287 vppcom_session_state_str (listen_session->state),
2288 listen_session->state);
Dave Wallace543852a2017-08-03 02:11:34 -04002289 clib_spinlock_unlock (&vcm->sessions_lockp);
2290
2291 while (1)
2292 {
2293 rv = vppcom_wait_for_client_session_index (wait_for);
2294 if (rv)
2295 {
2296 if ((VPPCOM_DEBUG > 0))
2297 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002298 getpid (), listen_session_index,
Dave Wallace543852a2017-08-03 02:11:34 -04002299 vppcom_retval_str (rv), rv);
2300 if ((wait_for == 0) || (wait_for_time > 0))
2301 return rv;
2302 }
2303 else
2304 break;
2305 }
2306
Dave Wallace543852a2017-08-03 02:11:34 -04002307 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002308 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002309 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002310 ASSERT (rv == VPPCOM_OK);
Dave Wallace35830af2017-10-09 01:43:42 -04002311 ASSERT (client_session->peer_addr.is_ip4 ==
2312 listen_session->lcl_addr.is_ip4);
Dave Wallace543852a2017-08-03 02:11:34 -04002313
Dave Wallace227867f2017-11-13 21:21:53 -05002314 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002315 if (VPPCOM_DEBUG > 0)
Dave Wallace227867f2017-11-13 21:21:53 -05002316 clib_warning ("[%d] Got a request: client sid %d, flags %d, "
2317 " is_nonblocking %u", getpid (), client_session_index,
2318 flags, client_session->is_nonblocking);
Dave Wallace543852a2017-08-03 02:11:34 -04002319
Dave Wallace33e002b2017-09-06 01:20:02 -04002320 ep->vrf = client_session->vrf;
2321 ep->is_cut_thru = client_session->is_cut_thru;
Dave Wallace35830af2017-10-09 01:43:42 -04002322 ep->is_ip4 = client_session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07002323 ep->port = client_session->peer_port;
Dave Wallace35830af2017-10-09 01:43:42 -04002324 if (client_session->peer_addr.is_ip4)
2325 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2326 sizeof (ip4_address_t));
Dave Wallace33e002b2017-09-06 01:20:02 -04002327 else
Dave Wallace35830af2017-10-09 01:43:42 -04002328 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2329 sizeof (ip6_address_t));
Dave Wallace60caa062017-11-10 17:07:13 -05002330
2331 if (client_session->is_server && client_session->is_cut_thru)
2332 {
2333 static svm_fifo_segment_create_args_t _a;
2334 svm_fifo_segment_create_args_t *a = &_a;
2335 svm_fifo_segment_private_t *seg;
2336
2337 cut_thru_str = "cut-thru ";
2338
2339 /* Create the segment */
2340 memset (a, 0, sizeof (*a));
2341 a->segment_name = (char *)
2342 format ((u8 *) a->segment_name, "%d:segment%d%c",
2343 getpid (), vcm->unique_segment_index++, 0);
2344 a->segment_size = vcm->cfg.segment_size;
2345 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2346 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2347 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2348
2349 rv = svm_fifo_segment_create (a);
2350 if (PREDICT_FALSE (rv))
2351 {
2352 if (VPPCOM_DEBUG > 1)
2353 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2354 getpid (), a->segment_name);
2355 vec_reset_length (a->new_segment_indices);
2356 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002357 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002358 clib_spinlock_unlock (&vcm->sessions_lockp);
2359 return VPPCOM_ENOMEM;
2360 }
2361
2362 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2363 client_session->sm_seg_index = a->new_segment_indices[0];
2364 vec_free (a->new_segment_indices);
2365
2366 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2367 client_session->server_rx_fifo =
2368 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2369 FIFO_SEGMENT_RX_FREELIST);
2370 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2371 {
2372 svm_fifo_segment_delete (seg);
2373 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2374 getpid (), vcm->cfg.rx_fifo_size,
2375 vcm->cfg.rx_fifo_size);
2376 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002377 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002378 clib_spinlock_unlock (&vcm->sessions_lockp);
2379 return VPPCOM_ENOMEM;
2380 }
2381 client_session->server_rx_fifo->master_session_index =
2382 client_session_index;
2383
2384 client_session->server_tx_fifo =
2385 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2386 FIFO_SEGMENT_TX_FREELIST);
2387 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2388 {
2389 svm_fifo_segment_delete (seg);
2390 if (VPPCOM_DEBUG > 1)
2391 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2392 getpid (), vcm->cfg.tx_fifo_size,
2393 vcm->cfg.tx_fifo_size);
2394 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallace227867f2017-11-13 21:21:53 -05002395 vppcom_send_connect_session_reply (client_session, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002396 clib_spinlock_unlock (&vcm->sessions_lockp);
2397 return VPPCOM_ENOMEM;
2398 }
2399 client_session->server_tx_fifo->master_session_index =
2400 client_session_index;
2401
2402 if (VPPCOM_DEBUG > 1)
2403 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2404 getpid (), client_session->segment_name,
2405 client_session->server_rx_fifo,
2406 client_session->server_tx_fifo);
2407
2408#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2409 {
2410 void *oldheap;
2411 ssvm_shared_header_t *sh = seg->ssvm.sh;
2412
2413 ssvm_lock_non_recursive (sh, 1);
2414 oldheap = ssvm_push_heap (sh);
2415 event_q = client_session->vpp_event_queue =
2416 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2417 sizeof (session_fifo_event_t),
2418 getpid (), 0 /* signal not sent */ );
2419 ssvm_pop_heap (oldheap);
2420 ssvm_unlock_non_recursive (sh);
2421 }
2422#endif
Dave Wallace227867f2017-11-13 21:21:53 -05002423 vppcom_send_connect_session_reply (client_session, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05002424 }
2425 else
2426 {
2427 cut_thru_str = " ";
2428 vppcom_send_accept_session_reply (0, client_session->vpp_handle);
2429 }
2430
Stevenac1f96d2017-10-24 16:03:58 -07002431 if (VPPCOM_DEBUG > 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002432 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2433 "%U peer port %u",
2434 getpid (), client_session_index, cut_thru_str,
2435 format_ip46_address, &client_session->peer_addr.ip46,
Stevenac1f96d2017-10-24 16:03:58 -07002436 client_session->peer_addr.is_ip4,
2437 clib_net_to_host_u16 (client_session->peer_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002438
Dave Wallace543852a2017-08-03 02:11:34 -04002439 clib_spinlock_unlock (&vcm->sessions_lockp);
2440 return (int) client_session_index;
2441}
2442
2443int
2444vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2445{
Dave Wallace543852a2017-08-03 02:11:34 -04002446 session_t *session = 0;
2447 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002448
2449 clib_spinlock_lock (&vcm->sessions_lockp);
2450 rv = vppcom_session_at_index (session_index, &session);
2451 if (PREDICT_FALSE (rv))
2452 {
2453 clib_spinlock_unlock (&vcm->sessions_lockp);
2454 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002455 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002456 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002457 return rv;
2458 }
2459
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002460 if (session->is_vep)
2461 {
2462 clib_spinlock_unlock (&vcm->sessions_lockp);
2463 if (VPPCOM_DEBUG > 0)
2464 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002465 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002466 return VPPCOM_EBADFD;
2467 }
2468
Dave Wallace543852a2017-08-03 02:11:34 -04002469 if (session->state == STATE_CONNECT)
2470 {
2471 clib_spinlock_unlock (&vcm->sessions_lockp);
2472 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002473 clib_warning ("[%d] session, sid (%u) already connected!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002474 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002475 return VPPCOM_OK;
2476 }
2477
2478 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002479 session->peer_addr.is_ip4 = server_ep->is_ip4;
2480 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002481 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002482
2483 if (VPPCOM_DEBUG > 0)
2484 {
2485 u8 *ip_str = format (0, "%U", format_ip46_address,
Dave Wallace35830af2017-10-09 01:43:42 -04002486 &session->peer_addr.ip46,
2487 session->peer_addr.is_ip4);
Dave Wallace7876d392017-10-19 03:53:57 -04002488 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002489 getpid (), session_index, ip_str,
Stevenac1f96d2017-10-24 16:03:58 -07002490 clib_net_to_host_u16 (session->peer_port),
Dave Wallace7876d392017-10-19 03:53:57 -04002491 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002492 vec_free (ip_str);
2493 }
2494
2495 vppcom_send_connect_sock (session, session_index);
2496 clib_spinlock_unlock (&vcm->sessions_lockp);
2497 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2498 vcm->cfg.session_timeout);
2499 if (PREDICT_FALSE (rv))
2500 {
2501 if (VPPCOM_DEBUG > 0)
2502 clib_warning ("[%d] connect timed out, rv = %s (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002503 getpid (), vppcom_retval_str (rv), rv);
Dave Wallace543852a2017-08-03 02:11:34 -04002504 return rv;
2505 }
Dave Wallace7876d392017-10-19 03:53:57 -04002506 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002507 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04002508
Dave Wallace543852a2017-08-03 02:11:34 -04002509 return VPPCOM_OK;
2510}
2511
Steven58f464e2017-10-25 12:33:12 -07002512static inline int
2513vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2514 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002515{
Dave Wallace543852a2017-08-03 02:11:34 -04002516 session_t *session = 0;
2517 svm_fifo_t *rx_fifo;
2518 int n_read = 0;
2519 int rv;
2520 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002521 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002522
2523 ASSERT (buf);
2524
2525 clib_spinlock_lock (&vcm->sessions_lockp);
2526 rv = vppcom_session_at_index (session_index, &session);
2527 if (PREDICT_FALSE (rv))
2528 {
2529 clib_spinlock_unlock (&vcm->sessions_lockp);
2530 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002531 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002532 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002533 return rv;
2534 }
2535
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002536 if (session->is_vep)
2537 {
2538 clib_spinlock_unlock (&vcm->sessions_lockp);
2539 if (VPPCOM_DEBUG > 0)
2540 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002541 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002542 return VPPCOM_EBADFD;
2543 }
2544
Dave Wallace33e002b2017-09-06 01:20:02 -04002545 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002546 {
Dave Wallace543852a2017-08-03 02:11:34 -04002547 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace33e002b2017-09-06 01:20:02 -04002548 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002549 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002550 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002551 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002552 }
Dave Wallace543852a2017-08-03 02:11:34 -04002553
Dave Wallace33e002b2017-09-06 01:20:02 -04002554 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2555 session->server_rx_fifo : session->server_tx_fifo);
2556 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2557 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace60caa062017-11-10 17:07:13 -05002558 poll_et =
2559 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002560 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002561
2562 do
2563 {
Steven58f464e2017-10-25 12:33:12 -07002564 if (peek)
2565 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2566 else
2567 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002568 }
2569 while (!session->is_nonblocking && (n_read <= 0));
2570
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002571 if (poll_et && (n_read <= 0))
2572 {
2573 clib_spinlock_lock (&vcm->sessions_lockp);
2574 session->vep.et_mask |= EPOLLIN;
2575 clib_spinlock_unlock (&vcm->sessions_lockp);
2576 }
Dave Wallace543852a2017-08-03 02:11:34 -04002577
Dave Wallacef7f809c2017-10-03 01:48:42 -04002578 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
Dave Wallace2e005bb2017-11-07 01:21:39 -05002579 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002580 session_index, n_read, fifo_str, rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002581
2582 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002583}
2584
Steven58f464e2017-10-25 12:33:12 -07002585int
2586vppcom_session_read (uint32_t session_index, void *buf, int n)
2587{
2588 return (vppcom_session_read_internal (session_index, buf, n, 0));
2589}
2590
2591static int
2592vppcom_session_peek (uint32_t session_index, void *buf, int n)
2593{
2594 return (vppcom_session_read_internal (session_index, buf, n, 1));
2595}
2596
Dave Wallace543852a2017-08-03 02:11:34 -04002597static inline int
2598vppcom_session_read_ready (session_t * session, u32 session_index)
2599{
Dave Wallace498b3a52017-11-09 13:00:34 -05002600 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002601 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002602 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002603
2604 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002605 if (session->is_vep)
2606 {
2607 clib_spinlock_unlock (&vcm->sessions_lockp);
2608 if (VPPCOM_DEBUG > 0)
2609 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002610 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002611 return VPPCOM_EBADFD;
2612 }
2613
Dave Wallace33e002b2017-09-06 01:20:02 -04002614 if (session->state == STATE_DISCONNECT)
Dave Wallace543852a2017-08-03 02:11:34 -04002615 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002616 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002617 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002618 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002619 return VPPCOM_ECONNRESET;
Dave Wallace543852a2017-08-03 02:11:34 -04002620 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002621
2622 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04002623 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2624 else
2625 {
Dave Wallace33e002b2017-09-06 01:20:02 -04002626 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2627 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002628
Dave Wallace33e002b2017-09-06 01:20:02 -04002629 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002630 }
2631
Dave Wallace60caa062017-11-10 17:07:13 -05002632 poll_et =
2633 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2634 if (poll_et && (ready == 0))
2635 {
2636 if (VPPCOM_DEBUG > 11)
2637 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2638 session_index, session->vep.et_mask);
2639 session->vep.et_mask |= EPOLLIN;
2640 if (VPPCOM_DEBUG > 11)
2641 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2642 session_index, session->vep.et_mask);
2643 }
2644
2645 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2646 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2647 "et_mask 0x%x",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002648 getpid (), session_index, session->is_listen,
Dave Wallace543852a2017-08-03 02:11:34 -04002649 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
Dave Wallace60caa062017-11-10 17:07:13 -05002650 rx_fifo, ready, session->vep.et_mask);
Dave Wallace543852a2017-08-03 02:11:34 -04002651 return ready;
2652}
2653
2654int
2655vppcom_session_write (uint32_t session_index, void *buf, int n)
2656{
Dave Wallace543852a2017-08-03 02:11:34 -04002657 session_t *session = 0;
2658 svm_fifo_t *tx_fifo;
2659 unix_shared_memory_queue_t *q;
2660 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002661 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002662 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002663 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002664
2665 ASSERT (buf);
2666
2667 clib_spinlock_lock (&vcm->sessions_lockp);
2668 rv = vppcom_session_at_index (session_index, &session);
2669 if (PREDICT_FALSE (rv))
2670 {
2671 clib_spinlock_unlock (&vcm->sessions_lockp);
2672 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002673 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002674 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002675 return rv;
2676 }
2677
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002678 if (session->is_vep)
2679 {
2680 clib_spinlock_unlock (&vcm->sessions_lockp);
2681 if (VPPCOM_DEBUG > 0)
2682 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002683 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002684 return VPPCOM_EBADFD;
2685 }
2686
Dave Wallace33e002b2017-09-06 01:20:02 -04002687 if (session->state == STATE_DISCONNECT)
2688 {
2689 clib_spinlock_unlock (&vcm->sessions_lockp);
2690 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002692 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002693 return VPPCOM_ECONNRESET;
2694 }
2695
Dave Wallace543852a2017-08-03 02:11:34 -04002696 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2697 session->server_tx_fifo : session->server_rx_fifo);
2698 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2699 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002700 q = session->vpp_event_queue;
Dave Wallace60caa062017-11-10 17:07:13 -05002701 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2702 (EPOLLET | EPOLLOUT));
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002703 clib_spinlock_unlock (&vcm->sessions_lockp);
2704
Dave Wallace543852a2017-08-03 02:11:34 -04002705 do
2706 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002707 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002708 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002709 while (!session->is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002710
2711 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002713 {
2714 int rval;
2715
2716 /* Fabricate TX event, send to vpp */
2717 evt.fifo = tx_fifo;
2718 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002719
Dave Wallace543852a2017-08-03 02:11:34 -04002720 rval = vppcom_session_at_index (session_index, &session);
2721 if (PREDICT_FALSE (rval))
2722 {
Dave Wallace543852a2017-08-03 02:11:34 -04002723 if (VPPCOM_DEBUG > 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002725 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002726 return rval;
2727 }
Dave Wallace543852a2017-08-03 02:11:34 -04002728 ASSERT (q);
2729 unix_shared_memory_queue_add (q, (u8 *) & evt,
2730 0 /* do wait for mutex */ );
2731 }
2732
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002733 if (poll_et && (n_write <= 0))
2734 {
2735 clib_spinlock_lock (&vcm->sessions_lockp);
2736 session->vep.et_mask |= EPOLLOUT;
2737 clib_spinlock_unlock (&vcm->sessions_lockp);
2738 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002739
Dave Wallace543852a2017-08-03 02:11:34 -04002740 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002741 {
2742 if (n_write == -2)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002743 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002744 session_index, fifo_str, tx_fifo);
2745 else
Dave Wallace2e005bb2017-11-07 01:21:39 -05002746 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002747 session_index, n_write, fifo_str, tx_fifo);
2748 }
2749 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002750}
2751
2752static inline int
2753vppcom_session_write_ready (session_t * session, u32 session_index)
2754{
Dave Wallace543852a2017-08-03 02:11:34 -04002755 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04002756 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002758 u32 poll_et;
Dave Wallace543852a2017-08-03 02:11:34 -04002759
2760 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002761 if (session->is_vep)
2762 {
2763 clib_spinlock_unlock (&vcm->sessions_lockp);
2764 if (VPPCOM_DEBUG > 0)
2765 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002766 getpid (), session_index);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002767 return VPPCOM_EBADFD;
2768 }
2769
Dave Wallace33e002b2017-09-06 01:20:02 -04002770 if (session->state == STATE_DISCONNECT)
2771 {
2772 if (VPPCOM_DEBUG > 0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002773 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002774 getpid (), session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002775 return VPPCOM_ECONNRESET;
2776 }
2777
Dave Wallace543852a2017-08-03 02:11:34 -04002778 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2779 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04002780 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2781 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04002782
Dave Wallacef7f809c2017-10-03 01:48:42 -04002783 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002784
Dave Wallace33e002b2017-09-06 01:20:02 -04002785 if (VPPCOM_DEBUG > 3)
Dave Wallace2e005bb2017-11-07 01:21:39 -05002786 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 session_index, fifo_str, tx_fifo, ready);
Dave Wallace60caa062017-11-10 17:07:13 -05002788 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2789 (EPOLLET | EPOLLOUT));
2790 if (poll_et && (ready == 0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 session->vep.et_mask |= EPOLLOUT;
2792
2793 return ready;
Dave Wallace543852a2017-08-03 02:11:34 -04002794}
2795
2796int
2797vppcom_select (unsigned long n_bits, unsigned long *read_map,
2798 unsigned long *write_map, unsigned long *except_map,
2799 double time_to_wait)
2800{
Dave Wallace543852a2017-08-03 02:11:34 -04002801 u32 session_index;
2802 session_t *session = 0;
2803 int rv, bits_set = 0;
2804 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2805 u32 minbits = clib_max (n_bits, BITS (uword));
2806
2807 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2808
Dave Wallace7876d392017-10-19 03:53:57 -04002809 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002810 {
2811 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2812 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2813 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2814 }
Dave Wallace7876d392017-10-19 03:53:57 -04002815 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002816 {
2817 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2818 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2819 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2820 }
Dave Wallace7876d392017-10-19 03:53:57 -04002821 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002822 {
2823 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2824 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2825 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2826 }
2827
2828 do
2829 {
2830 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002831 if (n_bits)
2832 {
2833 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002834 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002835 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2836 ({
2837 clib_spinlock_lock (&vcm->sessions_lockp);
2838 rv = vppcom_session_at_index (session_index, &session);
2839 if (rv < 0)
2840 {
2841 clib_spinlock_unlock (&vcm->sessions_lockp);
2842 if (VPPCOM_DEBUG > 1)
2843 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002844 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002845 session_index);
2846 bits_set = VPPCOM_EBADFD;
2847 goto select_done;
2848 }
2849
2850 rv = vppcom_session_read_ready (session, session_index);
2851 clib_spinlock_unlock (&vcm->sessions_lockp);
2852 if (except_map && vcm->ex_bitmap &&
2853 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2854 (rv < 0))
2855 {
2856 // TBD: clib_warning
2857 clib_bitmap_set_no_check (except_map, session_index, 1);
2858 bits_set++;
2859 }
2860 else if (rv > 0)
2861 {
2862 // TBD: clib_warning
2863 clib_bitmap_set_no_check (read_map, session_index, 1);
2864 bits_set++;
2865 }
2866 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002867 }
2868
Dave Wallacee22aa742017-10-20 12:30:38 -04002869 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002870 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002871 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2872 ({
2873 clib_spinlock_lock (&vcm->sessions_lockp);
2874 rv = vppcom_session_at_index (session_index, &session);
2875 if (rv < 0)
2876 {
2877 clib_spinlock_unlock (&vcm->sessions_lockp);
2878 if (VPPCOM_DEBUG > 0)
2879 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002880 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002881 session_index);
2882 bits_set = VPPCOM_EBADFD;
2883 goto select_done;
2884 }
Dave Wallace543852a2017-08-03 02:11:34 -04002885
Dave Wallacee22aa742017-10-20 12:30:38 -04002886 rv = vppcom_session_write_ready (session, session_index);
2887 clib_spinlock_unlock (&vcm->sessions_lockp);
2888 if (write_map && (rv > 0))
2889 {
2890 // TBD: clib_warning
2891 clib_bitmap_set_no_check (write_map, session_index, 1);
2892 bits_set++;
2893 }
2894 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002895 }
2896
Dave Wallacee22aa742017-10-20 12:30:38 -04002897 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002898 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002899 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2900 ({
2901 clib_spinlock_lock (&vcm->sessions_lockp);
2902 rv = vppcom_session_at_index (session_index, &session);
2903 if (rv < 0)
2904 {
2905 clib_spinlock_unlock (&vcm->sessions_lockp);
2906 if (VPPCOM_DEBUG > 1)
2907 clib_warning ("[%d] session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002908 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002909 session_index);
2910 bits_set = VPPCOM_EBADFD;
2911 goto select_done;
2912 }
Dave Wallace543852a2017-08-03 02:11:34 -04002913
Dave Wallacee22aa742017-10-20 12:30:38 -04002914 rv = vppcom_session_read_ready (session, session_index);
2915 clib_spinlock_unlock (&vcm->sessions_lockp);
2916 if (rv < 0)
2917 {
2918 // TBD: clib_warning
2919 clib_bitmap_set_no_check (except_map, session_index, 1);
2920 bits_set++;
2921 }
2922 }));
Dave Wallace543852a2017-08-03 02:11:34 -04002923 }
Dave Wallacee22aa742017-10-20 12:30:38 -04002924 }
Dave Wallace543852a2017-08-03 02:11:34 -04002925 /* *INDENT-ON* */
2926 }
2927 while (clib_time_now (&vcm->clib_time) < timeout);
2928
2929select_done:
2930 return (bits_set);
2931}
2932
Dave Wallacef7f809c2017-10-03 01:48:42 -04002933static inline void
2934vep_verify_epoll_chain (u32 vep_idx)
2935{
2936 session_t *session;
2937 vppcom_epoll_t *vep;
2938 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05002939 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002940
Dave Wallace498b3a52017-11-09 13:00:34 -05002941 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002942 return;
2943
2944 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2945 rv = vppcom_session_at_index (vep_idx, &session);
2946 if (PREDICT_FALSE (rv))
2947 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002948 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949 goto done;
2950 }
2951 if (PREDICT_FALSE (!session->is_vep))
2952 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002953 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002954 vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002955 goto done;
2956 }
Dave Wallace498b3a52017-11-09 13:00:34 -05002957 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2958 "{\n"
2959 " is_vep = %u\n"
2960 " is_vep_session = %u\n"
2961 " wait_cont_idx = 0x%x (%u)\n"
2962 "}\n", getpid (),
2963 vep_idx, session->is_vep, session->is_vep_session,
2964 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002965 do
2966 {
2967 vep = &session->vep;
Dave Wallacee695cb42017-11-02 22:04:42 -04002968 sid = vep->next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002969 if (sid != ~0)
2970 {
2971 rv = vppcom_session_at_index (sid, &session);
2972 if (PREDICT_FALSE (rv))
2973 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05002974 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002975 goto done;
2976 }
2977 if (PREDICT_FALSE (session->is_vep))
Dave Wallace774169b2017-11-01 20:07:40 -04002978 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002979 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002980 else if (PREDICT_FALSE (!session->is_vep_session))
2981 {
Dave Wallace774169b2017-11-01 20:07:40 -04002982 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002983 getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002984 goto done;
2985 }
2986 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
Dave Wallace774169b2017-11-01 20:07:40 -04002987 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002988 "vep_idx (%u)!", getpid (),
Dave Wallacef7f809c2017-10-03 01:48:42 -04002989 sid, session->vep.vep_idx, vep_idx);
Dave Wallace498b3a52017-11-09 13:00:34 -05002990 if (session->is_vep_session)
2991 {
2992 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2993 "{\n"
2994 " next_sid = 0x%x (%u)\n"
2995 " prev_sid = 0x%x (%u)\n"
2996 " vep_idx = 0x%x (%u)\n"
2997 " ev.events = 0x%x\n"
2998 " ev.data.u64 = 0x%llx\n"
2999 " et_mask = 0x%x\n"
3000 "}\n",
3001 vep_idx, sid, sid,
3002 vep->next_sid, vep->next_sid,
3003 vep->prev_sid, vep->prev_sid,
3004 vep->vep_idx, vep->vep_idx,
3005 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3006 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003007 }
3008 }
3009 while (sid != ~0);
3010
3011done:
Dave Wallace498b3a52017-11-09 13:00:34 -05003012 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003013}
3014
3015int
3016vppcom_epoll_create (void)
3017{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003018 session_t *vep_session;
3019 u32 vep_idx;
3020
3021 clib_spinlock_lock (&vcm->sessions_lockp);
3022 pool_get (vcm->sessions, vep_session);
3023 memset (vep_session, 0, sizeof (*vep_session));
3024 vep_idx = vep_session - vcm->sessions;
3025
3026 vep_session->is_vep = 1;
3027 vep_session->vep.vep_idx = ~0;
3028 vep_session->vep.next_sid = ~0;
3029 vep_session->vep.prev_sid = ~0;
3030 vep_session->wait_cont_idx = ~0;
3031 clib_spinlock_unlock (&vcm->sessions_lockp);
3032
3033 if (VPPCOM_DEBUG > 0)
Dave Wallace2e005bb2017-11-07 01:21:39 -05003034 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003035
3036 return (vep_idx);
3037}
3038
3039int
3040vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3041 struct epoll_event *event)
3042{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003043 session_t *vep_session;
3044 session_t *session;
3045 int rv;
3046
3047 if (vep_idx == session_index)
3048 {
3049 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003050 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003051 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003052 return VPPCOM_EINVAL;
3053 }
3054
3055 clib_spinlock_lock (&vcm->sessions_lockp);
3056 rv = vppcom_session_at_index (vep_idx, &vep_session);
3057 if (PREDICT_FALSE (rv))
3058 {
3059 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003060 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003061 goto done;
3062 }
3063 if (PREDICT_FALSE (!vep_session->is_vep))
3064 {
3065 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003066 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003067 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003068 rv = VPPCOM_EINVAL;
3069 goto done;
3070 }
3071
3072 ASSERT (vep_session->vep.vep_idx == ~0);
3073 ASSERT (vep_session->vep.prev_sid == ~0);
3074
3075 rv = vppcom_session_at_index (session_index, &session);
3076 if (PREDICT_FALSE (rv))
3077 {
3078 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003079 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003080 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003081 goto done;
3082 }
3083 if (PREDICT_FALSE (session->is_vep))
3084 {
3085 if (VPPCOM_DEBUG > 0)
3086 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3087 rv = VPPCOM_EINVAL;
3088 goto done;
3089 }
3090
3091 switch (op)
3092 {
3093 case EPOLL_CTL_ADD:
3094 if (PREDICT_FALSE (!event))
3095 {
Dave Wallace774169b2017-11-01 20:07:40 -04003096 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003097 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003098 rv = VPPCOM_EINVAL;
3099 goto done;
3100 }
3101 if (vep_session->vep.next_sid != ~0)
3102 {
3103 session_t *next_session;
3104 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3105 &next_session);
3106 if (PREDICT_FALSE (rv))
3107 {
3108 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003109 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3110 "vep.next_sid (%u) on vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003111 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003112 goto done;
3113 }
3114 ASSERT (next_session->vep.prev_sid == vep_idx);
3115 next_session->vep.prev_sid = session_index;
3116 }
3117 session->vep.next_sid = vep_session->vep.next_sid;
3118 session->vep.prev_sid = vep_idx;
3119 session->vep.vep_idx = vep_idx;
3120 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3121 session->vep.ev = *event;
3122 session->is_vep_session = 1;
3123 vep_session->vep.next_sid = session_index;
3124 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003125 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003126 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003127 event->events, event->data.u64);
3128 break;
3129
3130 case EPOLL_CTL_MOD:
3131 if (PREDICT_FALSE (!event))
3132 {
Dave Wallace774169b2017-11-01 20:07:40 -04003133 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003134 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003135 rv = VPPCOM_EINVAL;
3136 goto done;
3137 }
3138 if (PREDICT_FALSE (!session->is_vep_session &&
3139 (session->vep.vep_idx != vep_idx)))
3140 {
3141 if (VPPCOM_DEBUG > 0)
3142 {
3143 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003144 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3145 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003146 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003147 else
Dave Wallace774169b2017-11-01 20:07:40 -04003148 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3149 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003150 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003151 session->vep.vep_idx, vep_idx);
3152 }
3153 rv = VPPCOM_EINVAL;
3154 goto done;
3155 }
3156 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3157 session->vep.ev = *event;
3158 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003159 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Dave Wallace2e005bb2017-11-07 01:21:39 -05003160 " data 0x%llx!", getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003161 event->events, event->data.u64);
3162 break;
3163
3164 case EPOLL_CTL_DEL:
3165 if (PREDICT_FALSE (!session->is_vep_session &&
3166 (session->vep.vep_idx != vep_idx)))
3167 {
3168 if (VPPCOM_DEBUG > 0)
3169 {
3170 if (!session->is_vep_session)
Dave Wallace774169b2017-11-01 20:07:40 -04003171 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3172 "is not a vep session!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003173 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003174 else
Dave Wallace774169b2017-11-01 20:07:40 -04003175 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3176 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003177 getpid (), session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003178 session->vep.vep_idx, vep_idx);
3179 }
3180 rv = VPPCOM_EINVAL;
3181 goto done;
3182 }
3183
3184 vep_session->wait_cont_idx =
3185 (vep_session->wait_cont_idx == session_index) ?
3186 session->vep.next_sid : vep_session->wait_cont_idx;
3187
3188 if (session->vep.prev_sid == vep_idx)
3189 vep_session->vep.next_sid = session->vep.next_sid;
3190 else
3191 {
3192 session_t *prev_session;
3193 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3194 if (PREDICT_FALSE (rv))
3195 {
3196 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003197 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3198 "vep.prev_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003199 getpid (), session->vep.prev_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003200 session_index);
3201 goto done;
3202 }
3203 ASSERT (prev_session->vep.next_sid == session_index);
3204 prev_session->vep.next_sid = session->vep.next_sid;
3205 }
3206 if (session->vep.next_sid != ~0)
3207 {
3208 session_t *next_session;
3209 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3210 if (PREDICT_FALSE (rv))
3211 {
3212 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003213 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3214 "vep.next_sid (%u) on sid (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003215 getpid (), session->vep.next_sid,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003216 session_index);
3217 goto done;
3218 }
3219 ASSERT (next_session->vep.prev_sid == session_index);
3220 next_session->vep.prev_sid = session->vep.prev_sid;
3221 }
3222
3223 memset (&session->vep, 0, sizeof (session->vep));
3224 session->vep.next_sid = ~0;
3225 session->vep.prev_sid = ~0;
3226 session->vep.vep_idx = ~0;
3227 session->is_vep_session = 0;
3228 if (VPPCOM_DEBUG > 1)
Dave Wallace774169b2017-11-01 20:07:40 -04003229 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003230 getpid (), vep_idx, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003231 break;
3232
3233 default:
Dave Wallace2e005bb2017-11-07 01:21:39 -05003234 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003235 rv = VPPCOM_EINVAL;
3236 }
3237
3238 vep_verify_epoll_chain (vep_idx);
3239
3240done:
3241 clib_spinlock_unlock (&vcm->sessions_lockp);
3242 return rv;
3243}
3244
Dave Wallacef7f809c2017-10-03 01:48:42 -04003245int
3246vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3247 int maxevents, double wait_for_time)
3248{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003249 session_t *vep_session;
3250 int rv;
3251 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003252 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003253 int num_ev = 0;
3254 u32 vep_next_sid, wait_cont_idx;
3255 u8 is_vep;
3256
3257 if (PREDICT_FALSE (maxevents <= 0))
3258 {
3259 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003260 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003261 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003262 return VPPCOM_EINVAL;
3263 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003264 memset (events, 0, sizeof (*events) * maxevents);
3265
3266 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3267 vep_next_sid = vep_session->vep.next_sid;
3268 is_vep = vep_session->is_vep;
3269 wait_cont_idx = vep_session->wait_cont_idx;
3270 clib_spinlock_unlock (&vcm->sessions_lockp);
3271
3272 if (PREDICT_FALSE (!is_vep))
3273 {
3274 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003275 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003276 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003277 rv = VPPCOM_EINVAL;
3278 goto done;
3279 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003280 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003281 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003282 if (VPPCOM_DEBUG > 0)
3283 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003284 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003285 goto done;
3286 }
3287
3288 do
3289 {
3290 u32 sid;
3291 u32 next_sid = ~0;
3292 session_t *session;
3293
3294 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3295 sid != ~0; sid = next_sid)
3296 {
3297 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3298 u8 add_event, is_vep_session;
3299 int ready;
3300 u64 session_ev_data;
3301
3302 VCL_LOCK_AND_GET_SESSION (sid, &session);
3303 next_sid = session->vep.next_sid;
3304 session_events = session->vep.ev.events;
3305 et_mask = session->vep.et_mask;
3306 is_vep = session->is_vep;
3307 is_vep_session = session->is_vep_session;
3308 session_vep_idx = session->vep.vep_idx;
3309 session_ev_data = session->vep.ev.data.u64;
3310 clib_spinlock_unlock (&vcm->sessions_lockp);
3311
3312 if (PREDICT_FALSE (is_vep))
3313 {
3314 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003315 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003316 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003317 rv = VPPCOM_EINVAL;
3318 goto done;
3319 }
3320 if (PREDICT_FALSE (!is_vep_session))
3321 {
3322 if (VPPCOM_DEBUG > 0)
Dave Wallace774169b2017-11-01 20:07:40 -04003323 clib_warning ("[%d] ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003324 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003325 rv = VPPCOM_EINVAL;
3326 goto done;
3327 }
3328 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3329 {
Dave Wallace774169b2017-11-01 20:07:40 -04003330 clib_warning ("[%d] ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003331 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003332 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003333 rv = VPPCOM_EINVAL;
3334 goto done;
3335 }
3336
3337 add_event = clear_et_mask = 0;
3338
Dave Wallace60caa062017-11-10 17:07:13 -05003339 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003340 {
3341 VCL_LOCK_AND_GET_SESSION (sid, &session);
3342 ready = vppcom_session_read_ready (session, sid);
3343 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003344 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003345 {
3346 add_event = 1;
3347 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003348 if (((EPOLLET | EPOLLIN) & session_events) ==
3349 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003350 clear_et_mask |= EPOLLIN;
3351 }
3352 else if (ready < 0)
3353 {
3354 add_event = 1;
3355 switch (ready)
3356 {
3357 case VPPCOM_ECONNRESET:
3358 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3359 break;
3360
3361 default:
3362 events[num_ev].events |= EPOLLERR;
3363 break;
3364 }
3365 }
3366 }
3367
Dave Wallace60caa062017-11-10 17:07:13 -05003368 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003369 {
3370 VCL_LOCK_AND_GET_SESSION (sid, &session);
3371 ready = vppcom_session_write_ready (session, sid);
3372 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05003373 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003374 {
3375 add_event = 1;
3376 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003377 if (((EPOLLET | EPOLLOUT) & session_events) ==
3378 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003379 clear_et_mask |= EPOLLOUT;
3380 }
3381 else if (ready < 0)
3382 {
3383 add_event = 1;
3384 switch (ready)
3385 {
3386 case VPPCOM_ECONNRESET:
3387 events[num_ev].events |= EPOLLHUP;
3388 break;
3389
3390 default:
3391 events[num_ev].events |= EPOLLERR;
3392 break;
3393 }
3394 }
3395 }
3396
3397 if (add_event)
3398 {
3399 events[num_ev].data.u64 = session_ev_data;
3400 if (EPOLLONESHOT & session_events)
3401 {
3402 VCL_LOCK_AND_GET_SESSION (sid, &session);
3403 session->vep.ev.events = 0;
3404 clib_spinlock_unlock (&vcm->sessions_lockp);
3405 }
3406 num_ev++;
3407 if (num_ev == maxevents)
3408 {
3409 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3410 vep_session->wait_cont_idx = next_sid;
3411 clib_spinlock_unlock (&vcm->sessions_lockp);
3412 goto done;
3413 }
3414 }
3415 if (wait_cont_idx != ~0)
3416 {
3417 if (next_sid == ~0)
3418 next_sid = vep_next_sid;
3419 else if (next_sid == wait_cont_idx)
3420 next_sid = ~0;
3421 }
3422 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003423 if (wait_for_time != -1)
3424 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003425 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003426 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003427
3428 if (wait_cont_idx != ~0)
3429 {
3430 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3431 vep_session->wait_cont_idx = ~0;
3432 clib_spinlock_unlock (&vcm->sessions_lockp);
3433 }
3434done:
3435 return (rv != VPPCOM_OK) ? rv : num_ev;
3436}
3437
Dave Wallace35830af2017-10-09 01:43:42 -04003438int
3439vppcom_session_attr (uint32_t session_index, uint32_t op,
3440 void *buffer, uint32_t * buflen)
3441{
Dave Wallace35830af2017-10-09 01:43:42 -04003442 session_t *session;
3443 int rv = VPPCOM_OK;
3444 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003445 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003446
3447 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3448 switch (op)
3449 {
3450 case VPPCOM_ATTR_GET_NREAD:
3451 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05003452 if (VPPCOM_DEBUG > 2)
3453 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003454 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003455
3456 break;
3457
Dave Wallace227867f2017-11-13 21:21:53 -05003458 case VPPCOM_ATTR_GET_NWRITE:
3459 rv = vppcom_session_write_ready (session, session_index);
3460 if (VPPCOM_DEBUG > 2)
3461 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3462 getpid (), session_index, rv);
3463
Dave Wallace35830af2017-10-09 01:43:42 -04003464 break;
3465
3466 case VPPCOM_ATTR_GET_FLAGS:
3467 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3468 {
3469 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3470 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05003471 if (VPPCOM_DEBUG > 2)
3472 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3473 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3474 session_index, *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_SET_FLAGS:
3481 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3482 {
3483 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace227867f2017-11-13 21:21:53 -05003484 if (VPPCOM_DEBUG > 2)
3485 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3486 "flags = 0x%08x, is_nonblocking = %u",
3487 getpid (), *flags, session->is_nonblocking);
Dave Wallace35830af2017-10-09 01:43:42 -04003488 }
3489 else
3490 rv = VPPCOM_EINVAL;
3491 break;
3492
3493 case VPPCOM_ATTR_GET_PEER_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003494 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003495 {
Steven2199aab2017-10-15 20:18:47 -07003496 ep->vrf = session->vrf;
3497 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003498 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07003499 if (session->peer_addr.is_ip4)
3500 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3501 sizeof (ip4_address_t));
3502 else
3503 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3504 sizeof (ip6_address_t));
3505 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003506 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003507 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003508 "%u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003509 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003510 &session->peer_addr.ip46, ep->is_ip4,
3511 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003512 }
3513 else
3514 rv = VPPCOM_EINVAL;
3515 break;
3516
3517 case VPPCOM_ATTR_GET_LCL_ADDR:
Steven2199aab2017-10-15 20:18:47 -07003518 if (buffer && buflen && (*buflen >= sizeof (*ep)))
Dave Wallace35830af2017-10-09 01:43:42 -04003519 {
Steven2199aab2017-10-15 20:18:47 -07003520 ep->vrf = session->vrf;
3521 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07003522 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07003523 if (session->lcl_addr.is_ip4)
3524 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3525 sizeof (ip4_address_t));
3526 else
3527 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3528 sizeof (ip6_address_t));
3529 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003530 if (VPPCOM_DEBUG > 1)
Dave Wallace227867f2017-11-13 21:21:53 -05003531 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003532 "%u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04003533 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07003534 &session->lcl_addr.ip46, ep->is_ip4,
3535 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003536 }
3537 else
3538 rv = VPPCOM_EINVAL;
3539 break;
Stevenb5a11602017-10-11 09:59:30 -07003540
3541 case VPPCOM_ATTR_SET_REUSEADDR:
3542 break;
3543
3544 case VPPCOM_ATTR_SET_BROADCAST:
3545 break;
3546
3547 case VPPCOM_ATTR_SET_V6ONLY:
3548 break;
Stevenbccd3392017-10-12 20:42:21 -07003549
3550 case VPPCOM_ATTR_SET_KEEPALIVE:
3551 break;
3552
3553 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3554 break;
3555
3556 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3557 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003558
3559 default:
3560 rv = VPPCOM_EINVAL;
3561 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003562 }
3563
3564done:
3565 clib_spinlock_unlock (&vcm->sessions_lockp);
3566 return rv;
3567}
3568
Stevenac1f96d2017-10-24 16:03:58 -07003569int
3570vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3571 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3572{
Stevenac1f96d2017-10-24 16:03:58 -07003573 int rv = VPPCOM_OK;
3574 session_t *session = 0;
3575
3576 if (ep)
3577 {
3578 clib_spinlock_lock (&vcm->sessions_lockp);
3579 rv = vppcom_session_at_index (session_index, &session);
3580 if (PREDICT_FALSE (rv))
3581 {
3582 clib_spinlock_unlock (&vcm->sessions_lockp);
3583 if (VPPCOM_DEBUG > 0)
3584 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003585 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04003586 rv = VPPCOM_EBADFD;
3587 clib_spinlock_unlock (&vcm->sessions_lockp);
3588 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07003589 }
3590 ep->vrf = session->vrf;
3591 ep->is_ip4 = session->peer_addr.is_ip4;
3592 ep->port = session->peer_port;
3593 if (session->peer_addr.is_ip4)
3594 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3595 sizeof (ip4_address_t));
3596 else
3597 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3598 sizeof (ip6_address_t));
3599 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07003600 }
Steven58f464e2017-10-25 12:33:12 -07003601
3602 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003603 rv = vppcom_session_read (session_index, buffer, buflen);
3604 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003605 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003606 else
3607 {
Dave Wallace2e005bb2017-11-07 01:21:39 -05003608 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07003609 rv = VPPCOM_EAFNOSUPPORT;
3610 }
3611
Dave Wallacefaf9d772017-10-26 16:12:04 -04003612done:
Stevenac1f96d2017-10-24 16:03:58 -07003613 return rv;
3614}
3615
3616int
3617vppcom_session_sendto (uint32_t session_index, void *buffer,
3618 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3619{
Dave Wallace617dffa2017-10-26 14:47:06 -04003620 if (!buffer)
3621 return VPPCOM_EINVAL;
3622
3623 if (ep)
3624 {
3625 // TBD
3626 return VPPCOM_EINVAL;
3627 }
3628
3629 if (flags)
3630 {
3631 // TBD check the flags and do the right thing
3632 if (VPPCOM_DEBUG > 2)
3633 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003634 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003635 }
3636
3637 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003638}
3639
Dave Wallacee22aa742017-10-20 12:30:38 -04003640/*
3641 * fd.io coding-style-patch-verification: ON
3642 *
3643 * Local Variables:
3644 * eval: (c-set-style "gnu")
3645 * End:
3646 */