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_connect_to_vpp (char *app_name)
312 api_main_t *am = &api_main;
314 if (VPPCOM_DEBUG > 0)
315 printf ("\nConnecting to VPP api...");
316 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
318 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
319 return VPPCOM_ECONNREFUSED;
322 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
323 vcm->my_client_index = am->my_client_index;
324 if (VPPCOM_DEBUG > 0)
325 printf (" connected!\n");
327 vcm->app_state = STATE_APP_CONN_VPP;
332 format_api_error (u8 * s, va_list * args)
334 i32 error = va_arg (*args, u32);
337 p = hash_get (vcm->error_string_by_error_number, -error);
340 s = format (s, "%s (%d)", p[0], error);
342 s = format (s, "%d", error);
347 vppcom_init_error_string_table (void)
349 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
351 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
352 foreach_vnet_api_error;
355 hash_set (vcm->error_string_by_error_number, 99, "Misc");
359 vppcom_wait_for_app_state_change (app_state_t app_state)
361 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
363 while (clib_time_now (&vcm->clib_time) < timeout)
365 if (vcm->app_state == app_state)
368 if (VPPCOM_DEBUG > 0)
369 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
370 vppcom_app_state_str (app_state), app_state);
371 return VPPCOM_ETIMEDOUT;
375 vppcom_wait_for_session_state_change (u32 session_index,
376 session_state_t state,
379 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
380 session_t *volatile session;
385 clib_spinlock_lock (&vcm->sessions_lockp);
386 rv = vppcom_session_at_index (session_index, &session);
387 if (PREDICT_FALSE (rv))
389 clib_spinlock_unlock (&vcm->sessions_lockp);
392 if (session->state == state)
394 clib_spinlock_unlock (&vcm->sessions_lockp);
397 clib_spinlock_unlock (&vcm->sessions_lockp);
399 while (clib_time_now (&vcm->clib_time) < timeout);
401 if (VPPCOM_DEBUG > 0)
402 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
403 vppcom_session_state_str (state), state);
404 return VPPCOM_ETIMEDOUT;
408 vppcom_wait_for_client_session_index (f64 wait_for_time)
410 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
414 if (clib_fifo_elts (vcm->client_session_index_fifo))
417 while (clib_time_now (&vcm->clib_time) < timeout);
419 if (wait_for_time == 0)
420 return VPPCOM_EAGAIN;
422 if (VPPCOM_DEBUG > 0)
423 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
424 return VPPCOM_ETIMEDOUT;
428 * VPP-API message functions
431 vppcom_send_session_enable_disable (u8 is_enable)
433 vl_api_session_enable_disable_t *bmp;
434 bmp = vl_msg_api_alloc (sizeof (*bmp));
435 memset (bmp, 0, sizeof (*bmp));
437 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
438 bmp->client_index = vcm->my_client_index;
439 bmp->context = htonl (0xfeedface);
440 bmp->is_enable = is_enable;
441 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
445 vppcom_app_session_enable (void)
449 if (vcm->app_state != STATE_APP_ENABLED)
451 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
452 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
453 if (PREDICT_FALSE (rv))
455 if (VPPCOM_DEBUG > 0)
456 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
457 getpid (), vppcom_retval_str (rv), rv);
465 vl_api_session_enable_disable_reply_t_handler
466 (vl_api_session_enable_disable_reply_t * mp)
470 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
471 format_api_error, ntohl (mp->retval));
474 vcm->app_state = STATE_APP_ENABLED;
478 vppcom_app_send_attach (void)
480 vl_api_application_attach_t *bmp;
481 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
482 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
483 vcm->cfg.app_proxy_transport_udp);
485 bmp = vl_msg_api_alloc (sizeof (*bmp));
486 memset (bmp, 0, sizeof (*bmp));
488 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
489 bmp->client_index = vcm->my_client_index;
490 bmp->context = htonl (0xfeedface);
491 bmp->options[APP_OPTIONS_FLAGS] =
492 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
493 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
494 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
495 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
496 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
497 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
498 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
499 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
500 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
501 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
502 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
505 bmp->namespace_id_len = nsid_len;
506 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
507 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
509 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
513 vppcom_app_attach (void)
517 vppcom_app_send_attach ();
518 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
519 if (PREDICT_FALSE (rv))
521 if (VPPCOM_DEBUG > 0)
522 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
523 getpid (), vppcom_retval_str (rv), rv);
530 vppcom_app_detach (void)
532 vl_api_application_detach_t *bmp;
533 bmp = vl_msg_api_alloc (sizeof (*bmp));
534 memset (bmp, 0, sizeof (*bmp));
536 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
537 bmp->client_index = vcm->my_client_index;
538 bmp->context = htonl (0xfeedface);
539 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
543 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
546 static svm_fifo_segment_create_args_t _a;
547 svm_fifo_segment_create_args_t *a = &_a;
550 memset (a, 0, sizeof (*a));
553 clib_warning ("[%d] attach failed: %U", getpid (),
554 format_api_error, ntohl (mp->retval));
558 if (mp->segment_name_length == 0)
560 clib_warning ("[%d] segment_name_length zero", getpid ());
564 a->segment_name = (char *) mp->segment_name;
565 a->segment_size = mp->segment_size;
567 ASSERT (mp->app_event_queue_address);
569 /* Attach to the segment vpp created */
570 rv = svm_fifo_segment_attach (a);
571 vec_reset_length (a->new_segment_indices);
572 if (PREDICT_FALSE (rv))
574 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
579 vcm->app_event_queue =
580 uword_to_pointer (mp->app_event_queue_address,
581 unix_shared_memory_queue_t *);
583 vcm->app_state = STATE_APP_ATTACHED;
587 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
591 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
594 vcm->app_state = STATE_APP_ENABLED;
598 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
603 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
606 session_t *session = 0;
608 clib_spinlock_lock (&vcm->sessions_lockp);
609 rv = vppcom_session_at_index (p[0], &session);
610 if (PREDICT_FALSE (rv))
612 if (VPPCOM_DEBUG > 1)
613 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
616 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
617 session->state = STATE_DISCONNECT;
618 clib_spinlock_unlock (&vcm->sessions_lockp);
622 if (VPPCOM_DEBUG > 1)
623 clib_warning ("[%d] couldn't find session key %llx", getpid (),
628 clib_warning ("[%d] disconnect_session failed: %U", getpid (),
629 format_api_error, ntohl (mp->retval));
633 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
635 static svm_fifo_segment_create_args_t _a;
636 svm_fifo_segment_create_args_t *a = &_a;
639 memset (a, 0, sizeof (*a));
640 a->segment_name = (char *) mp->segment_name;
641 a->segment_size = mp->segment_size;
642 /* Attach to the segment vpp created */
643 rv = svm_fifo_segment_attach (a);
644 vec_reset_length (a->new_segment_indices);
645 if (PREDICT_FALSE (rv))
647 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
648 getpid (), mp->segment_name);
651 if (VPPCOM_DEBUG > 1)
652 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
653 mp->segment_name, mp->segment_size);
657 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
659 session_t *session = 0;
660 vl_api_disconnect_session_reply_t *rmp;
664 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
668 clib_spinlock_lock (&vcm->sessions_lockp);
669 rval = vppcom_session_at_index (p[0], &session);
670 if (PREDICT_FALSE (rval))
672 if (VPPCOM_DEBUG > 1)
673 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
677 pool_put (vcm->sessions, session);
678 clib_spinlock_unlock (&vcm->sessions_lockp);
679 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
683 clib_warning ("[%d] couldn't find session key %llx", getpid (),
688 rmp = vl_msg_api_alloc (sizeof (*rmp));
689 memset (rmp, 0, sizeof (*rmp));
691 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
692 rmp->retval = htonl (rv);
693 rmp->handle = mp->handle;
694 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
698 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
700 session_t *session = 0;
701 vl_api_reset_session_reply_t *rmp;
705 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
709 clib_spinlock_lock (&vcm->sessions_lockp);
710 rval = vppcom_session_at_index (p[0], &session);
711 if (PREDICT_FALSE (rval))
713 if (VPPCOM_DEBUG > 1)
714 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
718 pool_put (vcm->sessions, session);
719 clib_spinlock_unlock (&vcm->sessions_lockp);
720 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
724 clib_warning ("[%d] couldn't find session key %llx", getpid (),
729 rmp = vl_msg_api_alloc (sizeof (*rmp));
730 memset (rmp, 0, sizeof (*rmp));
731 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
732 rmp->retval = htonl (rv);
733 rmp->handle = mp->handle;
734 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
738 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
742 svm_fifo_t *rx_fifo, *tx_fifo;
748 clib_warning ("[%d] connect failed: %U", getpid (), format_api_error,
753 session_index = mp->context;
754 if (VPPCOM_DEBUG > 1)
755 clib_warning ("[%d] session_index = %d 0x%08x", getpid (),
756 session_index, session_index);
758 clib_spinlock_lock (&vcm->sessions_lockp);
759 if (pool_is_free_index (vcm->sessions, session_index))
761 clib_spinlock_unlock (&vcm->sessions_lockp);
762 if (VPPCOM_DEBUG > 1)
763 clib_warning ("[%d] invalid session, sid %d is closed!",
764 getpid (), session_index);
768 /* We've been redirected */
769 if (mp->segment_name_length > 0)
771 static svm_fifo_segment_create_args_t _a;
772 svm_fifo_segment_create_args_t *a = &_a;
775 memset (a, 0, sizeof (*a));
776 a->segment_name = (char *) mp->segment_name;
777 if (VPPCOM_DEBUG > 1)
778 clib_warning ("[%d] cut-thru segment: %s\n",
779 getpid (), a->segment_name);
781 rv = svm_fifo_segment_attach (a);
782 vec_reset_length (a->new_segment_indices);
783 if (PREDICT_FALSE (rv))
785 clib_spinlock_unlock (&vcm->sessions_lockp);
786 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
787 getpid (), a->segment_name);
795 session = pool_elt_at_index (vcm->sessions, session_index);
796 session->is_cut_thru = is_cut_thru;
797 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
798 unix_shared_memory_queue_t *);
800 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
801 rx_fifo->client_session_index = session_index;
802 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
803 tx_fifo->client_session_index = session_index;
805 session->server_rx_fifo = rx_fifo;
806 session->server_tx_fifo = tx_fifo;
807 session->vpp_handle = mp->handle;
808 session->state = STATE_CONNECT;
810 /* Add it to lookup table */
811 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
813 if (VPPCOM_DEBUG > 1)
814 clib_warning ("[%d] client sid %d\n"
815 " session_rx_fifo %p, refcnt %d\n"
816 " session_tx_fifo %p, refcnt %d",
817 getpid (), session_index,
818 session->server_rx_fifo,
819 session->server_rx_fifo->refcnt,
820 session->server_tx_fifo, session->server_tx_fifo->refcnt);
822 clib_spinlock_unlock (&vcm->sessions_lockp);
826 vppcom_send_connect_sock (session_t * session, u32 session_index)
828 vl_api_connect_sock_t *cmp;
830 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
831 session->is_server = 0;
832 cmp = vl_msg_api_alloc (sizeof (*cmp));
833 memset (cmp, 0, sizeof (*cmp));
834 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
835 cmp->client_index = vcm->my_client_index;
836 cmp->context = session_index;
838 if (VPPCOM_DEBUG > 1)
839 clib_warning ("[%d] session_index = %d 0x%08x",
840 getpid (), session_index, session_index);
842 cmp->vrf = session->vrf;
843 cmp->is_ip4 = session->peer_addr.is_ip4;
844 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
845 cmp->port = session->peer_port;
846 cmp->proto = session->proto;
847 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
848 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
852 vppcom_send_disconnect (session_t * session)
854 vl_api_disconnect_session_t *dmp;
856 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
857 dmp = vl_msg_api_alloc (sizeof (*dmp));
858 memset (dmp, 0, sizeof (*dmp));
859 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
860 dmp->client_index = vcm->my_client_index;
861 dmp->handle = session->vpp_handle;
862 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
866 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
868 session_t *session = 0;
872 clib_warning ("[%d] bind failed: %U", getpid (), format_api_error,
875 ASSERT (vcm->bind_session_index != ~0);
877 clib_spinlock_lock (&vcm->sessions_lockp);
878 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
881 session->vpp_handle = mp->handle;
882 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
883 vcm->bind_session_index);
884 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
885 vcm->bind_session_index = ~0;
887 clib_spinlock_unlock (&vcm->sessions_lockp);
891 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
893 session_t *session = 0;
896 clib_spinlock_lock (&vcm->sessions_lockp);
897 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
900 if ((VPPCOM_DEBUG > 1) && (mp->retval))
901 clib_warning ("[%d] unbind failed: %U", getpid (), format_api_error,
904 vcm->bind_session_index = ~0;
905 session->state = STATE_START;
907 clib_spinlock_unlock (&vcm->sessions_lockp);
911 format_ip4_address (u8 * s, va_list * args)
913 u8 *a = va_arg (*args, u8 *);
914 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
918 format_ip6_address (u8 * s, va_list * args)
920 ip6_address_t *a = va_arg (*args, ip6_address_t *);
921 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
923 i_max_n_zero = ARRAY_LEN (a->as_u16);
925 i_first_zero = i_max_n_zero;
927 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
929 u32 is_zero = a->as_u16[i] == 0;
930 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
936 if ((!is_zero && n_zeros > max_n_zeros)
937 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
939 i_max_n_zero = i_first_zero;
940 max_n_zeros = n_zeros;
941 i_first_zero = ARRAY_LEN (a->as_u16);
946 last_double_colon = 0;
947 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
949 if (i == i_max_n_zero && max_n_zeros > 1)
951 s = format (s, "::");
952 i += max_n_zeros - 1;
953 last_double_colon = 1;
957 s = format (s, "%s%x",
958 (last_double_colon || i == 0) ? "" : ":",
959 clib_net_to_host_u16 (a->as_u16[i]));
960 last_double_colon = 0;
967 /* Format an IP46 address. */
969 format_ip46_address (u8 * s, va_list * args)
971 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
972 ip46_type_t type = va_arg (*args, ip46_type_t);
978 is_ip4 = ip46_address_is_ip4 (ip46);
989 format (s, "%U", format_ip4_address, &ip46->ip4) :
990 format (s, "%U", format_ip6_address, &ip46->ip6);
994 vppcom_send_accept_session_reply (u32 handle, int retval)
996 vl_api_accept_session_reply_t *rmp;
998 rmp = vl_msg_api_alloc (sizeof (*rmp));
999 memset (rmp, 0, sizeof (*rmp));
1000 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1001 rmp->retval = htonl (retval);
1002 rmp->handle = handle;
1003 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1007 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1009 svm_fifo_t *rx_fifo, *tx_fifo;
1013 clib_spinlock_lock (&vcm->sessions_lockp);
1014 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1016 clib_warning ("[%d] client session queue is full!", getpid ());
1017 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL,
1019 clib_spinlock_unlock (&vcm->sessions_lockp);
1023 /* Allocate local session and set it up */
1024 pool_get (vcm->sessions, session);
1025 memset (session, 0, sizeof (*session));
1026 session_index = session - vcm->sessions;
1028 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1029 rx_fifo->client_session_index = session_index;
1030 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1031 tx_fifo->client_session_index = session_index;
1033 session->vpp_handle = mp->handle;
1034 session->server_rx_fifo = rx_fifo;
1035 session->server_tx_fifo = tx_fifo;
1036 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1037 unix_shared_memory_queue_t *);
1038 session->state = STATE_ACCEPT;
1039 session->is_cut_thru = 0;
1040 session->is_server = 1;
1041 session->peer_port = mp->port;
1042 session->peer_addr.is_ip4 = mp->is_ip4;
1043 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1044 sizeof (session->peer_addr.ip46));
1046 /* Add it to lookup table */
1047 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1049 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1050 clib_spinlock_unlock (&vcm->sessions_lockp);
1052 if (VPPCOM_DEBUG > 1)
1054 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1055 clib_warning ("[%d] received request to accept session (sid %d) "
1056 "from %s:%d", getpid (), session_index, ip_str,
1057 clib_net_to_host_u16 (mp->port));
1063 vppcom_send_connect_session_reply (session_t * session, u32 context,
1064 int retval, int handle)
1066 vl_api_connect_session_reply_t *rmp;
1068 unix_shared_memory_queue_t *client_q;
1070 rmp = vl_msg_api_alloc (sizeof (*rmp));
1071 memset (rmp, 0, sizeof (*rmp));
1073 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1074 rmp->context = session ? session->client_context : context;
1075 rmp->retval = htonl (retval);
1076 rmp->handle = session ? session->vpp_handle : handle;
1080 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1081 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1082 rmp->vpp_event_queue_address =
1083 pointer_to_uword (session->vpp_event_queue);
1084 rmp->segment_size = vcm->cfg.segment_size;
1085 len = vec_len (session->segment_name);
1086 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1087 clib_memcpy (rmp->segment_name, session->segment_name,
1088 rmp->segment_name_length - 1);
1089 clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
1090 sizeof (rmp->lcl_ip));
1091 rmp->is_ip4 = session->lcl_addr.is_ip4;
1094 client_q = uword_to_pointer (session->client_queue_address,
1095 unix_shared_memory_queue_t *);
1097 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1101 * Acting as server for redirected connect requests
1104 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1107 session_t *session = 0;
1109 clib_spinlock_lock (&vcm->sessions_lockp);
1110 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1112 if (VPPCOM_DEBUG > 1)
1113 clib_warning ("[%d] client session queue is full!", getpid ());
1114 clib_spinlock_unlock (&vcm->sessions_lockp);
1115 vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL, 0);
1119 pool_get (vcm->sessions, session);
1120 memset (session, 0, sizeof (*session));
1121 session_index = session - vcm->sessions;
1123 session->client_context = mp->context;
1124 session->vpp_handle = session_index;
1125 session->client_queue_address = mp->client_queue_address;
1126 session->is_cut_thru = 1;
1127 session->is_server = 1;
1128 session->peer_port = mp->port;
1129 session->peer_addr.is_ip4 = mp->is_ip4;
1130 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1131 sizeof (session->peer_addr.ip46));
1133 session->state = STATE_ACCEPT;
1134 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1135 if (VPPCOM_DEBUG > 1)
1137 ("[%d] Got a cut-thru connect request to client: "
1138 "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
1139 clib_fifo_elts (vcm->client_session_index_fifo));
1140 clib_spinlock_unlock (&vcm->sessions_lockp);
1144 vppcom_send_bind_sock (session_t * session)
1146 vl_api_bind_sock_t *bmp;
1148 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1149 session->is_server = 1;
1150 bmp = vl_msg_api_alloc (sizeof (*bmp));
1151 memset (bmp, 0, sizeof (*bmp));
1153 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1154 bmp->client_index = vcm->my_client_index;
1155 bmp->context = htonl (0xfeedface);
1156 bmp->vrf = session->vrf;
1157 bmp->is_ip4 = session->lcl_addr.is_ip4;
1158 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1159 bmp->port = session->lcl_port;
1160 bmp->proto = session->proto;
1161 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1162 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1166 vppcom_send_unbind_sock (u32 session_index)
1168 vl_api_unbind_sock_t *ump;
1169 session_t *session = 0;
1172 clib_spinlock_lock (&vcm->sessions_lockp);
1173 rv = vppcom_session_at_index (session_index, &session);
1174 if (PREDICT_FALSE (rv))
1176 clib_spinlock_unlock (&vcm->sessions_lockp);
1177 if (VPPCOM_DEBUG > 0)
1178 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1179 getpid (), session_index);
1183 ump = vl_msg_api_alloc (sizeof (*ump));
1184 memset (ump, 0, sizeof (*ump));
1186 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1187 ump->client_index = vcm->my_client_index;
1188 ump->handle = session->vpp_handle;
1189 clib_spinlock_unlock (&vcm->sessions_lockp);
1190 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1194 vppcom_session_unbind_cut_thru (session_t * session, u32 session_index)
1196 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1197 svm_fifo_segment_private_t *seg;
1200 if (VPPCOM_DEBUG > 1)
1201 clib_warning ("[%d] sid %d, seg_nxd %d:\n"
1202 " server_rx_fifo %p, refcnt = %d\n"
1203 " server_tx_fifo %p, refcnt = %d",
1204 getpid (), session_index, session->sm_seg_index,
1205 session->server_rx_fifo, session->server_rx_fifo->refcnt,
1206 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1208 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1209 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1210 FIFO_SEGMENT_RX_FREELIST);
1211 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1212 FIFO_SEGMENT_TX_FREELIST);
1213 svm_fifo_segment_delete (seg);
1219 vppcom_session_unbind (u32 session_index)
1223 clib_spinlock_lock (&vcm->sessions_lockp);
1224 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1226 clib_spinlock_unlock (&vcm->sessions_lockp);
1227 if (VPPCOM_DEBUG > 1)
1228 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1229 getpid (), session_index);
1230 return VPPCOM_EBADFD;
1232 clib_spinlock_unlock (&vcm->sessions_lockp);
1234 vcm->bind_session_index = session_index;
1235 vppcom_send_unbind_sock (session_index);
1236 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1237 vcm->cfg.session_timeout);
1238 if (PREDICT_FALSE (rv))
1240 vcm->bind_session_index = ~0;
1241 if (VPPCOM_DEBUG > 0)
1242 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1243 getpid (), vppcom_retval_str (rv), rv);
1250 vppcom_session_disconnect (u32 session_index)
1255 clib_spinlock_lock (&vcm->sessions_lockp);
1256 rv = vppcom_session_at_index (session_index, &session);
1257 if (PREDICT_FALSE (rv))
1259 clib_spinlock_unlock (&vcm->sessions_lockp);
1260 if (VPPCOM_DEBUG > 1)
1261 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1262 getpid (), session_index);
1266 if (!session->is_cut_thru)
1268 vppcom_send_disconnect (session);
1269 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 rv = vppcom_wait_for_session_state_change (session_index,
1272 STATE_DISCONNECT, 1.0);
1273 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1274 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1275 getpid (), session_index, vppcom_retval_str (rv), rv);
1278 clib_spinlock_unlock (&vcm->sessions_lockp);
1283 #define foreach_sock_msg \
1284 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1285 _(BIND_SOCK_REPLY, bind_sock_reply) \
1286 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1287 _(ACCEPT_SESSION, accept_session) \
1288 _(CONNECT_SOCK, connect_sock) \
1289 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1290 _(DISCONNECT_SESSION, disconnect_session) \
1291 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1292 _(RESET_SESSION, reset_session) \
1293 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1294 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1295 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1298 vppcom_api_hookup (void)
1301 vl_msg_api_set_handlers(VL_API_##N, #n, \
1302 vl_api_##n##_t_handler, \
1304 vl_api_##n##_t_endian, \
1305 vl_api_##n##_t_print, \
1306 sizeof(vl_api_##n##_t), 1);
1312 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1316 vcl_cfg->heapsize = (256ULL << 20);
1317 vcl_cfg->segment_baseva = 0x200000000ULL;
1318 vcl_cfg->segment_size = (256 << 20);
1319 vcl_cfg->add_segment_size = (128 << 20);
1320 vcl_cfg->preallocated_fifo_pairs = 8;
1321 vcl_cfg->rx_fifo_size = (1 << 20);
1322 vcl_cfg->tx_fifo_size = (1 << 20);
1323 vcl_cfg->event_queue_size = 2048;
1324 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1325 vcl_cfg->app_timeout = 10 * 60.0;
1326 vcl_cfg->session_timeout = 10 * 60.0;
1327 vcl_cfg->accept_timeout = 60.0;
1331 vppcom_cfg_heapsize (char *conf_fname)
1333 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1346 fp = fopen (conf_fname, "r");
1349 if (VPPCOM_DEBUG > 0)
1350 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1353 argv = calloc (1, sizeof (char *));
1359 if (fgets (inbuf, 4096, fp) == 0)
1361 p = strtok (inbuf, " \t\n");
1367 char **tmp = realloc (argv, argc * sizeof (char *));
1371 arg = strndup (p, 1024);
1374 argv[argc - 1] = arg;
1375 p = strtok (NULL, " \t\n");
1382 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1389 * Look for and parse the "heapsize" config parameter.
1390 * Manual since none of the clib infra has been bootstrapped yet.
1392 * Format: heapsize <nn>[mM][gG]
1395 for (i = 1; i < (argc - 1); i++)
1397 if (!strncmp (argv[i], "heapsize", 8))
1399 sizep = (u8 *) argv[i + 1];
1401 while (*sizep >= '0' && *sizep <= '9')
1404 size += *sizep++ - '0';
1408 if (VPPCOM_DEBUG > 0)
1409 clib_warning ("[%d] parse error '%s %s', "
1410 "using default heapsize %lld (0x%llx)",
1411 getpid (), argv[i], argv[i + 1],
1412 vcl_cfg->heapsize, vcl_cfg->heapsize);
1416 if (*sizep == 'g' || *sizep == 'G')
1417 vcl_cfg->heapsize = size << 30;
1418 else if (*sizep == 'm' || *sizep == 'M')
1419 vcl_cfg->heapsize = size << 20;
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);
1438 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1439 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1440 if (vcl_mem == MAP_FAILED)
1442 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1443 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1445 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1448 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1451 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1454 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1457 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1461 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1464 if (VPPCOM_DEBUG > 0)
1465 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1466 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1470 vppcom_cfg_read (char *conf_fname)
1472 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1474 unformat_input_t _input, *input = &_input;
1475 unformat_input_t _line_input, *line_input = &_line_input;
1476 u8 vc_cfg_input = 0;
1481 fd = open (conf_fname, O_RDONLY);
1484 if (VPPCOM_DEBUG > 0)
1485 clib_warning ("[%d] open configuration file '%s' failed!",
1486 getpid (), conf_fname);
1490 if (fstat (fd, &s) < 0)
1492 if (VPPCOM_DEBUG > 0)
1493 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1497 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1499 if (VPPCOM_DEBUG > 0)
1500 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1504 unformat_init_clib_file (input, fd);
1506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1508 (void) unformat_user (input, unformat_line_input, line_input);
1509 unformat_skip_white_space (line_input);
1511 if (unformat (line_input, "vcl {"))
1519 if (unformat (line_input, "heapsize %s", &chroot_path))
1521 vec_terminate_c_string (chroot_path);
1522 if (VPPCOM_DEBUG > 0)
1523 clib_warning ("[%d] configured heapsize %s, "
1524 "actual heapsize %lld (0x%llx)",
1525 getpid (), chroot_path, vcl_cfg->heapsize,
1527 vec_free (chroot_path);
1529 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1531 vec_terminate_c_string (chroot_path);
1532 vl_set_memory_root_path ((char *) chroot_path);
1533 if (VPPCOM_DEBUG > 0)
1534 clib_warning ("[%d] configured api-prefix %s",
1535 getpid (), chroot_path);
1536 chroot_path = 0; /* Don't vec_free() it! */
1538 else if (unformat (line_input, "uid %d", &uid))
1540 vl_set_memory_uid (uid);
1541 if (VPPCOM_DEBUG > 0)
1542 clib_warning ("[%d] configured uid %d", getpid (), uid);
1544 else if (unformat (line_input, "gid %d", &gid))
1546 vl_set_memory_gid (gid);
1547 if (VPPCOM_DEBUG > 0)
1548 clib_warning ("[%d] configured gid %d", getpid (), gid);
1550 else if (unformat (line_input, "segment-baseva 0x%lx",
1551 &vcl_cfg->segment_baseva))
1553 if (VPPCOM_DEBUG > 0)
1554 clib_warning ("[%d] configured segment_baseva 0x%lx",
1555 getpid (), vcl_cfg->segment_baseva);
1557 else if (unformat (line_input, "segment-size 0x%lx",
1558 &vcl_cfg->segment_size))
1560 if (VPPCOM_DEBUG > 0)
1561 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1562 getpid (), vcl_cfg->segment_size,
1563 vcl_cfg->segment_size);
1565 else if (unformat (line_input, "segment-size %ld",
1566 &vcl_cfg->segment_size))
1568 if (VPPCOM_DEBUG > 0)
1569 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1570 getpid (), vcl_cfg->segment_size,
1571 vcl_cfg->segment_size);
1573 else if (unformat (line_input, "add-segment-size 0x%lx",
1574 &vcl_cfg->add_segment_size))
1576 if (VPPCOM_DEBUG > 0)
1578 ("[%d] configured add_segment_size 0x%lx (%ld)",
1579 getpid (), vcl_cfg->add_segment_size,
1580 vcl_cfg->add_segment_size);
1582 else if (unformat (line_input, "add-segment-size %ld",
1583 &vcl_cfg->add_segment_size))
1585 if (VPPCOM_DEBUG > 0)
1587 ("[%d] configured add_segment_size %ld (0x%lx)",
1588 getpid (), vcl_cfg->add_segment_size,
1589 vcl_cfg->add_segment_size);
1591 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1592 &vcl_cfg->preallocated_fifo_pairs))
1594 if (VPPCOM_DEBUG > 0)
1595 clib_warning ("[%d] configured preallocated_fifo_pairs "
1596 "%d (0x%x)", getpid (),
1597 vcl_cfg->preallocated_fifo_pairs,
1598 vcl_cfg->preallocated_fifo_pairs);
1600 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1601 &vcl_cfg->rx_fifo_size))
1603 if (VPPCOM_DEBUG > 0)
1604 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1605 getpid (), vcl_cfg->rx_fifo_size,
1606 vcl_cfg->rx_fifo_size);
1608 else if (unformat (line_input, "rx-fifo-size %ld",
1609 &vcl_cfg->rx_fifo_size))
1611 if (VPPCOM_DEBUG > 0)
1612 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1613 getpid (), vcl_cfg->rx_fifo_size,
1614 vcl_cfg->rx_fifo_size);
1616 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1617 &vcl_cfg->tx_fifo_size))
1619 if (VPPCOM_DEBUG > 0)
1620 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1621 getpid (), vcl_cfg->tx_fifo_size,
1622 vcl_cfg->tx_fifo_size);
1624 else if (unformat (line_input, "tx-fifo-size %ld",
1625 &vcl_cfg->tx_fifo_size))
1627 if (VPPCOM_DEBUG > 0)
1628 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1629 getpid (), vcl_cfg->tx_fifo_size,
1630 vcl_cfg->tx_fifo_size);
1632 else if (unformat (line_input, "event-queue-size 0x%lx",
1633 &vcl_cfg->event_queue_size))
1635 if (VPPCOM_DEBUG > 0)
1636 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1637 getpid (), vcl_cfg->event_queue_size,
1638 vcl_cfg->event_queue_size);
1640 else if (unformat (line_input, "event-queue-size %ld",
1641 &vcl_cfg->event_queue_size))
1643 if (VPPCOM_DEBUG > 0)
1644 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1645 getpid (), vcl_cfg->event_queue_size,
1646 vcl_cfg->event_queue_size);
1648 else if (unformat (line_input, "listen-queue-size 0x%lx",
1649 &vcl_cfg->listen_queue_size))
1651 if (VPPCOM_DEBUG > 0)
1652 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1653 getpid (), vcl_cfg->listen_queue_size,
1654 vcl_cfg->listen_queue_size);
1656 else if (unformat (line_input, "listen-queue-size %ld",
1657 &vcl_cfg->listen_queue_size))
1659 if (VPPCOM_DEBUG > 0)
1660 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1661 getpid (), vcl_cfg->listen_queue_size,
1662 vcl_cfg->listen_queue_size);
1664 else if (unformat (line_input, "app-timeout %f",
1665 &vcl_cfg->app_timeout))
1667 if (VPPCOM_DEBUG > 0)
1668 clib_warning ("[%d] configured app_timeout %f",
1669 getpid (), vcl_cfg->app_timeout);
1671 else if (unformat (line_input, "session-timeout %f",
1672 &vcl_cfg->session_timeout))
1674 if (VPPCOM_DEBUG > 0)
1675 clib_warning ("[%d] configured session_timeout %f",
1676 getpid (), vcl_cfg->session_timeout);
1678 else if (unformat (line_input, "accept-timeout %f",
1679 &vcl_cfg->accept_timeout))
1681 if (VPPCOM_DEBUG > 0)
1682 clib_warning ("[%d] configured accept_timeout %f",
1683 getpid (), vcl_cfg->accept_timeout);
1685 else if (unformat (line_input, "app-proxy-transport-tcp"))
1687 vcl_cfg->app_proxy_transport_tcp = 1;
1688 if (VPPCOM_DEBUG > 0)
1689 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1690 getpid (), vcl_cfg->app_proxy_transport_tcp);
1692 else if (unformat (line_input, "app-proxy-transport-udp"))
1694 vcl_cfg->app_proxy_transport_udp = 1;
1695 if (VPPCOM_DEBUG > 0)
1696 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1697 getpid (), vcl_cfg->app_proxy_transport_udp);
1699 else if (unformat (line_input, "app-scope-local"))
1701 vcl_cfg->app_scope_local = 1;
1702 if (VPPCOM_DEBUG > 0)
1703 clib_warning ("[%d] configured app_scope_local (%d)",
1704 getpid (), vcl_cfg->app_scope_local);
1706 else if (unformat (line_input, "app-scope-global"))
1708 vcl_cfg->app_scope_global = 1;
1709 if (VPPCOM_DEBUG > 0)
1710 clib_warning ("[%d] configured app_scope_global (%d)",
1711 getpid (), vcl_cfg->app_scope_global);
1713 else if (unformat (line_input, "namespace-secret %lu",
1714 &vcl_cfg->namespace_secret))
1716 if (VPPCOM_DEBUG > 0)
1718 ("[%d] configured namespace_secret %lu (0x%lx)",
1719 getpid (), vcl_cfg->namespace_secret,
1720 vcl_cfg->namespace_secret);
1722 else if (unformat (line_input, "namespace-id %v",
1723 &vcl_cfg->namespace_id))
1725 vl_api_application_attach_t *mp;
1726 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1727 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1728 if (nsid_vec_len > max_nsid_vec_len)
1730 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1731 if (VPPCOM_DEBUG > 0)
1732 clib_warning ("[%d] configured namespace_id is too long,"
1733 " truncated to %d characters!", getpid (),
1737 if (VPPCOM_DEBUG > 0)
1738 clib_warning ("[%d] configured namespace_id %v",
1739 getpid (), vcl_cfg->namespace_id);
1741 else if (unformat (line_input, "}"))
1744 if (VPPCOM_DEBUG > 0)
1745 clib_warning ("[%d] completed parsing vppcom config!",
1751 if (line_input->buffer[line_input->index] != '#')
1753 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1755 &line_input->buffer[line_input->index]);
1762 unformat_free (input);
1770 * VPPCOM Public API functions
1773 vppcom_app_create (char *app_name)
1775 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1786 vppcom_cfg_init (vcl_cfg);
1787 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1791 if (sscanf (env_var_str, "%u", &tmp) != 1)
1792 clib_warning ("[%d] Invalid debug level specified in "
1793 "the environment variable "
1795 " (%s)!\n", getpid (), env_var_str);
1799 clib_warning ("[%d] configured debug level (%u) from "
1800 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1803 conf_fname = getenv (VPPCOM_ENV_CONF);
1806 conf_fname = VPPCOM_CONF_DEFAULT;
1807 if (VPPCOM_DEBUG > 0)
1808 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1811 vppcom_cfg_heapsize (conf_fname);
1812 clib_fifo_validate (vcm->client_session_index_fifo,
1813 vcm->cfg.listen_queue_size);
1814 vppcom_cfg_read (conf_fname);
1815 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1818 u32 ns_id_vec_len = strlen (env_var_str);
1820 vec_reset_length (vcm->cfg.namespace_id);
1821 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1822 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1824 if (VPPCOM_DEBUG > 0)
1825 clib_warning ("[%d] configured namespace_id (%v) from "
1826 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1827 vcm->cfg.namespace_id);
1829 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1833 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1834 clib_warning ("[%d] Invalid namespace secret specified in "
1835 "the environment variable "
1836 VPPCOM_ENV_APP_NAMESPACE_SECRET
1837 " (%s)!\n", getpid (), env_var_str);
1840 vcm->cfg.namespace_secret = tmp;
1841 if (VPPCOM_DEBUG > 0)
1842 clib_warning ("[%d] configured namespace secret (%lu) from "
1843 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1844 vcm->cfg.namespace_secret);
1847 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1849 vcm->cfg.app_proxy_transport_tcp = 1;
1850 if (VPPCOM_DEBUG > 0)
1851 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1852 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1853 vcm->cfg.app_proxy_transport_tcp);
1855 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1857 vcm->cfg.app_proxy_transport_udp = 1;
1858 if (VPPCOM_DEBUG > 0)
1859 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1860 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1861 vcm->cfg.app_proxy_transport_udp);
1863 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1865 vcm->cfg.app_scope_local = 1;
1866 if (VPPCOM_DEBUG > 0)
1867 clib_warning ("[%d] configured app_scope_local (%u) from "
1868 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1869 vcm->cfg.app_scope_local);
1871 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1873 vcm->cfg.app_scope_global = 1;
1874 if (VPPCOM_DEBUG > 0)
1875 clib_warning ("[%d] configured app_scope_global (%u) from "
1876 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1877 vcm->cfg.app_scope_global);
1880 vcm->bind_session_index = ~0;
1881 vcm->main_cpu = os_get_thread_index ();
1882 heap = clib_mem_get_per_cpu_heap ();
1883 h = mheap_header (heap);
1885 /* make the main heap thread-safe */
1886 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1888 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1890 clib_time_init (&vcm->clib_time);
1891 vppcom_init_error_string_table ();
1892 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1893 20 /* timeout in secs */ );
1894 clib_spinlock_init (&vcm->sessions_lockp);
1895 vppcom_api_hookup ();
1898 if (vcm->my_client_index == ~0)
1900 vcm->app_state = STATE_APP_START;
1901 rv = vppcom_connect_to_vpp (app_name);
1904 clib_warning ("[%d] couldn't connect to VPP.", getpid ());
1908 if (VPPCOM_DEBUG > 0)
1909 clib_warning ("[%d] sending session enable", getpid ());
1911 rv = vppcom_app_session_enable ();
1914 clib_warning ("[%d] vppcom_app_session_enable() failed!",
1919 if (VPPCOM_DEBUG > 0)
1920 clib_warning ("[%d] sending app attach", getpid ());
1922 rv = vppcom_app_attach ();
1925 clib_warning ("[%d] vppcom_app_attach() failed!", getpid ());
1929 if (VPPCOM_DEBUG > 0)
1930 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1931 getpid (), app_name, vcm->my_client_index,
1932 vcm->my_client_index);
1939 vppcom_app_destroy (void)
1943 if (vcm->my_client_index == ~0)
1946 if (VPPCOM_DEBUG > 0)
1947 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1948 getpid (), vcm->my_client_index, vcm->my_client_index);
1950 vppcom_app_detach ();
1951 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1952 if (PREDICT_FALSE (rv))
1954 if (VPPCOM_DEBUG > 0)
1955 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1956 getpid (), vppcom_retval_str (rv), rv);
1958 vl_client_disconnect_from_vlib ();
1959 vcm->my_client_index = ~0;
1960 vcm->app_state = STATE_APP_START;
1964 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1969 clib_spinlock_lock (&vcm->sessions_lockp);
1970 pool_get (vcm->sessions, session);
1971 memset (session, 0, sizeof (*session));
1972 session_index = session - vcm->sessions;
1975 session->proto = proto;
1976 session->state = STATE_START;
1977 session->is_nonblocking = is_nonblocking ? 1 : 0;
1978 clib_spinlock_unlock (&vcm->sessions_lockp);
1980 if (VPPCOM_DEBUG > 0)
1981 clib_warning ("[%d] sid %d", getpid (), session_index);
1983 return (int) session_index;
1987 vppcom_session_close (uint32_t session_index)
1989 session_t *session = 0;
1998 session_state_t state;
2000 clib_spinlock_lock (&vcm->sessions_lockp);
2001 rv = vppcom_session_at_index (session_index, &session);
2002 if (PREDICT_FALSE (rv))
2004 if (VPPCOM_DEBUG > 0)
2005 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2006 getpid (), session_index);
2007 clib_spinlock_unlock (&vcm->sessions_lockp);
2010 is_server = session->is_server;
2011 is_listen = session->is_listen;
2012 is_cut_thru = session->is_cut_thru;
2013 is_vep = session->is_vep;
2014 is_vep_session = session->is_vep_session;
2015 next_sid = session->vep.next_sid;
2016 vep_idx = session->vep.vep_idx;
2017 state = session->state;
2018 clib_spinlock_unlock (&vcm->sessions_lockp);
2020 if (VPPCOM_DEBUG > 0)
2021 clib_warning ("[%d] sid %d", getpid (), session_index);
2025 while (next_sid != ~0)
2027 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2028 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2029 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2030 "rv = %s (%d)", getpid (), vep_idx, next_sid,
2031 vppcom_retval_str (rv), rv);
2033 clib_spinlock_lock (&vcm->sessions_lockp);
2034 rv = vppcom_session_at_index (session_index, &session);
2035 if (PREDICT_FALSE (rv))
2037 if (VPPCOM_DEBUG > 0)
2039 ("[%d] invalid session, sid (%u) has been closed!",
2040 getpid (), session_index);
2041 clib_spinlock_unlock (&vcm->sessions_lockp);
2044 next_sid = session->vep.next_sid;
2045 clib_spinlock_unlock (&vcm->sessions_lockp);
2052 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2053 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2054 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2055 "rv = %s (%d)", getpid (), vep_idx, session_index,
2056 vppcom_retval_str (rv), rv);
2059 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
2061 rv = vppcom_session_unbind_cut_thru (session, session_index);
2062 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2063 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2065 getpid (), session_index,
2066 vppcom_retval_str (rv), rv);
2068 else if (is_server && is_listen)
2070 rv = vppcom_session_unbind (session_index);
2071 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2072 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
2073 getpid (), session_index,
2074 vppcom_retval_str (rv), rv);
2076 else if (state == STATE_CONNECT)
2077 if (vppcom_session_disconnect (session_index))
2080 clib_spinlock_lock (&vcm->sessions_lockp);
2081 pool_put_index (vcm->sessions, session_index);
2082 clib_spinlock_unlock (&vcm->sessions_lockp);
2088 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2090 session_t *session = 0;
2094 return VPPCOM_EINVAL;
2096 clib_spinlock_lock (&vcm->sessions_lockp);
2097 rv = vppcom_session_at_index (session_index, &session);
2098 if (PREDICT_FALSE (rv))
2100 clib_spinlock_unlock (&vcm->sessions_lockp);
2101 if (VPPCOM_DEBUG > 0)
2102 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2103 getpid (), session_index);
2107 if (session->is_vep)
2109 clib_spinlock_unlock (&vcm->sessions_lockp);
2110 if (VPPCOM_DEBUG > 0)
2111 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2112 getpid (), session_index);
2113 return VPPCOM_EBADFD;
2116 session->vrf = ep->vrf;
2117 session->lcl_addr.is_ip4 = ep->is_ip4;
2118 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2119 session->lcl_port = ep->port;
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
2123 getpid (), session_index, format_ip46_address,
2124 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2125 clib_net_to_host_u16 (session->lcl_port));
2127 clib_spinlock_unlock (&vcm->sessions_lockp);
2132 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2134 session_t *listen_session = 0;
2137 clib_spinlock_lock (&vcm->sessions_lockp);
2138 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2139 if (PREDICT_FALSE (rv))
2141 clib_spinlock_unlock (&vcm->sessions_lockp);
2142 if (VPPCOM_DEBUG > 0)
2143 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2144 getpid (), listen_session_index);
2148 if (listen_session->is_vep)
2150 clib_spinlock_unlock (&vcm->sessions_lockp);
2151 if (VPPCOM_DEBUG > 0)
2152 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2153 getpid (), listen_session_index);
2154 return VPPCOM_EBADFD;
2157 if (listen_session->is_listen)
2159 clib_spinlock_unlock (&vcm->sessions_lockp);
2160 if (VPPCOM_DEBUG > 0)
2161 clib_warning ("[%d] sid (%u) is already in listen state!",
2162 getpid (), listen_session_index);
2166 if (VPPCOM_DEBUG > 0)
2167 clib_warning ("[%d] sid %d", getpid (), listen_session_index);
2169 ASSERT (vcm->bind_session_index == ~0);
2170 vcm->bind_session_index = listen_session_index;
2171 vppcom_send_bind_sock (listen_session);
2172 clib_spinlock_unlock (&vcm->sessions_lockp);
2174 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2175 vcm->cfg.session_timeout);
2176 if (PREDICT_FALSE (rv))
2178 vcm->bind_session_index = ~0;
2179 if (VPPCOM_DEBUG > 0)
2180 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2181 getpid (), vppcom_retval_str (rv), rv);
2185 clib_spinlock_lock (&vcm->sessions_lockp);
2186 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2187 if (PREDICT_FALSE (rv))
2189 clib_spinlock_unlock (&vcm->sessions_lockp);
2190 if (VPPCOM_DEBUG > 0)
2191 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2192 getpid (), listen_session_index);
2195 listen_session->is_listen = 1;
2196 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2197 clib_spinlock_unlock (&vcm->sessions_lockp);
2203 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2204 double wait_for_time)
2206 session_t *listen_session = 0;
2207 session_t *client_session = 0;
2208 u32 client_session_index;
2213 clib_spinlock_lock (&vcm->sessions_lockp);
2214 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2215 if (PREDICT_FALSE (rv))
2217 clib_spinlock_unlock (&vcm->sessions_lockp);
2218 if (VPPCOM_DEBUG > 0)
2219 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2220 getpid (), listen_session_index);
2224 if (listen_session->is_vep)
2226 clib_spinlock_unlock (&vcm->sessions_lockp);
2227 if (VPPCOM_DEBUG > 0)
2228 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2229 getpid (), listen_session_index);
2230 return VPPCOM_EBADFD;
2233 if (listen_session->state != STATE_LISTEN)
2235 clib_spinlock_unlock (&vcm->sessions_lockp);
2236 if (VPPCOM_DEBUG > 0)
2237 clib_warning ("[%d] session not in listen state, state = %s",
2239 vppcom_session_state_str (listen_session->state));
2240 return VPPCOM_EBADFD;
2242 wait_for = listen_session->is_nonblocking ? 0 :
2243 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2245 if (VPPCOM_DEBUG > 0)
2246 clib_warning ("[%d] sid %d: %s (%d)", getpid (),
2247 listen_session_index,
2248 vppcom_session_state_str (listen_session->state),
2249 listen_session->state);
2250 clib_spinlock_unlock (&vcm->sessions_lockp);
2254 rv = vppcom_wait_for_client_session_index (wait_for);
2257 if ((VPPCOM_DEBUG > 0))
2258 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2259 getpid (), listen_session_index,
2260 vppcom_retval_str (rv), rv);
2261 if ((wait_for == 0) || (wait_for_time > 0))
2268 clib_spinlock_lock (&vcm->sessions_lockp);
2269 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2270 rv = vppcom_session_at_index (client_session_index, &client_session);
2271 ASSERT (rv == VPPCOM_OK);
2272 ASSERT (client_session->peer_addr.is_ip4 ==
2273 listen_session->lcl_addr.is_ip4);
2275 if (VPPCOM_DEBUG > 0)
2276 clib_warning ("[%d] Got a request: client sid %d", getpid (),
2277 client_session_index);
2279 ep->vrf = client_session->vrf;
2280 ep->is_cut_thru = client_session->is_cut_thru;
2281 ep->is_ip4 = client_session->peer_addr.is_ip4;
2282 ep->port = client_session->peer_port;
2283 if (client_session->peer_addr.is_ip4)
2284 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2285 sizeof (ip4_address_t));
2287 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2288 sizeof (ip6_address_t));
2290 if (client_session->is_server && client_session->is_cut_thru)
2292 static svm_fifo_segment_create_args_t _a;
2293 svm_fifo_segment_create_args_t *a = &_a;
2294 svm_fifo_segment_private_t *seg;
2296 cut_thru_str = "cut-thru ";
2298 /* Create the segment */
2299 memset (a, 0, sizeof (*a));
2300 a->segment_name = (char *)
2301 format ((u8 *) a->segment_name, "%d:segment%d%c",
2302 getpid (), vcm->unique_segment_index++, 0);
2303 a->segment_size = vcm->cfg.segment_size;
2304 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2305 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2306 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2308 rv = svm_fifo_segment_create (a);
2309 if (PREDICT_FALSE (rv))
2311 if (VPPCOM_DEBUG > 1)
2312 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
2313 getpid (), a->segment_name);
2314 vec_reset_length (a->new_segment_indices);
2315 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2316 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2317 clib_spinlock_unlock (&vcm->sessions_lockp);
2318 return VPPCOM_ENOMEM;
2321 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2322 client_session->sm_seg_index = a->new_segment_indices[0];
2323 vec_free (a->new_segment_indices);
2325 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2326 client_session->server_rx_fifo =
2327 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2328 FIFO_SEGMENT_RX_FREELIST);
2329 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2331 svm_fifo_segment_delete (seg);
2332 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
2333 getpid (), vcm->cfg.rx_fifo_size,
2334 vcm->cfg.rx_fifo_size);
2335 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2336 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2337 clib_spinlock_unlock (&vcm->sessions_lockp);
2338 return VPPCOM_ENOMEM;
2340 client_session->server_rx_fifo->master_session_index =
2341 client_session_index;
2343 client_session->server_tx_fifo =
2344 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2345 FIFO_SEGMENT_TX_FREELIST);
2346 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2348 svm_fifo_segment_delete (seg);
2349 if (VPPCOM_DEBUG > 1)
2350 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
2351 getpid (), vcm->cfg.tx_fifo_size,
2352 vcm->cfg.tx_fifo_size);
2353 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2354 vppcom_send_connect_session_reply (client_session, 0, rv, 0);
2355 clib_spinlock_unlock (&vcm->sessions_lockp);
2356 return VPPCOM_ENOMEM;
2358 client_session->server_tx_fifo->master_session_index =
2359 client_session_index;
2361 if (VPPCOM_DEBUG > 1)
2362 clib_warning ("[%d] created segment '%s': rx_fifo %p, tx_fifo %p",
2363 getpid (), client_session->segment_name,
2364 client_session->server_rx_fifo,
2365 client_session->server_tx_fifo);
2367 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2370 ssvm_shared_header_t *sh = seg->ssvm.sh;
2372 ssvm_lock_non_recursive (sh, 1);
2373 oldheap = ssvm_push_heap (sh);
2374 event_q = client_session->vpp_event_queue =
2375 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2376 sizeof (session_fifo_event_t),
2377 getpid (), 0 /* signal not sent */ );
2378 ssvm_pop_heap (oldheap);
2379 ssvm_unlock_non_recursive (sh);
2382 vppcom_send_connect_session_reply (client_session, 0, 0, 0);
2387 vppcom_send_accept_session_reply (0, client_session->vpp_handle);
2390 if (VPPCOM_DEBUG > 0)
2391 clib_warning ("[%d] sid %d, accepted %sconnection to peer address "
2393 getpid (), client_session_index, cut_thru_str,
2394 format_ip46_address, &client_session->peer_addr.ip46,
2395 client_session->peer_addr.is_ip4,
2396 clib_net_to_host_u16 (client_session->peer_port));
2398 clib_spinlock_unlock (&vcm->sessions_lockp);
2399 return (int) client_session_index;
2403 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2405 session_t *session = 0;
2408 clib_spinlock_lock (&vcm->sessions_lockp);
2409 rv = vppcom_session_at_index (session_index, &session);
2410 if (PREDICT_FALSE (rv))
2412 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 if (VPPCOM_DEBUG > 0)
2414 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2415 getpid (), session_index);
2419 if (session->is_vep)
2421 clib_spinlock_unlock (&vcm->sessions_lockp);
2422 if (VPPCOM_DEBUG > 0)
2423 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2424 getpid (), session_index);
2425 return VPPCOM_EBADFD;
2428 if (session->state == STATE_CONNECT)
2430 clib_spinlock_unlock (&vcm->sessions_lockp);
2431 if (VPPCOM_DEBUG > 0)
2432 clib_warning ("[%d] session, sid (%u) already connected!",
2433 getpid (), session_index);
2437 session->vrf = server_ep->vrf;
2438 session->peer_addr.is_ip4 = server_ep->is_ip4;
2439 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2440 session->peer_port = server_ep->port;
2442 if (VPPCOM_DEBUG > 0)
2444 u8 *ip_str = format (0, "%U", format_ip46_address,
2445 &session->peer_addr.ip46,
2446 session->peer_addr.is_ip4);
2447 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2448 getpid (), session_index, ip_str,
2449 clib_net_to_host_u16 (session->peer_port),
2450 session->proto ? "UDP" : "TCP");
2454 vppcom_send_connect_sock (session, session_index);
2455 clib_spinlock_unlock (&vcm->sessions_lockp);
2456 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2457 vcm->cfg.session_timeout);
2458 if (PREDICT_FALSE (rv))
2460 if (VPPCOM_DEBUG > 0)
2461 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2462 getpid (), vppcom_retval_str (rv), rv);
2465 if (VPPCOM_DEBUG > 0)
2466 clib_warning ("[%d] sid %d connected!", getpid (), session_index);
2472 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2475 session_t *session = 0;
2476 svm_fifo_t *rx_fifo;
2484 clib_spinlock_lock (&vcm->sessions_lockp);
2485 rv = vppcom_session_at_index (session_index, &session);
2486 if (PREDICT_FALSE (rv))
2488 clib_spinlock_unlock (&vcm->sessions_lockp);
2489 if (VPPCOM_DEBUG > 0)
2490 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2491 getpid (), session_index);
2495 if (session->is_vep)
2497 clib_spinlock_unlock (&vcm->sessions_lockp);
2498 if (VPPCOM_DEBUG > 0)
2499 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2500 getpid (), session_index);
2501 return VPPCOM_EBADFD;
2504 if (session->state == STATE_DISCONNECT)
2506 clib_spinlock_unlock (&vcm->sessions_lockp);
2507 if (VPPCOM_DEBUG > 0)
2508 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2509 getpid (), session_index);
2510 return VPPCOM_ECONNRESET;
2513 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2514 session->server_rx_fifo : session->server_tx_fifo);
2515 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2516 "server_rx_fifo" : "server_tx_fifo");
2518 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2519 clib_spinlock_unlock (&vcm->sessions_lockp);
2524 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2526 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2528 while (!session->is_nonblocking && (n_read <= 0));
2530 if (poll_et && (n_read <= 0))
2532 clib_spinlock_lock (&vcm->sessions_lockp);
2533 session->vep.et_mask |= EPOLLIN;
2534 clib_spinlock_unlock (&vcm->sessions_lockp);
2537 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2538 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", getpid (),
2539 session_index, n_read, fifo_str, rx_fifo);
2541 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2545 vppcom_session_read (uint32_t session_index, void *buf, int n)
2547 return (vppcom_session_read_internal (session_index, buf, n, 0));
2551 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2553 return (vppcom_session_read_internal (session_index, buf, n, 1));
2557 vppcom_session_read_ready (session_t * session, u32 session_index)
2559 svm_fifo_t *rx_fifo = 0;
2563 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2564 if (session->is_vep)
2566 clib_spinlock_unlock (&vcm->sessions_lockp);
2567 if (VPPCOM_DEBUG > 0)
2568 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2569 getpid (), session_index);
2570 return VPPCOM_EBADFD;
2573 if (session->state == STATE_DISCONNECT)
2575 if (VPPCOM_DEBUG > 0)
2576 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2577 getpid (), session_index);
2578 return VPPCOM_ECONNRESET;
2581 if (session->is_listen)
2582 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2585 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2586 session->server_rx_fifo : session->server_tx_fifo);
2588 ready = svm_fifo_max_dequeue (rx_fifo);
2592 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2593 if (poll_et && (ready == 0))
2595 if (VPPCOM_DEBUG > 11)
2596 clib_warning ("[%d] sid %d: current vep.et_mask = 0x%x", getpid (),
2597 session_index, session->vep.et_mask);
2598 session->vep.et_mask |= EPOLLIN;
2599 if (VPPCOM_DEBUG > 11)
2600 clib_warning ("[%d] sid %d: updated vep.et_mask = 0x%x", getpid (),
2601 session_index, session->vep.et_mask);
2604 if (session->vep.et_mask && (VPPCOM_DEBUG > 11))
2605 clib_warning ("[%d] sid %d, is_listen %u, peek %s (%p), ready = %d, "
2607 getpid (), session_index, session->is_listen,
2608 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2609 rx_fifo, ready, session->vep.et_mask);
2614 vppcom_session_write (uint32_t session_index, void *buf, int n)
2616 session_t *session = 0;
2617 svm_fifo_t *tx_fifo;
2618 unix_shared_memory_queue_t *q;
2619 session_fifo_event_t evt;
2626 clib_spinlock_lock (&vcm->sessions_lockp);
2627 rv = vppcom_session_at_index (session_index, &session);
2628 if (PREDICT_FALSE (rv))
2630 clib_spinlock_unlock (&vcm->sessions_lockp);
2631 if (VPPCOM_DEBUG > 0)
2632 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2633 getpid (), session_index);
2637 if (session->is_vep)
2639 clib_spinlock_unlock (&vcm->sessions_lockp);
2640 if (VPPCOM_DEBUG > 0)
2641 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2642 getpid (), session_index);
2643 return VPPCOM_EBADFD;
2646 if (session->state == STATE_DISCONNECT)
2648 clib_spinlock_unlock (&vcm->sessions_lockp);
2649 if (VPPCOM_DEBUG > 0)
2650 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2651 getpid (), session_index);
2652 return VPPCOM_ECONNRESET;
2655 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2656 session->server_tx_fifo : session->server_rx_fifo);
2657 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2658 "server_tx_fifo" : "server_rx_fifo");
2659 q = session->vpp_event_queue;
2660 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2661 (EPOLLET | EPOLLOUT));
2662 clib_spinlock_unlock (&vcm->sessions_lockp);
2666 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2668 while (!session->is_nonblocking && (n_write <= 0));
2670 /* If event wasn't set, add one */
2671 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2675 /* Fabricate TX event, send to vpp */
2677 evt.event_type = FIFO_EVENT_APP_TX;
2679 rval = vppcom_session_at_index (session_index, &session);
2680 if (PREDICT_FALSE (rval))
2682 if (VPPCOM_DEBUG > 1)
2683 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2684 getpid (), session_index);
2688 unix_shared_memory_queue_add (q, (u8 *) & evt,
2689 0 /* do wait for mutex */ );
2692 if (poll_et && (n_write <= 0))
2694 clib_spinlock_lock (&vcm->sessions_lockp);
2695 session->vep.et_mask |= EPOLLOUT;
2696 clib_spinlock_unlock (&vcm->sessions_lockp);
2699 if (VPPCOM_DEBUG > 2)
2702 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", getpid (),
2703 session_index, fifo_str, tx_fifo);
2705 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", getpid (),
2706 session_index, n_write, fifo_str, tx_fifo);
2708 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2712 vppcom_session_write_ready (session_t * session, u32 session_index)
2714 svm_fifo_t *tx_fifo;
2719 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2720 if (session->is_vep)
2722 clib_spinlock_unlock (&vcm->sessions_lockp);
2723 if (VPPCOM_DEBUG > 0)
2724 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2725 getpid (), session_index);
2726 return VPPCOM_EBADFD;
2729 if (session->state == STATE_DISCONNECT)
2731 if (VPPCOM_DEBUG > 0)
2732 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2733 getpid (), session_index);
2734 return VPPCOM_ECONNRESET;
2737 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2738 session->server_tx_fifo : session->server_rx_fifo);
2739 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2740 "server_tx_fifo" : "server_rx_fifo");
2742 ready = svm_fifo_max_enqueue (tx_fifo);
2744 if (VPPCOM_DEBUG > 3)
2745 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", getpid (),
2746 session_index, fifo_str, tx_fifo, ready);
2747 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2748 (EPOLLET | EPOLLOUT));
2749 if (poll_et && (ready == 0))
2750 session->vep.et_mask |= EPOLLOUT;
2756 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2757 unsigned long *write_map, unsigned long *except_map,
2758 double time_to_wait)
2761 session_t *session = 0;
2762 int rv, bits_set = 0;
2763 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2764 u32 minbits = clib_max (n_bits, BITS (uword));
2766 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2768 if (n_bits && read_map)
2770 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2771 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2772 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2774 if (n_bits && write_map)
2776 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2777 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2778 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2780 if (n_bits && except_map)
2782 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2783 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2784 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2794 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2796 clib_spinlock_lock (&vcm->sessions_lockp);
2797 rv = vppcom_session_at_index (session_index, &session);
2800 clib_spinlock_unlock (&vcm->sessions_lockp);
2801 if (VPPCOM_DEBUG > 1)
2802 clib_warning ("[%d] session %d specified in "
2803 "read_map is closed.", getpid (),
2805 bits_set = VPPCOM_EBADFD;
2809 rv = vppcom_session_read_ready (session, session_index);
2810 clib_spinlock_unlock (&vcm->sessions_lockp);
2811 if (except_map && vcm->ex_bitmap &&
2812 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2815 // TBD: clib_warning
2816 clib_bitmap_set_no_check (except_map, session_index, 1);
2821 // TBD: clib_warning
2822 clib_bitmap_set_no_check (read_map, session_index, 1);
2830 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2832 clib_spinlock_lock (&vcm->sessions_lockp);
2833 rv = vppcom_session_at_index (session_index, &session);
2836 clib_spinlock_unlock (&vcm->sessions_lockp);
2837 if (VPPCOM_DEBUG > 0)
2838 clib_warning ("[%d] session %d specified in "
2839 "write_map is closed.", getpid (),
2841 bits_set = VPPCOM_EBADFD;
2845 rv = vppcom_session_write_ready (session, session_index);
2846 clib_spinlock_unlock (&vcm->sessions_lockp);
2847 if (write_map && (rv > 0))
2849 // TBD: clib_warning
2850 clib_bitmap_set_no_check (write_map, session_index, 1);
2858 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2860 clib_spinlock_lock (&vcm->sessions_lockp);
2861 rv = vppcom_session_at_index (session_index, &session);
2864 clib_spinlock_unlock (&vcm->sessions_lockp);
2865 if (VPPCOM_DEBUG > 1)
2866 clib_warning ("[%d] session %d specified in "
2867 "except_map is closed.", getpid (),
2869 bits_set = VPPCOM_EBADFD;
2873 rv = vppcom_session_read_ready (session, session_index);
2874 clib_spinlock_unlock (&vcm->sessions_lockp);
2877 // TBD: clib_warning
2878 clib_bitmap_set_no_check (except_map, session_index, 1);
2886 while (clib_time_now (&vcm->clib_time) < timeout);
2893 vep_verify_epoll_chain (u32 vep_idx)
2896 vppcom_epoll_t *vep;
2900 if (VPPCOM_DEBUG <= 1)
2903 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2904 rv = vppcom_session_at_index (vep_idx, &session);
2905 if (PREDICT_FALSE (rv))
2907 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
2910 if (PREDICT_FALSE (!session->is_vep))
2912 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
2916 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2919 " is_vep_session = %u\n"
2920 " wait_cont_idx = 0x%x (%u)\n"
2922 vep_idx, session->is_vep, session->is_vep_session,
2923 session->wait_cont_idx, session->wait_cont_idx);
2926 vep = &session->vep;
2927 sid = vep->next_sid;
2930 rv = vppcom_session_at_index (sid, &session);
2931 if (PREDICT_FALSE (rv))
2933 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
2936 if (PREDICT_FALSE (session->is_vep))
2937 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
2938 getpid (), vep_idx);
2939 else if (PREDICT_FALSE (!session->is_vep_session))
2941 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
2945 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2946 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
2947 "vep_idx (%u)!", getpid (),
2948 sid, session->vep.vep_idx, vep_idx);
2949 if (session->is_vep_session)
2951 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2953 " next_sid = 0x%x (%u)\n"
2954 " prev_sid = 0x%x (%u)\n"
2955 " vep_idx = 0x%x (%u)\n"
2956 " ev.events = 0x%x\n"
2957 " ev.data.u64 = 0x%llx\n"
2961 vep->next_sid, vep->next_sid,
2962 vep->prev_sid, vep->prev_sid,
2963 vep->vep_idx, vep->vep_idx,
2964 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2971 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
2975 vppcom_epoll_create (void)
2977 session_t *vep_session;
2980 clib_spinlock_lock (&vcm->sessions_lockp);
2981 pool_get (vcm->sessions, vep_session);
2982 memset (vep_session, 0, sizeof (*vep_session));
2983 vep_idx = vep_session - vcm->sessions;
2985 vep_session->is_vep = 1;
2986 vep_session->vep.vep_idx = ~0;
2987 vep_session->vep.next_sid = ~0;
2988 vep_session->vep.prev_sid = ~0;
2989 vep_session->wait_cont_idx = ~0;
2990 clib_spinlock_unlock (&vcm->sessions_lockp);
2992 if (VPPCOM_DEBUG > 0)
2993 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
2999 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3000 struct epoll_event *event)
3002 session_t *vep_session;
3006 if (vep_idx == session_index)
3008 if (VPPCOM_DEBUG > 0)
3009 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3010 getpid (), vep_idx);
3011 return VPPCOM_EINVAL;
3014 clib_spinlock_lock (&vcm->sessions_lockp);
3015 rv = vppcom_session_at_index (vep_idx, &vep_session);
3016 if (PREDICT_FALSE (rv))
3018 if (VPPCOM_DEBUG > 0)
3019 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3022 if (PREDICT_FALSE (!vep_session->is_vep))
3024 if (VPPCOM_DEBUG > 0)
3025 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3026 getpid (), vep_idx);
3031 ASSERT (vep_session->vep.vep_idx == ~0);
3032 ASSERT (vep_session->vep.prev_sid == ~0);
3034 rv = vppcom_session_at_index (session_index, &session);
3035 if (PREDICT_FALSE (rv))
3037 if (VPPCOM_DEBUG > 0)
3038 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3039 getpid (), session_index);
3042 if (PREDICT_FALSE (session->is_vep))
3044 if (VPPCOM_DEBUG > 0)
3045 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3053 if (PREDICT_FALSE (!event))
3055 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3056 "epoll_event structure!", getpid ());
3060 if (vep_session->vep.next_sid != ~0)
3062 session_t *next_session;
3063 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3065 if (PREDICT_FALSE (rv))
3067 if (VPPCOM_DEBUG > 0)
3068 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3069 "vep.next_sid (%u) on vep_idx (%u)!",
3070 getpid (), vep_session->vep.next_sid, vep_idx);
3073 ASSERT (next_session->vep.prev_sid == vep_idx);
3074 next_session->vep.prev_sid = session_index;
3076 session->vep.next_sid = vep_session->vep.next_sid;
3077 session->vep.prev_sid = vep_idx;
3078 session->vep.vep_idx = vep_idx;
3079 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3080 session->vep.ev = *event;
3081 session->is_vep_session = 1;
3082 vep_session->vep.next_sid = session_index;
3083 if (VPPCOM_DEBUG > 1)
3084 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3085 " data 0x%llx!", getpid (), vep_idx, session_index,
3086 event->events, event->data.u64);
3090 if (PREDICT_FALSE (!event))
3092 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3093 "epoll_event structure!", getpid ());
3097 if (PREDICT_FALSE (!session->is_vep_session &&
3098 (session->vep.vep_idx != vep_idx)))
3100 if (VPPCOM_DEBUG > 0)
3102 if (!session->is_vep_session)
3103 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3104 "is not a vep session!",
3105 getpid (), session_index);
3107 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3108 "vep_idx (%u) != vep_idx (%u)!",
3109 getpid (), session_index,
3110 session->vep.vep_idx, vep_idx);
3115 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3116 session->vep.ev = *event;
3117 if (VPPCOM_DEBUG > 1)
3118 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3119 " data 0x%llx!", getpid (), vep_idx, session_index,
3120 event->events, event->data.u64);
3124 if (PREDICT_FALSE (!session->is_vep_session &&
3125 (session->vep.vep_idx != vep_idx)))
3127 if (VPPCOM_DEBUG > 0)
3129 if (!session->is_vep_session)
3130 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3131 "is not a vep session!",
3132 getpid (), session_index);
3134 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3135 "vep_idx (%u) != vep_idx (%u)!",
3136 getpid (), session_index,
3137 session->vep.vep_idx, vep_idx);
3143 vep_session->wait_cont_idx =
3144 (vep_session->wait_cont_idx == session_index) ?
3145 session->vep.next_sid : vep_session->wait_cont_idx;
3147 if (session->vep.prev_sid == vep_idx)
3148 vep_session->vep.next_sid = session->vep.next_sid;
3151 session_t *prev_session;
3152 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3153 if (PREDICT_FALSE (rv))
3155 if (VPPCOM_DEBUG > 0)
3156 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3157 "vep.prev_sid (%u) on sid (%u)!",
3158 getpid (), session->vep.prev_sid,
3162 ASSERT (prev_session->vep.next_sid == session_index);
3163 prev_session->vep.next_sid = session->vep.next_sid;
3165 if (session->vep.next_sid != ~0)
3167 session_t *next_session;
3168 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3169 if (PREDICT_FALSE (rv))
3171 if (VPPCOM_DEBUG > 0)
3172 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3173 "vep.next_sid (%u) on sid (%u)!",
3174 getpid (), session->vep.next_sid,
3178 ASSERT (next_session->vep.prev_sid == session_index);
3179 next_session->vep.prev_sid = session->vep.prev_sid;
3182 memset (&session->vep, 0, sizeof (session->vep));
3183 session->vep.next_sid = ~0;
3184 session->vep.prev_sid = ~0;
3185 session->vep.vep_idx = ~0;
3186 session->is_vep_session = 0;
3187 if (VPPCOM_DEBUG > 1)
3188 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3189 getpid (), vep_idx, session_index);
3193 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3197 vep_verify_epoll_chain (vep_idx);
3200 clib_spinlock_unlock (&vcm->sessions_lockp);
3205 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3206 int maxevents, double wait_for_time)
3208 session_t *vep_session;
3210 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3211 u32 keep_trying = 1;
3213 u32 vep_next_sid, wait_cont_idx;
3216 if (PREDICT_FALSE (maxevents <= 0))
3218 if (VPPCOM_DEBUG > 0)
3219 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3220 getpid (), maxevents);
3221 return VPPCOM_EINVAL;
3223 memset (events, 0, sizeof (*events) * maxevents);
3225 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3226 vep_next_sid = vep_session->vep.next_sid;
3227 is_vep = vep_session->is_vep;
3228 wait_cont_idx = vep_session->wait_cont_idx;
3229 clib_spinlock_unlock (&vcm->sessions_lockp);
3231 if (PREDICT_FALSE (!is_vep))
3233 if (VPPCOM_DEBUG > 0)
3234 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3235 getpid (), vep_idx);
3239 if (PREDICT_FALSE (vep_next_sid == ~0))
3241 if (VPPCOM_DEBUG > 0)
3242 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3243 getpid (), vep_idx);
3253 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3254 sid != ~0; sid = next_sid)
3256 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3257 u8 add_event, is_vep_session;
3259 u64 session_ev_data;
3261 VCL_LOCK_AND_GET_SESSION (sid, &session);
3262 next_sid = session->vep.next_sid;
3263 session_events = session->vep.ev.events;
3264 et_mask = session->vep.et_mask;
3265 is_vep = session->is_vep;
3266 is_vep_session = session->is_vep_session;
3267 session_vep_idx = session->vep.vep_idx;
3268 session_ev_data = session->vep.ev.data.u64;
3269 clib_spinlock_unlock (&vcm->sessions_lockp);
3271 if (PREDICT_FALSE (is_vep))
3273 if (VPPCOM_DEBUG > 0)
3274 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3275 getpid (), vep_idx);
3279 if (PREDICT_FALSE (!is_vep_session))
3281 if (VPPCOM_DEBUG > 0)
3282 clib_warning ("[%d] ERROR: session (%u) is not "
3283 "a vep session!", getpid (), sid);
3287 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3289 clib_warning ("[%d] ERROR: session (%u) "
3290 "vep_idx (%u) != vep_idx (%u)!",
3291 getpid (), sid, session->vep.vep_idx, vep_idx);
3296 add_event = clear_et_mask = 0;
3298 if (EPOLLIN & session_events)
3300 VCL_LOCK_AND_GET_SESSION (sid, &session);
3301 ready = vppcom_session_read_ready (session, sid);
3302 clib_spinlock_unlock (&vcm->sessions_lockp);
3303 if ((ready > 0) && (EPOLLIN & et_mask))
3306 events[num_ev].events |= EPOLLIN;
3307 if (((EPOLLET | EPOLLIN) & session_events) ==
3308 (EPOLLET | EPOLLIN))
3309 clear_et_mask |= EPOLLIN;
3316 case VPPCOM_ECONNRESET:
3317 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3321 events[num_ev].events |= EPOLLERR;
3327 if (EPOLLOUT & session_events)
3329 VCL_LOCK_AND_GET_SESSION (sid, &session);
3330 ready = vppcom_session_write_ready (session, sid);
3331 clib_spinlock_unlock (&vcm->sessions_lockp);
3332 if ((ready > 0) && (EPOLLOUT & et_mask))
3335 events[num_ev].events |= EPOLLOUT;
3336 if (((EPOLLET | EPOLLOUT) & session_events) ==
3337 (EPOLLET | EPOLLOUT))
3338 clear_et_mask |= EPOLLOUT;
3345 case VPPCOM_ECONNRESET:
3346 events[num_ev].events |= EPOLLHUP;
3350 events[num_ev].events |= EPOLLERR;
3358 events[num_ev].data.u64 = session_ev_data;
3359 if (EPOLLONESHOT & session_events)
3361 VCL_LOCK_AND_GET_SESSION (sid, &session);
3362 session->vep.ev.events = 0;
3363 clib_spinlock_unlock (&vcm->sessions_lockp);
3366 if (num_ev == maxevents)
3368 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3369 vep_session->wait_cont_idx = next_sid;
3370 clib_spinlock_unlock (&vcm->sessions_lockp);
3374 if (wait_cont_idx != ~0)
3377 next_sid = vep_next_sid;
3378 else if (next_sid == wait_cont_idx)
3382 if (wait_for_time != -1)
3383 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3385 while ((num_ev == 0) && keep_trying);
3387 if (wait_cont_idx != ~0)
3389 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3390 vep_session->wait_cont_idx = ~0;
3391 clib_spinlock_unlock (&vcm->sessions_lockp);
3394 return (rv != VPPCOM_OK) ? rv : num_ev;
3398 vppcom_session_attr (uint32_t session_index, uint32_t op,
3399 void *buffer, uint32_t * buflen)
3403 u32 *flags = buffer;
3404 vppcom_endpt_t *ep = buffer;
3406 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3409 case VPPCOM_ATTR_GET_NREAD:
3410 rv = vppcom_session_read_ready (session, session_index);
3411 if (VPPCOM_DEBUG > 1)
3412 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: nread = %d",
3417 case VPPCOM_ATTR_PEEK_NREAD:
3421 case VPPCOM_ATTR_GET_FLAGS:
3422 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3424 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3425 *buflen = sizeof (*flags);
3426 if (VPPCOM_DEBUG > 1)
3427 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3428 "is_nonblocking = %u", getpid (), *flags,
3429 session->is_nonblocking);
3435 case VPPCOM_ATTR_SET_FLAGS:
3436 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3438 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3439 if (VPPCOM_DEBUG > 1)
3440 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3441 "is_nonblocking = %u", getpid (), *flags,
3442 session->is_nonblocking);
3448 case VPPCOM_ATTR_GET_PEER_ADDR:
3449 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3451 ep->vrf = session->vrf;
3452 ep->is_ip4 = session->peer_addr.is_ip4;
3453 ep->port = session->peer_port;
3454 if (session->peer_addr.is_ip4)
3455 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3456 sizeof (ip4_address_t));
3458 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3459 sizeof (ip6_address_t));
3460 *buflen = sizeof (*ep);
3461 if (VPPCOM_DEBUG > 1)
3462 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = "
3463 "%u, addr = %U, port %u", getpid (),
3464 session_index, ep->is_ip4, format_ip46_address,
3465 &session->peer_addr.ip46, ep->is_ip4,
3466 clib_net_to_host_u16 (ep->port));
3472 case VPPCOM_ATTR_GET_LCL_ADDR:
3473 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3475 ep->vrf = session->vrf;
3476 ep->is_ip4 = session->lcl_addr.is_ip4;
3477 ep->port = session->lcl_port;
3478 if (session->lcl_addr.is_ip4)
3479 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3480 sizeof (ip4_address_t));
3482 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3483 sizeof (ip6_address_t));
3484 *buflen = sizeof (*ep);
3485 if (VPPCOM_DEBUG > 1)
3486 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = "
3487 "%u, addr = %U port %d", getpid (),
3488 session_index, ep->is_ip4, format_ip46_address,
3489 &session->lcl_addr.ip46, ep->is_ip4,
3490 clib_net_to_host_u16 (ep->port));
3496 case VPPCOM_ATTR_SET_REUSEADDR:
3499 case VPPCOM_ATTR_SET_BROADCAST:
3502 case VPPCOM_ATTR_SET_V6ONLY:
3505 case VPPCOM_ATTR_SET_KEEPALIVE:
3508 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3511 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3520 clib_spinlock_unlock (&vcm->sessions_lockp);
3525 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3526 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3529 session_t *session = 0;
3533 clib_spinlock_lock (&vcm->sessions_lockp);
3534 rv = vppcom_session_at_index (session_index, &session);
3535 if (PREDICT_FALSE (rv))
3537 clib_spinlock_unlock (&vcm->sessions_lockp);
3538 if (VPPCOM_DEBUG > 0)
3539 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3540 getpid (), session_index);
3542 clib_spinlock_unlock (&vcm->sessions_lockp);
3545 ep->vrf = session->vrf;
3546 ep->is_ip4 = session->peer_addr.is_ip4;
3547 ep->port = session->peer_port;
3548 if (session->peer_addr.is_ip4)
3549 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3550 sizeof (ip4_address_t));
3552 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3553 sizeof (ip6_address_t));
3554 clib_spinlock_unlock (&vcm->sessions_lockp);
3558 rv = vppcom_session_read (session_index, buffer, buflen);
3559 else if (flags & MSG_PEEK)
3560 rv = vppcom_session_peek (session_index, buffer, buflen);
3563 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3564 rv = VPPCOM_EAFNOSUPPORT;
3572 vppcom_session_sendto (uint32_t session_index, void *buffer,
3573 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3576 return VPPCOM_EINVAL;
3581 return VPPCOM_EINVAL;
3586 // TBD check the flags and do the right thing
3587 if (VPPCOM_DEBUG > 2)
3588 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3589 getpid (), flags, flags);
3592 return (vppcom_session_write (session_index, buffer, buflen));
3596 * fd.io coding-style-patch-verification: ON
3599 * eval: (c-set-style "gnu")