2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 #include <vcl/vppcom.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
27 #define vl_typedefs /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
31 /* declare message handlers for each api */
33 #define vl_endianfun /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
40 #include <vpp/api/vpe_all_api_h.h>
44 /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */
45 #define VPPCOM_DEBUG 1
47 #define VPPCOM_DEBUG 0
51 * VPPCOM Private definitions and functions.
71 typedef struct epoll_event vppcom_epoll_event_t;
78 vppcom_epoll_event_t ev;
79 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
91 volatile session_state_t state;
93 svm_fifo_t *server_rx_fifo;
94 svm_fifo_t *server_tx_fifo;
96 u64 vpp_session_handle;
97 unix_shared_memory_queue_t *vpp_event_queue;
99 /* Socket configuration state */
100 /* TBD: covert 'is_*' vars to bit in u8 flags; */
110 vppcom_ip46_t lcl_addr;
111 vppcom_ip46_t peer_addr;
112 u16 lcl_port; // network order
113 u16 peer_port; // network order
115 u64 client_queue_address;
119 typedef struct vppcom_cfg_t_
124 u32 add_segment_size;
125 u32 preallocated_fifo_pairs;
128 u32 event_queue_size;
129 u32 listen_queue_size;
130 u8 app_proxy_transport_tcp;
131 u8 app_proxy_transport_udp;
135 u64 namespace_secret;
141 typedef struct vppcom_main_t_
144 u32 *client_session_index_fifo;
145 volatile u32 bind_session_index;
148 /* vpe input queue */
149 unix_shared_memory_queue_t *vl_input_queue;
151 /* API client handle */
155 clib_spinlock_t sessions_lockp;
158 /* Hash table for disconnect processing */
159 uword *session_index_by_vpp_handles;
162 clib_bitmap_t *rd_bitmap;
163 clib_bitmap_t *wr_bitmap;
164 clib_bitmap_t *ex_bitmap;
166 /* Our event queue */
167 unix_shared_memory_queue_t *app_event_queue;
169 /* unique segment name counter */
170 u32 unique_segment_index;
172 /* For deadman timers */
173 clib_time_t clib_time;
175 /* State of the connection, shared between msg RX thread and main thread */
176 volatile app_state_t app_state;
180 /* VNET_API_ERROR_FOO -> "Foo" hash table */
181 uword *error_string_by_error_number;
184 /* NOTE: _vppcom_main is only used until the heap is allocated.
185 * Do not access it directly -- use vcm which will point to
186 * the heap allocated copy after init.
188 static vppcom_main_t _vppcom_main = {.my_client_index = ~0 };
190 static vppcom_main_t *vcm = &_vppcom_main;
193 vppcom_app_state_str (app_state_t state)
199 case STATE_APP_START:
200 st = "STATE_APP_START";
203 case STATE_APP_CONN_VPP:
204 st = "STATE_APP_CONN_VPP";
207 case STATE_APP_ENABLED:
208 st = "STATE_APP_ENABLED";
211 case STATE_APP_ATTACHED:
212 st = "STATE_APP_ATTACHED";
216 st = "UNKNOWN_APP_STATE";
224 vppcom_session_state_str (session_state_t state)
235 st = "STATE_CONNECT";
246 case STATE_DISCONNECT:
247 st = "STATE_DISCONNECT";
255 st = "UNKNOWN_STATE";
263 * VPPCOM Utility Functions
266 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
268 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
269 if (PREDICT_FALSE ((session_index == ~0) ||
270 pool_is_free_index (vcm->sessions, session_index)))
272 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
273 getpid (), session_index);
274 return VPPCOM_EBADFD;
276 *sess = pool_elt_at_index (vcm->sessions, session_index);
281 vppcom_connect_to_vpp (char *app_name)
283 api_main_t *am = &api_main;
285 if (VPPCOM_DEBUG > 0)
286 printf ("\nConnecting to VPP api...");
287 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
289 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
290 return VPPCOM_ECONNREFUSED;
293 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
294 vcm->my_client_index = am->my_client_index;
295 if (VPPCOM_DEBUG > 0)
296 printf (" connected!\n");
298 vcm->app_state = STATE_APP_CONN_VPP;
303 format_api_error (u8 * s, va_list * args)
305 i32 error = va_arg (*args, u32);
308 p = hash_get (vcm->error_string_by_error_number, -error);
311 s = format (s, "%s (%d)", p[0], error);
313 s = format (s, "%d", error);
318 vppcom_init_error_string_table (void)
320 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
322 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
323 foreach_vnet_api_error;
326 hash_set (vcm->error_string_by_error_number, 99, "Misc");
330 vppcom_wait_for_app_state_change (app_state_t app_state)
332 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
334 while (clib_time_now (&vcm->clib_time) < timeout)
336 if (vcm->app_state == app_state)
339 if (VPPCOM_DEBUG > 0)
340 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
341 vppcom_app_state_str (app_state), app_state);
342 return VPPCOM_ETIMEDOUT;
346 vppcom_wait_for_session_state_change (u32 session_index,
347 session_state_t state,
350 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
351 session_t *volatile session;
356 clib_spinlock_lock (&vcm->sessions_lockp);
357 rv = vppcom_session_at_index (session_index, &session);
358 if (PREDICT_FALSE (rv))
360 clib_spinlock_unlock (&vcm->sessions_lockp);
363 if (session->state == state)
365 clib_spinlock_unlock (&vcm->sessions_lockp);
368 clib_spinlock_unlock (&vcm->sessions_lockp);
370 while (clib_time_now (&vcm->clib_time) < timeout);
372 if (VPPCOM_DEBUG > 0)
373 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
374 vppcom_session_state_str (state), state);
375 return VPPCOM_ETIMEDOUT;
379 vppcom_wait_for_client_session_index (f64 wait_for_time)
381 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
385 if (clib_fifo_elts (vcm->client_session_index_fifo))
388 while (clib_time_now (&vcm->clib_time) < timeout);
390 if (wait_for_time == 0)
391 return VPPCOM_EAGAIN;
393 if (VPPCOM_DEBUG > 0)
394 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
395 return VPPCOM_ETIMEDOUT;
399 * VPP-API message functions
402 vppcom_send_session_enable_disable (u8 is_enable)
404 vl_api_session_enable_disable_t *bmp;
405 bmp = vl_msg_api_alloc (sizeof (*bmp));
406 memset (bmp, 0, sizeof (*bmp));
408 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
409 bmp->client_index = vcm->my_client_index;
410 bmp->context = htonl (0xfeedface);
411 bmp->is_enable = is_enable;
412 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
416 vppcom_app_session_enable (void)
420 if (vcm->app_state != STATE_APP_ENABLED)
422 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
423 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
424 if (PREDICT_FALSE (rv))
426 if (VPPCOM_DEBUG > 0)
427 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
428 getpid (), vppcom_retval_str (rv), rv);
436 vl_api_session_enable_disable_reply_t_handler
437 (vl_api_session_enable_disable_reply_t * mp)
441 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
442 format_api_error, ntohl (mp->retval));
445 vcm->app_state = STATE_APP_ENABLED;
449 vppcom_app_send_attach (void)
451 vl_api_application_attach_t *bmp;
452 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
453 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
454 vcm->cfg.app_proxy_transport_udp);
456 bmp = vl_msg_api_alloc (sizeof (*bmp));
457 memset (bmp, 0, sizeof (*bmp));
459 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
460 bmp->client_index = vcm->my_client_index;
461 bmp->context = htonl (0xfeedface);
462 bmp->options[APP_OPTIONS_FLAGS] =
463 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
464 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
465 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
466 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
467 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
468 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
469 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
470 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
471 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
472 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
473 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
476 bmp->namespace_id_len = nsid_len;
477 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
478 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
480 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
484 vppcom_app_attach (void)
488 vppcom_app_send_attach ();
489 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
490 if (PREDICT_FALSE (rv))
492 if (VPPCOM_DEBUG > 0)
493 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
494 getpid (), vppcom_retval_str (rv), rv);
501 vppcom_app_detach (void)
503 vl_api_application_detach_t *bmp;
504 bmp = vl_msg_api_alloc (sizeof (*bmp));
505 memset (bmp, 0, sizeof (*bmp));
507 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
508 bmp->client_index = vcm->my_client_index;
509 bmp->context = htonl (0xfeedface);
510 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
514 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
517 static svm_fifo_segment_create_args_t _a;
518 svm_fifo_segment_create_args_t *a = &_a;
521 memset (a, 0, sizeof (*a));
524 clib_warning ("[%d] attach failed: %U", getpid (),
525 format_api_error, ntohl (mp->retval));
529 if (mp->segment_name_length == 0)
531 clib_warning ("[%d] segment_name_length zero", getpid ());
535 a->segment_name = (char *) mp->segment_name;
536 a->segment_size = mp->segment_size;
538 ASSERT (mp->app_event_queue_address);
540 /* Attach to the segment vpp created */
541 rv = svm_fifo_segment_attach (a);
542 vec_reset_length (a->new_segment_indices);
543 if (PREDICT_FALSE (rv))
545 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
550 vcm->app_event_queue =
551 uword_to_pointer (mp->app_event_queue_address,
552 unix_shared_memory_queue_t *);
554 vcm->app_state = STATE_APP_ATTACHED;
558 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
562 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
565 vcm->app_state = STATE_APP_ENABLED;
569 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
574 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
577 session_t *session = 0;
579 clib_spinlock_lock (&vcm->sessions_lockp);
580 rv = vppcom_session_at_index (p[0], &session);
581 if (PREDICT_FALSE (rv))
583 if (VPPCOM_DEBUG > 1)
584 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
587 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
588 session->state = STATE_DISCONNECT;
589 clib_spinlock_unlock (&vcm->sessions_lockp);
593 if (VPPCOM_DEBUG > 1)
594 clib_warning ("[%d] couldn't find session key %llx", getpid (),
599 clib_warning ("[%d] disconnect_session failed: %U", getpid (),
600 format_api_error, ntohl (mp->retval));
604 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
606 static svm_fifo_segment_create_args_t _a;
607 svm_fifo_segment_create_args_t *a = &_a;
610 memset (a, 0, sizeof (*a));
611 a->segment_name = (char *) mp->segment_name;
612 a->segment_size = mp->segment_size;
613 /* Attach to the segment vpp created */
614 rv = svm_fifo_segment_attach (a);
615 vec_reset_length (a->new_segment_indices);
616 if (PREDICT_FALSE (rv))
618 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
619 getpid (), mp->segment_name);
622 if (VPPCOM_DEBUG > 1)
623 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
624 mp->segment_name, mp->segment_size);
628 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
630 session_t *session = 0;
631 vl_api_disconnect_session_reply_t *rmp;
635 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
639 clib_spinlock_lock (&vcm->sessions_lockp);
640 rval = vppcom_session_at_index (p[0], &session);
641 if (PREDICT_FALSE (rval))
643 if (VPPCOM_DEBUG > 1)
644 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
648 pool_put (vcm->sessions, session);
649 clib_spinlock_unlock (&vcm->sessions_lockp);
650 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
654 clib_warning ("[%d] couldn't find session key %llx", getpid (),
659 rmp = vl_msg_api_alloc (sizeof (*rmp));
660 memset (rmp, 0, sizeof (*rmp));
662 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
663 rmp->retval = htonl (rv);
664 rmp->handle = mp->handle;
665 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
669 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
671 session_t *session = 0;
672 vl_api_reset_session_reply_t *rmp;
676 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
680 clib_spinlock_lock (&vcm->sessions_lockp);
681 rval = vppcom_session_at_index (p[0], &session);
682 if (PREDICT_FALSE (rval))
684 if (VPPCOM_DEBUG > 1)
685 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
689 pool_put (vcm->sessions, session);
690 clib_spinlock_unlock (&vcm->sessions_lockp);
691 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
695 clib_warning ("[%d] couldn't find session key %llx", getpid (),
700 rmp = vl_msg_api_alloc (sizeof (*rmp));
701 memset (rmp, 0, sizeof (*rmp));
702 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
703 rmp->retval = htonl (rv);
704 rmp->handle = mp->handle;
705 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
709 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
713 svm_fifo_t *rx_fifo, *tx_fifo;
719 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
724 session_index = mp->context;
725 if (VPPCOM_DEBUG > 1)
726 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
727 session_index, session_index);
729 clib_spinlock_lock (&vcm->sessions_lockp);
730 if (pool_is_free_index (vcm->sessions, session_index))
732 clib_spinlock_unlock (&vcm->sessions_lockp);
733 if (VPPCOM_DEBUG > 1)
734 clib_warning ("[%d] invalid session, sid %d is closed!",
735 getpid (), session_index);
739 /* We've been redirected */
740 if (mp->segment_name_length > 0)
742 static svm_fifo_segment_create_args_t _a;
743 svm_fifo_segment_create_args_t *a = &_a;
746 memset (a, 0, sizeof (*a));
747 a->segment_name = (char *) mp->segment_name;
748 if (VPPCOM_DEBUG > 1)
749 clib_warning ("[%d] cut-thru segment: %s", getpid (),
751 rv = svm_fifo_segment_attach (a);
752 vec_reset_length (a->new_segment_indices);
753 if (PREDICT_FALSE (rv))
755 clib_spinlock_unlock (&vcm->sessions_lockp);
756 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
757 getpid (), a->segment_name);
765 if (VPPCOM_DEBUG > 1)
766 clib_warning ("[%d] client sid %d", getpid (), session_index);
768 session = pool_elt_at_index (vcm->sessions, session_index);
769 session->is_cut_thru = is_cut_thru;
770 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
771 unix_shared_memory_queue_t *);
773 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
774 rx_fifo->client_session_index = session_index;
775 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
776 tx_fifo->client_session_index = session_index;
778 session->server_rx_fifo = rx_fifo;
779 session->server_tx_fifo = tx_fifo;
780 session->vpp_session_handle = mp->handle;
781 session->state = STATE_CONNECT;
783 /* Add it to lookup table */
784 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
785 clib_spinlock_unlock (&vcm->sessions_lockp);
789 vppcom_send_connect_sock (session_t * session, u32 session_index)
791 vl_api_connect_sock_t *cmp;
793 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
794 session->is_server = 0;
795 cmp = vl_msg_api_alloc (sizeof (*cmp));
796 memset (cmp, 0, sizeof (*cmp));
797 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
798 cmp->client_index = vcm->my_client_index;
799 cmp->context = session_index;
801 if (VPPCOM_DEBUG > 1)
802 clib_warning ("[%d] session_index = %d 0x%08x",
803 getpid (), session_index, session_index);
805 cmp->vrf = session->vrf;
806 cmp->is_ip4 = session->peer_addr.is_ip4;
807 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
808 cmp->port = session->peer_port;
809 cmp->proto = session->proto;
810 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
811 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
815 vppcom_send_disconnect (session_t * session)
817 vl_api_disconnect_session_t *dmp;
819 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
820 dmp = vl_msg_api_alloc (sizeof (*dmp));
821 memset (dmp, 0, sizeof (*dmp));
822 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
823 dmp->client_index = vcm->my_client_index;
824 dmp->handle = session->vpp_session_handle;
825 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
829 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
831 session_t *session = 0;
835 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
838 ASSERT (vcm->bind_session_index != ~0);
840 clib_spinlock_lock (&vcm->sessions_lockp);
841 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
844 session->vpp_session_handle = mp->handle;
845 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
846 vcm->bind_session_index);
847 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
848 vcm->bind_session_index = ~0;
850 clib_spinlock_unlock (&vcm->sessions_lockp);
854 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
856 session_t *session = 0;
859 clib_spinlock_lock (&vcm->sessions_lockp);
860 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
863 if ((VPPCOM_DEBUG > 1) && (mp->retval))
864 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
867 vcm->bind_session_index = ~0;
868 session->state = STATE_START;
870 clib_spinlock_unlock (&vcm->sessions_lockp);
874 format_ip4_address (u8 * s, va_list * args)
876 u8 *a = va_arg (*args, u8 *);
877 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
881 format_ip6_address (u8 * s, va_list * args)
883 ip6_address_t *a = va_arg (*args, ip6_address_t *);
884 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
886 i_max_n_zero = ARRAY_LEN (a->as_u16);
888 i_first_zero = i_max_n_zero;
890 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
892 u32 is_zero = a->as_u16[i] == 0;
893 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
899 if ((!is_zero && n_zeros > max_n_zeros)
900 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
902 i_max_n_zero = i_first_zero;
903 max_n_zeros = n_zeros;
904 i_first_zero = ARRAY_LEN (a->as_u16);
909 last_double_colon = 0;
910 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
912 if (i == i_max_n_zero && max_n_zeros > 1)
914 s = format (s, "::");
915 i += max_n_zeros - 1;
916 last_double_colon = 1;
920 s = format (s, "%s%x",
921 (last_double_colon || i == 0) ? "" : ":",
922 clib_net_to_host_u16 (a->as_u16[i]));
923 last_double_colon = 0;
930 /* Format an IP46 address. */
932 format_ip46_address (u8 * s, va_list * args)
934 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
935 ip46_type_t type = va_arg (*args, ip46_type_t);
941 is_ip4 = ip46_address_is_ip4 (ip46);
952 format (s, "%U", format_ip4_address, &ip46->ip4) :
953 format (s, "%U", format_ip6_address, &ip46->ip6);
957 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
959 vl_api_accept_session_reply_t *rmp;
960 svm_fifo_t *rx_fifo, *tx_fifo;
965 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
967 clib_warning ("[%d] client session queue is full!", getpid ());
968 rv = VNET_API_ERROR_QUEUE_FULL;
972 if (VPPCOM_DEBUG > 1)
974 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
975 clib_warning ("[%d] accepted session from: %s:%d", getpid (), ip_str,
976 clib_net_to_host_u16 (mp->port));
980 clib_spinlock_lock (&vcm->sessions_lockp);
981 /* Allocate local session and set it up */
982 pool_get (vcm->sessions, session);
983 memset (session, 0, sizeof (*session));
984 session_index = session - vcm->sessions;
986 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
987 rx_fifo->client_session_index = session_index;
988 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
989 tx_fifo->client_session_index = session_index;
991 session->server_rx_fifo = rx_fifo;
992 session->server_tx_fifo = tx_fifo;
993 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
994 unix_shared_memory_queue_t *);
995 session->state = STATE_ACCEPT;
996 session->is_cut_thru = 0;
997 session->is_server = 1;
998 session->peer_port = mp->port;
999 session->peer_addr.is_ip4 = mp->is_ip4;
1000 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1001 sizeof (session->peer_addr.ip46));
1003 /* Add it to lookup table */
1004 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1006 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1007 clib_spinlock_unlock (&vcm->sessions_lockp);
1010 * Send accept reply to vpp
1013 rmp = vl_msg_api_alloc (sizeof (*rmp));
1014 memset (rmp, 0, sizeof (*rmp));
1015 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1016 rmp->retval = htonl (rv);
1017 rmp->handle = mp->handle;
1018 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1022 * Acting as server for redirected connect requests
1025 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1027 static svm_fifo_segment_create_args_t _a;
1028 svm_fifo_segment_create_args_t *a = &_a;
1030 svm_fifo_segment_private_t *seg;
1031 unix_shared_memory_queue_t *client_q;
1032 vl_api_connect_session_reply_t *rmp;
1033 session_t *session = 0;
1035 svm_fifo_t *rx_fifo;
1036 svm_fifo_t *tx_fifo;
1037 unix_shared_memory_queue_t *event_q = 0;
1039 clib_spinlock_lock (&vcm->sessions_lockp);
1040 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1042 if (VPPCOM_DEBUG > 1)
1043 clib_warning ("[%d] client session queue is full!", getpid ());
1044 rv = VNET_API_ERROR_QUEUE_FULL;
1045 clib_spinlock_unlock (&vcm->sessions_lockp);
1049 /* Create the segment */
1050 memset (a, 0, sizeof (*a));
1051 a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
1052 getpid (), vcm->unique_segment_index++,
1054 a->segment_size = vcm->cfg.segment_size;
1055 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
1056 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
1057 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
1059 rv = svm_fifo_segment_create (a);
1060 if (PREDICT_FALSE (rv))
1062 if (VPPCOM_DEBUG > 1)
1063 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
1064 getpid (), a->segment_name);
1065 vec_reset_length (a->new_segment_indices);
1066 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1070 if (VPPCOM_DEBUG > 1)
1071 clib_warning ("[%d] created segment '%s'", getpid (), a->segment_name);
1073 pool_get (vcm->sessions, session);
1074 memset (session, 0, sizeof (*session));
1075 session_index = session - vcm->sessions;
1077 session->sm_seg_index = a->new_segment_indices[0];
1078 vec_reset_length (a->new_segment_indices);
1080 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1081 rx_fifo = session->server_rx_fifo =
1082 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
1083 FIFO_SEGMENT_RX_FREELIST);
1084 if (PREDICT_FALSE (!session->server_rx_fifo))
1086 svm_fifo_segment_delete (seg);
1087 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
1088 getpid (), vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
1089 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1090 clib_spinlock_unlock (&vcm->sessions_lockp);
1094 tx_fifo = session->server_tx_fifo =
1095 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
1096 FIFO_SEGMENT_TX_FREELIST);
1097 if (PREDICT_FALSE (!session->server_tx_fifo))
1099 svm_fifo_segment_delete (seg);
1100 if (VPPCOM_DEBUG > 1)
1101 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
1102 getpid (), vcm->cfg.tx_fifo_size,
1103 vcm->cfg.tx_fifo_size);
1104 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1105 clib_spinlock_unlock (&vcm->sessions_lockp);
1109 session->server_rx_fifo->master_session_index = session_index;
1110 session->server_tx_fifo->master_session_index = session_index;
1111 session->client_queue_address = mp->client_queue_address;
1112 session->is_cut_thru = 1;
1113 session->is_server = 1;
1114 session->peer_port = mp->port;
1115 session->peer_addr.is_ip4 = mp->is_ip4;
1116 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1117 sizeof (session->peer_addr.ip46));
1120 ssvm_shared_header_t *sh = seg->ssvm.sh;
1122 ssvm_lock_non_recursive (sh, 1);
1123 oldheap = ssvm_push_heap (sh);
1124 event_q = session->vpp_event_queue =
1125 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
1126 sizeof (session_fifo_event_t),
1127 getpid (), 0 /* signal not sent */ );
1128 ssvm_pop_heap (oldheap);
1129 ssvm_unlock_non_recursive (sh);
1132 session->state = STATE_ACCEPT;
1133 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1134 if (VPPCOM_DEBUG > 1)
1136 ("[%d] Connected cut-thru to client: sid %d, clib_fifo_elts %u!",
1137 getpid (), session_index,
1138 clib_fifo_elts (vcm->client_session_index_fifo));
1139 clib_spinlock_unlock (&vcm->sessions_lockp);
1142 rmp = vl_msg_api_alloc (sizeof (*rmp));
1143 memset (rmp, 0, sizeof (*rmp));
1145 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1146 rmp->context = mp->context;
1147 rmp->retval = htonl (rv);
1148 rmp->segment_name_length = vec_len (a->segment_name);
1149 clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
1150 vec_reset_length (a->segment_name);
1154 rmp->vpp_event_queue_address = pointer_to_uword (event_q);
1155 rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
1156 rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
1159 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1162 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1166 vppcom_send_bind_sock (session_t * session)
1168 vl_api_bind_sock_t *bmp;
1170 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1171 session->is_server = 1;
1172 bmp = vl_msg_api_alloc (sizeof (*bmp));
1173 memset (bmp, 0, sizeof (*bmp));
1175 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1176 bmp->client_index = vcm->my_client_index;
1177 bmp->context = htonl (0xfeedface);
1178 bmp->vrf = session->vrf;
1179 bmp->is_ip4 = session->lcl_addr.is_ip4;
1180 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1181 bmp->port = session->lcl_port;
1182 bmp->proto = session->proto;
1183 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1184 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1188 vppcom_send_unbind_sock (u32 session_index)
1190 vl_api_unbind_sock_t *ump;
1191 session_t *session = 0;
1194 clib_spinlock_lock (&vcm->sessions_lockp);
1195 rv = vppcom_session_at_index (session_index, &session);
1196 if (PREDICT_FALSE (rv))
1198 clib_spinlock_unlock (&vcm->sessions_lockp);
1199 if (VPPCOM_DEBUG > 0)
1200 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1201 getpid (), session_index);
1205 ump = vl_msg_api_alloc (sizeof (*ump));
1206 memset (ump, 0, sizeof (*ump));
1208 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1209 ump->client_index = vcm->my_client_index;
1210 ump->handle = session->vpp_session_handle;
1211 clib_spinlock_unlock (&vcm->sessions_lockp);
1212 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1216 vppcom_session_unbind_cut_thru (session_t * session)
1218 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1219 svm_fifo_segment_private_t *seg;
1222 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1223 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1224 FIFO_SEGMENT_RX_FREELIST);
1225 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1226 FIFO_SEGMENT_TX_FREELIST);
1227 svm_fifo_segment_delete (seg);
1233 vppcom_session_unbind (u32 session_index)
1237 clib_spinlock_lock (&vcm->sessions_lockp);
1238 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1240 clib_spinlock_unlock (&vcm->sessions_lockp);
1241 if (VPPCOM_DEBUG > 1)
1242 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1243 getpid (), session_index);
1244 return VPPCOM_EBADFD;
1246 clib_spinlock_unlock (&vcm->sessions_lockp);
1248 vcm->bind_session_index = session_index;
1249 vppcom_send_unbind_sock (session_index);
1250 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1251 vcm->cfg.session_timeout);
1252 if (PREDICT_FALSE (rv))
1254 vcm->bind_session_index = ~0;
1255 if (VPPCOM_DEBUG > 0)
1256 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1257 getpid (), vppcom_retval_str (rv), rv);
1264 vppcom_session_disconnect (u32 session_index)
1269 clib_spinlock_lock (&vcm->sessions_lockp);
1270 rv = vppcom_session_at_index (session_index, &session);
1271 if (PREDICT_FALSE (rv))
1273 clib_spinlock_unlock (&vcm->sessions_lockp);
1274 if (VPPCOM_DEBUG > 1)
1275 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1276 getpid (), session_index);
1280 if (!session->is_cut_thru)
1282 vppcom_send_disconnect (session);
1283 clib_spinlock_unlock (&vcm->sessions_lockp);
1285 rv = vppcom_wait_for_session_state_change (session_index,
1286 STATE_DISCONNECT, 1.0);
1287 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1288 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1289 getpid (), session_index, vppcom_retval_str (rv), rv);
1292 clib_spinlock_unlock (&vcm->sessions_lockp);
1297 #define foreach_sock_msg \
1298 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1299 _(BIND_SOCK_REPLY, bind_sock_reply) \
1300 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1301 _(ACCEPT_SESSION, accept_session) \
1302 _(CONNECT_SOCK, connect_sock) \
1303 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1304 _(DISCONNECT_SESSION, disconnect_session) \
1305 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1306 _(RESET_SESSION, reset_session) \
1307 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1308 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1309 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1312 vppcom_api_hookup (void)
1315 vl_msg_api_set_handlers(VL_API_##N, #n, \
1316 vl_api_##n##_t_handler, \
1318 vl_api_##n##_t_endian, \
1319 vl_api_##n##_t_print, \
1320 sizeof(vl_api_##n##_t), 1);
1326 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1330 vcl_cfg->heapsize = (256ULL << 20);
1331 vcl_cfg->segment_baseva = 0x200000000ULL;
1332 vcl_cfg->segment_size = (256 << 20);
1333 vcl_cfg->add_segment_size = (128 << 20);
1334 vcl_cfg->preallocated_fifo_pairs = 8;
1335 vcl_cfg->rx_fifo_size = (1 << 20);
1336 vcl_cfg->tx_fifo_size = (1 << 20);
1337 vcl_cfg->event_queue_size = 2048;
1338 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1339 vcl_cfg->app_timeout = 10 * 60.0;
1340 vcl_cfg->session_timeout = 10 * 60.0;
1341 vcl_cfg->accept_timeout = 60.0;
1345 vppcom_cfg_heapsize (char *conf_fname)
1347 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1360 fp = fopen (conf_fname, "r");
1363 if (VPPCOM_DEBUG > 0)
1364 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1367 argv = calloc (1, sizeof (char *));
1373 if (fgets (inbuf, 4096, fp) == 0)
1375 p = strtok (inbuf, " \t\n");
1381 char **tmp = realloc (argv, argc * sizeof (char *));
1385 arg = strndup (p, 1024);
1388 argv[argc - 1] = arg;
1389 p = strtok (NULL, " \t\n");
1396 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1403 * Look for and parse the "heapsize" config parameter.
1404 * Manual since none of the clib infra has been bootstrapped yet.
1406 * Format: heapsize <nn>[mM][gG]
1409 for (i = 1; i < (argc - 1); i++)
1411 if (!strncmp (argv[i], "heapsize", 8))
1413 sizep = (u8 *) argv[i + 1];
1415 while (*sizep >= '0' && *sizep <= '9')
1418 size += *sizep++ - '0';
1422 if (VPPCOM_DEBUG > 0)
1423 clib_warning ("[%d] parse error '%s %s', "
1424 "using default heapsize %lld (0x%llx)",
1425 getpid (), argv[i], argv[i + 1],
1426 vcl_cfg->heapsize, vcl_cfg->heapsize);
1430 if (*sizep == 'g' || *sizep == 'G')
1431 vcl_cfg->heapsize = size << 30;
1432 else if (*sizep == 'm' || *sizep == 'M')
1433 vcl_cfg->heapsize = size << 20;
1436 if (VPPCOM_DEBUG > 0)
1437 clib_warning ("[%d] parse error '%s %s', "
1438 "using default heapsize %lld (0x%llx)",
1439 getpid (), argv[i], argv[i + 1],
1440 vcl_cfg->heapsize, vcl_cfg->heapsize);
1451 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1452 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1454 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1455 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1457 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1459 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1461 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1462 else if (VPPCOM_DEBUG > 0)
1464 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1465 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1467 vcm = clib_mem_alloc (sizeof (_vppcom_main));
1468 clib_memcpy (vcm, &_vppcom_main, sizeof (_vppcom_main));
1473 vppcom_cfg_read (char *conf_fname)
1475 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1477 unformat_input_t _input, *input = &_input;
1478 unformat_input_t _line_input, *line_input = &_line_input;
1479 u8 vc_cfg_input = 0;
1484 fd = open (conf_fname, O_RDONLY);
1487 if (VPPCOM_DEBUG > 0)
1488 clib_warning ("[%d] open configuration file '%s' failed!",
1489 getpid (), conf_fname);
1493 if (fstat (fd, &s) < 0)
1495 if (VPPCOM_DEBUG > 0)
1496 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1500 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1502 if (VPPCOM_DEBUG > 0)
1503 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1507 unformat_init_clib_file (input, fd);
1509 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1511 (void) unformat_user (input, unformat_line_input, line_input);
1512 unformat_skip_white_space (line_input);
1514 if (unformat (line_input, "vcl {"))
1522 if (unformat (line_input, "heapsize %s", &chroot_path))
1524 vec_terminate_c_string (chroot_path);
1525 if (VPPCOM_DEBUG > 0)
1526 clib_warning ("[%d] configured heapsize %s, "
1527 "actual heapsize %lld (0x%llx)",
1528 getpid (), chroot_path, vcl_cfg->heapsize,
1530 vec_free (chroot_path);
1532 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1534 vec_terminate_c_string (chroot_path);
1535 vl_set_memory_root_path ((char *) chroot_path);
1536 if (VPPCOM_DEBUG > 0)
1537 clib_warning ("[%d] configured api-prefix %s",
1538 getpid (), chroot_path);
1539 chroot_path = 0; /* Don't vec_free() it! */
1541 else if (unformat (line_input, "uid %d", &uid))
1543 vl_set_memory_uid (uid);
1544 if (VPPCOM_DEBUG > 0)
1545 clib_warning ("[%d] configured uid %d", getpid (), uid);
1547 else if (unformat (line_input, "gid %d", &gid))
1549 vl_set_memory_gid (gid);
1550 if (VPPCOM_DEBUG > 0)
1551 clib_warning ("[%d] configured gid %d", getpid (), gid);
1553 else if (unformat (line_input, "segment-baseva 0x%lx",
1554 &vcl_cfg->segment_baseva))
1556 if (VPPCOM_DEBUG > 0)
1557 clib_warning ("[%d] configured segment_baseva 0x%lx",
1558 getpid (), vcl_cfg->segment_baseva);
1560 else if (unformat (line_input, "segment-size 0x%lx",
1561 &vcl_cfg->segment_size))
1563 if (VPPCOM_DEBUG > 0)
1564 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1565 getpid (), vcl_cfg->segment_size,
1566 vcl_cfg->segment_size);
1568 else if (unformat (line_input, "segment-size %ld",
1569 &vcl_cfg->segment_size))
1571 if (VPPCOM_DEBUG > 0)
1572 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1573 getpid (), vcl_cfg->segment_size,
1574 vcl_cfg->segment_size);
1576 else if (unformat (line_input, "add-segment-size 0x%lx",
1577 &vcl_cfg->add_segment_size))
1579 if (VPPCOM_DEBUG > 0)
1581 ("[%d] configured add_segment_size 0x%lx (%ld)",
1582 getpid (), vcl_cfg->add_segment_size,
1583 vcl_cfg->add_segment_size);
1585 else if (unformat (line_input, "add-segment-size %ld",
1586 &vcl_cfg->add_segment_size))
1588 if (VPPCOM_DEBUG > 0)
1590 ("[%d] configured add_segment_size %ld (0x%lx)",
1591 getpid (), vcl_cfg->add_segment_size,
1592 vcl_cfg->add_segment_size);
1594 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1595 &vcl_cfg->preallocated_fifo_pairs))
1597 if (VPPCOM_DEBUG > 0)
1598 clib_warning ("[%d] configured preallocated_fifo_pairs "
1599 "%d (0x%x)", getpid (),
1600 vcl_cfg->preallocated_fifo_pairs,
1601 vcl_cfg->preallocated_fifo_pairs);
1603 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1604 &vcl_cfg->rx_fifo_size))
1606 if (VPPCOM_DEBUG > 0)
1607 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1608 getpid (), vcl_cfg->rx_fifo_size,
1609 vcl_cfg->rx_fifo_size);
1611 else if (unformat (line_input, "rx-fifo-size %ld",
1612 &vcl_cfg->rx_fifo_size))
1614 if (VPPCOM_DEBUG > 0)
1615 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1616 getpid (), vcl_cfg->rx_fifo_size,
1617 vcl_cfg->rx_fifo_size);
1619 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1620 &vcl_cfg->tx_fifo_size))
1622 if (VPPCOM_DEBUG > 0)
1623 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1624 getpid (), vcl_cfg->tx_fifo_size,
1625 vcl_cfg->tx_fifo_size);
1627 else if (unformat (line_input, "tx-fifo-size %ld",
1628 &vcl_cfg->tx_fifo_size))
1630 if (VPPCOM_DEBUG > 0)
1631 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1632 getpid (), vcl_cfg->tx_fifo_size,
1633 vcl_cfg->tx_fifo_size);
1635 else if (unformat (line_input, "event-queue-size 0x%lx",
1636 &vcl_cfg->event_queue_size))
1638 if (VPPCOM_DEBUG > 0)
1639 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1640 getpid (), vcl_cfg->event_queue_size,
1641 vcl_cfg->event_queue_size);
1643 else if (unformat (line_input, "event-queue-size %ld",
1644 &vcl_cfg->event_queue_size))
1646 if (VPPCOM_DEBUG > 0)
1647 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1648 getpid (), vcl_cfg->event_queue_size,
1649 vcl_cfg->event_queue_size);
1651 else if (unformat (line_input, "listen-queue-size 0x%lx",
1652 &vcl_cfg->listen_queue_size))
1654 if (VPPCOM_DEBUG > 0)
1655 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1656 getpid (), vcl_cfg->listen_queue_size,
1657 vcl_cfg->listen_queue_size);
1659 else if (unformat (line_input, "listen-queue-size %ld",
1660 &vcl_cfg->listen_queue_size))
1662 if (VPPCOM_DEBUG > 0)
1663 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1664 getpid (), vcl_cfg->listen_queue_size,
1665 vcl_cfg->listen_queue_size);
1667 else if (unformat (line_input, "app-timeout %f",
1668 &vcl_cfg->app_timeout))
1670 if (VPPCOM_DEBUG > 0)
1671 clib_warning ("[%d] configured app_timeout %f",
1672 getpid (), vcl_cfg->app_timeout);
1674 else if (unformat (line_input, "session-timeout %f",
1675 &vcl_cfg->session_timeout))
1677 if (VPPCOM_DEBUG > 0)
1678 clib_warning ("[%d] configured session_timeout %f",
1679 getpid (), vcl_cfg->session_timeout);
1681 else if (unformat (line_input, "accept-timeout %f",
1682 &vcl_cfg->accept_timeout))
1684 if (VPPCOM_DEBUG > 0)
1685 clib_warning ("[%d] configured accept_timeout %f",
1686 getpid (), vcl_cfg->accept_timeout);
1688 else if (unformat (line_input, "app-proxy-transport-tcp"))
1690 vcl_cfg->app_proxy_transport_tcp = 1;
1691 if (VPPCOM_DEBUG > 0)
1692 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1693 getpid (), vcl_cfg->app_proxy_transport_tcp);
1695 else if (unformat (line_input, "app-proxy-transport-udp"))
1697 vcl_cfg->app_proxy_transport_udp = 1;
1698 if (VPPCOM_DEBUG > 0)
1699 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1700 getpid (), vcl_cfg->app_proxy_transport_udp);
1702 else if (unformat (line_input, "app-scope-local"))
1704 vcl_cfg->app_scope_local = 1;
1705 if (VPPCOM_DEBUG > 0)
1706 clib_warning ("[%d] configured app_scope_local (%d)",
1707 getpid (), vcl_cfg->app_scope_local);
1709 else if (unformat (line_input, "app-scope-global"))
1711 vcl_cfg->app_scope_global = 1;
1712 if (VPPCOM_DEBUG > 0)
1713 clib_warning ("[%d] configured app_scope_global (%d)",
1714 getpid (), vcl_cfg->app_scope_global);
1716 else if (unformat (line_input, "namespace-secret %lu",
1717 &vcl_cfg->namespace_secret))
1719 if (VPPCOM_DEBUG > 0)
1721 ("[%d] configured namespace_secret %lu (0x%lx)",
1722 getpid (), vcl_cfg->namespace_secret,
1723 vcl_cfg->namespace_secret);
1725 else if (unformat (line_input, "namespace-id %v",
1726 &vcl_cfg->namespace_id))
1728 vl_api_application_attach_t *mp;
1729 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1730 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1731 if (nsid_vec_len > max_nsid_vec_len)
1733 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1734 if (VPPCOM_DEBUG > 0)
1735 clib_warning ("[%d] configured namespace_id is too long,"
1736 " truncated to %d characters!", getpid (),
1740 if (VPPCOM_DEBUG > 0)
1741 clib_warning ("[%d] configured namespace_id %v",
1742 getpid (), vcl_cfg->namespace_id);
1744 else if (unformat (line_input, "}"))
1747 if (VPPCOM_DEBUG > 0)
1748 clib_warning ("[%d] completed parsing vppcom config!",
1754 if (line_input->buffer[line_input->index] != '#')
1756 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1758 &line_input->buffer[line_input->index]);
1765 unformat_free (input);
1773 * VPPCOM Public API functions
1776 vppcom_app_create (char *app_name)
1778 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1789 vppcom_cfg_init (vcl_cfg);
1790 conf_fname = getenv (VPPCOM_ENV_CONF);
1793 conf_fname = VPPCOM_CONF_DEFAULT;
1794 if (VPPCOM_DEBUG > 0)
1795 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1798 vppcom_cfg_heapsize (conf_fname);
1799 clib_fifo_validate (vcm->client_session_index_fifo,
1800 vcm->cfg.listen_queue_size);
1801 vppcom_cfg_read (conf_fname);
1802 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1805 u32 ns_id_vec_len = strlen (env_var_str);
1807 vec_reset_length (vcm->cfg.namespace_id);
1808 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1809 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1811 if (VPPCOM_DEBUG > 0)
1812 clib_warning ("[%d] configured namespace_id (%v) from "
1813 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1814 vcm->cfg.namespace_id);
1816 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1820 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1821 clib_warning ("[%d] Invalid namespace secret specified in "
1822 "the environment variable "
1823 VPPCOM_ENV_APP_NAMESPACE_SECRET
1824 " (%s)!\n", getpid (), env_var_str);
1827 vcm->cfg.namespace_secret = tmp;
1828 if (VPPCOM_DEBUG > 0)
1829 clib_warning ("[%d] configured namespace secret (%lu) from "
1830 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1831 vcm->cfg.namespace_secret);
1834 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1836 vcm->cfg.app_proxy_transport_tcp = 1;
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1839 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1840 vcm->cfg.app_proxy_transport_tcp);
1842 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1844 vcm->cfg.app_proxy_transport_udp = 1;
1845 if (VPPCOM_DEBUG > 0)
1846 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1847 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1848 vcm->cfg.app_proxy_transport_udp);
1850 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1852 vcm->cfg.app_scope_local = 1;
1853 if (VPPCOM_DEBUG > 0)
1854 clib_warning ("[%d] configured app_scope_local (%u) from "
1855 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1856 vcm->cfg.app_scope_local);
1858 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1860 vcm->cfg.app_scope_global = 1;
1861 if (VPPCOM_DEBUG > 0)
1862 clib_warning ("[%d] configured app_scope_global (%u) from "
1863 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1864 vcm->cfg.app_scope_global);
1867 vcm->bind_session_index = ~0;
1868 vcm->main_cpu = os_get_thread_index ();
1869 heap = clib_mem_get_per_cpu_heap ();
1870 h = mheap_header (heap);
1872 /* make the main heap thread-safe */
1873 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1875 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1877 clib_time_init (&vcm->clib_time);
1878 vppcom_init_error_string_table ();
1879 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1880 20 /* timeout in secs */ );
1881 clib_spinlock_init (&vcm->sessions_lockp);
1882 vppcom_api_hookup ();
1885 if (vcm->my_client_index == ~0)
1887 vcm->app_state = STATE_APP_START;
1888 rv = vppcom_connect_to_vpp (app_name);
1891 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
1895 if (VPPCOM_DEBUG > 0)
1896 clib_warning ("[%d] sending session enable", getpid ());
1898 rv = vppcom_app_session_enable ();
1901 clib_warning ("[%d] vppcom_app_session_enable() failed!",
1906 if (VPPCOM_DEBUG > 0)
1907 clib_warning ("[%d] sending app attach", getpid ());
1909 rv = vppcom_app_attach ();
1912 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
1916 if (VPPCOM_DEBUG > 0)
1917 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1918 getpid (), app_name, vcm->my_client_index,
1919 vcm->my_client_index);
1926 vppcom_app_destroy (void)
1930 if (vcm->my_client_index == ~0)
1933 if (VPPCOM_DEBUG > 0)
1934 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1935 getpid (), vcm->my_client_index, vcm->my_client_index);
1937 vppcom_app_detach ();
1938 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1939 if (PREDICT_FALSE (rv))
1941 if (VPPCOM_DEBUG > 0)
1942 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1943 getpid (), vppcom_retval_str (rv), rv);
1945 vl_client_disconnect_from_vlib ();
1946 vcm->my_client_index = ~0;
1947 vcm->app_state = STATE_APP_START;
1951 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1956 clib_spinlock_lock (&vcm->sessions_lockp);
1957 pool_get (vcm->sessions, session);
1958 memset (session, 0, sizeof (*session));
1959 session_index = session - vcm->sessions;
1962 session->proto = proto;
1963 session->state = STATE_START;
1964 session->is_nonblocking = is_nonblocking ? 1 : 0;
1965 clib_spinlock_unlock (&vcm->sessions_lockp);
1967 if (VPPCOM_DEBUG > 0)
1968 clib_warning ("[%d] sid %d", getpid (), session_index);
1970 return (int) session_index;
1974 vppcom_session_close (uint32_t session_index)
1976 session_t *session = 0;
1985 session_state_t state;
1987 clib_spinlock_lock (&vcm->sessions_lockp);
1988 rv = vppcom_session_at_index (session_index, &session);
1989 if (PREDICT_FALSE (rv))
1991 if (VPPCOM_DEBUG > 0)
1992 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1993 getpid (), session_index);
1994 clib_spinlock_unlock (&vcm->sessions_lockp);
1997 is_server = session->is_server;
1998 is_listen = session->is_listen;
1999 is_cut_thru = session->is_cut_thru;
2000 is_vep = session->is_vep;
2001 is_vep_session = session->is_vep_session;
2002 next_sid = session->vep.next_sid;
2003 vep_idx = session->vep.vep_idx;
2004 state = session->state;
2005 clib_spinlock_unlock (&vcm->sessions_lockp);
2007 if (VPPCOM_DEBUG > 0)
2008 clib_warning ("[%d] sid %d", getpid (), session_index);
2012 while (next_sid != ~0)
2014 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2015 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2016 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2017 "rv = %s (%d)", getpid (), vep_idx, next_sid,
2018 vppcom_retval_str (rv), rv);
2020 clib_spinlock_lock (&vcm->sessions_lockp);
2021 rv = vppcom_session_at_index (session_index, &session);
2022 if (PREDICT_FALSE (rv))
2024 if (VPPCOM_DEBUG > 0)
2026 ("[%d] invalid session, sid (%u) has been closed!",
2027 getpid (), session_index);
2028 clib_spinlock_unlock (&vcm->sessions_lockp);
2031 next_sid = session->vep.next_sid;
2032 clib_spinlock_unlock (&vcm->sessions_lockp);
2039 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2040 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2041 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2042 "rv = %s (%d)", getpid (), vep_idx, session_index,
2043 vppcom_retval_str (rv), rv);
2046 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
2048 rv = vppcom_session_unbind_cut_thru (session);
2049 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2050 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2052 getpid (), session_index,
2053 vppcom_retval_str (rv), rv);
2055 else if (is_server && is_listen)
2057 rv = vppcom_session_unbind (session_index);
2058 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2059 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
2060 getpid (), session_index,
2061 vppcom_retval_str (rv), rv);
2063 else if (state == STATE_CONNECT)
2064 if (vppcom_session_disconnect (session_index))
2067 clib_spinlock_lock (&vcm->sessions_lockp);
2068 pool_put_index (vcm->sessions, session_index);
2069 clib_spinlock_unlock (&vcm->sessions_lockp);
2075 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2077 session_t *session = 0;
2081 return VPPCOM_EINVAL;
2083 clib_spinlock_lock (&vcm->sessions_lockp);
2084 rv = vppcom_session_at_index (session_index, &session);
2085 if (PREDICT_FALSE (rv))
2087 clib_spinlock_unlock (&vcm->sessions_lockp);
2088 if (VPPCOM_DEBUG > 0)
2089 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2090 getpid (), session_index);
2094 if (session->is_vep)
2096 clib_spinlock_unlock (&vcm->sessions_lockp);
2097 if (VPPCOM_DEBUG > 0)
2098 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2099 getpid (), session_index);
2100 return VPPCOM_EBADFD;
2103 session->vrf = ep->vrf;
2104 session->lcl_addr.is_ip4 = ep->is_ip4;
2105 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2106 session->lcl_port = ep->port;
2108 if (VPPCOM_DEBUG > 0)
2109 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
2110 getpid (), session_index, format_ip46_address,
2111 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2112 clib_net_to_host_u16 (session->lcl_port));
2114 clib_spinlock_unlock (&vcm->sessions_lockp);
2119 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2121 session_t *listen_session = 0;
2124 clib_spinlock_lock (&vcm->sessions_lockp);
2125 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2126 if (PREDICT_FALSE (rv))
2128 clib_spinlock_unlock (&vcm->sessions_lockp);
2129 if (VPPCOM_DEBUG > 0)
2130 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2131 getpid (), listen_session_index);
2135 if (listen_session->is_vep)
2137 clib_spinlock_unlock (&vcm->sessions_lockp);
2138 if (VPPCOM_DEBUG > 0)
2139 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2140 getpid (), listen_session_index);
2141 return VPPCOM_EBADFD;
2144 if (listen_session->is_listen)
2146 clib_spinlock_unlock (&vcm->sessions_lockp);
2147 if (VPPCOM_DEBUG > 0)
2148 clib_warning ("[%d] sid (%u) is already in listen state!",
2149 getpid (), listen_session_index);
2153 if (VPPCOM_DEBUG > 0)
2154 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
2156 ASSERT (vcm->bind_session_index == ~0);
2157 vcm->bind_session_index = listen_session_index;
2158 vppcom_send_bind_sock (listen_session);
2159 clib_spinlock_unlock (&vcm->sessions_lockp);
2161 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2162 vcm->cfg.session_timeout);
2163 if (PREDICT_FALSE (rv))
2165 vcm->bind_session_index = ~0;
2166 if (VPPCOM_DEBUG > 0)
2167 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2168 getpid (), vppcom_retval_str (rv), rv);
2172 clib_spinlock_lock (&vcm->sessions_lockp);
2173 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2174 if (PREDICT_FALSE (rv))
2176 clib_spinlock_unlock (&vcm->sessions_lockp);
2177 if (VPPCOM_DEBUG > 0)
2178 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2179 getpid (), listen_session_index);
2182 listen_session->is_listen = 1;
2183 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2184 clib_spinlock_unlock (&vcm->sessions_lockp);
2190 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2191 double wait_for_time)
2193 session_t *listen_session = 0;
2194 session_t *client_session = 0;
2195 u32 client_session_index;
2199 clib_spinlock_lock (&vcm->sessions_lockp);
2200 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2201 if (PREDICT_FALSE (rv))
2203 clib_spinlock_unlock (&vcm->sessions_lockp);
2204 if (VPPCOM_DEBUG > 0)
2205 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2206 getpid (), listen_session_index);
2210 if (listen_session->is_vep)
2212 clib_spinlock_unlock (&vcm->sessions_lockp);
2213 if (VPPCOM_DEBUG > 0)
2214 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2215 getpid (), listen_session_index);
2216 return VPPCOM_EBADFD;
2219 if (listen_session->state != STATE_LISTEN)
2221 clib_spinlock_unlock (&vcm->sessions_lockp);
2222 if (VPPCOM_DEBUG > 0)
2223 clib_warning ("[%d] session not in listen state, state = %s",
2225 vppcom_session_state_str (listen_session->state));
2226 return VPPCOM_EBADFD;
2228 wait_for = listen_session->is_nonblocking ? 0 :
2229 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2231 if (VPPCOM_DEBUG > 0)
2232 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
2233 listen_session_index,
2234 vppcom_session_state_str (listen_session->state),
2235 listen_session->state);
2236 clib_spinlock_unlock (&vcm->sessions_lockp);
2240 rv = vppcom_wait_for_client_session_index (wait_for);
2243 if ((VPPCOM_DEBUG > 0))
2244 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2245 getpid (), listen_session_index,
2246 vppcom_retval_str (rv), rv);
2247 if ((wait_for == 0) || (wait_for_time > 0))
2254 clib_spinlock_lock (&vcm->sessions_lockp);
2255 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2256 rv = vppcom_session_at_index (client_session_index, &client_session);
2257 ASSERT (rv == VPPCOM_OK);
2258 ASSERT (client_session->peer_addr.is_ip4 ==
2259 listen_session->lcl_addr.is_ip4);
2261 if (VPPCOM_DEBUG > 0)
2262 clib_warning ("[%d] Got a request: client sid %d", getpid (),
2263 client_session_index);
2265 // Copy the lcl information from the listening session to the client session
2266 // client_session->lcl_port = listen_session->lcl_port;
2267 // client_session->lcl_addr = listen_session->lcl_addr;
2269 ep->vrf = client_session->vrf;
2270 ep->is_cut_thru = client_session->is_cut_thru;
2271 ep->is_ip4 = client_session->peer_addr.is_ip4;
2272 ep->port = client_session->peer_port;
2273 if (client_session->peer_addr.is_ip4)
2274 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2275 sizeof (ip4_address_t));
2277 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2278 sizeof (ip6_address_t));
2279 if (VPPCOM_DEBUG > 0)
2280 clib_warning ("[%d] sid %d, accepted peer address %U peer port %u",
2281 getpid (), client_session_index, format_ip46_address,
2282 &client_session->peer_addr.ip46,
2283 client_session->peer_addr.is_ip4,
2284 clib_net_to_host_u16 (client_session->peer_port));
2285 clib_spinlock_unlock (&vcm->sessions_lockp);
2286 return (int) client_session_index;
2290 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2292 session_t *session = 0;
2295 clib_spinlock_lock (&vcm->sessions_lockp);
2296 rv = vppcom_session_at_index (session_index, &session);
2297 if (PREDICT_FALSE (rv))
2299 clib_spinlock_unlock (&vcm->sessions_lockp);
2300 if (VPPCOM_DEBUG > 0)
2301 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2302 getpid (), session_index);
2306 if (session->is_vep)
2308 clib_spinlock_unlock (&vcm->sessions_lockp);
2309 if (VPPCOM_DEBUG > 0)
2310 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2311 getpid (), session_index);
2312 return VPPCOM_EBADFD;
2315 if (session->state == STATE_CONNECT)
2317 clib_spinlock_unlock (&vcm->sessions_lockp);
2318 if (VPPCOM_DEBUG > 0)
2319 clib_warning ("[%d] session, sid (%u) already connected!",
2320 getpid (), session_index);
2324 session->vrf = server_ep->vrf;
2325 session->peer_addr.is_ip4 = server_ep->is_ip4;
2326 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2327 session->peer_port = server_ep->port;
2329 if (VPPCOM_DEBUG > 0)
2331 u8 *ip_str = format (0, "%U", format_ip46_address,
2332 &session->peer_addr.ip46,
2333 session->peer_addr.is_ip4);
2334 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2335 getpid (), session_index, ip_str,
2336 clib_net_to_host_u16 (session->peer_port),
2337 session->proto ? "UDP" : "TCP");
2341 vppcom_send_connect_sock (session, session_index);
2342 clib_spinlock_unlock (&vcm->sessions_lockp);
2343 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2344 vcm->cfg.session_timeout);
2345 if (PREDICT_FALSE (rv))
2347 if (VPPCOM_DEBUG > 0)
2348 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2349 getpid (), vppcom_retval_str (rv), rv);
2352 if (VPPCOM_DEBUG > 0)
2353 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
2359 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2362 session_t *session = 0;
2363 svm_fifo_t *rx_fifo;
2371 clib_spinlock_lock (&vcm->sessions_lockp);
2372 rv = vppcom_session_at_index (session_index, &session);
2373 if (PREDICT_FALSE (rv))
2375 clib_spinlock_unlock (&vcm->sessions_lockp);
2376 if (VPPCOM_DEBUG > 0)
2377 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2378 getpid (), session_index);
2382 if (session->is_vep)
2384 clib_spinlock_unlock (&vcm->sessions_lockp);
2385 if (VPPCOM_DEBUG > 0)
2386 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2387 getpid (), session_index);
2388 return VPPCOM_EBADFD;
2391 if (session->state == STATE_DISCONNECT)
2393 clib_spinlock_unlock (&vcm->sessions_lockp);
2394 if (VPPCOM_DEBUG > 0)
2395 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2396 getpid (), session_index);
2397 return VPPCOM_ECONNRESET;
2400 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2401 session->server_rx_fifo : session->server_tx_fifo);
2402 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2403 "server_rx_fifo" : "server_tx_fifo");
2404 poll_et = EPOLLET & session->vep.ev.events;
2405 clib_spinlock_unlock (&vcm->sessions_lockp);
2410 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2412 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2414 while (!session->is_nonblocking && (n_read <= 0));
2416 if (poll_et && (n_read <= 0))
2418 clib_spinlock_lock (&vcm->sessions_lockp);
2419 session->vep.et_mask |= EPOLLIN;
2420 clib_spinlock_unlock (&vcm->sessions_lockp);
2423 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2424 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
2425 session_index, n_read, fifo_str, rx_fifo);
2427 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2431 vppcom_session_read (uint32_t session_index, void *buf, int n)
2433 return (vppcom_session_read_internal (session_index, buf, n, 0));
2437 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2439 return (vppcom_session_read_internal (session_index, buf, n, 1));
2443 vppcom_session_read_ready (session_t * session, u32 session_index)
2445 svm_fifo_t *rx_fifo;
2448 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2449 if (session->is_vep)
2451 clib_spinlock_unlock (&vcm->sessions_lockp);
2452 if (VPPCOM_DEBUG > 0)
2453 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2454 getpid (), session_index);
2455 return VPPCOM_EBADFD;
2458 if (session->state == STATE_DISCONNECT)
2460 if (VPPCOM_DEBUG > 0)
2461 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2462 getpid (), session_index);
2463 return VPPCOM_ECONNRESET;
2466 if (session->is_listen)
2467 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2470 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2471 session->server_rx_fifo : session->server_tx_fifo);
2473 ready = svm_fifo_max_dequeue (rx_fifo);
2476 if (VPPCOM_DEBUG > 3)
2477 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d",
2478 getpid (), session_index, session->is_listen,
2479 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2481 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2482 session->vep.et_mask |= EPOLLIN;
2488 vppcom_session_write (uint32_t session_index, void *buf, int n)
2490 session_t *session = 0;
2491 svm_fifo_t *tx_fifo;
2492 unix_shared_memory_queue_t *q;
2493 session_fifo_event_t evt;
2500 clib_spinlock_lock (&vcm->sessions_lockp);
2501 rv = vppcom_session_at_index (session_index, &session);
2502 if (PREDICT_FALSE (rv))
2504 clib_spinlock_unlock (&vcm->sessions_lockp);
2505 if (VPPCOM_DEBUG > 0)
2506 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2507 getpid (), session_index);
2511 if (session->is_vep)
2513 clib_spinlock_unlock (&vcm->sessions_lockp);
2514 if (VPPCOM_DEBUG > 0)
2515 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2516 getpid (), session_index);
2517 return VPPCOM_EBADFD;
2520 if (session->state == STATE_DISCONNECT)
2522 clib_spinlock_unlock (&vcm->sessions_lockp);
2523 if (VPPCOM_DEBUG > 0)
2524 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2525 getpid (), session_index);
2526 return VPPCOM_ECONNRESET;
2529 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2530 session->server_tx_fifo : session->server_rx_fifo);
2531 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2532 "server_tx_fifo" : "server_rx_fifo");
2533 q = session->vpp_event_queue;
2534 poll_et = EPOLLET & session->vep.ev.events;
2535 clib_spinlock_unlock (&vcm->sessions_lockp);
2539 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2541 while (!session->is_nonblocking && (n_write <= 0));
2543 /* If event wasn't set, add one */
2544 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2548 /* Fabricate TX event, send to vpp */
2550 evt.event_type = FIFO_EVENT_APP_TX;
2552 rval = vppcom_session_at_index (session_index, &session);
2553 if (PREDICT_FALSE (rval))
2555 if (VPPCOM_DEBUG > 1)
2556 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2557 getpid (), session_index);
2561 unix_shared_memory_queue_add (q, (u8 *) & evt,
2562 0 /* do wait for mutex */ );
2565 if (poll_et && (n_write <= 0))
2567 clib_spinlock_lock (&vcm->sessions_lockp);
2568 session->vep.et_mask |= EPOLLOUT;
2569 clib_spinlock_unlock (&vcm->sessions_lockp);
2572 if (VPPCOM_DEBUG > 2)
2575 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
2576 session_index, fifo_str, tx_fifo);
2578 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
2579 session_index, n_write, fifo_str, tx_fifo);
2581 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2585 vppcom_session_write_ready (session_t * session, u32 session_index)
2587 svm_fifo_t *tx_fifo;
2591 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2592 if (session->is_vep)
2594 clib_spinlock_unlock (&vcm->sessions_lockp);
2595 if (VPPCOM_DEBUG > 0)
2596 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2597 getpid (), session_index);
2598 return VPPCOM_EBADFD;
2601 if (session->state == STATE_DISCONNECT)
2603 if (VPPCOM_DEBUG > 0)
2604 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2605 getpid (), session_index);
2606 return VPPCOM_ECONNRESET;
2609 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2610 session->server_tx_fifo : session->server_rx_fifo);
2611 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2612 "server_tx_fifo" : "server_rx_fifo");
2614 ready = svm_fifo_max_enqueue (tx_fifo);
2616 if (VPPCOM_DEBUG > 3)
2617 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
2618 session_index, fifo_str, tx_fifo, ready);
2619 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2620 session->vep.et_mask |= EPOLLOUT;
2626 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2627 unsigned long *write_map, unsigned long *except_map,
2628 double time_to_wait)
2631 session_t *session = 0;
2632 int rv, bits_set = 0;
2633 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2634 u32 minbits = clib_max (n_bits, BITS (uword));
2636 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2638 if (n_bits && read_map)
2640 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2641 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2642 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2644 if (n_bits && write_map)
2646 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2647 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2648 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2650 if (n_bits && except_map)
2652 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2653 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2654 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2664 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2666 clib_spinlock_lock (&vcm->sessions_lockp);
2667 rv = vppcom_session_at_index (session_index, &session);
2670 clib_spinlock_unlock (&vcm->sessions_lockp);
2671 if (VPPCOM_DEBUG > 1)
2672 clib_warning ("[%d] session %d specified in "
2673 "read_map is closed.", getpid (),
2675 bits_set = VPPCOM_EBADFD;
2679 rv = vppcom_session_read_ready (session, session_index);
2680 clib_spinlock_unlock (&vcm->sessions_lockp);
2681 if (except_map && vcm->ex_bitmap &&
2682 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2685 // TBD: clib_warning
2686 clib_bitmap_set_no_check (except_map, session_index, 1);
2691 // TBD: clib_warning
2692 clib_bitmap_set_no_check (read_map, session_index, 1);
2700 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2702 clib_spinlock_lock (&vcm->sessions_lockp);
2703 rv = vppcom_session_at_index (session_index, &session);
2706 clib_spinlock_unlock (&vcm->sessions_lockp);
2707 if (VPPCOM_DEBUG > 0)
2708 clib_warning ("[%d] session %d specified in "
2709 "write_map is closed.", getpid (),
2711 bits_set = VPPCOM_EBADFD;
2715 rv = vppcom_session_write_ready (session, session_index);
2716 clib_spinlock_unlock (&vcm->sessions_lockp);
2717 if (write_map && (rv > 0))
2719 // TBD: clib_warning
2720 clib_bitmap_set_no_check (write_map, session_index, 1);
2728 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2730 clib_spinlock_lock (&vcm->sessions_lockp);
2731 rv = vppcom_session_at_index (session_index, &session);
2734 clib_spinlock_unlock (&vcm->sessions_lockp);
2735 if (VPPCOM_DEBUG > 1)
2736 clib_warning ("[%d] session %d specified in "
2737 "except_map is closed.", getpid (),
2739 bits_set = VPPCOM_EBADFD;
2743 rv = vppcom_session_read_ready (session, session_index);
2744 clib_spinlock_unlock (&vcm->sessions_lockp);
2747 // TBD: clib_warning
2748 clib_bitmap_set_no_check (except_map, session_index, 1);
2756 while (clib_time_now (&vcm->clib_time) < timeout);
2763 vep_verify_epoll_chain (u32 vep_idx)
2766 vppcom_epoll_t *vep;
2770 if (VPPCOM_DEBUG < 1)
2773 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2774 rv = vppcom_session_at_index (vep_idx, &session);
2775 if (PREDICT_FALSE (rv))
2777 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
2780 if (PREDICT_FALSE (!session->is_vep))
2782 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
2786 if (VPPCOM_DEBUG > 1)
2787 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2790 " is_vep_session = %u\n"
2791 " wait_cont_idx = 0x%x (%u)\n"
2793 vep_idx, session->is_vep, session->is_vep_session,
2794 session->wait_cont_idx, session->wait_cont_idx);
2797 vep = &session->vep;
2798 if (session->is_vep_session && (VPPCOM_DEBUG > 1))
2800 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2802 " next_sid = 0x%x (%u)\n"
2803 " prev_sid = 0x%x (%u)\n"
2804 " vep_idx = 0x%x (%u)\n"
2805 " ev.events = 0x%x\n"
2806 " ev.data.u64 = 0x%llx\n"
2810 vep->next_sid, vep->next_sid,
2811 vep->prev_sid, vep->prev_sid,
2812 vep->vep_idx, vep->vep_idx,
2813 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2815 sid = vep->next_sid;
2818 rv = vppcom_session_at_index (sid, &session);
2819 if (PREDICT_FALSE (rv))
2821 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
2824 if (PREDICT_FALSE (session->is_vep))
2825 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
2826 getpid (), vep_idx);
2827 else if (PREDICT_FALSE (!session->is_vep_session))
2829 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
2833 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2834 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
2835 "vep_idx (%u)!", getpid (),
2836 sid, session->vep.vep_idx, vep_idx);
2842 if (VPPCOM_DEBUG > 1)
2843 clib_warning ("[%d] vep_idx (%u): Dump complete!", getpid (), vep_idx);
2847 vppcom_epoll_create (void)
2849 session_t *vep_session;
2852 clib_spinlock_lock (&vcm->sessions_lockp);
2853 pool_get (vcm->sessions, vep_session);
2854 memset (vep_session, 0, sizeof (*vep_session));
2855 vep_idx = vep_session - vcm->sessions;
2857 vep_session->is_vep = 1;
2858 vep_session->vep.vep_idx = ~0;
2859 vep_session->vep.next_sid = ~0;
2860 vep_session->vep.prev_sid = ~0;
2861 vep_session->wait_cont_idx = ~0;
2862 clib_spinlock_unlock (&vcm->sessions_lockp);
2864 if (VPPCOM_DEBUG > 0)
2865 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
2871 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2872 struct epoll_event *event)
2874 session_t *vep_session;
2878 if (vep_idx == session_index)
2880 if (VPPCOM_DEBUG > 0)
2881 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
2882 getpid (), vep_idx);
2883 return VPPCOM_EINVAL;
2886 clib_spinlock_lock (&vcm->sessions_lockp);
2887 rv = vppcom_session_at_index (vep_idx, &vep_session);
2888 if (PREDICT_FALSE (rv))
2890 if (VPPCOM_DEBUG > 0)
2891 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
2894 if (PREDICT_FALSE (!vep_session->is_vep))
2896 if (VPPCOM_DEBUG > 0)
2897 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
2898 getpid (), vep_idx);
2903 ASSERT (vep_session->vep.vep_idx == ~0);
2904 ASSERT (vep_session->vep.prev_sid == ~0);
2906 rv = vppcom_session_at_index (session_index, &session);
2907 if (PREDICT_FALSE (rv))
2909 if (VPPCOM_DEBUG > 0)
2910 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
2911 getpid (), session_index);
2914 if (PREDICT_FALSE (session->is_vep))
2916 if (VPPCOM_DEBUG > 0)
2917 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2925 if (PREDICT_FALSE (!event))
2927 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
2928 "epoll_event structure!", getpid ());
2932 if (vep_session->vep.next_sid != ~0)
2934 session_t *next_session;
2935 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2937 if (PREDICT_FALSE (rv))
2939 if (VPPCOM_DEBUG > 0)
2940 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
2941 "vep.next_sid (%u) on vep_idx (%u)!",
2942 getpid (), vep_session->vep.next_sid, vep_idx);
2945 ASSERT (next_session->vep.prev_sid == vep_idx);
2946 next_session->vep.prev_sid = session_index;
2948 session->vep.next_sid = vep_session->vep.next_sid;
2949 session->vep.prev_sid = vep_idx;
2950 session->vep.vep_idx = vep_idx;
2951 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2952 session->vep.ev = *event;
2953 session->is_vep_session = 1;
2954 vep_session->vep.next_sid = session_index;
2955 if (VPPCOM_DEBUG > 1)
2956 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2957 " data 0x%llx!", getpid (), vep_idx, session_index,
2958 event->events, event->data.u64);
2962 if (PREDICT_FALSE (!event))
2964 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
2965 "epoll_event structure!", getpid ());
2969 if (PREDICT_FALSE (!session->is_vep_session &&
2970 (session->vep.vep_idx != vep_idx)))
2972 if (VPPCOM_DEBUG > 0)
2974 if (!session->is_vep_session)
2975 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
2976 "is not a vep session!",
2977 getpid (), session_index);
2979 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
2980 "vep_idx (%u) != vep_idx (%u)!",
2981 getpid (), session_index,
2982 session->vep.vep_idx, vep_idx);
2987 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2988 session->vep.ev = *event;
2989 if (VPPCOM_DEBUG > 1)
2990 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2991 " data 0x%llx!", getpid (), vep_idx, session_index,
2992 event->events, event->data.u64);
2996 if (PREDICT_FALSE (!session->is_vep_session &&
2997 (session->vep.vep_idx != vep_idx)))
2999 if (VPPCOM_DEBUG > 0)
3001 if (!session->is_vep_session)
3002 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3003 "is not a vep session!",
3004 getpid (), session_index);
3006 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3007 "vep_idx (%u) != vep_idx (%u)!",
3008 getpid (), session_index,
3009 session->vep.vep_idx, vep_idx);
3015 vep_session->wait_cont_idx =
3016 (vep_session->wait_cont_idx == session_index) ?
3017 session->vep.next_sid : vep_session->wait_cont_idx;
3019 if (session->vep.prev_sid == vep_idx)
3020 vep_session->vep.next_sid = session->vep.next_sid;
3023 session_t *prev_session;
3024 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3025 if (PREDICT_FALSE (rv))
3027 if (VPPCOM_DEBUG > 0)
3028 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3029 "vep.prev_sid (%u) on sid (%u)!",
3030 getpid (), session->vep.prev_sid,
3034 ASSERT (prev_session->vep.next_sid == session_index);
3035 prev_session->vep.next_sid = session->vep.next_sid;
3037 if (session->vep.next_sid != ~0)
3039 session_t *next_session;
3040 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3041 if (PREDICT_FALSE (rv))
3043 if (VPPCOM_DEBUG > 0)
3044 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3045 "vep.next_sid (%u) on sid (%u)!",
3046 getpid (), session->vep.next_sid,
3050 ASSERT (next_session->vep.prev_sid == session_index);
3051 next_session->vep.prev_sid = session->vep.prev_sid;
3054 memset (&session->vep, 0, sizeof (session->vep));
3055 session->vep.next_sid = ~0;
3056 session->vep.prev_sid = ~0;
3057 session->vep.vep_idx = ~0;
3058 session->is_vep_session = 0;
3059 if (VPPCOM_DEBUG > 1)
3060 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3061 getpid (), vep_idx, session_index);
3065 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3069 vep_verify_epoll_chain (vep_idx);
3072 clib_spinlock_unlock (&vcm->sessions_lockp);
3076 #define VCL_LOCK_AND_GET_SESSION(I, S) \
3078 clib_spinlock_lock (&vcm->sessions_lockp); \
3079 rv = vppcom_session_at_index (I, S); \
3080 if (PREDICT_FALSE (rv)) \
3082 clib_spinlock_unlock (&vcm->sessions_lockp); \
3084 if (VPPCOM_DEBUG > 0) \
3085 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
3093 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3094 int maxevents, double wait_for_time)
3096 session_t *vep_session;
3098 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3099 u32 keep_trying = 1;
3101 u32 vep_next_sid, wait_cont_idx;
3104 if (PREDICT_FALSE (maxevents <= 0))
3106 if (VPPCOM_DEBUG > 0)
3107 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3108 getpid (), maxevents);
3109 return VPPCOM_EINVAL;
3111 memset (events, 0, sizeof (*events) * maxevents);
3113 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3114 vep_next_sid = vep_session->vep.next_sid;
3115 is_vep = vep_session->is_vep;
3116 wait_cont_idx = vep_session->wait_cont_idx;
3117 clib_spinlock_unlock (&vcm->sessions_lockp);
3119 if (PREDICT_FALSE (!is_vep))
3121 if (VPPCOM_DEBUG > 0)
3122 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3123 getpid (), vep_idx);
3127 if (PREDICT_FALSE (vep_next_sid == ~0))
3129 if (VPPCOM_DEBUG > 0)
3130 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3131 getpid (), vep_idx);
3141 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3142 sid != ~0; sid = next_sid)
3144 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3145 u8 add_event, is_vep_session;
3147 u64 session_ev_data;
3149 VCL_LOCK_AND_GET_SESSION (sid, &session);
3150 next_sid = session->vep.next_sid;
3151 session_events = session->vep.ev.events;
3152 et_mask = session->vep.et_mask;
3153 is_vep = session->is_vep;
3154 is_vep_session = session->is_vep_session;
3155 session_vep_idx = session->vep.vep_idx;
3156 session_ev_data = session->vep.ev.data.u64;
3157 clib_spinlock_unlock (&vcm->sessions_lockp);
3159 if (PREDICT_FALSE (is_vep))
3161 if (VPPCOM_DEBUG > 0)
3162 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3163 getpid (), vep_idx);
3167 if (PREDICT_FALSE (!is_vep_session))
3169 if (VPPCOM_DEBUG > 0)
3170 clib_warning ("[%d] ERROR: session (%u) is not "
3171 "a vep session!", getpid (), sid);
3175 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3177 clib_warning ("[%d] ERROR: session (%u) "
3178 "vep_idx (%u) != vep_idx (%u)!",
3179 getpid (), sid, session->vep.vep_idx, vep_idx);
3184 add_event = clear_et_mask = 0;
3186 if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
3188 VCL_LOCK_AND_GET_SESSION (sid, &session);
3189 ready = vppcom_session_read_ready (session, sid);
3190 clib_spinlock_unlock (&vcm->sessions_lockp);
3194 events[num_ev].events |= EPOLLIN;
3195 if (EPOLLET & session_events)
3196 clear_et_mask |= EPOLLIN;
3203 case VPPCOM_ECONNRESET:
3204 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3208 events[num_ev].events |= EPOLLERR;
3214 if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
3216 VCL_LOCK_AND_GET_SESSION (sid, &session);
3217 ready = vppcom_session_write_ready (session, sid);
3218 clib_spinlock_unlock (&vcm->sessions_lockp);
3222 events[num_ev].events |= EPOLLOUT;
3223 if (EPOLLET & session_events)
3224 clear_et_mask |= EPOLLOUT;
3231 case VPPCOM_ECONNRESET:
3232 events[num_ev].events |= EPOLLHUP;
3236 events[num_ev].events |= EPOLLERR;
3244 events[num_ev].data.u64 = session_ev_data;
3245 if (EPOLLONESHOT & session_events)
3247 VCL_LOCK_AND_GET_SESSION (sid, &session);
3248 session->vep.ev.events = 0;
3249 clib_spinlock_unlock (&vcm->sessions_lockp);
3252 if (num_ev == maxevents)
3254 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3255 vep_session->wait_cont_idx = next_sid;
3256 clib_spinlock_unlock (&vcm->sessions_lockp);
3260 if (wait_cont_idx != ~0)
3263 next_sid = vep_next_sid;
3264 else if (next_sid == wait_cont_idx)
3268 if (wait_for_time != -1)
3269 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3271 while ((num_ev == 0) && keep_trying);
3273 if (wait_cont_idx != ~0)
3275 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3276 vep_session->wait_cont_idx = ~0;
3277 clib_spinlock_unlock (&vcm->sessions_lockp);
3280 return (rv != VPPCOM_OK) ? rv : num_ev;
3284 vppcom_session_attr (uint32_t session_index, uint32_t op,
3285 void *buffer, uint32_t * buflen)
3289 u32 *flags = buffer;
3290 vppcom_endpt_t *ep = buffer;
3292 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3295 case VPPCOM_ATTR_GET_NREAD:
3296 rv = vppcom_session_read_ready (session, session_index);
3297 if (VPPCOM_DEBUG > 1)
3298 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: nread = %d",
3303 case VPPCOM_ATTR_PEEK_NREAD:
3307 case VPPCOM_ATTR_GET_FLAGS:
3308 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3310 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3311 *buflen = sizeof (*flags);
3312 if (VPPCOM_DEBUG > 1)
3313 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3314 "is_nonblocking = %u", getpid (), *flags,
3315 session->is_nonblocking);
3321 case VPPCOM_ATTR_SET_FLAGS:
3322 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3324 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3325 if (VPPCOM_DEBUG > 1)
3326 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3327 "is_nonblocking = %u", getpid (), *flags,
3328 session->is_nonblocking);
3334 case VPPCOM_ATTR_GET_PEER_ADDR:
3335 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3337 ep->vrf = session->vrf;
3338 ep->is_ip4 = session->peer_addr.is_ip4;
3339 ep->port = session->peer_port;
3340 if (session->peer_addr.is_ip4)
3341 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3342 sizeof (ip4_address_t));
3344 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3345 sizeof (ip6_address_t));
3346 *buflen = sizeof (*ep);
3347 if (VPPCOM_DEBUG > 1)
3348 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = "
3349 "%u, addr = %U, port %u", getpid (),
3350 session_index, ep->is_ip4, format_ip46_address,
3351 &session->peer_addr.ip46, ep->is_ip4,
3352 clib_net_to_host_u16 (ep->port));
3358 case VPPCOM_ATTR_GET_LCL_ADDR:
3359 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3361 ep->vrf = session->vrf;
3362 ep->is_ip4 = session->lcl_addr.is_ip4;
3363 ep->port = session->lcl_port;
3364 if (session->lcl_addr.is_ip4)
3365 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3366 sizeof (ip4_address_t));
3368 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3369 sizeof (ip6_address_t));
3370 *buflen = sizeof (*ep);
3371 if (VPPCOM_DEBUG > 1)
3372 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = "
3373 "%u, addr = %U port %d", getpid (),
3374 session_index, ep->is_ip4, format_ip46_address,
3375 &session->lcl_addr.ip46, ep->is_ip4,
3376 clib_net_to_host_u16 (ep->port));
3382 case VPPCOM_ATTR_SET_REUSEADDR:
3385 case VPPCOM_ATTR_SET_BROADCAST:
3388 case VPPCOM_ATTR_SET_V6ONLY:
3391 case VPPCOM_ATTR_SET_KEEPALIVE:
3394 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3397 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3406 clib_spinlock_unlock (&vcm->sessions_lockp);
3411 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3412 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3415 session_t *session = 0;
3419 clib_spinlock_lock (&vcm->sessions_lockp);
3420 rv = vppcom_session_at_index (session_index, &session);
3421 if (PREDICT_FALSE (rv))
3423 clib_spinlock_unlock (&vcm->sessions_lockp);
3424 if (VPPCOM_DEBUG > 0)
3425 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3426 getpid (), session_index);
3428 clib_spinlock_unlock (&vcm->sessions_lockp);
3431 ep->vrf = session->vrf;
3432 ep->is_ip4 = session->peer_addr.is_ip4;
3433 ep->port = session->peer_port;
3434 if (session->peer_addr.is_ip4)
3435 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3436 sizeof (ip4_address_t));
3438 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3439 sizeof (ip6_address_t));
3440 clib_spinlock_unlock (&vcm->sessions_lockp);
3444 rv = vppcom_session_read (session_index, buffer, buflen);
3445 else if (flags & MSG_PEEK)
3446 rv = vppcom_session_peek (session_index, buffer, buflen);
3449 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3450 rv = VPPCOM_EAFNOSUPPORT;
3458 vppcom_session_sendto (uint32_t session_index, void *buffer,
3459 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3462 return VPPCOM_EINVAL;
3467 return VPPCOM_EINVAL;
3472 // TBD check the flags and do the right thing
3473 if (VPPCOM_DEBUG > 2)
3474 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3475 getpid (), flags, flags);
3478 return (vppcom_session_write (session_index, buffer, buflen));
3482 * fd.io coding-style-patch-verification: ON
3485 * eval: (c-set-style "gnu")