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_INIT 2 for connection debug,
45 * 3 for read/write debug output
47 * export VCL_DEBUG=<#> to set dynamically.
49 #define VPPCOM_DEBUG_INIT 1
51 #define VPPCOM_DEBUG_INIT 0
54 #define VPPCOM_DEBUG vcm->debug
57 * VPPCOM Private definitions and functions.
77 typedef struct epoll_event vppcom_epoll_event_t;
84 vppcom_epoll_event_t ev;
85 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
86 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
98 volatile session_state_t state;
100 svm_fifo_t *server_rx_fifo;
101 svm_fifo_t *server_tx_fifo;
106 unix_shared_memory_queue_t *vpp_event_queue;
108 /* Socket configuration state */
109 /* TBD: covert 'is_*' vars to bit in u8 flags; */
119 vppcom_ip46_t lcl_addr;
120 vppcom_ip46_t peer_addr;
121 u16 lcl_port; // network order
122 u16 peer_port; // network order
124 u64 client_queue_address;
128 typedef struct vppcom_cfg_t_
133 u32 add_segment_size;
134 u32 preallocated_fifo_pairs;
137 u32 event_queue_size;
138 u32 listen_queue_size;
139 u8 app_proxy_transport_tcp;
140 u8 app_proxy_transport_udp;
144 u64 namespace_secret;
150 typedef struct vppcom_main_t_
154 u32 *client_session_index_fifo;
155 volatile u32 bind_session_index;
158 /* vpe input queue */
159 unix_shared_memory_queue_t *vl_input_queue;
161 /* API client handle */
165 clib_spinlock_t sessions_lockp;
168 /* Hash table for disconnect processing */
169 uword *session_index_by_vpp_handles;
172 clib_bitmap_t *rd_bitmap;
173 clib_bitmap_t *wr_bitmap;
174 clib_bitmap_t *ex_bitmap;
176 /* Our event queue */
177 unix_shared_memory_queue_t *app_event_queue;
179 /* unique segment name counter */
180 u32 unique_segment_index;
182 /* For deadman timers */
183 clib_time_t clib_time;
185 /* State of the connection, shared between msg RX thread and main thread */
186 volatile app_state_t app_state;
190 /* VNET_API_ERROR_FOO -> "Foo" hash table */
191 uword *error_string_by_error_number;
194 /* 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.
198 static vppcom_main_t _vppcom_main = {
199 .debug = VPPCOM_DEBUG_INIT,
200 .my_client_index = ~0
203 static vppcom_main_t *vcm = &_vppcom_main;
205 #define VCL_LOCK_AND_GET_SESSION(I, S) \
207 clib_spinlock_lock (&vcm->sessions_lockp); \
208 rv = vppcom_session_at_index (I, S); \
209 if (PREDICT_FALSE (rv)) \
211 clib_spinlock_unlock (&vcm->sessions_lockp); \
213 if (VPPCOM_DEBUG > 0) \
214 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
222 vppcom_app_state_str (app_state_t state)
228 case STATE_APP_START:
229 st = "STATE_APP_START";
232 case STATE_APP_CONN_VPP:
233 st = "STATE_APP_CONN_VPP";
236 case STATE_APP_ENABLED:
237 st = "STATE_APP_ENABLED";
240 case STATE_APP_ATTACHED:
241 st = "STATE_APP_ATTACHED";
245 st = "UNKNOWN_APP_STATE";
253 vppcom_session_state_str (session_state_t state)
264 st = "STATE_CONNECT";
275 case STATE_DISCONNECT:
276 st = "STATE_DISCONNECT";
284 st = "UNKNOWN_STATE";
292 * VPPCOM Utility Functions
295 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
297 /* 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)))
301 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
302 getpid (), session_index);
303 return VPPCOM_EBADFD;
305 *sess = pool_elt_at_index (vcm->sessions, session_index);
310 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
312 /* Session and listener handles have different formats. The latter has
313 * the thread index in the upper 32 bits while the former has the session
314 * type. Knowing that, for listeners we just flip the MSB to 1 */
315 listener_handle |= 1ULL << 63;
316 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
319 static inline session_t *
320 vppcom_session_table_lookup_listener (u64 listener_handle)
323 u64 handle = listener_handle | (1ULL << 63);
324 p = hash_get (vcm->session_index_by_vpp_handles, handle);
327 clib_warning ("[%d] couldn't find listen session: unknown vpp "
328 "listener handle %llx", getpid (), listener_handle);
331 if (pool_is_free_index (vcm->sessions, p[0]))
333 if (VPPCOM_DEBUG > 1)
334 clib_warning ("[%d] invalid listen session, sid (%u)", getpid (),
339 return pool_elt_at_index (vcm->sessions, p[0]);
343 vppcom_session_table_del_listener (u64 listener_handle)
345 listener_handle |= 1ULL << 63;
346 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
350 vppcom_connect_to_vpp (char *app_name)
352 api_main_t *am = &api_main;
354 if (VPPCOM_DEBUG > 0)
355 printf ("\nConnecting to VPP api...");
356 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
358 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
359 return VPPCOM_ECONNREFUSED;
362 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
363 vcm->my_client_index = am->my_client_index;
364 if (VPPCOM_DEBUG > 0)
365 printf (" connected!\n");
367 vcm->app_state = STATE_APP_CONN_VPP;
372 format_api_error (u8 * s, va_list * args)
374 i32 error = va_arg (*args, u32);
377 p = hash_get (vcm->error_string_by_error_number, -error);
380 s = format (s, "%s (%d)", p[0], error);
382 s = format (s, "%d", error);
387 vppcom_init_error_string_table (void)
389 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
391 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
392 foreach_vnet_api_error;
395 hash_set (vcm->error_string_by_error_number, 99, "Misc");
399 vppcom_wait_for_app_state_change (app_state_t app_state)
401 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
403 while (clib_time_now (&vcm->clib_time) < timeout)
405 if (vcm->app_state == app_state)
408 if (VPPCOM_DEBUG > 0)
409 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
410 vppcom_app_state_str (app_state), app_state);
411 return VPPCOM_ETIMEDOUT;
415 vppcom_wait_for_session_state_change (u32 session_index,
416 session_state_t state,
419 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
420 session_t *volatile session;
425 clib_spinlock_lock (&vcm->sessions_lockp);
426 rv = vppcom_session_at_index (session_index, &session);
427 if (PREDICT_FALSE (rv))
429 clib_spinlock_unlock (&vcm->sessions_lockp);
432 if (session->state == state)
434 clib_spinlock_unlock (&vcm->sessions_lockp);
437 clib_spinlock_unlock (&vcm->sessions_lockp);
439 while (clib_time_now (&vcm->clib_time) < timeout);
441 if (VPPCOM_DEBUG > 0)
442 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
443 vppcom_session_state_str (state), state);
444 return VPPCOM_ETIMEDOUT;
448 vppcom_wait_for_client_session_index (f64 wait_for_time)
450 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
454 if (clib_fifo_elts (vcm->client_session_index_fifo))
457 while (clib_time_now (&vcm->clib_time) < timeout);
459 if (wait_for_time == 0)
460 return VPPCOM_EAGAIN;
462 if (VPPCOM_DEBUG > 0)
463 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
464 return VPPCOM_ETIMEDOUT;
468 * VPP-API message functions
471 vppcom_send_session_enable_disable (u8 is_enable)
473 vl_api_session_enable_disable_t *bmp;
474 bmp = vl_msg_api_alloc (sizeof (*bmp));
475 memset (bmp, 0, sizeof (*bmp));
477 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
478 bmp->client_index = vcm->my_client_index;
479 bmp->context = htonl (0xfeedface);
480 bmp->is_enable = is_enable;
481 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
485 vppcom_app_session_enable (void)
489 if (vcm->app_state != STATE_APP_ENABLED)
491 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
492 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
493 if (PREDICT_FALSE (rv))
495 if (VPPCOM_DEBUG > 0)
496 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
497 getpid (), vppcom_retval_str (rv), rv);
505 vl_api_session_enable_disable_reply_t_handler
506 (vl_api_session_enable_disable_reply_t * mp)
510 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
511 format_api_error, ntohl (mp->retval));
514 vcm->app_state = STATE_APP_ENABLED;
518 vppcom_app_send_attach (void)
520 vl_api_application_attach_t *bmp;
521 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
522 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
523 vcm->cfg.app_proxy_transport_udp);
525 bmp = vl_msg_api_alloc (sizeof (*bmp));
526 memset (bmp, 0, sizeof (*bmp));
528 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
529 bmp->client_index = vcm->my_client_index;
530 bmp->context = htonl (0xfeedface);
531 bmp->options[APP_OPTIONS_FLAGS] =
532 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
533 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
534 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
535 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
536 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
537 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
538 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
539 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
540 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
541 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
542 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
545 bmp->namespace_id_len = nsid_len;
546 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
547 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
549 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
553 vppcom_app_attach (void)
557 vppcom_app_send_attach ();
558 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
559 if (PREDICT_FALSE (rv))
561 if (VPPCOM_DEBUG > 0)
562 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
563 getpid (), vppcom_retval_str (rv), rv);
570 vppcom_app_detach (void)
572 vl_api_application_detach_t *bmp;
573 bmp = vl_msg_api_alloc (sizeof (*bmp));
574 memset (bmp, 0, sizeof (*bmp));
576 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
577 bmp->client_index = vcm->my_client_index;
578 bmp->context = htonl (0xfeedface);
579 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
583 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
586 static svm_fifo_segment_create_args_t _a;
587 svm_fifo_segment_create_args_t *a = &_a;
590 memset (a, 0, sizeof (*a));
593 clib_warning ("[%d] attach failed: %U", getpid (),
594 format_api_error, ntohl (mp->retval));
598 if (mp->segment_name_length == 0)
600 clib_warning ("[%d] segment_name_length zero", getpid ());
604 a->segment_name = (char *) mp->segment_name;
605 a->segment_size = mp->segment_size;
607 ASSERT (mp->app_event_queue_address);
609 /* Attach to the segment vpp created */
610 rv = svm_fifo_segment_attach (a);
611 vec_reset_length (a->new_segment_indices);
612 if (PREDICT_FALSE (rv))
614 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
619 vcm->app_event_queue =
620 uword_to_pointer (mp->app_event_queue_address,
621 unix_shared_memory_queue_t *);
623 vcm->app_state = STATE_APP_ATTACHED;
627 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
631 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
634 vcm->app_state = STATE_APP_ENABLED;
638 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
642 u32 session_index = ~0;
644 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
647 session_t *session = 0;
650 session_index = p[0];
651 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
652 clib_spinlock_lock (&vcm->sessions_lockp);
653 rv = vppcom_session_at_index (session_index, &session);
654 if (PREDICT_FALSE (rv))
656 if (VPPCOM_DEBUG > 1)
657 clib_warning ("[%d] invalid session, sid %u has been closed!",
658 getpid (), session_index);
662 if (VPPCOM_DEBUG > 1)
663 clib_warning ("[%d] vpp handle 0x%llx, sid %u disconnected!",
664 getpid (), mp->handle, session_index);
666 session->state = STATE_DISCONNECT;
668 clib_spinlock_unlock (&vcm->sessions_lockp);
672 if (VPPCOM_DEBUG > 1)
673 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
674 getpid (), mp->handle);
678 clib_warning ("[%d] disconnect_session vpp handle 0x%llx, sid %u "
679 "failed: %U", getpid (), mp->handle, session_index,
680 format_api_error, ntohl (mp->retval));
684 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
686 static svm_fifo_segment_create_args_t _a;
687 svm_fifo_segment_create_args_t *a = &_a;
690 memset (a, 0, sizeof (*a));
691 a->segment_name = (char *) mp->segment_name;
692 a->segment_size = mp->segment_size;
693 /* Attach to the segment vpp created */
694 rv = svm_fifo_segment_attach (a);
695 vec_reset_length (a->new_segment_indices);
696 if (PREDICT_FALSE (rv))
698 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
699 getpid (), mp->segment_name);
702 if (VPPCOM_DEBUG > 1)
703 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
704 mp->segment_name, mp->segment_size);
708 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
710 session_t *session = 0;
711 vl_api_disconnect_session_reply_t *rmp;
715 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
716 if (VPPCOM_DEBUG > 0)
720 clib_warning ("[%d] request to disconnect invalid handle (%u)!",
721 getpid (), mp->handle);
725 clib_warning ("[%d] disconnecting handle %u sid (%u)!", getpid (),
733 clib_spinlock_lock (&vcm->sessions_lockp);
734 rval = vppcom_session_at_index (p[0], &session);
735 if (PREDICT_FALSE (rval))
737 if (VPPCOM_DEBUG > 1)
738 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
742 pool_put (vcm->sessions, session);
743 clib_spinlock_unlock (&vcm->sessions_lockp);
744 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
748 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
749 getpid (), mp->handle);
754 rmp = vl_msg_api_alloc (sizeof (*rmp));
755 memset (rmp, 0, sizeof (*rmp));
757 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
758 rmp->retval = htonl (rv);
759 rmp->handle = mp->handle;
760 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
764 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
766 session_t *session = 0;
767 vl_api_reset_session_reply_t *rmp;
771 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
775 clib_spinlock_lock (&vcm->sessions_lockp);
776 rval = vppcom_session_at_index (p[0], &session);
777 if (PREDICT_FALSE (rval))
779 if (VPPCOM_DEBUG > 1)
780 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
784 pool_put (vcm->sessions, session);
785 clib_spinlock_unlock (&vcm->sessions_lockp);
786 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
790 clib_warning ("[%d] couldn't find session: unknown vpp handle 0x%llx",
791 getpid (), mp->handle);
795 rmp = vl_msg_api_alloc (sizeof (*rmp));
796 memset (rmp, 0, sizeof (*rmp));
797 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
798 rmp->retval = htonl (rv);
799 rmp->handle = mp->handle;
800 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
804 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
808 svm_fifo_t *rx_fifo, *tx_fifo;
814 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
819 session_index = mp->context;
820 if (VPPCOM_DEBUG > 1)
821 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
822 session_index, session_index);
824 clib_spinlock_lock (&vcm->sessions_lockp);
825 if (pool_is_free_index (vcm->sessions, session_index))
827 clib_spinlock_unlock (&vcm->sessions_lockp);
828 if (VPPCOM_DEBUG > 1)
829 clib_warning ("[%d] invalid session, sid %d is closed!",
830 getpid (), session_index);
834 /* We've been redirected */
835 if (mp->segment_name_length > 0)
837 static svm_fifo_segment_create_args_t _a;
838 svm_fifo_segment_create_args_t *a = &_a;
841 memset (a, 0, sizeof (*a));
842 a->segment_name = (char *) mp->segment_name;
843 if (VPPCOM_DEBUG > 1)
844 clib_warning ("[%d] cut-thru segment: %s\n",
845 getpid (), a->segment_name);
847 rv = svm_fifo_segment_attach (a);
848 vec_reset_length (a->new_segment_indices);
849 if (PREDICT_FALSE (rv))
851 clib_spinlock_unlock (&vcm->sessions_lockp);
852 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
853 getpid (), a->segment_name);
861 session = pool_elt_at_index (vcm->sessions, session_index);
862 session->is_cut_thru = is_cut_thru;
863 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
864 unix_shared_memory_queue_t *);
866 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
867 rx_fifo->client_session_index = session_index;
868 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
869 tx_fifo->client_session_index = session_index;
871 session->server_rx_fifo = rx_fifo;
872 session->server_tx_fifo = tx_fifo;
873 session->vpp_handle = mp->handle;
874 session->lcl_addr.is_ip4 = mp->is_ip4;
875 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
876 sizeof (session->peer_addr.ip46));
877 session->lcl_port = mp->lcl_port;
878 session->state = STATE_CONNECT;
880 /* Add it to lookup table */
881 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
883 if (VPPCOM_DEBUG > 1)
884 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
885 " session_rx_fifo %p, refcnt %d\n"
886 " session_tx_fifo %p, refcnt %d",
887 getpid (), session_index, mp->handle,
888 session->server_rx_fifo,
889 session->server_rx_fifo->refcnt,
890 session->server_tx_fifo, session->server_tx_fifo->refcnt);
892 clib_spinlock_unlock (&vcm->sessions_lockp);
896 vppcom_send_connect_sock (session_t * session, u32 session_index)
898 vl_api_connect_sock_t *cmp;
900 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
901 session->is_server = 0;
902 cmp = vl_msg_api_alloc (sizeof (*cmp));
903 memset (cmp, 0, sizeof (*cmp));
904 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
905 cmp->client_index = vcm->my_client_index;
906 cmp->context = session_index;
908 if (VPPCOM_DEBUG > 1)
909 clib_warning ("[%d] session_index = %d 0x%08x",
910 getpid (), session_index, session_index);
912 cmp->vrf = session->vrf;
913 cmp->is_ip4 = session->peer_addr.is_ip4;
914 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
915 cmp->port = session->peer_port;
916 cmp->proto = session->proto;
917 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
918 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
922 vppcom_send_disconnect (session_t * session)
924 vl_api_disconnect_session_t *dmp;
926 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
927 dmp = vl_msg_api_alloc (sizeof (*dmp));
928 memset (dmp, 0, sizeof (*dmp));
929 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
930 dmp->client_index = vcm->my_client_index;
931 dmp->handle = session->vpp_handle;
932 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
936 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
938 session_t *session = 0;
942 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
945 ASSERT (vcm->bind_session_index != ~0);
947 clib_spinlock_lock (&vcm->sessions_lockp);
948 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
951 session->vpp_handle = mp->handle;
952 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
953 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
954 sizeof (session->peer_addr.ip46));
955 session->lcl_port = mp->lcl_port;
956 vppcom_session_table_add_listener (mp->handle, vcm->bind_session_index);
957 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
958 vcm->bind_session_index = ~0;
960 clib_spinlock_unlock (&vcm->sessions_lockp);
964 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
966 session_t *session = 0;
968 clib_spinlock_lock (&vcm->sessions_lockp);
969 session = vppcom_session_table_lookup_listener (vcm->bind_session_index);
973 if ((VPPCOM_DEBUG > 1) && (mp->retval))
974 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
977 vppcom_session_table_del_listener (vcm->bind_session_index);
978 vcm->bind_session_index = ~0;
979 session->state = STATE_START;
981 clib_spinlock_unlock (&vcm->sessions_lockp);
985 format_ip4_address (u8 * s, va_list * args)
987 u8 *a = va_arg (*args, u8 *);
988 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
992 format_ip6_address (u8 * s, va_list * args)
994 ip6_address_t *a = va_arg (*args, ip6_address_t *);
995 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
997 i_max_n_zero = ARRAY_LEN (a->as_u16);
999 i_first_zero = i_max_n_zero;
1001 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1003 u32 is_zero = a->as_u16[i] == 0;
1004 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1010 if ((!is_zero && n_zeros > max_n_zeros)
1011 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1013 i_max_n_zero = i_first_zero;
1014 max_n_zeros = n_zeros;
1015 i_first_zero = ARRAY_LEN (a->as_u16);
1020 last_double_colon = 0;
1021 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1023 if (i == i_max_n_zero && max_n_zeros > 1)
1025 s = format (s, "::");
1026 i += max_n_zeros - 1;
1027 last_double_colon = 1;
1031 s = format (s, "%s%x",
1032 (last_double_colon || i == 0) ? "" : ":",
1033 clib_net_to_host_u16 (a->as_u16[i]));
1034 last_double_colon = 0;
1041 /* Format an IP46 address. */
1043 format_ip46_address (u8 * s, va_list * args)
1045 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1046 ip46_type_t type = va_arg (*args, ip46_type_t);
1052 is_ip4 = ip46_address_is_ip4 (ip46);
1063 format (s, "%U", format_ip4_address, &ip46->ip4) :
1064 format (s, "%U", format_ip6_address, &ip46->ip6);
1068 vppcom_send_accept_session_reply (u32 handle, int retval)
1070 vl_api_accept_session_reply_t *rmp;
1072 rmp = vl_msg_api_alloc (sizeof (*rmp));
1073 memset (rmp, 0, sizeof (*rmp));
1074 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1075 rmp->retval = htonl (retval);
1076 rmp->handle = handle;
1077 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1081 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1083 svm_fifo_t *rx_fifo, *tx_fifo;
1084 session_t *session, *listen_session;
1087 clib_spinlock_lock (&vcm->sessions_lockp);
1088 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1090 clib_warning ("[%d] client session queue is full!", getpid ());
1091 vppcom_send_accept_session_reply (mp->handle,
1092 VNET_API_ERROR_QUEUE_FULL);
1093 clib_spinlock_unlock (&vcm->sessions_lockp);
1097 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1098 if (!listen_session)
1100 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1101 "listener handle %llx", getpid (), mp->listener_handle);
1102 clib_spinlock_unlock (&vcm->sessions_lockp);
1106 /* Allocate local session and set it up */
1107 pool_get (vcm->sessions, session);
1108 memset (session, 0, sizeof (*session));
1109 session_index = session - vcm->sessions;
1111 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1112 rx_fifo->client_session_index = session_index;
1113 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1114 tx_fifo->client_session_index = session_index;
1116 session->vpp_handle = mp->handle;
1117 session->server_rx_fifo = rx_fifo;
1118 session->server_tx_fifo = tx_fifo;
1119 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1120 unix_shared_memory_queue_t *);
1121 session->state = STATE_ACCEPT;
1122 session->is_cut_thru = 0;
1123 session->is_server = 1;
1124 session->peer_port = mp->port;
1125 session->peer_addr.is_ip4 = mp->is_ip4;
1126 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1127 sizeof (session->peer_addr.ip46));
1129 /* Add it to lookup table */
1130 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1131 session->lcl_port = listen_session->lcl_port;
1132 session->lcl_addr = listen_session->lcl_addr;
1134 /* TBD: move client_session_index_fifo into listener session */
1135 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1137 clib_spinlock_unlock (&vcm->sessions_lockp);
1139 if (VPPCOM_DEBUG > 1)
1141 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1142 clib_warning ("[%d] received request to accept session (sid %d) "
1143 "from %s:%d", getpid (), session_index, ip_str,
1144 clib_net_to_host_u16 (mp->port));
1150 vppcom_send_connect_session_reply (session_t * session, int retval)
1152 vl_api_connect_session_reply_t *rmp;
1154 unix_shared_memory_queue_t *client_q;
1156 rmp = vl_msg_api_alloc (sizeof (*rmp));
1157 memset (rmp, 0, sizeof (*rmp));
1159 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1160 rmp->context = session->client_context;
1161 rmp->retval = htonl (retval);
1162 rmp->handle = session->vpp_handle;
1163 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1164 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1165 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1166 rmp->segment_size = vcm->cfg.segment_size;
1167 len = vec_len (session->segment_name);
1168 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1169 clib_memcpy (rmp->segment_name, session->segment_name,
1170 rmp->segment_name_length - 1);
1171 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1172 sizeof (rmp->lcl_ip));
1173 rmp->is_ip4 = session->lcl_addr.is_ip4;
1174 client_q = uword_to_pointer (session->client_queue_address,
1175 unix_shared_memory_queue_t *);
1177 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1181 * Acting as server for redirected connect requests
1184 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1187 session_t *session = 0;
1189 clib_spinlock_lock (&vcm->sessions_lockp);
1190 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1192 if (VPPCOM_DEBUG > 1)
1193 clib_warning ("[%d] client session queue is full!", getpid ());
1194 clib_spinlock_unlock (&vcm->sessions_lockp);
1195 /* TBD: fix handle */
1196 vppcom_send_accept_session_reply (0, VNET_API_ERROR_QUEUE_FULL);
1200 pool_get (vcm->sessions, session);
1201 memset (session, 0, sizeof (*session));
1202 session_index = session - vcm->sessions;
1204 session->client_context = mp->context;
1205 session->vpp_handle = session_index;
1206 session->client_queue_address = mp->client_queue_address;
1207 session->is_cut_thru = 1;
1208 session->is_server = 1;
1209 session->peer_port = mp->port;
1210 session->peer_addr.is_ip4 = mp->is_ip4;
1211 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1212 sizeof (session->peer_addr.ip46));
1214 session->state = STATE_ACCEPT;
1215 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1216 if (VPPCOM_DEBUG > 1)
1218 ("[%d] Got a cut-thru connect request to client: "
1219 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
1220 clib_fifo_elts (vcm->client_session_index_fifo));
1221 clib_spinlock_unlock (&vcm->sessions_lockp);
1225 vppcom_send_bind_sock (session_t * session)
1227 vl_api_bind_sock_t *bmp;
1229 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1230 session->is_server = 1;
1231 bmp = vl_msg_api_alloc (sizeof (*bmp));
1232 memset (bmp, 0, sizeof (*bmp));
1234 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1235 bmp->client_index = vcm->my_client_index;
1236 bmp->context = htonl (0xfeedface);
1237 bmp->vrf = session->vrf;
1238 bmp->is_ip4 = session->lcl_addr.is_ip4;
1239 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1240 bmp->port = session->lcl_port;
1241 bmp->proto = session->proto;
1242 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1243 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1247 vppcom_send_unbind_sock (u32 session_index)
1249 vl_api_unbind_sock_t *ump;
1250 session_t *session = 0;
1253 clib_spinlock_lock (&vcm->sessions_lockp);
1254 rv = vppcom_session_at_index (session_index, &session);
1255 if (PREDICT_FALSE (rv))
1257 clib_spinlock_unlock (&vcm->sessions_lockp);
1258 if (VPPCOM_DEBUG > 0)
1259 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1260 getpid (), session_index);
1264 ump = vl_msg_api_alloc (sizeof (*ump));
1265 memset (ump, 0, sizeof (*ump));
1267 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1268 ump->client_index = vcm->my_client_index;
1269 ump->handle = session->vpp_handle;
1270 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1275 vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
1277 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1278 svm_fifo_segment_private_t *seg;
1281 if (VPPCOM_DEBUG > 1)
1282 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1283 " server_rx_fifo %p, refcnt = %d\n"
1284 " server_tx_fifo %p, refcnt = %d",
1285 getpid (), session_index, session->sm_seg_index,
1286 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1287 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1289 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1290 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1291 FIFO_SEGMENT_RX_FREELIST);
1292 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1293 FIFO_SEGMENT_TX_FREELIST);
1294 svm_fifo_segment_delete (seg);
1300 vppcom_session_unbind (u32 session_index)
1304 clib_spinlock_lock (&vcm->sessions_lockp);
1305 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1307 clib_spinlock_unlock (&vcm->sessions_lockp);
1308 if (VPPCOM_DEBUG > 1)
1309 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1310 getpid (), session_index);
1311 return VPPCOM_EBADFD;
1313 clib_spinlock_unlock (&vcm->sessions_lockp);
1315 if (vcm->bind_session_index != session_index)
1316 clib_warning ("[%d] ERROR: unbinding a not bound listener %u (%u)",
1317 session_index, vcm->bind_session_index);
1318 vcm->bind_session_index = session_index;
1319 vppcom_send_unbind_sock (session_index);
1320 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1321 vcm->cfg.session_timeout);
1322 if (PREDICT_FALSE (rv))
1324 vcm->bind_session_index = ~0;
1325 if (VPPCOM_DEBUG > 0)
1326 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1327 getpid (), vppcom_retval_str (rv), rv);
1334 vppcom_session_disconnect (u32 session_index)
1339 clib_spinlock_lock (&vcm->sessions_lockp);
1340 rv = vppcom_session_at_index (session_index, &session);
1341 if (PREDICT_FALSE (rv))
1343 clib_spinlock_unlock (&vcm->sessions_lockp);
1344 if (VPPCOM_DEBUG > 1)
1345 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1346 getpid (), session_index);
1350 if (!session->is_cut_thru)
1352 if (VPPCOM_DEBUG > 1)
1353 clib_warning ("[%d] disconnecting sid (%u)", getpid (),
1355 vppcom_send_disconnect (session);
1356 clib_spinlock_unlock (&vcm->sessions_lockp);
1358 rv = vppcom_wait_for_session_state_change (session_index,
1359 STATE_DISCONNECT, 1.0);
1360 /* TBD: Force clean up on error/timeout since there is no other
1361 * way to recover from a failed disconnect.
1363 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1364 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1365 getpid (), session_index, vppcom_retval_str (rv), rv);
1369 /* TBD: Handle cut-thru disconnect */
1370 clib_spinlock_unlock (&vcm->sessions_lockp);
1376 #define foreach_sock_msg \
1377 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1378 _(BIND_SOCK_REPLY, bind_sock_reply) \
1379 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1380 _(ACCEPT_SESSION, accept_session) \
1381 _(CONNECT_SOCK, connect_sock) \
1382 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1383 _(DISCONNECT_SESSION, disconnect_session) \
1384 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1385 _(RESET_SESSION, reset_session) \
1386 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1387 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1388 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1391 vppcom_api_hookup (void)
1394 vl_msg_api_set_handlers(VL_API_##N, #n, \
1395 vl_api_##n##_t_handler, \
1397 vl_api_##n##_t_endian, \
1398 vl_api_##n##_t_print, \
1399 sizeof(vl_api_##n##_t), 1);
1405 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1409 vcl_cfg->heapsize = (256ULL << 20);
1410 vcl_cfg->segment_baseva = 0x200000000ULL;
1411 vcl_cfg->segment_size = (256 << 20);
1412 vcl_cfg->add_segment_size = (128 << 20);
1413 vcl_cfg->preallocated_fifo_pairs = 8;
1414 vcl_cfg->rx_fifo_size = (1 << 20);
1415 vcl_cfg->tx_fifo_size = (1 << 20);
1416 vcl_cfg->event_queue_size = 2048;
1417 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1418 vcl_cfg->app_timeout = 10 * 60.0;
1419 vcl_cfg->session_timeout = 10 * 60.0;
1420 vcl_cfg->accept_timeout = 60.0;
1424 vppcom_cfg_heapsize (char *conf_fname)
1426 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1439 fp = fopen (conf_fname, "r");
1442 if (VPPCOM_DEBUG > 0)
1443 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1446 argv = calloc (1, sizeof (char *));
1452 if (fgets (inbuf, 4096, fp) == 0)
1454 p = strtok (inbuf, " \t\n");
1460 char **tmp = realloc (argv, argc * sizeof (char *));
1464 arg = strndup (p, 1024);
1467 argv[argc - 1] = arg;
1468 p = strtok (NULL, " \t\n");
1475 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1482 * Look for and parse the "heapsize" config parameter.
1483 * Manual since none of the clib infra has been bootstrapped yet.
1485 * Format: heapsize <nn>[mM][gG]
1488 for (i = 1; i < (argc - 1); i++)
1490 if (!strncmp (argv[i], "heapsize", 8))
1492 sizep = (u8 *) argv[i + 1];
1494 while (*sizep >= '0' && *sizep <= '9')
1497 size += *sizep++ - '0';
1501 if (VPPCOM_DEBUG > 0)
1502 clib_warning ("[%d] parse error '%s %s', "
1503 "using default heapsize %lld (0x%llx)",
1504 getpid (), argv[i], argv[i + 1],
1505 vcl_cfg->heapsize, vcl_cfg->heapsize);
1509 if (*sizep == 'g' || *sizep == 'G')
1510 vcl_cfg->heapsize = size << 30;
1511 else if (*sizep == 'm' || *sizep == 'M')
1512 vcl_cfg->heapsize = size << 20;
1515 if (VPPCOM_DEBUG > 0)
1516 clib_warning ("[%d] parse error '%s %s', "
1517 "using default heapsize %lld (0x%llx)",
1518 getpid (), argv[i], argv[i + 1],
1519 vcl_cfg->heapsize, vcl_cfg->heapsize);
1531 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1532 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1533 if (vcl_mem == MAP_FAILED)
1535 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1536 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1538 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1541 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1544 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1547 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1550 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1554 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1557 if (VPPCOM_DEBUG > 0)
1558 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1559 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1563 vppcom_cfg_read (char *conf_fname)
1565 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1567 unformat_input_t _input, *input = &_input;
1568 unformat_input_t _line_input, *line_input = &_line_input;
1569 u8 vc_cfg_input = 0;
1574 fd = open (conf_fname, O_RDONLY);
1577 if (VPPCOM_DEBUG > 0)
1578 clib_warning ("[%d] open configuration file '%s' failed!",
1579 getpid (), conf_fname);
1583 if (fstat (fd, &s) < 0)
1585 if (VPPCOM_DEBUG > 0)
1586 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1590 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1592 if (VPPCOM_DEBUG > 0)
1593 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1597 unformat_init_clib_file (input, fd);
1599 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1601 (void) unformat_user (input, unformat_line_input, line_input);
1602 unformat_skip_white_space (line_input);
1604 if (unformat (line_input, "vcl {"))
1612 if (unformat (line_input, "heapsize %s", &chroot_path))
1614 vec_terminate_c_string (chroot_path);
1615 if (VPPCOM_DEBUG > 0)
1616 clib_warning ("[%d] configured heapsize %s, "
1617 "actual heapsize %lld (0x%llx)",
1618 getpid (), chroot_path, vcl_cfg->heapsize,
1620 vec_free (chroot_path);
1622 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1624 vec_terminate_c_string (chroot_path);
1625 vl_set_memory_root_path ((char *) chroot_path);
1626 if (VPPCOM_DEBUG > 0)
1627 clib_warning ("[%d] configured api-prefix %s",
1628 getpid (), chroot_path);
1629 chroot_path = 0; /* Don't vec_free() it! */
1631 else if (unformat (line_input, "uid %d", &uid))
1633 vl_set_memory_uid (uid);
1634 if (VPPCOM_DEBUG > 0)
1635 clib_warning ("[%d] configured uid %d", getpid (), uid);
1637 else if (unformat (line_input, "gid %d", &gid))
1639 vl_set_memory_gid (gid);
1640 if (VPPCOM_DEBUG > 0)
1641 clib_warning ("[%d] configured gid %d", getpid (), gid);
1643 else if (unformat (line_input, "segment-baseva 0x%lx",
1644 &vcl_cfg->segment_baseva))
1646 if (VPPCOM_DEBUG > 0)
1647 clib_warning ("[%d] configured segment_baseva 0x%lx",
1648 getpid (), vcl_cfg->segment_baseva);
1650 else if (unformat (line_input, "segment-size 0x%lx",
1651 &vcl_cfg->segment_size))
1653 if (VPPCOM_DEBUG > 0)
1654 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1655 getpid (), vcl_cfg->segment_size,
1656 vcl_cfg->segment_size);
1658 else if (unformat (line_input, "segment-size %ld",
1659 &vcl_cfg->segment_size))
1661 if (VPPCOM_DEBUG > 0)
1662 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1663 getpid (), vcl_cfg->segment_size,
1664 vcl_cfg->segment_size);
1666 else if (unformat (line_input, "add-segment-size 0x%lx",
1667 &vcl_cfg->add_segment_size))
1669 if (VPPCOM_DEBUG > 0)
1671 ("[%d] configured add_segment_size 0x%lx (%ld)",
1672 getpid (), vcl_cfg->add_segment_size,
1673 vcl_cfg->add_segment_size);
1675 else if (unformat (line_input, "add-segment-size %ld",
1676 &vcl_cfg->add_segment_size))
1678 if (VPPCOM_DEBUG > 0)
1680 ("[%d] configured add_segment_size %ld (0x%lx)",
1681 getpid (), vcl_cfg->add_segment_size,
1682 vcl_cfg->add_segment_size);
1684 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1685 &vcl_cfg->preallocated_fifo_pairs))
1687 if (VPPCOM_DEBUG > 0)
1688 clib_warning ("[%d] configured preallocated_fifo_pairs "
1689 "%d (0x%x)", getpid (),
1690 vcl_cfg->preallocated_fifo_pairs,
1691 vcl_cfg->preallocated_fifo_pairs);
1693 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1694 &vcl_cfg->rx_fifo_size))
1696 if (VPPCOM_DEBUG > 0)
1697 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1698 getpid (), vcl_cfg->rx_fifo_size,
1699 vcl_cfg->rx_fifo_size);
1701 else if (unformat (line_input, "rx-fifo-size %ld",
1702 &vcl_cfg->rx_fifo_size))
1704 if (VPPCOM_DEBUG > 0)
1705 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1706 getpid (), vcl_cfg->rx_fifo_size,
1707 vcl_cfg->rx_fifo_size);
1709 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1710 &vcl_cfg->tx_fifo_size))
1712 if (VPPCOM_DEBUG > 0)
1713 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1714 getpid (), vcl_cfg->tx_fifo_size,
1715 vcl_cfg->tx_fifo_size);
1717 else if (unformat (line_input, "tx-fifo-size %ld",
1718 &vcl_cfg->tx_fifo_size))
1720 if (VPPCOM_DEBUG > 0)
1721 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1722 getpid (), vcl_cfg->tx_fifo_size,
1723 vcl_cfg->tx_fifo_size);
1725 else if (unformat (line_input, "event-queue-size 0x%lx",
1726 &vcl_cfg->event_queue_size))
1728 if (VPPCOM_DEBUG > 0)
1729 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1730 getpid (), vcl_cfg->event_queue_size,
1731 vcl_cfg->event_queue_size);
1733 else if (unformat (line_input, "event-queue-size %ld",
1734 &vcl_cfg->event_queue_size))
1736 if (VPPCOM_DEBUG > 0)
1737 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1738 getpid (), vcl_cfg->event_queue_size,
1739 vcl_cfg->event_queue_size);
1741 else if (unformat (line_input, "listen-queue-size 0x%lx",
1742 &vcl_cfg->listen_queue_size))
1744 if (VPPCOM_DEBUG > 0)
1745 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1746 getpid (), vcl_cfg->listen_queue_size,
1747 vcl_cfg->listen_queue_size);
1749 else if (unformat (line_input, "listen-queue-size %ld",
1750 &vcl_cfg->listen_queue_size))
1752 if (VPPCOM_DEBUG > 0)
1753 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1754 getpid (), vcl_cfg->listen_queue_size,
1755 vcl_cfg->listen_queue_size);
1757 else if (unformat (line_input, "app-timeout %f",
1758 &vcl_cfg->app_timeout))
1760 if (VPPCOM_DEBUG > 0)
1761 clib_warning ("[%d] configured app_timeout %f",
1762 getpid (), vcl_cfg->app_timeout);
1764 else if (unformat (line_input, "session-timeout %f",
1765 &vcl_cfg->session_timeout))
1767 if (VPPCOM_DEBUG > 0)
1768 clib_warning ("[%d] configured session_timeout %f",
1769 getpid (), vcl_cfg->session_timeout);
1771 else if (unformat (line_input, "accept-timeout %f",
1772 &vcl_cfg->accept_timeout))
1774 if (VPPCOM_DEBUG > 0)
1775 clib_warning ("[%d] configured accept_timeout %f",
1776 getpid (), vcl_cfg->accept_timeout);
1778 else if (unformat (line_input, "app-proxy-transport-tcp"))
1780 vcl_cfg->app_proxy_transport_tcp = 1;
1781 if (VPPCOM_DEBUG > 0)
1782 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1783 getpid (), vcl_cfg->app_proxy_transport_tcp);
1785 else if (unformat (line_input, "app-proxy-transport-udp"))
1787 vcl_cfg->app_proxy_transport_udp = 1;
1788 if (VPPCOM_DEBUG > 0)
1789 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1790 getpid (), vcl_cfg->app_proxy_transport_udp);
1792 else if (unformat (line_input, "app-scope-local"))
1794 vcl_cfg->app_scope_local = 1;
1795 if (VPPCOM_DEBUG > 0)
1796 clib_warning ("[%d] configured app_scope_local (%d)",
1797 getpid (), vcl_cfg->app_scope_local);
1799 else if (unformat (line_input, "app-scope-global"))
1801 vcl_cfg->app_scope_global = 1;
1802 if (VPPCOM_DEBUG > 0)
1803 clib_warning ("[%d] configured app_scope_global (%d)",
1804 getpid (), vcl_cfg->app_scope_global);
1806 else if (unformat (line_input, "namespace-secret %lu",
1807 &vcl_cfg->namespace_secret))
1809 if (VPPCOM_DEBUG > 0)
1811 ("[%d] configured namespace_secret %lu (0x%lx)",
1812 getpid (), vcl_cfg->namespace_secret,
1813 vcl_cfg->namespace_secret);
1815 else if (unformat (line_input, "namespace-id %v",
1816 &vcl_cfg->namespace_id))
1818 vl_api_application_attach_t *mp;
1819 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1820 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1821 if (nsid_vec_len > max_nsid_vec_len)
1823 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1824 if (VPPCOM_DEBUG > 0)
1825 clib_warning ("[%d] configured namespace_id is too long,"
1826 " truncated to %d characters!", getpid (),
1830 if (VPPCOM_DEBUG > 0)
1831 clib_warning ("[%d] configured namespace_id %v",
1832 getpid (), vcl_cfg->namespace_id);
1834 else if (unformat (line_input, "}"))
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("[%d] completed parsing vppcom config!",
1844 if (line_input->buffer[line_input->index] != '#')
1846 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1848 &line_input->buffer[line_input->index]);
1855 unformat_free (input);
1863 * VPPCOM Public API functions
1866 vppcom_app_create (char *app_name)
1868 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1879 vppcom_cfg_init (vcl_cfg);
1880 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1884 if (sscanf (env_var_str, "%u", &tmp) != 1)
1885 clib_warning ("[%d] Invalid debug level specified in "
1886 "the environment variable "
1888 " (%s)!\n", getpid (), env_var_str);
1892 clib_warning ("[%d] configured debug level (%u) from "
1893 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1896 conf_fname = getenv (VPPCOM_ENV_CONF);
1899 conf_fname = VPPCOM_CONF_DEFAULT;
1900 if (VPPCOM_DEBUG > 0)
1901 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1904 vppcom_cfg_heapsize (conf_fname);
1905 clib_fifo_validate (vcm->client_session_index_fifo,
1906 vcm->cfg.listen_queue_size);
1907 vppcom_cfg_read (conf_fname);
1908 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1911 u32 ns_id_vec_len = strlen (env_var_str);
1913 vec_reset_length (vcm->cfg.namespace_id);
1914 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1915 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1917 if (VPPCOM_DEBUG > 0)
1918 clib_warning ("[%d] configured namespace_id (%v) from "
1919 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1920 vcm->cfg.namespace_id);
1922 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1926 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1927 clib_warning ("[%d] Invalid namespace secret specified in "
1928 "the environment variable "
1929 VPPCOM_ENV_APP_NAMESPACE_SECRET
1930 " (%s)!\n", getpid (), env_var_str);
1933 vcm->cfg.namespace_secret = tmp;
1934 if (VPPCOM_DEBUG > 0)
1935 clib_warning ("[%d] configured namespace secret (%lu) from "
1936 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1937 vcm->cfg.namespace_secret);
1940 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1942 vcm->cfg.app_proxy_transport_tcp = 1;
1943 if (VPPCOM_DEBUG > 0)
1944 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1945 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1946 vcm->cfg.app_proxy_transport_tcp);
1948 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1950 vcm->cfg.app_proxy_transport_udp = 1;
1951 if (VPPCOM_DEBUG > 0)
1952 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1953 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1954 vcm->cfg.app_proxy_transport_udp);
1956 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1958 vcm->cfg.app_scope_local = 1;
1959 if (VPPCOM_DEBUG > 0)
1960 clib_warning ("[%d] configured app_scope_local (%u) from "
1961 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1962 vcm->cfg.app_scope_local);
1964 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1966 vcm->cfg.app_scope_global = 1;
1967 if (VPPCOM_DEBUG > 0)
1968 clib_warning ("[%d] configured app_scope_global (%u) from "
1969 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1970 vcm->cfg.app_scope_global);
1973 vcm->bind_session_index = ~0;
1974 vcm->main_cpu = os_get_thread_index ();
1975 heap = clib_mem_get_per_cpu_heap ();
1976 h = mheap_header (heap);
1978 /* make the main heap thread-safe */
1979 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1981 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1983 clib_time_init (&vcm->clib_time);
1984 vppcom_init_error_string_table ();
1985 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1986 20 /* timeout in secs */ );
1987 clib_spinlock_init (&vcm->sessions_lockp);
1988 vppcom_api_hookup ();
1991 if (vcm->my_client_index == ~0)
1993 vcm->app_state = STATE_APP_START;
1994 rv = vppcom_connect_to_vpp (app_name);
1997 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
2001 if (VPPCOM_DEBUG > 0)
2002 clib_warning ("[%d] sending session enable", getpid ());
2004 rv = vppcom_app_session_enable ();
2007 clib_warning ("[%d] vppcom_app_session_enable() failed!",
2012 if (VPPCOM_DEBUG > 0)
2013 clib_warning ("[%d] sending app attach", getpid ());
2015 rv = vppcom_app_attach ();
2018 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
2022 if (VPPCOM_DEBUG > 0)
2023 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
2024 getpid (), app_name, vcm->my_client_index,
2025 vcm->my_client_index);
2032 vppcom_app_destroy (void)
2036 if (vcm->my_client_index == ~0)
2039 if (VPPCOM_DEBUG > 0)
2040 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
2041 getpid (), vcm->my_client_index, vcm->my_client_index);
2043 vppcom_app_detach ();
2044 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2045 if (PREDICT_FALSE (rv))
2047 if (VPPCOM_DEBUG > 0)
2048 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
2049 getpid (), vppcom_retval_str (rv), rv);
2051 vl_client_disconnect_from_vlib ();
2052 vcm->my_client_index = ~0;
2053 vcm->app_state = STATE_APP_START;
2057 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2062 clib_spinlock_lock (&vcm->sessions_lockp);
2063 pool_get (vcm->sessions, session);
2064 memset (session, 0, sizeof (*session));
2065 session_index = session - vcm->sessions;
2068 session->proto = proto;
2069 session->state = STATE_START;
2070 session->is_nonblocking = is_nonblocking ? 1 : 0;
2071 clib_spinlock_unlock (&vcm->sessions_lockp);
2073 if (VPPCOM_DEBUG > 0)
2074 clib_warning ("[%d] sid %d", getpid (), session_index);
2076 return (int) session_index;
2080 vppcom_session_close (uint32_t session_index)
2082 session_t *session = 0;
2091 session_state_t state;
2093 clib_spinlock_lock (&vcm->sessions_lockp);
2094 rv = vppcom_session_at_index (session_index, &session);
2095 if (PREDICT_FALSE (rv))
2097 if (VPPCOM_DEBUG > 0)
2098 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2099 getpid (), session_index);
2100 clib_spinlock_unlock (&vcm->sessions_lockp);
2103 is_server = session->is_server;
2104 is_listen = session->is_listen;
2105 is_cut_thru = session->is_cut_thru;
2106 is_vep = session->is_vep;
2107 is_vep_session = session->is_vep_session;
2108 next_sid = session->vep.next_sid;
2109 vep_idx = session->vep.vep_idx;
2110 state = session->state;
2111 clib_spinlock_unlock (&vcm->sessions_lockp);
2113 if (VPPCOM_DEBUG > 0)
2114 clib_warning ("[%d] sid %d", getpid (), session_index);
2118 while (next_sid != ~0)
2120 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2121 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2122 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2123 "rv = %s (%d)", getpid (), vep_idx, next_sid,
2124 vppcom_retval_str (rv), rv);
2126 clib_spinlock_lock (&vcm->sessions_lockp);
2127 rv = vppcom_session_at_index (session_index, &session);
2128 if (PREDICT_FALSE (rv))
2130 if (VPPCOM_DEBUG > 0)
2132 ("[%d] invalid session, sid (%u) has been closed!",
2133 getpid (), session_index);
2134 clib_spinlock_unlock (&vcm->sessions_lockp);
2137 next_sid = session->vep.next_sid;
2138 clib_spinlock_unlock (&vcm->sessions_lockp);
2145 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2146 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2147 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2148 "rv = %s (%d)", getpid (), vep_idx, session_index,
2149 vppcom_retval_str (rv), rv);
2152 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
2154 rv = vppcom_session_unbind_cut_thru (session, session_index);
2155 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2156 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2158 getpid (), session_index,
2159 vppcom_retval_str (rv), rv);
2161 else if (is_server && is_listen)
2163 rv = vppcom_session_unbind (session_index);
2164 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2165 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
2166 getpid (), session_index,
2167 vppcom_retval_str (rv), rv);
2169 else if (state == STATE_CONNECT)
2170 if (vppcom_session_disconnect (session_index))
2173 clib_spinlock_lock (&vcm->sessions_lockp);
2174 pool_put_index (vcm->sessions, session_index);
2175 clib_spinlock_unlock (&vcm->sessions_lockp);
2181 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2183 session_t *session = 0;
2187 return VPPCOM_EINVAL;
2189 clib_spinlock_lock (&vcm->sessions_lockp);
2190 rv = vppcom_session_at_index (session_index, &session);
2191 if (PREDICT_FALSE (rv))
2193 clib_spinlock_unlock (&vcm->sessions_lockp);
2194 if (VPPCOM_DEBUG > 0)
2195 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2196 getpid (), session_index);
2200 if (session->is_vep)
2202 clib_spinlock_unlock (&vcm->sessions_lockp);
2203 if (VPPCOM_DEBUG > 0)
2204 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2205 getpid (), session_index);
2206 return VPPCOM_EBADFD;
2209 session->vrf = ep->vrf;
2210 session->lcl_addr.is_ip4 = ep->is_ip4;
2211 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2212 session->lcl_port = ep->port;
2214 if (VPPCOM_DEBUG > 0)
2215 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
2216 getpid (), session_index, format_ip46_address,
2217 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2218 clib_net_to_host_u16 (session->lcl_port));
2220 clib_spinlock_unlock (&vcm->sessions_lockp);
2225 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2227 session_t *listen_session = 0;
2230 clib_spinlock_lock (&vcm->sessions_lockp);
2231 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2232 if (PREDICT_FALSE (rv))
2234 clib_spinlock_unlock (&vcm->sessions_lockp);
2235 if (VPPCOM_DEBUG > 0)
2236 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2237 getpid (), listen_session_index);
2241 if (listen_session->is_vep)
2243 clib_spinlock_unlock (&vcm->sessions_lockp);
2244 if (VPPCOM_DEBUG > 0)
2245 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2246 getpid (), listen_session_index);
2247 return VPPCOM_EBADFD;
2250 if (listen_session->is_listen)
2252 clib_spinlock_unlock (&vcm->sessions_lockp);
2253 if (VPPCOM_DEBUG > 0)
2254 clib_warning ("[%d] sid (%u) is already in listen state!",
2255 getpid (), listen_session_index);
2259 if (VPPCOM_DEBUG > 0)
2260 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
2262 ASSERT (vcm->bind_session_index == ~0);
2263 vcm->bind_session_index = listen_session_index;
2264 vppcom_send_bind_sock (listen_session);
2265 clib_spinlock_unlock (&vcm->sessions_lockp);
2267 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2268 vcm->cfg.session_timeout);
2269 if (PREDICT_FALSE (rv))
2271 vcm->bind_session_index = ~0;
2272 if (VPPCOM_DEBUG > 0)
2273 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2274 getpid (), vppcom_retval_str (rv), rv);
2278 clib_spinlock_lock (&vcm->sessions_lockp);
2279 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2280 if (PREDICT_FALSE (rv))
2282 clib_spinlock_unlock (&vcm->sessions_lockp);
2283 if (VPPCOM_DEBUG > 0)
2284 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2285 getpid (), listen_session_index);
2288 listen_session->is_listen = 1;
2289 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2290 clib_spinlock_unlock (&vcm->sessions_lockp);
2296 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2297 uint32_t flags, double wait_for_time)
2299 session_t *listen_session = 0;
2300 session_t *client_session = 0;
2301 u32 client_session_index = ~0;
2306 clib_spinlock_lock (&vcm->sessions_lockp);
2307 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2308 if (PREDICT_FALSE (rv))
2310 clib_spinlock_unlock (&vcm->sessions_lockp);
2311 if (VPPCOM_DEBUG > 0)
2312 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2313 getpid (), listen_session_index);
2317 if (listen_session->is_vep)
2319 clib_spinlock_unlock (&vcm->sessions_lockp);
2320 if (VPPCOM_DEBUG > 0)
2321 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2322 getpid (), listen_session_index);
2323 return VPPCOM_EBADFD;
2326 if (listen_session->state != STATE_LISTEN)
2328 clib_spinlock_unlock (&vcm->sessions_lockp);
2329 if (VPPCOM_DEBUG > 0)
2330 clib_warning ("[%d] session not in listen state, state = %s",
2332 vppcom_session_state_str (listen_session->state));
2333 return VPPCOM_EBADFD;
2335 wait_for = listen_session->is_nonblocking ? 0 :
2336 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2338 if (VPPCOM_DEBUG > 0)
2339 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
2340 listen_session_index,
2341 vppcom_session_state_str (listen_session->state),
2342 listen_session->state);
2343 clib_spinlock_unlock (&vcm->sessions_lockp);
2347 rv = vppcom_wait_for_client_session_index (wait_for);
2350 if ((VPPCOM_DEBUG > 0))
2351 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2352 getpid (), listen_session_index,
2353 vppcom_retval_str (rv), rv);
2354 if ((wait_for == 0) || (wait_for_time > 0))
2361 clib_spinlock_lock (&vcm->sessions_lockp);
2362 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2363 rv = vppcom_session_at_index (client_session_index, &client_session);
2364 ASSERT (rv == VPPCOM_OK);
2365 ASSERT (client_session->peer_addr.is_ip4 ==
2366 listen_session->lcl_addr.is_ip4);
2368 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2369 if (VPPCOM_DEBUG > 0)
2370 clib_warning ("[%d] Got a request: client sid %d, flags %d, "
2371 " is_nonblocking %u", getpid (), client_session_index,
2372 flags, client_session->is_nonblocking);
2374 ep->vrf = client_session->vrf;
2375 ep->is_cut_thru = client_session->is_cut_thru;
2376 ep->is_ip4 = client_session->peer_addr.is_ip4;
2377 ep->port = client_session->peer_port;
2378 if (client_session->peer_addr.is_ip4)
2379 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2380 sizeof (ip4_address_t));
2382 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2383 sizeof (ip6_address_t));
2385 if (client_session->is_server && client_session->is_cut_thru)
2387 static svm_fifo_segment_create_args_t _a;
2388 svm_fifo_segment_create_args_t *a = &_a;
2389 svm_fifo_segment_private_t *seg;
2391 cut_thru_str = "cut-thru ";
2393 /* Create the segment */
2394 memset (a, 0, sizeof (*a));
2395 a->segment_name = (char *)
2396 format ((u8 *) a->segment_name, "%d:segment%d%c",
2397 getpid (), vcm->unique_segment_index++, 0);
2398 a->segment_size = vcm->cfg.segment_size;
2399 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2400 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2401 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2403 rv = svm_fifo_segment_create (a);
2404 if (PREDICT_FALSE (rv))
2406 if (VPPCOM_DEBUG > 1)
2407 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2408 getpid (), a->segment_name);
2409 vec_reset_length (a->new_segment_indices);
2410 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2411 vppcom_send_connect_session_reply (client_session, rv);
2412 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 return VPPCOM_ENOMEM;
2416 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2417 client_session->sm_seg_index = a->new_segment_indices[0];
2418 vec_free (a->new_segment_indices);
2420 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2421 client_session->server_rx_fifo =
2422 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2423 FIFO_SEGMENT_RX_FREELIST);
2424 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2426 svm_fifo_segment_delete (seg);
2427 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2428 getpid (), vcm->cfg.rx_fifo_size,
2429 vcm->cfg.rx_fifo_size);
2430 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2431 vppcom_send_connect_session_reply (client_session, rv);
2432 clib_spinlock_unlock (&vcm->sessions_lockp);
2433 return VPPCOM_ENOMEM;
2435 client_session->server_rx_fifo->master_session_index =
2436 client_session_index;
2438 client_session->server_tx_fifo =
2439 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2440 FIFO_SEGMENT_TX_FREELIST);
2441 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2443 svm_fifo_segment_delete (seg);
2444 if (VPPCOM_DEBUG > 1)
2445 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2446 getpid (), vcm->cfg.tx_fifo_size,
2447 vcm->cfg.tx_fifo_size);
2448 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2449 vppcom_send_connect_session_reply (client_session, rv);
2450 clib_spinlock_unlock (&vcm->sessions_lockp);
2451 return VPPCOM_ENOMEM;
2453 client_session->server_tx_fifo->master_session_index =
2454 client_session_index;
2456 if (VPPCOM_DEBUG > 1)
2457 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2458 getpid (), client_session->segment_name,
2459 client_session->server_rx_fifo,
2460 client_session->server_tx_fifo);
2462 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2465 ssvm_shared_header_t *sh = seg->ssvm.sh;
2467 ssvm_lock_non_recursive (sh, 1);
2468 oldheap = ssvm_push_heap (sh);
2469 event_q = client_session->vpp_event_queue =
2470 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2471 sizeof (session_fifo_event_t),
2472 getpid (), 0 /* signal not sent */ );
2473 ssvm_pop_heap (oldheap);
2474 ssvm_unlock_non_recursive (sh);
2477 vppcom_send_connect_session_reply (client_session, 0);
2482 vppcom_send_accept_session_reply (client_session->vpp_handle, 0);
2485 if (VPPCOM_DEBUG > 0)
2486 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2488 getpid (), client_session_index, cut_thru_str,
2489 format_ip46_address, &client_session->peer_addr.ip46,
2490 client_session->peer_addr.is_ip4,
2491 clib_net_to_host_u16 (client_session->peer_port));
2493 clib_spinlock_unlock (&vcm->sessions_lockp);
2494 return (int) client_session_index;
2498 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2500 session_t *session = 0;
2503 clib_spinlock_lock (&vcm->sessions_lockp);
2504 rv = vppcom_session_at_index (session_index, &session);
2505 if (PREDICT_FALSE (rv))
2507 clib_spinlock_unlock (&vcm->sessions_lockp);
2508 if (VPPCOM_DEBUG > 0)
2509 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2510 getpid (), session_index);
2514 if (session->is_vep)
2516 clib_spinlock_unlock (&vcm->sessions_lockp);
2517 if (VPPCOM_DEBUG > 0)
2518 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2519 getpid (), session_index);
2520 return VPPCOM_EBADFD;
2523 if (session->state == STATE_CONNECT)
2525 clib_spinlock_unlock (&vcm->sessions_lockp);
2526 if (VPPCOM_DEBUG > 0)
2527 clib_warning ("[%d] session, sid (%u) already connected!",
2528 getpid (), session_index);
2532 session->vrf = server_ep->vrf;
2533 session->peer_addr.is_ip4 = server_ep->is_ip4;
2534 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2535 session->peer_port = server_ep->port;
2537 if (VPPCOM_DEBUG > 0)
2539 u8 *ip_str = format (0, "%U", format_ip46_address,
2540 &session->peer_addr.ip46,
2541 session->peer_addr.is_ip4);
2542 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2543 getpid (), session_index, ip_str,
2544 clib_net_to_host_u16 (session->peer_port),
2545 session->proto ? "UDP" : "TCP");
2549 vppcom_send_connect_sock (session, session_index);
2550 clib_spinlock_unlock (&vcm->sessions_lockp);
2551 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2552 vcm->cfg.session_timeout);
2553 if (PREDICT_FALSE (rv))
2555 if (VPPCOM_DEBUG > 0)
2556 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2557 getpid (), vppcom_retval_str (rv), rv);
2560 if (VPPCOM_DEBUG > 0)
2561 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
2567 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2570 session_t *session = 0;
2571 svm_fifo_t *rx_fifo;
2579 clib_spinlock_lock (&vcm->sessions_lockp);
2580 rv = vppcom_session_at_index (session_index, &session);
2581 if (PREDICT_FALSE (rv))
2583 clib_spinlock_unlock (&vcm->sessions_lockp);
2584 if (VPPCOM_DEBUG > 0)
2585 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2586 getpid (), session_index);
2590 if (session->is_vep)
2592 clib_spinlock_unlock (&vcm->sessions_lockp);
2593 if (VPPCOM_DEBUG > 0)
2594 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2595 getpid (), session_index);
2596 return VPPCOM_EBADFD;
2599 if (session->state == STATE_DISCONNECT)
2601 clib_spinlock_unlock (&vcm->sessions_lockp);
2602 if (VPPCOM_DEBUG > 0)
2603 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2604 getpid (), session_index);
2605 return VPPCOM_ECONNRESET;
2608 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2609 session->server_rx_fifo : session->server_tx_fifo);
2610 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2611 "server_rx_fifo" : "server_tx_fifo");
2613 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2614 clib_spinlock_unlock (&vcm->sessions_lockp);
2619 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2621 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2623 while (!session->is_nonblocking && (n_read <= 0));
2625 if (poll_et && (n_read <= 0))
2627 clib_spinlock_lock (&vcm->sessions_lockp);
2628 session->vep.et_mask |= EPOLLIN;
2629 clib_spinlock_unlock (&vcm->sessions_lockp);
2632 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2633 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
2634 session_index, n_read, fifo_str, rx_fifo);
2636 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2640 vppcom_session_read (uint32_t session_index, void *buf, int n)
2642 return (vppcom_session_read_internal (session_index, buf, n, 0));
2646 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2648 return (vppcom_session_read_internal (session_index, buf, n, 1));
2652 vppcom_session_read_ready (session_t * session, u32 session_index)
2654 svm_fifo_t *rx_fifo = 0;
2658 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2659 if (session->is_vep)
2661 clib_spinlock_unlock (&vcm->sessions_lockp);
2662 if (VPPCOM_DEBUG > 0)
2663 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2664 getpid (), session_index);
2665 return VPPCOM_EBADFD;
2668 if (session->state == STATE_DISCONNECT)
2670 if (VPPCOM_DEBUG > 0)
2671 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2672 getpid (), session_index);
2673 return VPPCOM_ECONNRESET;
2676 if (session->is_listen)
2677 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2680 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2681 session->server_rx_fifo : session->server_tx_fifo);
2683 ready = svm_fifo_max_dequeue (rx_fifo);
2687 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2688 if (poll_et && (ready == 0))
2690 if (VPPCOM_DEBUG > 11)
2691 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2692 session_index, session->vep.et_mask);
2693 session->vep.et_mask |= EPOLLIN;
2694 if (VPPCOM_DEBUG > 11)
2695 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2696 session_index, session->vep.et_mask);
2699 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2700 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2702 getpid (), session_index, session->is_listen,
2703 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2704 rx_fifo, ready, session->vep.et_mask);
2709 vppcom_session_write (uint32_t session_index, void *buf, int n)
2711 session_t *session = 0;
2712 svm_fifo_t *tx_fifo;
2713 unix_shared_memory_queue_t *q;
2714 session_fifo_event_t evt;
2721 clib_spinlock_lock (&vcm->sessions_lockp);
2722 rv = vppcom_session_at_index (session_index, &session);
2723 if (PREDICT_FALSE (rv))
2725 clib_spinlock_unlock (&vcm->sessions_lockp);
2726 if (VPPCOM_DEBUG > 0)
2727 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2728 getpid (), session_index);
2732 if (session->is_vep)
2734 clib_spinlock_unlock (&vcm->sessions_lockp);
2735 if (VPPCOM_DEBUG > 0)
2736 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2737 getpid (), session_index);
2738 return VPPCOM_EBADFD;
2741 if (session->state == STATE_DISCONNECT)
2743 clib_spinlock_unlock (&vcm->sessions_lockp);
2744 if (VPPCOM_DEBUG > 0)
2745 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2746 getpid (), session_index);
2747 return VPPCOM_ECONNRESET;
2750 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2751 session->server_tx_fifo : session->server_rx_fifo);
2752 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2753 "server_tx_fifo" : "server_rx_fifo");
2754 q = session->vpp_event_queue;
2755 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2756 (EPOLLET | EPOLLOUT));
2757 clib_spinlock_unlock (&vcm->sessions_lockp);
2761 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2763 while (!session->is_nonblocking && (n_write <= 0));
2765 /* If event wasn't set, add one */
2766 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2770 /* Fabricate TX event, send to vpp */
2772 evt.event_type = FIFO_EVENT_APP_TX;
2774 rval = vppcom_session_at_index (session_index, &session);
2775 if (PREDICT_FALSE (rval))
2777 if (VPPCOM_DEBUG > 1)
2778 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2779 getpid (), session_index);
2783 unix_shared_memory_queue_add (q, (u8 *) & evt,
2784 0 /* do wait for mutex */ );
2787 if (poll_et && (n_write <= 0))
2789 clib_spinlock_lock (&vcm->sessions_lockp);
2790 session->vep.et_mask |= EPOLLOUT;
2791 clib_spinlock_unlock (&vcm->sessions_lockp);
2794 if (VPPCOM_DEBUG > 2)
2797 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
2798 session_index, fifo_str, tx_fifo);
2800 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
2801 session_index, n_write, fifo_str, tx_fifo);
2803 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2807 vppcom_session_write_ready (session_t * session, u32 session_index)
2809 svm_fifo_t *tx_fifo;
2814 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2815 if (session->is_vep)
2817 clib_spinlock_unlock (&vcm->sessions_lockp);
2818 if (VPPCOM_DEBUG > 0)
2819 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2820 getpid (), session_index);
2821 return VPPCOM_EBADFD;
2824 if (session->state == STATE_DISCONNECT)
2826 if (VPPCOM_DEBUG > 0)
2827 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2828 getpid (), session_index);
2829 return VPPCOM_ECONNRESET;
2832 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2833 session->server_tx_fifo : session->server_rx_fifo);
2834 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2835 "server_tx_fifo" : "server_rx_fifo");
2837 ready = svm_fifo_max_enqueue (tx_fifo);
2839 if (VPPCOM_DEBUG > 3)
2840 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
2841 session_index, fifo_str, tx_fifo, ready);
2842 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2843 (EPOLLET | EPOLLOUT));
2844 if (poll_et && (ready == 0))
2845 session->vep.et_mask |= EPOLLOUT;
2851 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2852 unsigned long *write_map, unsigned long *except_map,
2853 double time_to_wait)
2856 session_t *session = 0;
2857 int rv, bits_set = 0;
2858 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2859 u32 minbits = clib_max (n_bits, BITS (uword));
2861 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2863 if (n_bits && read_map)
2865 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2866 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2867 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2869 if (n_bits && write_map)
2871 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2872 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2873 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2875 if (n_bits && except_map)
2877 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2878 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2879 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2889 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2891 clib_spinlock_lock (&vcm->sessions_lockp);
2892 rv = vppcom_session_at_index (session_index, &session);
2895 clib_spinlock_unlock (&vcm->sessions_lockp);
2896 if (VPPCOM_DEBUG > 1)
2897 clib_warning ("[%d] session %d specified in "
2898 "read_map is closed.", getpid (),
2900 bits_set = VPPCOM_EBADFD;
2904 rv = vppcom_session_read_ready (session, session_index);
2905 clib_spinlock_unlock (&vcm->sessions_lockp);
2906 if (except_map && vcm->ex_bitmap &&
2907 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2910 // TBD: clib_warning
2911 clib_bitmap_set_no_check (except_map, session_index, 1);
2916 // TBD: clib_warning
2917 clib_bitmap_set_no_check (read_map, session_index, 1);
2925 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2927 clib_spinlock_lock (&vcm->sessions_lockp);
2928 rv = vppcom_session_at_index (session_index, &session);
2931 clib_spinlock_unlock (&vcm->sessions_lockp);
2932 if (VPPCOM_DEBUG > 0)
2933 clib_warning ("[%d] session %d specified in "
2934 "write_map is closed.", getpid (),
2936 bits_set = VPPCOM_EBADFD;
2940 rv = vppcom_session_write_ready (session, session_index);
2941 clib_spinlock_unlock (&vcm->sessions_lockp);
2942 if (write_map && (rv > 0))
2944 // TBD: clib_warning
2945 clib_bitmap_set_no_check (write_map, session_index, 1);
2953 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2955 clib_spinlock_lock (&vcm->sessions_lockp);
2956 rv = vppcom_session_at_index (session_index, &session);
2959 clib_spinlock_unlock (&vcm->sessions_lockp);
2960 if (VPPCOM_DEBUG > 1)
2961 clib_warning ("[%d] session %d specified in "
2962 "except_map is closed.", getpid (),
2964 bits_set = VPPCOM_EBADFD;
2968 rv = vppcom_session_read_ready (session, session_index);
2969 clib_spinlock_unlock (&vcm->sessions_lockp);
2972 // TBD: clib_warning
2973 clib_bitmap_set_no_check (except_map, session_index, 1);
2981 while (clib_time_now (&vcm->clib_time) < timeout);
2988 vep_verify_epoll_chain (u32 vep_idx)
2991 vppcom_epoll_t *vep;
2995 if (VPPCOM_DEBUG <= 1)
2998 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2999 rv = vppcom_session_at_index (vep_idx, &session);
3000 if (PREDICT_FALSE (rv))
3002 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
3005 if (PREDICT_FALSE (!session->is_vep))
3007 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
3011 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3014 " is_vep_session = %u\n"
3015 " wait_cont_idx = 0x%x (%u)\n"
3017 vep_idx, session->is_vep, session->is_vep_session,
3018 session->wait_cont_idx, session->wait_cont_idx);
3021 vep = &session->vep;
3022 sid = vep->next_sid;
3025 rv = vppcom_session_at_index (sid, &session);
3026 if (PREDICT_FALSE (rv))
3028 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
3031 if (PREDICT_FALSE (session->is_vep))
3032 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3033 getpid (), vep_idx);
3034 else if (PREDICT_FALSE (!session->is_vep_session))
3036 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
3040 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3041 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
3042 "vep_idx (%u)!", getpid (),
3043 sid, session->vep.vep_idx, vep_idx);
3044 if (session->is_vep_session)
3046 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3048 " next_sid = 0x%x (%u)\n"
3049 " prev_sid = 0x%x (%u)\n"
3050 " vep_idx = 0x%x (%u)\n"
3051 " ev.events = 0x%x\n"
3052 " ev.data.u64 = 0x%llx\n"
3056 vep->next_sid, vep->next_sid,
3057 vep->prev_sid, vep->prev_sid,
3058 vep->vep_idx, vep->vep_idx,
3059 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3066 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
3070 vppcom_epoll_create (void)
3072 session_t *vep_session;
3075 clib_spinlock_lock (&vcm->sessions_lockp);
3076 pool_get (vcm->sessions, vep_session);
3077 memset (vep_session, 0, sizeof (*vep_session));
3078 vep_idx = vep_session - vcm->sessions;
3080 vep_session->is_vep = 1;
3081 vep_session->vep.vep_idx = ~0;
3082 vep_session->vep.next_sid = ~0;
3083 vep_session->vep.prev_sid = ~0;
3084 vep_session->wait_cont_idx = ~0;
3085 clib_spinlock_unlock (&vcm->sessions_lockp);
3087 if (VPPCOM_DEBUG > 0)
3088 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
3094 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3095 struct epoll_event *event)
3097 session_t *vep_session;
3101 if (vep_idx == session_index)
3103 if (VPPCOM_DEBUG > 0)
3104 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3105 getpid (), vep_idx);
3106 return VPPCOM_EINVAL;
3109 clib_spinlock_lock (&vcm->sessions_lockp);
3110 rv = vppcom_session_at_index (vep_idx, &vep_session);
3111 if (PREDICT_FALSE (rv))
3113 if (VPPCOM_DEBUG > 0)
3114 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3117 if (PREDICT_FALSE (!vep_session->is_vep))
3119 if (VPPCOM_DEBUG > 0)
3120 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3121 getpid (), vep_idx);
3126 ASSERT (vep_session->vep.vep_idx == ~0);
3127 ASSERT (vep_session->vep.prev_sid == ~0);
3129 rv = vppcom_session_at_index (session_index, &session);
3130 if (PREDICT_FALSE (rv))
3132 if (VPPCOM_DEBUG > 0)
3133 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3134 getpid (), session_index);
3137 if (PREDICT_FALSE (session->is_vep))
3139 if (VPPCOM_DEBUG > 0)
3140 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3148 if (PREDICT_FALSE (!event))
3150 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3151 "epoll_event structure!", getpid ());
3155 if (vep_session->vep.next_sid != ~0)
3157 session_t *next_session;
3158 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3160 if (PREDICT_FALSE (rv))
3162 if (VPPCOM_DEBUG > 0)
3163 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3164 "vep.next_sid (%u) on vep_idx (%u)!",
3165 getpid (), vep_session->vep.next_sid, vep_idx);
3168 ASSERT (next_session->vep.prev_sid == vep_idx);
3169 next_session->vep.prev_sid = session_index;
3171 session->vep.next_sid = vep_session->vep.next_sid;
3172 session->vep.prev_sid = vep_idx;
3173 session->vep.vep_idx = vep_idx;
3174 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3175 session->vep.ev = *event;
3176 session->is_vep_session = 1;
3177 vep_session->vep.next_sid = session_index;
3178 if (VPPCOM_DEBUG > 1)
3179 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3180 " data 0x%llx!", getpid (), vep_idx, session_index,
3181 event->events, event->data.u64);
3185 if (PREDICT_FALSE (!event))
3187 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3188 "epoll_event structure!", getpid ());
3192 if (PREDICT_FALSE (!session->is_vep_session &&
3193 (session->vep.vep_idx != vep_idx)))
3195 if (VPPCOM_DEBUG > 0)
3197 if (!session->is_vep_session)
3198 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3199 "is not a vep session!",
3200 getpid (), session_index);
3202 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3203 "vep_idx (%u) != vep_idx (%u)!",
3204 getpid (), session_index,
3205 session->vep.vep_idx, vep_idx);
3210 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3211 session->vep.ev = *event;
3212 if (VPPCOM_DEBUG > 1)
3213 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3214 " data 0x%llx!", getpid (), vep_idx, session_index,
3215 event->events, event->data.u64);
3219 if (PREDICT_FALSE (!session->is_vep_session &&
3220 (session->vep.vep_idx != vep_idx)))
3222 if (VPPCOM_DEBUG > 0)
3224 if (!session->is_vep_session)
3225 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3226 "is not a vep session!",
3227 getpid (), session_index);
3229 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3230 "vep_idx (%u) != vep_idx (%u)!",
3231 getpid (), session_index,
3232 session->vep.vep_idx, vep_idx);
3238 vep_session->wait_cont_idx =
3239 (vep_session->wait_cont_idx == session_index) ?
3240 session->vep.next_sid : vep_session->wait_cont_idx;
3242 if (session->vep.prev_sid == vep_idx)
3243 vep_session->vep.next_sid = session->vep.next_sid;
3246 session_t *prev_session;
3247 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3248 if (PREDICT_FALSE (rv))
3250 if (VPPCOM_DEBUG > 0)
3251 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3252 "vep.prev_sid (%u) on sid (%u)!",
3253 getpid (), session->vep.prev_sid,
3257 ASSERT (prev_session->vep.next_sid == session_index);
3258 prev_session->vep.next_sid = session->vep.next_sid;
3260 if (session->vep.next_sid != ~0)
3262 session_t *next_session;
3263 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3264 if (PREDICT_FALSE (rv))
3266 if (VPPCOM_DEBUG > 0)
3267 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3268 "vep.next_sid (%u) on sid (%u)!",
3269 getpid (), session->vep.next_sid,
3273 ASSERT (next_session->vep.prev_sid == session_index);
3274 next_session->vep.prev_sid = session->vep.prev_sid;
3277 memset (&session->vep, 0, sizeof (session->vep));
3278 session->vep.next_sid = ~0;
3279 session->vep.prev_sid = ~0;
3280 session->vep.vep_idx = ~0;
3281 session->is_vep_session = 0;
3282 if (VPPCOM_DEBUG > 1)
3283 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3284 getpid (), vep_idx, session_index);
3288 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3292 vep_verify_epoll_chain (vep_idx);
3295 clib_spinlock_unlock (&vcm->sessions_lockp);
3300 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3301 int maxevents, double wait_for_time)
3303 session_t *vep_session;
3305 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3306 u32 keep_trying = 1;
3308 u32 vep_next_sid, wait_cont_idx;
3311 if (PREDICT_FALSE (maxevents <= 0))
3313 if (VPPCOM_DEBUG > 0)
3314 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3315 getpid (), maxevents);
3316 return VPPCOM_EINVAL;
3318 memset (events, 0, sizeof (*events) * maxevents);
3320 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3321 vep_next_sid = vep_session->vep.next_sid;
3322 is_vep = vep_session->is_vep;
3323 wait_cont_idx = vep_session->wait_cont_idx;
3324 clib_spinlock_unlock (&vcm->sessions_lockp);
3326 if (PREDICT_FALSE (!is_vep))
3328 if (VPPCOM_DEBUG > 0)
3329 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3330 getpid (), vep_idx);
3334 if (PREDICT_FALSE (vep_next_sid == ~0))
3336 if (VPPCOM_DEBUG > 0)
3337 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3338 getpid (), vep_idx);
3348 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3349 sid != ~0; sid = next_sid)
3351 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3352 u8 add_event, is_vep_session;
3354 u64 session_ev_data;
3356 VCL_LOCK_AND_GET_SESSION (sid, &session);
3357 next_sid = session->vep.next_sid;
3358 session_events = session->vep.ev.events;
3359 et_mask = session->vep.et_mask;
3360 is_vep = session->is_vep;
3361 is_vep_session = session->is_vep_session;
3362 session_vep_idx = session->vep.vep_idx;
3363 session_ev_data = session->vep.ev.data.u64;
3364 clib_spinlock_unlock (&vcm->sessions_lockp);
3366 if (PREDICT_FALSE (is_vep))
3368 if (VPPCOM_DEBUG > 0)
3369 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3370 getpid (), vep_idx);
3374 if (PREDICT_FALSE (!is_vep_session))
3376 if (VPPCOM_DEBUG > 0)
3377 clib_warning ("[%d] ERROR: session (%u) is not "
3378 "a vep session!", getpid (), sid);
3382 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3384 clib_warning ("[%d] ERROR: session (%u) "
3385 "vep_idx (%u) != vep_idx (%u)!",
3386 getpid (), sid, session->vep.vep_idx, vep_idx);
3391 add_event = clear_et_mask = 0;
3393 if (EPOLLIN & session_events)
3395 VCL_LOCK_AND_GET_SESSION (sid, &session);
3396 ready = vppcom_session_read_ready (session, sid);
3397 clib_spinlock_unlock (&vcm->sessions_lockp);
3398 if ((ready > 0) && (EPOLLIN & et_mask))
3401 events[num_ev].events |= EPOLLIN;
3402 if (((EPOLLET | EPOLLIN) & session_events) ==
3403 (EPOLLET | EPOLLIN))
3404 clear_et_mask |= EPOLLIN;
3411 case VPPCOM_ECONNRESET:
3412 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3416 events[num_ev].events |= EPOLLERR;
3422 if (EPOLLOUT & session_events)
3424 VCL_LOCK_AND_GET_SESSION (sid, &session);
3425 ready = vppcom_session_write_ready (session, sid);
3426 clib_spinlock_unlock (&vcm->sessions_lockp);
3427 if ((ready > 0) && (EPOLLOUT & et_mask))
3430 events[num_ev].events |= EPOLLOUT;
3431 if (((EPOLLET | EPOLLOUT) & session_events) ==
3432 (EPOLLET | EPOLLOUT))
3433 clear_et_mask |= EPOLLOUT;
3440 case VPPCOM_ECONNRESET:
3441 events[num_ev].events |= EPOLLHUP;
3445 events[num_ev].events |= EPOLLERR;
3453 events[num_ev].data.u64 = session_ev_data;
3454 if (EPOLLONESHOT & session_events)
3456 VCL_LOCK_AND_GET_SESSION (sid, &session);
3457 session->vep.ev.events = 0;
3458 clib_spinlock_unlock (&vcm->sessions_lockp);
3461 if (num_ev == maxevents)
3463 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3464 vep_session->wait_cont_idx = next_sid;
3465 clib_spinlock_unlock (&vcm->sessions_lockp);
3469 if (wait_cont_idx != ~0)
3472 next_sid = vep_next_sid;
3473 else if (next_sid == wait_cont_idx)
3477 if (wait_for_time != -1)
3478 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3480 while ((num_ev == 0) && keep_trying);
3482 if (wait_cont_idx != ~0)
3484 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3485 vep_session->wait_cont_idx = ~0;
3486 clib_spinlock_unlock (&vcm->sessions_lockp);
3489 return (rv != VPPCOM_OK) ? rv : num_ev;
3493 vppcom_session_attr (uint32_t session_index, uint32_t op,
3494 void *buffer, uint32_t * buflen)
3498 u32 *flags = buffer;
3499 vppcom_endpt_t *ep = buffer;
3501 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3504 case VPPCOM_ATTR_GET_NREAD:
3505 rv = vppcom_session_read_ready (session, session_index);
3506 if (VPPCOM_DEBUG > 2)
3507 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3512 case VPPCOM_ATTR_GET_NWRITE:
3513 rv = vppcom_session_write_ready (session, session_index);
3514 if (VPPCOM_DEBUG > 2)
3515 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3516 getpid (), session_index, rv);
3520 case VPPCOM_ATTR_GET_FLAGS:
3521 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3523 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3524 *buflen = sizeof (*flags);
3525 if (VPPCOM_DEBUG > 2)
3526 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3527 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3528 session_index, *flags, session->is_nonblocking);
3534 case VPPCOM_ATTR_SET_FLAGS:
3535 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3537 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3538 if (VPPCOM_DEBUG > 2)
3539 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3540 "flags = 0x%08x, is_nonblocking = %u",
3541 getpid (), *flags, session->is_nonblocking);
3547 case VPPCOM_ATTR_GET_PEER_ADDR:
3548 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3550 ep->vrf = session->vrf;
3551 ep->is_ip4 = session->peer_addr.is_ip4;
3552 ep->port = session->peer_port;
3553 if (session->peer_addr.is_ip4)
3554 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3555 sizeof (ip4_address_t));
3557 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3558 sizeof (ip6_address_t));
3559 *buflen = sizeof (*ep);
3560 if (VPPCOM_DEBUG > 1)
3561 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
3562 "%u, addr = %U, port %u", getpid (),
3563 session_index, ep->is_ip4, format_ip46_address,
3564 &session->peer_addr.ip46, ep->is_ip4,
3565 clib_net_to_host_u16 (ep->port));
3571 case VPPCOM_ATTR_GET_LCL_ADDR:
3572 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3574 ep->vrf = session->vrf;
3575 ep->is_ip4 = session->lcl_addr.is_ip4;
3576 ep->port = session->lcl_port;
3577 if (session->lcl_addr.is_ip4)
3578 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3579 sizeof (ip4_address_t));
3581 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3582 sizeof (ip6_address_t));
3583 *buflen = sizeof (*ep);
3584 if (VPPCOM_DEBUG > 1)
3585 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
3586 "%u, addr = %U port %d", getpid (),
3587 session_index, ep->is_ip4, format_ip46_address,
3588 &session->lcl_addr.ip46, ep->is_ip4,
3589 clib_net_to_host_u16 (ep->port));
3595 case VPPCOM_ATTR_SET_REUSEADDR:
3598 case VPPCOM_ATTR_SET_BROADCAST:
3601 case VPPCOM_ATTR_SET_V6ONLY:
3604 case VPPCOM_ATTR_SET_KEEPALIVE:
3607 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3610 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3619 clib_spinlock_unlock (&vcm->sessions_lockp);
3624 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3625 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3628 session_t *session = 0;
3632 clib_spinlock_lock (&vcm->sessions_lockp);
3633 rv = vppcom_session_at_index (session_index, &session);
3634 if (PREDICT_FALSE (rv))
3636 clib_spinlock_unlock (&vcm->sessions_lockp);
3637 if (VPPCOM_DEBUG > 0)
3638 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3639 getpid (), session_index);
3641 clib_spinlock_unlock (&vcm->sessions_lockp);
3644 ep->vrf = session->vrf;
3645 ep->is_ip4 = session->peer_addr.is_ip4;
3646 ep->port = session->peer_port;
3647 if (session->peer_addr.is_ip4)
3648 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3649 sizeof (ip4_address_t));
3651 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3652 sizeof (ip6_address_t));
3653 clib_spinlock_unlock (&vcm->sessions_lockp);
3657 rv = vppcom_session_read (session_index, buffer, buflen);
3658 else if (flags & MSG_PEEK)
3659 rv = vppcom_session_peek (session_index, buffer, buflen);
3662 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3663 rv = VPPCOM_EAFNOSUPPORT;
3671 vppcom_session_sendto (uint32_t session_index, void *buffer,
3672 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3675 return VPPCOM_EINVAL;
3680 return VPPCOM_EINVAL;
3685 // TBD check the flags and do the right thing
3686 if (VPPCOM_DEBUG > 2)
3687 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3688 getpid (), flags, flags);
3691 return (vppcom_session_write (session_index, buffer, buflen));
3695 * fd.io coding-style-patch-verification: ON
3698 * eval: (c-set-style "gnu")