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.
73 STATE_CLOSE_ON_EMPTY = 0x10,
74 STATE_DISCONNECT = 0x20,
78 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
79 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
81 typedef struct epoll_event vppcom_epoll_event_t;
88 vppcom_epoll_event_t ev;
89 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
90 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
102 volatile session_state_t state;
104 svm_fifo_t *server_rx_fifo;
105 svm_fifo_t *server_tx_fifo;
110 unix_shared_memory_queue_t *vpp_event_queue;
112 /* Socket configuration state */
113 /* TBD: covert 'is_*' vars to bit in u8 flags; */
123 vppcom_ip46_t lcl_addr;
124 vppcom_ip46_t peer_addr;
125 u16 lcl_port; // network order
126 u16 peer_port; // network order
128 u64 client_queue_address;
132 typedef struct vppcom_cfg_t_
137 u32 add_segment_size;
138 u32 preallocated_fifo_pairs;
141 u32 event_queue_size;
142 u32 listen_queue_size;
143 u8 app_proxy_transport_tcp;
144 u8 app_proxy_transport_udp;
148 u64 namespace_secret;
154 typedef struct vppcom_main_t_
158 u32 *client_session_index_fifo;
161 /* vpe input queue */
162 unix_shared_memory_queue_t *vl_input_queue;
164 /* API client handle */
168 clib_spinlock_t sessions_lockp;
171 /* Hash table for disconnect processing */
172 uword *session_index_by_vpp_handles;
175 clib_bitmap_t *rd_bitmap;
176 clib_bitmap_t *wr_bitmap;
177 clib_bitmap_t *ex_bitmap;
179 /* Our event queue */
180 unix_shared_memory_queue_t *app_event_queue;
182 /* unique segment name counter */
183 u32 unique_segment_index;
185 /* For deadman timers */
186 clib_time_t clib_time;
188 /* State of the connection, shared between msg RX thread and main thread */
189 volatile app_state_t app_state;
193 /* VNET_API_ERROR_FOO -> "Foo" hash table */
194 uword *error_string_by_error_number;
197 /* NOTE: _vppcom_main is only used until the heap is allocated.
198 * Do not access it directly -- use vcm which will point to
199 * the heap allocated copy after init.
201 static vppcom_main_t _vppcom_main = {
202 .debug = VPPCOM_DEBUG_INIT,
203 .my_client_index = ~0
206 static vppcom_main_t *vcm = &_vppcom_main;
208 #define VCL_LOCK_AND_GET_SESSION(I, S) \
210 clib_spinlock_lock (&vcm->sessions_lockp); \
211 rv = vppcom_session_at_index (I, S); \
212 if (PREDICT_FALSE (rv)) \
214 clib_spinlock_unlock (&vcm->sessions_lockp); \
215 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_CLOSE_ON_EMPTY:
276 st = "STATE_CLOSE_ON_EMPTY";
279 case STATE_DISCONNECT:
280 st = "STATE_DISCONNECT";
288 st = "UNKNOWN_STATE";
296 * VPPCOM Utility Functions
299 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
301 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
302 if (PREDICT_FALSE ((session_index == ~0) ||
303 pool_is_free_index (vcm->sessions, session_index)))
305 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
306 getpid (), session_index);
307 return VPPCOM_EBADFD;
309 *sess = pool_elt_at_index (vcm->sessions, session_index);
314 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
316 /* Session and listener handles have different formats. The latter has
317 * the thread index in the upper 32 bits while the former has the session
318 * type. Knowing that, for listeners we just flip the MSB to 1 */
319 listener_handle |= 1ULL << 63;
320 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
323 static inline session_t *
324 vppcom_session_table_lookup_listener (u64 listener_handle)
327 u64 handle = listener_handle | (1ULL << 63);
330 p = hash_get (vcm->session_index_by_vpp_handles, handle);
333 clib_warning ("[%d] couldn't find listen session: unknown vpp "
334 "listener handle %llx", getpid (), listener_handle);
337 if (pool_is_free_index (vcm->sessions, p[0]))
339 if (VPPCOM_DEBUG > 1)
340 clib_warning ("[%d] invalid listen session, sid (%u)", getpid (),
345 session = pool_elt_at_index (vcm->sessions, p[0]);
346 ASSERT (session->is_listen);
351 vppcom_session_table_del_listener (u64 listener_handle)
353 listener_handle |= 1ULL << 63;
354 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
358 vppcom_connect_to_vpp (char *app_name)
360 api_main_t *am = &api_main;
362 if (VPPCOM_DEBUG > 0)
363 printf ("\nConnecting to VPP api...");
364 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
366 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
367 return VPPCOM_ECONNREFUSED;
370 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
371 vcm->my_client_index = am->my_client_index;
372 if (VPPCOM_DEBUG > 0)
373 printf (" connected!\n");
375 vcm->app_state = STATE_APP_CONN_VPP;
380 format_api_error (u8 * s, va_list * args)
382 i32 error = va_arg (*args, u32);
385 p = hash_get (vcm->error_string_by_error_number, -error);
388 s = format (s, "%s (%d)", p[0], error);
390 s = format (s, "%d", error);
395 vppcom_init_error_string_table (void)
397 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
399 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
400 foreach_vnet_api_error;
403 hash_set (vcm->error_string_by_error_number, 99, "Misc");
407 vppcom_wait_for_app_state_change (app_state_t app_state)
409 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
411 while (clib_time_now (&vcm->clib_time) < timeout)
413 if (vcm->app_state == app_state)
416 if (VPPCOM_DEBUG > 0)
417 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
418 vppcom_app_state_str (app_state), app_state);
419 return VPPCOM_ETIMEDOUT;
423 vppcom_wait_for_session_state_change (u32 session_index,
424 session_state_t state,
427 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
428 session_t *volatile session;
433 clib_spinlock_lock (&vcm->sessions_lockp);
434 rv = vppcom_session_at_index (session_index, &session);
435 if (PREDICT_FALSE (rv))
437 clib_spinlock_unlock (&vcm->sessions_lockp);
440 if (session->state == state)
442 clib_spinlock_unlock (&vcm->sessions_lockp);
445 if (session->state == STATE_FAILED)
447 clib_spinlock_unlock (&vcm->sessions_lockp);
448 return VPPCOM_ECONNREFUSED;
451 clib_spinlock_unlock (&vcm->sessions_lockp);
453 while (clib_time_now (&vcm->clib_time) < timeout);
455 if (VPPCOM_DEBUG > 0)
456 clib_warning ("[%d] timeout waiting for state 0x%x (%s)", getpid (),
457 state, vppcom_session_state_str (state));
458 return VPPCOM_ETIMEDOUT;
462 vppcom_wait_for_client_session_index (f64 wait_for_time)
464 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
468 if (clib_fifo_elts (vcm->client_session_index_fifo))
471 while (clib_time_now (&vcm->clib_time) < timeout);
473 if (wait_for_time == 0)
474 return VPPCOM_EAGAIN;
476 if (VPPCOM_DEBUG > 0)
477 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
478 return VPPCOM_ETIMEDOUT;
482 * VPP-API message functions
485 vppcom_send_session_enable_disable (u8 is_enable)
487 vl_api_session_enable_disable_t *bmp;
488 bmp = vl_msg_api_alloc (sizeof (*bmp));
489 memset (bmp, 0, sizeof (*bmp));
491 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
492 bmp->client_index = vcm->my_client_index;
493 bmp->context = htonl (0xfeedface);
494 bmp->is_enable = is_enable;
495 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
499 vppcom_app_session_enable (void)
503 if (vcm->app_state != STATE_APP_ENABLED)
505 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
506 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
507 if (PREDICT_FALSE (rv))
509 if (VPPCOM_DEBUG > 0)
510 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
511 getpid (), vppcom_retval_str (rv), rv);
519 vl_api_session_enable_disable_reply_t_handler
520 (vl_api_session_enable_disable_reply_t * mp)
524 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
525 format_api_error, ntohl (mp->retval));
528 vcm->app_state = STATE_APP_ENABLED;
532 vppcom_app_send_attach (void)
534 vl_api_application_attach_t *bmp;
535 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
536 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
537 vcm->cfg.app_proxy_transport_udp);
539 bmp = vl_msg_api_alloc (sizeof (*bmp));
540 memset (bmp, 0, sizeof (*bmp));
542 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
543 bmp->client_index = vcm->my_client_index;
544 bmp->context = htonl (0xfeedface);
545 bmp->options[APP_OPTIONS_FLAGS] =
546 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
547 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
548 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
549 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
550 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
551 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
552 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
553 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
554 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
555 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
556 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
559 bmp->namespace_id_len = nsid_len;
560 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
561 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
563 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
567 vppcom_app_attach (void)
571 vppcom_app_send_attach ();
572 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
573 if (PREDICT_FALSE (rv))
575 if (VPPCOM_DEBUG > 0)
576 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
577 getpid (), vppcom_retval_str (rv), rv);
584 vppcom_app_detach (void)
586 vl_api_application_detach_t *bmp;
587 bmp = vl_msg_api_alloc (sizeof (*bmp));
588 memset (bmp, 0, sizeof (*bmp));
590 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
591 bmp->client_index = vcm->my_client_index;
592 bmp->context = htonl (0xfeedface);
593 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
597 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
600 static svm_fifo_segment_create_args_t _a;
601 svm_fifo_segment_create_args_t *a = &_a;
604 memset (a, 0, sizeof (*a));
607 clib_warning ("[%d] attach failed: %U", getpid (),
608 format_api_error, ntohl (mp->retval));
612 if (mp->segment_name_length == 0)
614 clib_warning ("[%d] segment_name_length zero", getpid ());
618 a->segment_name = (char *) mp->segment_name;
619 a->segment_size = mp->segment_size;
621 ASSERT (mp->app_event_queue_address);
623 /* Attach to the segment vpp created */
624 rv = svm_fifo_segment_attach (a);
625 vec_reset_length (a->new_segment_indices);
626 if (PREDICT_FALSE (rv))
628 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
633 vcm->app_event_queue =
634 uword_to_pointer (mp->app_event_queue_address,
635 unix_shared_memory_queue_t *);
637 vcm->app_state = STATE_APP_ATTACHED;
641 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
645 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
648 vcm->app_state = STATE_APP_ENABLED;
652 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
656 clib_warning ("[%d] vpp handle 0x%llx: disconnect session failed: %U",
657 getpid (), mp->handle, format_api_error,
662 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
664 static svm_fifo_segment_create_args_t _a;
665 svm_fifo_segment_create_args_t *a = &_a;
668 memset (a, 0, sizeof (*a));
669 a->segment_name = (char *) mp->segment_name;
670 a->segment_size = mp->segment_size;
671 /* Attach to the segment vpp created */
672 rv = svm_fifo_segment_attach (a);
673 vec_reset_length (a->new_segment_indices);
674 if (PREDICT_FALSE (rv))
676 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
677 getpid (), mp->segment_name);
680 if (VPPCOM_DEBUG > 1)
681 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
682 mp->segment_name, mp->segment_size);
686 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
690 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
694 session_t *session = 0;
695 u32 session_index = p[0];
697 VCL_LOCK_AND_GET_SESSION (session_index, &session);
698 session->state = STATE_CLOSE_ON_EMPTY;
700 if (VPPCOM_DEBUG > 1)
701 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
702 "setting state to %d (%s)",
703 getpid (), mp->handle, session_index, session->state,
704 vppcom_session_state_str (session->state));
705 clib_spinlock_unlock (&vcm->sessions_lockp);
709 if (VPPCOM_DEBUG > 1)
710 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
711 "session lookup failed!",
712 getpid (), mp->handle, session_index);
715 clib_warning ("[%d] vpp handle 0x%llx: session lookup by "
716 "handle failed!", getpid (), mp->handle);
720 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
722 session_t *session = 0;
723 vl_api_reset_session_reply_t *rmp;
727 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
731 clib_spinlock_lock (&vcm->sessions_lockp);
732 rval = vppcom_session_at_index (p[0], &session);
733 if (PREDICT_FALSE (rval))
735 rv = VNET_API_ERROR_INVALID_VALUE_2;
736 clib_warning ("[%d] ERROR: sid %u: session lookup failed! "
738 getpid (), p[0], rv, format_api_error, rv);
742 /* TBD: should this disconnect immediately and
745 session->state = STATE_CLOSE_ON_EMPTY;
747 if (VPPCOM_DEBUG > 1)
748 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
749 "state set to %d (%s)!", getpid (),
750 mp->handle, p[0], session->state,
751 vppcom_session_state_str (session->state));
753 clib_spinlock_unlock (&vcm->sessions_lockp);
757 rv = VNET_API_ERROR_INVALID_VALUE;
758 clib_warning ("[%d] ERROR: vpp handle 0x%llx: session lookup "
759 "failed! returning %d %U",
760 getpid (), mp->handle, rv, format_api_error, rv);
763 rmp = vl_msg_api_alloc (sizeof (*rmp));
764 memset (rmp, 0, sizeof (*rmp));
765 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
766 rmp->retval = htonl (rv);
767 rmp->handle = mp->handle;
768 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
772 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
776 svm_fifo_t *rx_fifo, *tx_fifo;
780 session_index = mp->context;
783 clib_warning ("[%d] ERROR: sid %u: connect failed! %U",
784 getpid (), session_index, format_api_error,
789 if (VPPCOM_DEBUG > 1)
790 clib_warning ("[%d] sid %u: got a connect reply!",
791 getpid (), session_index);
793 clib_spinlock_lock (&vcm->sessions_lockp);
794 if (pool_is_free_index (vcm->sessions, session_index))
796 clib_spinlock_unlock (&vcm->sessions_lockp);
797 if (VPPCOM_DEBUG > 1)
798 clib_warning ("[%d] sid %d: session lookup failed!",
799 getpid (), session_index);
803 /* We've been redirected */
804 if (mp->segment_name_length > 0)
806 static svm_fifo_segment_create_args_t _a;
807 svm_fifo_segment_create_args_t *a = &_a;
810 memset (a, 0, sizeof (*a));
811 a->segment_name = (char *) mp->segment_name;
812 if (VPPCOM_DEBUG > 1)
813 clib_warning ("[%d] cut-thru segment: %s\n",
814 getpid (), a->segment_name);
816 rv = svm_fifo_segment_attach (a);
817 vec_reset_length (a->new_segment_indices);
818 if (PREDICT_FALSE (rv))
820 clib_spinlock_unlock (&vcm->sessions_lockp);
821 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
822 getpid (), a->segment_name);
830 session = pool_elt_at_index (vcm->sessions, session_index);
831 session->is_cut_thru = is_cut_thru;
832 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
833 unix_shared_memory_queue_t *);
835 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
836 rx_fifo->client_session_index = session_index;
837 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
838 tx_fifo->client_session_index = session_index;
840 session->server_rx_fifo = rx_fifo;
841 session->server_tx_fifo = tx_fifo;
842 session->vpp_handle = mp->handle;
843 session->lcl_addr.is_ip4 = mp->is_ip4;
844 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
845 sizeof (session->peer_addr.ip46));
846 session->lcl_port = mp->lcl_port;
847 session->state = STATE_CONNECT;
849 /* Add it to lookup table */
850 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
852 if (VPPCOM_DEBUG > 1)
853 clib_warning ("[%d] client sid %d, vpp handle 0x%llx\n"
854 " session_rx_fifo %p, refcnt %d\n"
855 " session_tx_fifo %p, refcnt %d",
856 getpid (), session_index, mp->handle,
857 session->server_rx_fifo,
858 session->server_rx_fifo->refcnt,
859 session->server_tx_fifo, session->server_tx_fifo->refcnt);
861 clib_spinlock_unlock (&vcm->sessions_lockp);
865 vppcom_send_connect_sock (session_t * session, u32 session_index)
867 vl_api_connect_sock_t *cmp;
869 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
870 session->is_server = 0;
871 cmp = vl_msg_api_alloc (sizeof (*cmp));
872 memset (cmp, 0, sizeof (*cmp));
873 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
874 cmp->client_index = vcm->my_client_index;
875 cmp->context = session_index;
877 cmp->vrf = session->vrf;
878 cmp->is_ip4 = session->peer_addr.is_ip4;
879 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
880 cmp->port = session->peer_port;
881 cmp->proto = session->proto;
882 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
883 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
887 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
890 vl_api_disconnect_session_reply_t *rmp;
892 if (VPPCOM_DEBUG > 1)
893 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
894 getpid (), vpp_handle, session_index);
896 rmp = vl_msg_api_alloc (sizeof (*rmp));
897 memset (rmp, 0, sizeof (*rmp));
899 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
900 rmp->retval = htonl (rv);
901 rmp->handle = vpp_handle;
902 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
906 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
908 vl_api_disconnect_session_t *dmp;
910 if (VPPCOM_DEBUG > 1)
911 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
912 getpid (), vpp_handle, session_index);
914 dmp = vl_msg_api_alloc (sizeof (*dmp));
915 memset (dmp, 0, sizeof (*dmp));
916 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
917 dmp->client_index = vcm->my_client_index;
918 dmp->handle = vpp_handle;
919 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
923 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
925 session_t *session = 0;
926 u32 session_index = mp->context;
931 clib_warning ("[%d] ERROR: sid %u: bind failed: %U",
932 getpid (), session_index, format_api_error,
934 rv = vppcom_session_at_index (session_index, &session);
936 session->state = STATE_FAILED;
940 clib_spinlock_lock (&vcm->sessions_lockp);
941 rv = vppcom_session_at_index (session_index, &session);
944 session->vpp_handle = mp->handle;
945 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
946 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
947 sizeof (session->peer_addr.ip46));
948 session->lcl_port = mp->lcl_port;
949 vppcom_session_table_add_listener (mp->handle, session_index);
950 session->is_listen = 1;
951 session->state = STATE_LISTEN;
953 if (VPPCOM_DEBUG > 1)
954 clib_warning ("[%d] sid %u: bind succeeded! ", getpid (),
957 clib_spinlock_unlock (&vcm->sessions_lockp);
961 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
964 clib_warning ("[%d] ERROR: sid %u: unbind failed: %U",
965 getpid (), mp->context, format_api_error,
968 else if (VPPCOM_DEBUG > 1)
969 clib_warning ("[%d] sid %u: unbind succeeded!", getpid (), mp->context);
973 format_ip4_address (u8 * s, va_list * args)
975 u8 *a = va_arg (*args, u8 *);
976 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
980 format_ip6_address (u8 * s, va_list * args)
982 ip6_address_t *a = va_arg (*args, ip6_address_t *);
983 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
985 i_max_n_zero = ARRAY_LEN (a->as_u16);
987 i_first_zero = i_max_n_zero;
989 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
991 u32 is_zero = a->as_u16[i] == 0;
992 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
998 if ((!is_zero && n_zeros > max_n_zeros)
999 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1001 i_max_n_zero = i_first_zero;
1002 max_n_zeros = n_zeros;
1003 i_first_zero = ARRAY_LEN (a->as_u16);
1008 last_double_colon = 0;
1009 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1011 if (i == i_max_n_zero && max_n_zeros > 1)
1013 s = format (s, "::");
1014 i += max_n_zeros - 1;
1015 last_double_colon = 1;
1019 s = format (s, "%s%x",
1020 (last_double_colon || i == 0) ? "" : ":",
1021 clib_net_to_host_u16 (a->as_u16[i]));
1022 last_double_colon = 0;
1029 /* Format an IP46 address. */
1031 format_ip46_address (u8 * s, va_list * args)
1033 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1034 ip46_type_t type = va_arg (*args, ip46_type_t);
1040 is_ip4 = ip46_address_is_ip4 (ip46);
1051 format (s, "%U", format_ip4_address, &ip46->ip4) :
1052 format (s, "%U", format_ip6_address, &ip46->ip6);
1056 vppcom_send_accept_session_reply (u32 handle, int retval)
1058 vl_api_accept_session_reply_t *rmp;
1060 rmp = vl_msg_api_alloc (sizeof (*rmp));
1061 memset (rmp, 0, sizeof (*rmp));
1062 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1063 rmp->retval = htonl (retval);
1064 rmp->handle = handle;
1065 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1069 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1071 svm_fifo_t *rx_fifo, *tx_fifo;
1072 session_t *session, *listen_session;
1075 clib_spinlock_lock (&vcm->sessions_lockp);
1076 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1078 clib_warning ("[%d] client session queue is full!", getpid ());
1079 vppcom_send_accept_session_reply (mp->handle,
1080 VNET_API_ERROR_QUEUE_FULL);
1081 clib_spinlock_unlock (&vcm->sessions_lockp);
1085 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1086 if (!listen_session)
1088 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1089 "listener handle %llx", getpid (), mp->listener_handle);
1090 clib_spinlock_unlock (&vcm->sessions_lockp);
1094 /* Allocate local session and set it up */
1095 pool_get (vcm->sessions, session);
1096 memset (session, 0, sizeof (*session));
1097 session_index = session - vcm->sessions;
1099 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1100 rx_fifo->client_session_index = session_index;
1101 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1102 tx_fifo->client_session_index = session_index;
1104 session->vpp_handle = mp->handle;
1105 session->server_rx_fifo = rx_fifo;
1106 session->server_tx_fifo = tx_fifo;
1107 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1108 unix_shared_memory_queue_t *);
1109 session->state = STATE_ACCEPT;
1110 session->is_cut_thru = 0;
1111 session->is_server = 1;
1112 session->peer_port = mp->port;
1113 session->peer_addr.is_ip4 = mp->is_ip4;
1114 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1115 sizeof (session->peer_addr.ip46));
1117 /* Add it to lookup table */
1118 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1119 session->lcl_port = listen_session->lcl_port;
1120 session->lcl_addr = listen_session->lcl_addr;
1122 /* TBD: move client_session_index_fifo into listener session */
1123 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1125 clib_spinlock_unlock (&vcm->sessions_lockp);
1127 if (VPPCOM_DEBUG > 1)
1129 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1130 clib_warning ("[%d] received request to accept session (sid %d) "
1131 "from %s:%d", getpid (), session_index, ip_str,
1132 clib_net_to_host_u16 (mp->port));
1138 vppcom_send_connect_session_reply (session_t * session, int retval)
1140 vl_api_connect_session_reply_t *rmp;
1142 unix_shared_memory_queue_t *client_q;
1144 rmp = vl_msg_api_alloc (sizeof (*rmp));
1145 memset (rmp, 0, sizeof (*rmp));
1147 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1148 rmp->context = session->client_context;
1149 rmp->retval = htonl (retval);
1150 rmp->handle = session->vpp_handle;
1151 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1152 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1153 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1154 rmp->segment_size = vcm->cfg.segment_size;
1155 len = vec_len (session->segment_name);
1156 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1157 clib_memcpy (rmp->segment_name, session->segment_name,
1158 rmp->segment_name_length - 1);
1159 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1160 sizeof (rmp->lcl_ip));
1161 rmp->is_ip4 = session->peer_addr.is_ip4;
1162 rmp->lcl_port = session->peer_port;
1163 client_q = uword_to_pointer (session->client_queue_address,
1164 unix_shared_memory_queue_t *);
1166 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1170 * Acting as server for redirected connect requests
1173 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1176 session_t *session = 0;
1178 clib_spinlock_lock (&vcm->sessions_lockp);
1179 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1181 clib_spinlock_unlock (&vcm->sessions_lockp);
1183 if (VPPCOM_DEBUG > 1)
1184 clib_warning ("[%d] client session queue is full!", getpid ());
1186 /* TBD: fix handle missing in api msg. */
1187 vppcom_send_accept_session_reply (0, VNET_API_ERROR_QUEUE_FULL);
1191 pool_get (vcm->sessions, session);
1192 memset (session, 0, sizeof (*session));
1193 session_index = session - vcm->sessions;
1195 session->client_context = mp->context;
1196 session->vpp_handle = session_index;
1197 session->client_queue_address = mp->client_queue_address;
1198 session->is_cut_thru = 1;
1199 session->is_server = 1;
1200 session->lcl_port = mp->port;
1201 session->lcl_addr.is_ip4 = mp->is_ip4;
1202 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1203 sizeof (session->lcl_addr.ip46));
1205 /* TBD: missing peer info in api msg.
1207 session->peer_addr.is_ip4 = mp->is_ip4;
1208 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1210 session->state = STATE_ACCEPT;
1211 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1212 if (VPPCOM_DEBUG > 1)
1213 clib_warning ("[%d] sid %u: Got a cut-thru connect request! "
1214 "clib_fifo_elts %u!\n", getpid (), session_index,
1215 clib_fifo_elts (vcm->client_session_index_fifo));
1216 clib_spinlock_unlock (&vcm->sessions_lockp);
1220 vppcom_send_bind_sock (session_t * session, u32 session_index)
1222 vl_api_bind_sock_t *bmp;
1224 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1225 session->is_server = 1;
1226 bmp = vl_msg_api_alloc (sizeof (*bmp));
1227 memset (bmp, 0, sizeof (*bmp));
1229 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1230 bmp->client_index = vcm->my_client_index;
1231 bmp->context = session_index;
1232 bmp->vrf = session->vrf;
1233 bmp->is_ip4 = session->lcl_addr.is_ip4;
1234 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1235 bmp->port = session->lcl_port;
1236 bmp->proto = session->proto;
1237 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1238 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1242 vppcom_send_unbind_sock (u64 vpp_handle)
1244 vl_api_unbind_sock_t *ump;
1246 ump = vl_msg_api_alloc (sizeof (*ump));
1247 memset (ump, 0, sizeof (*ump));
1249 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1250 ump->client_index = vcm->my_client_index;
1251 ump->handle = vpp_handle;
1252 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1256 vppcom_session_unbind (u32 session_index)
1258 session_t *session = 0;
1262 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1264 vpp_handle = session->vpp_handle;
1265 vppcom_session_table_del_listener (vpp_handle);
1266 session->vpp_handle = ~0;
1267 session->state = STATE_DISCONNECT;
1269 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 if (VPPCOM_DEBUG > 1)
1272 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1273 "sending unbind msg! new state 0x%x (%s)",
1274 getpid (), vpp_handle, session_index,
1275 session->state, vppcom_session_state_str (session->state));
1277 vppcom_send_unbind_sock (vpp_handle);
1284 vppcom_session_disconnect (u32 session_index)
1288 u8 is_cut_thru, is_listen, is_server;
1290 session_state_t state;
1292 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1294 vpp_handle = session->vpp_handle;
1295 is_server = session->is_server;
1296 is_listen = session->is_listen;
1297 is_cut_thru = session->is_cut_thru;
1298 state = session->state;
1299 clib_spinlock_unlock (&vcm->sessions_lockp);
1301 if (VPPCOM_DEBUG > 1)
1303 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s state 0x%x (%s), "
1304 "is_cut_thru %d, is_listen %d",
1305 getpid (), vpp_handle, session_index,
1306 is_server ? "server" : "client",
1307 state, vppcom_session_state_str (state),
1308 is_cut_thru, is_listen);
1311 if (PREDICT_FALSE (is_listen))
1313 clib_warning ("[%d] ERROR: sid %u: Cannot disconnect a "
1314 "listen socket!", getpid (), session_index);
1319 /* Through the VPP host stack...
1321 else if (!is_cut_thru)
1323 /* The peer has already initiated the close,
1324 * so send the disconnect session reply.
1326 if (state & STATE_CLOSE_ON_EMPTY)
1328 vppcom_send_disconnect_session_reply (vpp_handle,
1329 session_index, 0 /* rv */ );
1330 if (VPPCOM_DEBUG > 1)
1331 clib_warning ("[%d] sid %u: sending disconnect REPLY...",
1332 getpid (), session_index);
1335 /* Otherwise, send a disconnect session msg...
1339 if (VPPCOM_DEBUG > 1)
1340 clib_warning ("[%d] sid %u: sending disconnect...",
1341 getpid (), session_index, state,
1342 vppcom_session_state_str (state));
1344 vppcom_send_disconnect_session (vpp_handle, session_index);
1348 /* Cut-thru connections...
1350 * server: free fifos and segment allocated during connect/redirect
1351 * client: no cleanup required
1357 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1358 svm_fifo_segment_private_t *seg;
1360 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1362 if (VPPCOM_DEBUG > 1)
1363 clib_warning ("[%d] sid %d: freeing cut-thru fifos in "
1365 " server_rx_fifo %p, refcnt = %d\n"
1366 " server_tx_fifo %p, refcnt = %d",
1367 getpid (), session_index, session->sm_seg_index,
1368 session->server_rx_fifo,
1369 session->server_rx_fifo->refcnt,
1370 session->server_tx_fifo,
1371 session->server_tx_fifo->refcnt);
1373 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1374 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1375 FIFO_SEGMENT_RX_FREELIST);
1376 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1377 FIFO_SEGMENT_TX_FREELIST);
1378 svm_fifo_segment_delete (seg);
1380 /* TBD: Send cut-thru disconnect event to client */
1382 clib_spinlock_unlock (&vcm->sessions_lockp);
1386 /* TBD: Send cut-thru disconnect event to server */
1394 #define foreach_sock_msg \
1395 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1396 _(BIND_SOCK_REPLY, bind_sock_reply) \
1397 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1398 _(ACCEPT_SESSION, accept_session) \
1399 _(CONNECT_SOCK, connect_sock) \
1400 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1401 _(DISCONNECT_SESSION, disconnect_session) \
1402 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1403 _(RESET_SESSION, reset_session) \
1404 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1405 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1406 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1409 vppcom_api_hookup (void)
1412 vl_msg_api_set_handlers(VL_API_##N, #n, \
1413 vl_api_##n##_t_handler, \
1415 vl_api_##n##_t_endian, \
1416 vl_api_##n##_t_print, \
1417 sizeof(vl_api_##n##_t), 1);
1423 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1427 vcl_cfg->heapsize = (256ULL << 20);
1428 vcl_cfg->segment_baseva = 0x200000000ULL;
1429 vcl_cfg->segment_size = (256 << 20);
1430 vcl_cfg->add_segment_size = (128 << 20);
1431 vcl_cfg->preallocated_fifo_pairs = 8;
1432 vcl_cfg->rx_fifo_size = (1 << 20);
1433 vcl_cfg->tx_fifo_size = (1 << 20);
1434 vcl_cfg->event_queue_size = 2048;
1435 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1436 vcl_cfg->app_timeout = 10 * 60.0;
1437 vcl_cfg->session_timeout = 10 * 60.0;
1438 vcl_cfg->accept_timeout = 60.0;
1442 vppcom_cfg_heapsize (char *conf_fname)
1444 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1457 fp = fopen (conf_fname, "r");
1460 if (VPPCOM_DEBUG > 0)
1461 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1464 argv = calloc (1, sizeof (char *));
1470 if (fgets (inbuf, 4096, fp) == 0)
1472 p = strtok (inbuf, " \t\n");
1478 char **tmp = realloc (argv, argc * sizeof (char *));
1482 arg = strndup (p, 1024);
1485 argv[argc - 1] = arg;
1486 p = strtok (NULL, " \t\n");
1493 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1500 * Look for and parse the "heapsize" config parameter.
1501 * Manual since none of the clib infra has been bootstrapped yet.
1503 * Format: heapsize <nn>[mM][gG]
1506 for (i = 1; i < (argc - 1); i++)
1508 if (!strncmp (argv[i], "heapsize", 8))
1510 sizep = (u8 *) argv[i + 1];
1512 while (*sizep >= '0' && *sizep <= '9')
1515 size += *sizep++ - '0';
1519 if (VPPCOM_DEBUG > 0)
1520 clib_warning ("[%d] parse error '%s %s', "
1521 "using default heapsize %lld (0x%llx)",
1522 getpid (), argv[i], argv[i + 1],
1523 vcl_cfg->heapsize, vcl_cfg->heapsize);
1527 if (*sizep == 'g' || *sizep == 'G')
1528 vcl_cfg->heapsize = size << 30;
1529 else if (*sizep == 'm' || *sizep == 'M')
1530 vcl_cfg->heapsize = size << 20;
1533 if (VPPCOM_DEBUG > 0)
1534 clib_warning ("[%d] parse error '%s %s', "
1535 "using default heapsize %lld (0x%llx)",
1536 getpid (), argv[i], argv[i + 1],
1537 vcl_cfg->heapsize, vcl_cfg->heapsize);
1549 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1550 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1551 if (vcl_mem == MAP_FAILED)
1553 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1554 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1556 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1559 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1562 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1565 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1568 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1572 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1575 if (VPPCOM_DEBUG > 0)
1576 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1577 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1581 vppcom_cfg_read (char *conf_fname)
1583 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1585 unformat_input_t _input, *input = &_input;
1586 unformat_input_t _line_input, *line_input = &_line_input;
1587 u8 vc_cfg_input = 0;
1592 fd = open (conf_fname, O_RDONLY);
1595 if (VPPCOM_DEBUG > 0)
1596 clib_warning ("[%d] open configuration file '%s' failed!",
1597 getpid (), conf_fname);
1601 if (fstat (fd, &s) < 0)
1603 if (VPPCOM_DEBUG > 0)
1604 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1608 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1610 if (VPPCOM_DEBUG > 0)
1611 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1615 unformat_init_clib_file (input, fd);
1617 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1619 (void) unformat_user (input, unformat_line_input, line_input);
1620 unformat_skip_white_space (line_input);
1622 if (unformat (line_input, "vcl {"))
1630 if (unformat (line_input, "heapsize %s", &chroot_path))
1632 vec_terminate_c_string (chroot_path);
1633 if (VPPCOM_DEBUG > 0)
1634 clib_warning ("[%d] configured heapsize %s, "
1635 "actual heapsize %lld (0x%llx)",
1636 getpid (), chroot_path, vcl_cfg->heapsize,
1638 vec_free (chroot_path);
1640 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1642 vec_terminate_c_string (chroot_path);
1643 vl_set_memory_root_path ((char *) chroot_path);
1644 if (VPPCOM_DEBUG > 0)
1645 clib_warning ("[%d] configured api-prefix %s",
1646 getpid (), chroot_path);
1647 chroot_path = 0; /* Don't vec_free() it! */
1649 else if (unformat (line_input, "uid %d", &uid))
1651 vl_set_memory_uid (uid);
1652 if (VPPCOM_DEBUG > 0)
1653 clib_warning ("[%d] configured uid %d", getpid (), uid);
1655 else if (unformat (line_input, "gid %d", &gid))
1657 vl_set_memory_gid (gid);
1658 if (VPPCOM_DEBUG > 0)
1659 clib_warning ("[%d] configured gid %d", getpid (), gid);
1661 else if (unformat (line_input, "segment-baseva 0x%lx",
1662 &vcl_cfg->segment_baseva))
1664 if (VPPCOM_DEBUG > 0)
1665 clib_warning ("[%d] configured segment_baseva 0x%lx",
1666 getpid (), vcl_cfg->segment_baseva);
1668 else if (unformat (line_input, "segment-size 0x%lx",
1669 &vcl_cfg->segment_size))
1671 if (VPPCOM_DEBUG > 0)
1672 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1673 getpid (), vcl_cfg->segment_size,
1674 vcl_cfg->segment_size);
1676 else if (unformat (line_input, "segment-size %ld",
1677 &vcl_cfg->segment_size))
1679 if (VPPCOM_DEBUG > 0)
1680 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1681 getpid (), vcl_cfg->segment_size,
1682 vcl_cfg->segment_size);
1684 else if (unformat (line_input, "add-segment-size 0x%lx",
1685 &vcl_cfg->add_segment_size))
1687 if (VPPCOM_DEBUG > 0)
1689 ("[%d] configured add_segment_size 0x%lx (%ld)",
1690 getpid (), vcl_cfg->add_segment_size,
1691 vcl_cfg->add_segment_size);
1693 else if (unformat (line_input, "add-segment-size %ld",
1694 &vcl_cfg->add_segment_size))
1696 if (VPPCOM_DEBUG > 0)
1698 ("[%d] configured add_segment_size %ld (0x%lx)",
1699 getpid (), vcl_cfg->add_segment_size,
1700 vcl_cfg->add_segment_size);
1702 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1703 &vcl_cfg->preallocated_fifo_pairs))
1705 if (VPPCOM_DEBUG > 0)
1706 clib_warning ("[%d] configured preallocated_fifo_pairs "
1707 "%d (0x%x)", getpid (),
1708 vcl_cfg->preallocated_fifo_pairs,
1709 vcl_cfg->preallocated_fifo_pairs);
1711 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1712 &vcl_cfg->rx_fifo_size))
1714 if (VPPCOM_DEBUG > 0)
1715 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1716 getpid (), vcl_cfg->rx_fifo_size,
1717 vcl_cfg->rx_fifo_size);
1719 else if (unformat (line_input, "rx-fifo-size %ld",
1720 &vcl_cfg->rx_fifo_size))
1722 if (VPPCOM_DEBUG > 0)
1723 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1724 getpid (), vcl_cfg->rx_fifo_size,
1725 vcl_cfg->rx_fifo_size);
1727 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1728 &vcl_cfg->tx_fifo_size))
1730 if (VPPCOM_DEBUG > 0)
1731 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1732 getpid (), vcl_cfg->tx_fifo_size,
1733 vcl_cfg->tx_fifo_size);
1735 else if (unformat (line_input, "tx-fifo-size %ld",
1736 &vcl_cfg->tx_fifo_size))
1738 if (VPPCOM_DEBUG > 0)
1739 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1740 getpid (), vcl_cfg->tx_fifo_size,
1741 vcl_cfg->tx_fifo_size);
1743 else if (unformat (line_input, "event-queue-size 0x%lx",
1744 &vcl_cfg->event_queue_size))
1746 if (VPPCOM_DEBUG > 0)
1747 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1748 getpid (), vcl_cfg->event_queue_size,
1749 vcl_cfg->event_queue_size);
1751 else if (unformat (line_input, "event-queue-size %ld",
1752 &vcl_cfg->event_queue_size))
1754 if (VPPCOM_DEBUG > 0)
1755 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1756 getpid (), vcl_cfg->event_queue_size,
1757 vcl_cfg->event_queue_size);
1759 else if (unformat (line_input, "listen-queue-size 0x%lx",
1760 &vcl_cfg->listen_queue_size))
1762 if (VPPCOM_DEBUG > 0)
1763 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1764 getpid (), vcl_cfg->listen_queue_size,
1765 vcl_cfg->listen_queue_size);
1767 else if (unformat (line_input, "listen-queue-size %ld",
1768 &vcl_cfg->listen_queue_size))
1770 if (VPPCOM_DEBUG > 0)
1771 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1772 getpid (), vcl_cfg->listen_queue_size,
1773 vcl_cfg->listen_queue_size);
1775 else if (unformat (line_input, "app-timeout %f",
1776 &vcl_cfg->app_timeout))
1778 if (VPPCOM_DEBUG > 0)
1779 clib_warning ("[%d] configured app_timeout %f",
1780 getpid (), vcl_cfg->app_timeout);
1782 else if (unformat (line_input, "session-timeout %f",
1783 &vcl_cfg->session_timeout))
1785 if (VPPCOM_DEBUG > 0)
1786 clib_warning ("[%d] configured session_timeout %f",
1787 getpid (), vcl_cfg->session_timeout);
1789 else if (unformat (line_input, "accept-timeout %f",
1790 &vcl_cfg->accept_timeout))
1792 if (VPPCOM_DEBUG > 0)
1793 clib_warning ("[%d] configured accept_timeout %f",
1794 getpid (), vcl_cfg->accept_timeout);
1796 else if (unformat (line_input, "app-proxy-transport-tcp"))
1798 vcl_cfg->app_proxy_transport_tcp = 1;
1799 if (VPPCOM_DEBUG > 0)
1800 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1801 getpid (), vcl_cfg->app_proxy_transport_tcp);
1803 else if (unformat (line_input, "app-proxy-transport-udp"))
1805 vcl_cfg->app_proxy_transport_udp = 1;
1806 if (VPPCOM_DEBUG > 0)
1807 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1808 getpid (), vcl_cfg->app_proxy_transport_udp);
1810 else if (unformat (line_input, "app-scope-local"))
1812 vcl_cfg->app_scope_local = 1;
1813 if (VPPCOM_DEBUG > 0)
1814 clib_warning ("[%d] configured app_scope_local (%d)",
1815 getpid (), vcl_cfg->app_scope_local);
1817 else if (unformat (line_input, "app-scope-global"))
1819 vcl_cfg->app_scope_global = 1;
1820 if (VPPCOM_DEBUG > 0)
1821 clib_warning ("[%d] configured app_scope_global (%d)",
1822 getpid (), vcl_cfg->app_scope_global);
1824 else if (unformat (line_input, "namespace-secret %lu",
1825 &vcl_cfg->namespace_secret))
1827 if (VPPCOM_DEBUG > 0)
1829 ("[%d] configured namespace_secret %lu (0x%lx)",
1830 getpid (), vcl_cfg->namespace_secret,
1831 vcl_cfg->namespace_secret);
1833 else if (unformat (line_input, "namespace-id %v",
1834 &vcl_cfg->namespace_id))
1836 vl_api_application_attach_t *mp;
1837 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1838 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1839 if (nsid_vec_len > max_nsid_vec_len)
1841 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1842 if (VPPCOM_DEBUG > 0)
1843 clib_warning ("[%d] configured namespace_id is too long,"
1844 " truncated to %d characters!", getpid (),
1848 if (VPPCOM_DEBUG > 0)
1849 clib_warning ("[%d] configured namespace_id %v",
1850 getpid (), vcl_cfg->namespace_id);
1852 else if (unformat (line_input, "}"))
1855 if (VPPCOM_DEBUG > 0)
1856 clib_warning ("[%d] completed parsing vppcom config!",
1862 if (line_input->buffer[line_input->index] != '#')
1864 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1866 &line_input->buffer[line_input->index]);
1873 unformat_free (input);
1881 * VPPCOM Public API functions
1884 vppcom_app_create (char *app_name)
1886 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1897 vppcom_cfg_init (vcl_cfg);
1898 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1902 if (sscanf (env_var_str, "%u", &tmp) != 1)
1903 clib_warning ("[%d] Invalid debug level specified in "
1904 "the environment variable "
1906 " (%s)!\n", getpid (), env_var_str);
1910 clib_warning ("[%d] configured debug level (%u) from "
1911 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1914 conf_fname = getenv (VPPCOM_ENV_CONF);
1917 conf_fname = VPPCOM_CONF_DEFAULT;
1918 if (VPPCOM_DEBUG > 0)
1919 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1922 vppcom_cfg_heapsize (conf_fname);
1923 clib_fifo_validate (vcm->client_session_index_fifo,
1924 vcm->cfg.listen_queue_size);
1925 vppcom_cfg_read (conf_fname);
1926 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1929 u32 ns_id_vec_len = strlen (env_var_str);
1931 vec_reset_length (vcm->cfg.namespace_id);
1932 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1933 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1935 if (VPPCOM_DEBUG > 0)
1936 clib_warning ("[%d] configured namespace_id (%v) from "
1937 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1938 vcm->cfg.namespace_id);
1940 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1944 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1945 clib_warning ("[%d] Invalid namespace secret specified in "
1946 "the environment variable "
1947 VPPCOM_ENV_APP_NAMESPACE_SECRET
1948 " (%s)!\n", getpid (), env_var_str);
1951 vcm->cfg.namespace_secret = tmp;
1952 if (VPPCOM_DEBUG > 0)
1953 clib_warning ("[%d] configured namespace secret (%lu) from "
1954 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1955 vcm->cfg.namespace_secret);
1958 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1960 vcm->cfg.app_proxy_transport_tcp = 1;
1961 if (VPPCOM_DEBUG > 0)
1962 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1963 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1964 vcm->cfg.app_proxy_transport_tcp);
1966 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1968 vcm->cfg.app_proxy_transport_udp = 1;
1969 if (VPPCOM_DEBUG > 0)
1970 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1971 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1972 vcm->cfg.app_proxy_transport_udp);
1974 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1976 vcm->cfg.app_scope_local = 1;
1977 if (VPPCOM_DEBUG > 0)
1978 clib_warning ("[%d] configured app_scope_local (%u) from "
1979 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1980 vcm->cfg.app_scope_local);
1982 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1984 vcm->cfg.app_scope_global = 1;
1985 if (VPPCOM_DEBUG > 0)
1986 clib_warning ("[%d] configured app_scope_global (%u) from "
1987 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1988 vcm->cfg.app_scope_global);
1991 vcm->main_cpu = os_get_thread_index ();
1992 heap = clib_mem_get_per_cpu_heap ();
1993 h = mheap_header (heap);
1995 /* make the main heap thread-safe */
1996 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1998 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2000 clib_time_init (&vcm->clib_time);
2001 vppcom_init_error_string_table ();
2002 svm_fifo_segment_init (vcl_cfg->segment_baseva,
2003 20 /* timeout in secs */ );
2004 clib_spinlock_init (&vcm->sessions_lockp);
2005 vppcom_api_hookup ();
2008 if (vcm->my_client_index == ~0)
2010 vcm->app_state = STATE_APP_START;
2011 rv = vppcom_connect_to_vpp (app_name);
2014 clib_warning ("[%d] ERROR: couldn't connect to VPP!", getpid ());
2018 if (VPPCOM_DEBUG > 0)
2019 clib_warning ("[%d] sending session enable", getpid ());
2021 rv = vppcom_app_session_enable ();
2024 clib_warning ("[%d] ERROR: vppcom_app_session_enable() failed!",
2029 if (VPPCOM_DEBUG > 0)
2030 clib_warning ("[%d] sending app attach", getpid ());
2032 rv = vppcom_app_attach ();
2035 clib_warning ("[%d] ERROR: vppcom_app_attach() failed!", getpid ());
2039 if (VPPCOM_DEBUG > 0)
2040 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
2041 getpid (), app_name, vcm->my_client_index,
2042 vcm->my_client_index);
2049 vppcom_app_destroy (void)
2053 if (vcm->my_client_index == ~0)
2056 if (VPPCOM_DEBUG > 0)
2057 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
2058 getpid (), vcm->my_client_index, vcm->my_client_index);
2060 vppcom_app_detach ();
2061 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2062 if (PREDICT_FALSE (rv))
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
2066 getpid (), vppcom_retval_str (rv), rv);
2068 vl_client_disconnect_from_vlib ();
2069 vcm->my_client_index = ~0;
2070 vcm->app_state = STATE_APP_START;
2074 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2079 clib_spinlock_lock (&vcm->sessions_lockp);
2080 pool_get (vcm->sessions, session);
2081 memset (session, 0, sizeof (*session));
2082 session_index = session - vcm->sessions;
2085 session->proto = proto;
2086 session->state = STATE_START;
2087 session->is_nonblocking = is_nonblocking ? 1 : 0;
2088 session->vpp_handle = ~0;
2089 clib_spinlock_unlock (&vcm->sessions_lockp);
2091 if (VPPCOM_DEBUG > 0)
2092 clib_warning ("[%d] sid %u", getpid (), session_index);
2094 return (int) session_index;
2098 vppcom_session_close (uint32_t session_index)
2100 session_t *session = 0;
2108 session_state_t state;
2110 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2111 is_listen = session->is_listen;
2112 is_vep = session->is_vep;
2113 is_vep_session = session->is_vep_session;
2114 next_sid = session->vep.next_sid;
2115 vep_idx = session->vep.vep_idx;
2116 state = session->state;
2117 clib_spinlock_unlock (&vcm->sessions_lockp);
2119 if (VPPCOM_DEBUG > 0)
2120 clib_warning ("[%d] sid %d: closing session...", getpid (),
2125 while (next_sid != ~0)
2127 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2128 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2129 clib_warning ("[%d] sid %u: EPOLL_CTL_DEL vep_idx %u failed, "
2130 "rv = %d (%s)", getpid (), next_sid, vep_idx,
2131 rv, vppcom_retval_str (rv));
2133 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2134 next_sid = session->vep.next_sid;
2135 clib_spinlock_unlock (&vcm->sessions_lockp);
2142 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2143 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2144 clib_warning ("[%d] sid %u: EPOLL_CTL_DEL vep_idx %u failed! "
2145 "rv = %d (%s)", getpid (), session_index, vep_idx,
2146 rv, vppcom_retval_str (rv));
2151 if (state == STATE_LISTEN)
2153 rv = vppcom_session_unbind (session_index);
2154 if (PREDICT_FALSE (rv < 0))
2156 if (VPPCOM_DEBUG > 0)
2157 clib_warning ("[%d] sid %u: listener unbind failed! "
2159 getpid (), session_index, rv,
2160 vppcom_retval_str (rv));
2165 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2167 rv = vppcom_session_disconnect (session_index);
2168 if (PREDICT_FALSE (rv < 0))
2169 clib_warning ("[%d] ERROR: sid %u: session disconnect failed! "
2170 "rv %d (%s)", getpid (), session_index, rv,
2171 vppcom_retval_str (rv));
2175 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2177 if (session->vpp_handle != ~0)
2179 p = hash_get (vcm->session_index_by_vpp_handles, session->vpp_handle);
2181 hash_unset (vcm->session_index_by_vpp_handles, session->vpp_handle);
2183 pool_put_index (vcm->sessions, session_index);
2184 clib_spinlock_unlock (&vcm->sessions_lockp);
2186 if (VPPCOM_DEBUG > 0)
2187 clib_warning ("[%d] sid %u: session removed.", getpid (), session_index);
2194 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2196 session_t *session = 0;
2200 return VPPCOM_EINVAL;
2202 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2204 if (session->is_vep)
2206 clib_spinlock_unlock (&vcm->sessions_lockp);
2207 clib_warning ("[%d] ERROR: sid %u: cannot bind to an epoll session!",
2208 getpid (), session_index);
2213 session->vrf = ep->vrf;
2214 session->lcl_addr.is_ip4 = ep->is_ip4;
2215 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2216 session->lcl_port = ep->port;
2218 if (VPPCOM_DEBUG > 0)
2219 clib_warning ("[%d] sid %u: bound to local address %U "
2220 "port %u, proto %s",
2221 getpid (), session_index, format_ip46_address,
2222 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2223 clib_net_to_host_u16 (session->lcl_port),
2224 session->proto ? "UDP" : "TCP");
2226 clib_spinlock_unlock (&vcm->sessions_lockp);
2232 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2234 session_t *listen_session = 0;
2237 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2239 if (listen_session->is_vep)
2241 clib_spinlock_unlock (&vcm->sessions_lockp);
2242 clib_warning ("[%d] ERROR: sid %u: cannot listen on an "
2243 "epoll session!", getpid (), listen_session_index);
2248 if (listen_session->is_listen)
2250 clib_spinlock_unlock (&vcm->sessions_lockp);
2251 if (VPPCOM_DEBUG > 0)
2252 clib_warning ("[%d] sid %u: already in listen state!",
2253 getpid (), listen_session_index);
2258 if (VPPCOM_DEBUG > 0)
2259 clib_warning ("[%d] sid %u: sending bind request...",
2260 getpid (), listen_session_index);
2262 vppcom_send_bind_sock (listen_session, listen_session_index);
2263 clib_spinlock_unlock (&vcm->sessions_lockp);
2265 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2266 vcm->cfg.session_timeout);
2267 if (PREDICT_FALSE (rv))
2269 if (VPPCOM_DEBUG > 0)
2270 clib_warning ("[%d] sid %u: bind failed! returning %d (%d)",
2271 getpid (), vppcom_retval_str (rv), rv);
2275 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2276 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2277 clib_spinlock_unlock (&vcm->sessions_lockp);
2283 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2284 uint32_t flags, double wait_for_time)
2286 session_t *listen_session = 0;
2287 session_t *client_session = 0;
2288 u32 client_session_index = ~0;
2293 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2295 if (listen_session->is_vep)
2297 clib_spinlock_unlock (&vcm->sessions_lockp);
2298 clib_warning ("[%d] ERROR: sid %u: cannot accept on an "
2299 "epoll session!", getpid (), listen_session_index);
2304 if (listen_session->state != STATE_LISTEN)
2306 clib_spinlock_unlock (&vcm->sessions_lockp);
2307 clib_warning ("[%d] ERROR: sid %u: not in listen state! "
2308 "state 0x%x (%s)", getpid (), listen_session->state,
2309 vppcom_session_state_str (listen_session->state));
2313 wait_for = ((listen_session->is_nonblocking) ? 0 :
2314 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time);
2316 clib_spinlock_unlock (&vcm->sessions_lockp);
2320 rv = vppcom_wait_for_client_session_index (wait_for);
2323 if ((VPPCOM_DEBUG > 0))
2324 clib_warning ("[%d] sid %u: accept failed! "
2325 "returning = %d (%s)",
2326 getpid (), listen_session_index,
2327 rv, vppcom_retval_str (rv));
2328 if ((wait_for == 0) || (wait_for_time > 0))
2335 clib_spinlock_lock (&vcm->sessions_lockp);
2336 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2337 rv = vppcom_session_at_index (client_session_index, &client_session);
2338 ASSERT (rv == VPPCOM_OK);
2339 ASSERT (client_session->peer_addr.is_ip4 ==
2340 listen_session->lcl_addr.is_ip4);
2342 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2343 if (VPPCOM_DEBUG > 0)
2344 clib_warning ("[%d] sid %u: Got a client request! flags %d, "
2345 "is_nonblocking %u", getpid (), client_session_index,
2346 flags, client_session->is_nonblocking);
2348 ep->vrf = client_session->vrf;
2349 ep->is_cut_thru = client_session->is_cut_thru;
2350 ep->is_ip4 = client_session->peer_addr.is_ip4;
2351 ep->port = client_session->peer_port;
2352 if (client_session->peer_addr.is_ip4)
2353 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2354 sizeof (ip4_address_t));
2356 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2357 sizeof (ip6_address_t));
2359 if (client_session->is_server && client_session->is_cut_thru)
2361 static svm_fifo_segment_create_args_t _a;
2362 svm_fifo_segment_create_args_t *a = &_a;
2363 svm_fifo_segment_private_t *seg;
2365 cut_thru_str = " cut-thru ";
2367 /* Create the segment */
2368 memset (a, 0, sizeof (*a));
2369 a->segment_name = (char *)
2370 format ((u8 *) a->segment_name, "%d:segment%d%c",
2371 getpid (), vcm->unique_segment_index++, 0);
2372 a->segment_size = vcm->cfg.segment_size;
2373 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2374 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2375 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2377 rv = svm_fifo_segment_create (a);
2378 if (PREDICT_FALSE (rv))
2380 clib_warning ("[%d] ERROR: sid %u: "
2381 "svm_fifo_segment_create ('%s') failed! rv %d",
2382 getpid (), client_session_index, a->segment_name, rv);
2383 vec_reset_length (a->new_segment_indices);
2384 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2385 vppcom_send_connect_session_reply (client_session, rv);
2386 clib_spinlock_unlock (&vcm->sessions_lockp);
2391 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2392 client_session->sm_seg_index = a->new_segment_indices[0];
2393 vec_free (a->new_segment_indices);
2395 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2396 client_session->server_rx_fifo =
2397 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2398 FIFO_SEGMENT_RX_FREELIST);
2399 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2401 svm_fifo_segment_delete (seg);
2402 clib_warning ("[%d] ERROR: sid %u: rx fifo alloc failed! "
2403 "size %ld (0x%lx)", getpid (),
2404 client_session_index,
2405 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2406 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2407 vppcom_send_connect_session_reply (client_session, rv);
2408 clib_spinlock_unlock (&vcm->sessions_lockp);
2412 client_session->server_rx_fifo->master_session_index =
2413 client_session_index;
2415 client_session->server_tx_fifo =
2416 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2417 FIFO_SEGMENT_TX_FREELIST);
2418 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2420 svm_fifo_segment_delete (seg);
2421 clib_warning ("[%d] ERROR: sid %u: tx fifo alloc failed! "
2423 getpid (), client_session_index,
2424 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2425 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2426 vppcom_send_connect_session_reply (client_session, rv);
2427 clib_spinlock_unlock (&vcm->sessions_lockp);
2431 client_session->server_tx_fifo->master_session_index =
2432 client_session_index;
2434 if (VPPCOM_DEBUG > 1)
2435 clib_warning ("[%d] sid %u: created segment '%s', "
2436 "rx_fifo %p, tx_fifo %p",
2437 getpid (), client_session_index,
2438 client_session->segment_name,
2439 client_session->server_rx_fifo,
2440 client_session->server_tx_fifo);
2442 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2445 ssvm_shared_header_t *sh = seg->ssvm.sh;
2447 ssvm_lock_non_recursive (sh, 1);
2448 oldheap = ssvm_push_heap (sh);
2449 event_q = client_session->vpp_event_queue =
2450 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2451 sizeof (session_fifo_event_t),
2452 getpid (), 0 /* signal not sent */ );
2453 ssvm_pop_heap (oldheap);
2454 ssvm_unlock_non_recursive (sh);
2457 vppcom_send_connect_session_reply (client_session, 0);
2462 vppcom_send_accept_session_reply (client_session->vpp_handle, 0);
2465 if (VPPCOM_DEBUG > 0)
2466 clib_warning ("[%d] sid %u: accepted%sconnection to local %s address "
2468 getpid (), client_session_index, cut_thru_str,
2469 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2470 format_ip46_address, &client_session->lcl_addr.ip46,
2471 client_session->lcl_addr.is_ip4,
2472 clib_net_to_host_u16 (client_session->lcl_port));
2474 clib_spinlock_unlock (&vcm->sessions_lockp);
2475 rv = (int) client_session_index;
2481 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2483 session_t *session = 0;
2486 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2488 if (PREDICT_FALSE (session->is_vep))
2490 clib_spinlock_unlock (&vcm->sessions_lockp);
2491 clib_warning ("[%d] ERROR: sid %u: cannot connect on an epoll session!",
2492 getpid (), session_index);
2497 if (PREDICT_FALSE (session->is_server))
2499 clib_spinlock_unlock (&vcm->sessions_lockp);
2500 clib_warning ("[%d] ERROR: sid %u: is in use as a server session!",
2501 getpid (), session_index);
2506 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2508 if (VPPCOM_DEBUG > 0)
2509 clib_warning ("[%d] sid %u: session already connected to "
2510 "%s %U port %d proto %s, state 0x%x (%s)",
2511 getpid (), session_index,
2512 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2513 format_ip46_address,
2514 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2515 clib_net_to_host_u16 (session->peer_port),
2516 session->proto ? "UDP" : "TCP", session->state,
2517 vppcom_session_state_str (session->state));
2519 clib_spinlock_unlock (&vcm->sessions_lockp);
2524 session->vrf = server_ep->vrf;
2525 session->peer_addr.is_ip4 = server_ep->is_ip4;
2526 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2527 session->peer_port = server_ep->port;
2529 if (VPPCOM_DEBUG > 0)
2530 clib_warning ("[%d] sid %u: connecting to server %s %U port %d proto %s",
2531 getpid (), session_index,
2532 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2533 format_ip46_address,
2534 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2535 clib_net_to_host_u16 (session->peer_port),
2536 session->proto ? "UDP" : "TCP");
2538 vppcom_send_connect_sock (session, session_index);
2539 clib_spinlock_unlock (&vcm->sessions_lockp);
2541 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2542 vcm->cfg.session_timeout);
2543 if (PREDICT_FALSE (rv))
2545 if (VPPCOM_DEBUG > 0)
2546 clib_warning ("[%d] sid %u: connect failed! returning %d (%s)",
2547 getpid (), session_index, rv, vppcom_retval_str (rv));
2549 else if (VPPCOM_DEBUG > 0)
2550 clib_warning ("[%d] sid %d: connected!", getpid (), session_index);
2557 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2560 session_t *session = 0;
2561 svm_fifo_t *rx_fifo;
2566 session_state_t state;
2572 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2574 if (PREDICT_FALSE (session->is_vep))
2576 clib_spinlock_unlock (&vcm->sessions_lockp);
2577 clib_warning ("[%d] ERROR: sid %u: cannot read from an epoll session!",
2578 getpid (), session_index);
2583 is_server = session->is_server;
2584 is_nonblocking = session->is_nonblocking;
2585 state = session->state;
2586 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
2588 clib_spinlock_unlock (&vcm->sessions_lockp);
2589 rv = ((state == STATE_DISCONNECT) ?
2590 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2592 if (VPPCOM_DEBUG > 0)
2593 clib_warning ("[%d] sid %u: %s session is not open! "
2594 "state 0x%x (%s), returning %d (%s)",
2595 getpid (), session_index, is_server ?
2596 "server" : "client", state,
2597 vppcom_session_state_str (state),
2598 rv, vppcom_retval_str (rv));
2602 rx_fifo = ((!session->is_cut_thru || is_server) ?
2603 session->server_rx_fifo : session->server_tx_fifo);
2604 fifo_str = ((!session->is_cut_thru || is_server) ?
2605 "server_rx_fifo" : "server_tx_fifo");
2606 clib_spinlock_unlock (&vcm->sessions_lockp);
2611 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2613 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2615 while (!is_nonblocking && (n_read <= 0));
2619 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2621 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2622 (EPOLLET | EPOLLIN));
2624 session->vep.et_mask |= EPOLLIN;
2626 if (state == STATE_CLOSE_ON_EMPTY)
2628 session_state_t new_state = STATE_DISCONNECT;
2629 rv = VPPCOM_ECONNRESET;
2631 if (VPPCOM_DEBUG > 1)
2633 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2634 "state 0x%x (%s)!\n"
2635 " Setting state to %d (%s), returning %d (%s)",
2636 getpid (), session_index,
2637 is_server ? "server" : "client",
2638 state, vppcom_session_state_str (state),
2639 new_state, vppcom_session_state_str (new_state),
2640 rv, vppcom_retval_str (rv));
2643 session->state = new_state;
2648 clib_spinlock_unlock (&vcm->sessions_lockp);
2653 if (VPPCOM_DEBUG > 2)
2656 clib_warning ("[%d] sid %u: read %d bytes from %s (%p)", getpid (),
2657 session_index, n_read, fifo_str, rx_fifo);
2659 clib_warning ("[%d] sid %u: nothing read, returning %d (%s)",
2660 getpid (), session_index, rv, vppcom_retval_str (rv));
2667 vppcom_session_read (uint32_t session_index, void *buf, int n)
2669 return (vppcom_session_read_internal (session_index, buf, n, 0));
2673 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2675 return (vppcom_session_read_internal (session_index, buf, n, 1));
2679 vppcom_session_read_ready (session_t * session, u32 session_index)
2681 svm_fifo_t *rx_fifo = 0;
2685 u8 is_server = session->is_server;
2686 session_state_t state = session->state;
2688 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2689 if (PREDICT_FALSE (session->is_vep))
2691 clib_warning ("[%d] ERROR: sid %u: cannot read from an "
2692 "epoll session!", getpid (), session_index);
2697 if (session->is_listen)
2698 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2701 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2703 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2706 if (VPPCOM_DEBUG > 1)
2707 clib_warning ("[%d] sid %u: %s session is not open! "
2708 "state 0x%x (%s), returning %d (%s)", getpid (),
2709 session_index, is_server ? "server" : "client",
2710 state, vppcom_session_state_str (state),
2711 rv, vppcom_retval_str (rv));
2715 rx_fifo = ((!session->is_cut_thru || is_server) ?
2716 session->server_rx_fifo : session->server_tx_fifo);
2718 ready = svm_fifo_max_dequeue (rx_fifo);
2724 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2726 session->vep.et_mask |= EPOLLIN;
2728 if (state == STATE_CLOSE_ON_EMPTY)
2730 rv = VPPCOM_ECONNRESET;
2731 session_state_t new_state = STATE_DISCONNECT;
2733 if (VPPCOM_DEBUG > 1)
2735 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2736 "state 0x%x (%s)!\n"
2737 " Setting state to %d (%s), returning %d (%s)",
2738 getpid (), session_index,
2739 is_server ? "server" : "client",
2740 state, vppcom_session_state_str (state),
2741 new_state, vppcom_session_state_str (new_state),
2742 rv, vppcom_retval_str (rv));
2744 session->state = new_state;
2754 vppcom_session_write (uint32_t session_index, void *buf, int n)
2756 session_t *session = 0;
2757 svm_fifo_t *tx_fifo;
2758 unix_shared_memory_queue_t *q;
2759 session_fifo_event_t evt;
2765 session_state_t state;
2769 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2771 if (PREDICT_FALSE (session->is_vep))
2773 clib_spinlock_unlock (&vcm->sessions_lockp);
2774 clib_warning ("[%d] ERROR: sid %u: cannot write to an epoll session!",
2775 getpid (), session_index);
2781 is_server = session->is_server;
2782 is_nonblocking = session->is_nonblocking;
2783 state = session->state;
2784 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2786 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2789 clib_spinlock_unlock (&vcm->sessions_lockp);
2790 if (VPPCOM_DEBUG > 1)
2791 clib_warning ("[%d] sid %u: %s session is not open! state 0x%x (%s)",
2792 getpid (), session_index, is_server ?
2793 "server" : "client", state,
2794 vppcom_session_state_str (state));
2798 tx_fifo = ((!session->is_cut_thru || is_server) ?
2799 session->server_tx_fifo : session->server_rx_fifo);
2800 fifo_str = ((!session->is_cut_thru || is_server) ?
2801 "server_tx_fifo" : "server_rx_fifo");
2802 clib_spinlock_unlock (&vcm->sessions_lockp);
2806 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2808 while (!is_nonblocking && (n_write <= 0));
2810 /* If event wasn't set, add one */
2811 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2813 /* Fabricate TX event, send to vpp */
2815 evt.event_type = FIFO_EVENT_APP_TX;
2817 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2818 q = session->vpp_event_queue;
2820 unix_shared_memory_queue_add (q, (u8 *) & evt,
2821 0 /* do wait for mutex */ );
2822 clib_spinlock_unlock (&vcm->sessions_lockp);
2823 if (VPPCOM_DEBUG > 1)
2824 clib_warning ("[%d] sid %u: added FIFO_EVENT_APP_TX to "
2825 "vpp_event_q %p, n_write %d", getpid (),
2826 session_index, q, n_write);
2831 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2833 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2834 (EPOLLET | EPOLLOUT));
2836 session->vep.et_mask |= EPOLLOUT;
2838 if (state == STATE_CLOSE_ON_EMPTY)
2840 session_state_t new_state = STATE_DISCONNECT;
2841 rv = VPPCOM_ECONNRESET;
2843 if (VPPCOM_DEBUG > 1)
2845 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2846 "state 0x%x (%s)!\n"
2847 " Setting state to %d (%s), returning %d (%s)",
2848 getpid (), session_index,
2849 is_server ? "server" : "client",
2850 state, vppcom_session_state_str (state),
2851 new_state, vppcom_session_state_str (new_state),
2852 rv, vppcom_retval_str (rv));
2855 session->state = new_state;
2860 clib_spinlock_unlock (&vcm->sessions_lockp);
2865 if (VPPCOM_DEBUG > 2)
2868 clib_warning ("[%d] sid %u: FIFO-FULL %s (%p)", getpid (),
2869 session_index, fifo_str, tx_fifo);
2871 clib_warning ("[%d] sid %u: wrote %d bytes to %s (%p)", getpid (),
2872 session_index, n_write, fifo_str, tx_fifo);
2879 vppcom_session_write_ready (session_t * session, u32 session_index)
2881 svm_fifo_t *tx_fifo;
2886 u8 is_server = session->is_server;
2887 session_state_t state = session->state;
2889 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2890 if (PREDICT_FALSE (session->is_vep))
2892 clib_warning ("[%d] ERROR: sid %u: cannot write to an "
2893 "epoll session!", getpid (), session_index);
2898 if (PREDICT_FALSE (session->is_listen))
2900 clib_warning ("[%d] ERROR: sid %u: cannot write to a listen session!",
2901 getpid (), session_index);
2906 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2908 session_state_t state = session->state;
2910 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2913 clib_warning ("[%d] ERROR: sid %u: %s session is not open! "
2914 "state 0x%x (%s), returning %d (%s)", getpid (),
2915 session_index, is_server ? "server" : "client",
2916 state, vppcom_session_state_str (state),
2917 rv, vppcom_retval_str (rv));
2921 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2922 session->server_tx_fifo : session->server_rx_fifo);
2923 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2924 "server_tx_fifo" : "server_rx_fifo");
2926 ready = svm_fifo_max_enqueue (tx_fifo);
2928 if (VPPCOM_DEBUG > 3)
2929 clib_warning ("[%d] sid %u: peek %s (%p), ready = %d", getpid (),
2930 session_index, fifo_str, tx_fifo, ready);
2934 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2935 (EPOLLET | EPOLLOUT));
2937 session->vep.et_mask |= EPOLLOUT;
2939 if (state == STATE_CLOSE_ON_EMPTY)
2941 rv = VPPCOM_ECONNRESET;
2942 session_state_t new_state = STATE_DISCONNECT;
2944 if (VPPCOM_DEBUG > 1)
2946 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2947 "state 0x%x (%s)!\n"
2948 " Setting state to %d (%s), returning %d (%s)",
2949 getpid (), session_index,
2950 is_server ? "server" : "client",
2951 state, vppcom_session_state_str (state),
2952 new_state, vppcom_session_state_str (new_state),
2953 rv, vppcom_retval_str (rv));
2955 session->state = new_state;
2965 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2966 unsigned long *write_map, unsigned long *except_map,
2967 double time_to_wait)
2970 session_t *session = 0;
2971 int rv, bits_set = 0;
2972 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2973 u32 minbits = clib_max (n_bits, BITS (uword));
2975 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2977 if (n_bits && read_map)
2979 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2980 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2981 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2983 if (n_bits && write_map)
2985 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2986 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2987 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2989 if (n_bits && except_map)
2991 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2992 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2993 memset (except_map, 0, vec_len (vcm->ex_bitmap));
3003 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3005 clib_spinlock_lock (&vcm->sessions_lockp);
3006 rv = vppcom_session_at_index (session_index, &session);
3009 clib_spinlock_unlock (&vcm->sessions_lockp);
3010 if (VPPCOM_DEBUG > 1)
3011 clib_warning ("[%d] session %d specified in "
3012 "read_map is closed.", getpid (),
3014 bits_set = VPPCOM_EBADFD;
3018 rv = vppcom_session_read_ready (session, session_index);
3019 clib_spinlock_unlock (&vcm->sessions_lockp);
3020 if (except_map && vcm->ex_bitmap &&
3021 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3024 // TBD: clib_warning
3025 clib_bitmap_set_no_check (except_map, session_index, 1);
3030 // TBD: clib_warning
3031 clib_bitmap_set_no_check (read_map, session_index, 1);
3039 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3041 clib_spinlock_lock (&vcm->sessions_lockp);
3042 rv = vppcom_session_at_index (session_index, &session);
3045 clib_spinlock_unlock (&vcm->sessions_lockp);
3046 if (VPPCOM_DEBUG > 0)
3047 clib_warning ("[%d] session %d specified in "
3048 "write_map is closed.", getpid (),
3050 bits_set = VPPCOM_EBADFD;
3054 rv = vppcom_session_write_ready (session, session_index);
3055 clib_spinlock_unlock (&vcm->sessions_lockp);
3056 if (write_map && (rv > 0))
3058 // TBD: clib_warning
3059 clib_bitmap_set_no_check (write_map, session_index, 1);
3067 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3069 clib_spinlock_lock (&vcm->sessions_lockp);
3070 rv = vppcom_session_at_index (session_index, &session);
3073 clib_spinlock_unlock (&vcm->sessions_lockp);
3074 if (VPPCOM_DEBUG > 1)
3075 clib_warning ("[%d] session %d specified in "
3076 "except_map is closed.", getpid (),
3078 bits_set = VPPCOM_EBADFD;
3082 rv = vppcom_session_read_ready (session, session_index);
3083 clib_spinlock_unlock (&vcm->sessions_lockp);
3086 // TBD: clib_warning
3087 clib_bitmap_set_no_check (except_map, session_index, 1);
3095 while (clib_time_now (&vcm->clib_time) < timeout);
3102 vep_verify_epoll_chain (u32 vep_idx)
3105 vppcom_epoll_t *vep;
3109 if (VPPCOM_DEBUG <= 1)
3112 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3113 rv = vppcom_session_at_index (vep_idx, &session);
3114 if (PREDICT_FALSE (rv))
3116 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
3119 if (PREDICT_FALSE (!session->is_vep))
3121 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
3125 vep = &session->vep;
3126 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3129 " is_vep_session = %u\n"
3130 " next_sid = 0x%x (%u)\n"
3131 " wait_cont_idx = 0x%x (%u)\n"
3132 "}\n", getpid (), vep_idx,
3133 session->is_vep, session->is_vep_session,
3134 vep->next_sid, vep->next_sid,
3135 session->wait_cont_idx, session->wait_cont_idx);
3137 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3139 rv = vppcom_session_at_index (sid, &session);
3140 if (PREDICT_FALSE (rv))
3142 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
3145 if (PREDICT_FALSE (session->is_vep))
3146 clib_warning ("[%d] ERROR: sid (%u) is a vep!", getpid (), vep_idx);
3147 else if (PREDICT_FALSE (!session->is_vep_session))
3149 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
3153 vep = &session->vep;
3154 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3155 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
3156 "vep_idx (%u)!", getpid (),
3157 sid, session->vep.vep_idx, vep_idx);
3158 if (session->is_vep_session)
3160 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3162 " next_sid = 0x%x (%u)\n"
3163 " prev_sid = 0x%x (%u)\n"
3164 " vep_idx = 0x%x (%u)\n"
3165 " ev.events = 0x%x\n"
3166 " ev.data.u64 = 0x%llx\n"
3170 vep->next_sid, vep->next_sid,
3171 vep->prev_sid, vep->prev_sid,
3172 vep->vep_idx, vep->vep_idx,
3173 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3178 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
3182 vppcom_epoll_create (void)
3184 session_t *vep_session;
3187 clib_spinlock_lock (&vcm->sessions_lockp);
3188 pool_get (vcm->sessions, vep_session);
3189 memset (vep_session, 0, sizeof (*vep_session));
3190 vep_idx = vep_session - vcm->sessions;
3192 vep_session->is_vep = 1;
3193 vep_session->vep.vep_idx = ~0;
3194 vep_session->vep.next_sid = ~0;
3195 vep_session->vep.prev_sid = ~0;
3196 vep_session->wait_cont_idx = ~0;
3197 vep_session->vpp_handle = ~0;
3198 clib_spinlock_unlock (&vcm->sessions_lockp);
3200 if (VPPCOM_DEBUG > 0)
3201 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
3207 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3208 struct epoll_event *event)
3210 session_t *vep_session;
3214 if (vep_idx == session_index)
3216 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3217 getpid (), vep_idx);
3218 return VPPCOM_EINVAL;
3221 clib_spinlock_lock (&vcm->sessions_lockp);
3222 rv = vppcom_session_at_index (vep_idx, &vep_session);
3223 if (PREDICT_FALSE (rv))
3225 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3228 if (PREDICT_FALSE (!vep_session->is_vep))
3230 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3231 getpid (), vep_idx);
3236 ASSERT (vep_session->vep.vep_idx == ~0);
3237 ASSERT (vep_session->vep.prev_sid == ~0);
3239 rv = vppcom_session_at_index (session_index, &session);
3240 if (PREDICT_FALSE (rv))
3242 if (VPPCOM_DEBUG > 0)
3243 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3244 getpid (), session_index);
3247 if (PREDICT_FALSE (session->is_vep))
3249 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3257 if (PREDICT_FALSE (!event))
3259 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3260 "epoll_event structure!", getpid ());
3264 if (vep_session->vep.next_sid != ~0)
3266 session_t *next_session;
3267 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3269 if (PREDICT_FALSE (rv))
3271 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3272 "vep.next_sid (%u) on vep_idx (%u)!",
3273 getpid (), vep_session->vep.next_sid, vep_idx);
3276 ASSERT (next_session->vep.prev_sid == vep_idx);
3277 next_session->vep.prev_sid = session_index;
3279 session->vep.next_sid = vep_session->vep.next_sid;
3280 session->vep.prev_sid = vep_idx;
3281 session->vep.vep_idx = vep_idx;
3282 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3283 session->vep.ev = *event;
3284 session->is_vep = 0;
3285 session->is_vep_session = 1;
3286 vep_session->vep.next_sid = session_index;
3287 if (VPPCOM_DEBUG > 1)
3288 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3289 " data 0x%llx!", getpid (), vep_idx, session_index,
3290 event->events, event->data.u64);
3294 if (PREDICT_FALSE (!event))
3296 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3297 "epoll_event structure!", getpid ());
3301 else if (PREDICT_FALSE (!session->is_vep_session))
3303 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3304 "not a vep session!", getpid (), session_index);
3308 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3310 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3311 "vep_idx (%u) != vep_idx (%u)!",
3312 getpid (), session_index,
3313 session->vep.vep_idx, vep_idx);
3317 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3318 session->vep.ev = *event;
3319 if (VPPCOM_DEBUG > 1)
3320 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3321 " data 0x%llx!", getpid (), vep_idx, session_index,
3322 event->events, event->data.u64);
3326 if (PREDICT_FALSE (!session->is_vep_session))
3328 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3329 "not a vep session!", getpid (), session_index);
3333 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3335 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3336 "vep_idx (%u) != vep_idx (%u)!",
3337 getpid (), session_index,
3338 session->vep.vep_idx, vep_idx);
3343 vep_session->wait_cont_idx =
3344 (vep_session->wait_cont_idx == session_index) ?
3345 session->vep.next_sid : vep_session->wait_cont_idx;
3347 if (session->vep.prev_sid == vep_idx)
3348 vep_session->vep.next_sid = session->vep.next_sid;
3351 session_t *prev_session;
3352 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3353 if (PREDICT_FALSE (rv))
3355 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3356 "vep.prev_sid (%u) on sid (%u)!",
3357 getpid (), session->vep.prev_sid, session_index);
3360 ASSERT (prev_session->vep.next_sid == session_index);
3361 prev_session->vep.next_sid = session->vep.next_sid;
3363 if (session->vep.next_sid != ~0)
3365 session_t *next_session;
3366 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3367 if (PREDICT_FALSE (rv))
3369 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3370 "vep.next_sid (%u) on sid (%u)!",
3371 getpid (), session->vep.next_sid, session_index);
3374 ASSERT (next_session->vep.prev_sid == session_index);
3375 next_session->vep.prev_sid = session->vep.prev_sid;
3378 memset (&session->vep, 0, sizeof (session->vep));
3379 session->vep.next_sid = ~0;
3380 session->vep.prev_sid = ~0;
3381 session->vep.vep_idx = ~0;
3382 session->is_vep_session = 0;
3383 if (VPPCOM_DEBUG > 1)
3384 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3385 getpid (), vep_idx, session_index);
3389 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3393 vep_verify_epoll_chain (vep_idx);
3396 clib_spinlock_unlock (&vcm->sessions_lockp);
3401 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3402 int maxevents, double wait_for_time)
3404 session_t *vep_session;
3406 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3407 u32 keep_trying = 1;
3409 u32 vep_next_sid, wait_cont_idx;
3412 if (PREDICT_FALSE (maxevents <= 0))
3414 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3415 getpid (), maxevents);
3416 return VPPCOM_EINVAL;
3418 memset (events, 0, sizeof (*events) * maxevents);
3420 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3421 vep_next_sid = vep_session->vep.next_sid;
3422 is_vep = vep_session->is_vep;
3423 wait_cont_idx = vep_session->wait_cont_idx;
3424 clib_spinlock_unlock (&vcm->sessions_lockp);
3426 if (PREDICT_FALSE (!is_vep))
3428 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3429 getpid (), vep_idx);
3433 if (PREDICT_FALSE (vep_next_sid == ~0))
3435 if (VPPCOM_DEBUG > 0)
3436 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3437 getpid (), vep_idx);
3447 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3448 sid != ~0; sid = next_sid)
3450 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3451 u8 add_event, is_vep_session;
3453 u64 session_ev_data;
3455 VCL_LOCK_AND_GET_SESSION (sid, &session);
3456 next_sid = session->vep.next_sid;
3457 session_events = session->vep.ev.events;
3458 et_mask = session->vep.et_mask;
3459 is_vep = session->is_vep;
3460 is_vep_session = session->is_vep_session;
3461 session_vep_idx = session->vep.vep_idx;
3462 session_ev_data = session->vep.ev.data.u64;
3463 clib_spinlock_unlock (&vcm->sessions_lockp);
3465 if (PREDICT_FALSE (is_vep))
3467 if (VPPCOM_DEBUG > 0)
3468 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3469 getpid (), vep_idx);
3473 if (PREDICT_FALSE (!is_vep_session))
3475 if (VPPCOM_DEBUG > 0)
3476 clib_warning ("[%d] ERROR: session (%u) is not "
3477 "a vep session!", getpid (), sid);
3481 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3483 clib_warning ("[%d] ERROR: session (%u) "
3484 "vep_idx (%u) != vep_idx (%u)!",
3485 getpid (), sid, session->vep.vep_idx, vep_idx);
3490 add_event = clear_et_mask = 0;
3492 if (EPOLLIN & session_events)
3494 VCL_LOCK_AND_GET_SESSION (sid, &session);
3495 ready = vppcom_session_read_ready (session, sid);
3496 clib_spinlock_unlock (&vcm->sessions_lockp);
3497 if ((ready > 0) && (EPOLLIN & et_mask))
3500 events[num_ev].events |= EPOLLIN;
3501 if (((EPOLLET | EPOLLIN) & session_events) ==
3502 (EPOLLET | EPOLLIN))
3503 clear_et_mask |= EPOLLIN;
3510 case VPPCOM_ECONNRESET:
3511 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3515 events[num_ev].events |= EPOLLERR;
3521 if (EPOLLOUT & session_events)
3523 VCL_LOCK_AND_GET_SESSION (sid, &session);
3524 ready = vppcom_session_write_ready (session, sid);
3525 clib_spinlock_unlock (&vcm->sessions_lockp);
3526 if ((ready > 0) && (EPOLLOUT & et_mask))
3529 events[num_ev].events |= EPOLLOUT;
3530 if (((EPOLLET | EPOLLOUT) & session_events) ==
3531 (EPOLLET | EPOLLOUT))
3532 clear_et_mask |= EPOLLOUT;
3539 case VPPCOM_ECONNRESET:
3540 events[num_ev].events |= EPOLLHUP;
3544 events[num_ev].events |= EPOLLERR;
3552 events[num_ev].data.u64 = session_ev_data;
3553 if (EPOLLONESHOT & session_events)
3555 VCL_LOCK_AND_GET_SESSION (sid, &session);
3556 session->vep.ev.events = 0;
3557 clib_spinlock_unlock (&vcm->sessions_lockp);
3560 if (num_ev == maxevents)
3562 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3563 vep_session->wait_cont_idx = next_sid;
3564 clib_spinlock_unlock (&vcm->sessions_lockp);
3568 if (wait_cont_idx != ~0)
3571 next_sid = vep_next_sid;
3572 else if (next_sid == wait_cont_idx)
3576 if (wait_for_time != -1)
3577 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3579 while ((num_ev == 0) && keep_trying);
3581 if (wait_cont_idx != ~0)
3583 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3584 vep_session->wait_cont_idx = ~0;
3585 clib_spinlock_unlock (&vcm->sessions_lockp);
3588 return (rv != VPPCOM_OK) ? rv : num_ev;
3592 vppcom_session_attr (uint32_t session_index, uint32_t op,
3593 void *buffer, uint32_t * buflen)
3597 u32 *flags = buffer;
3598 vppcom_endpt_t *ep = buffer;
3600 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3603 case VPPCOM_ATTR_GET_NREAD:
3604 rv = vppcom_session_read_ready (session, session_index);
3605 if (VPPCOM_DEBUG > 2)
3606 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3610 case VPPCOM_ATTR_GET_NWRITE:
3611 rv = vppcom_session_write_ready (session, session_index);
3612 if (VPPCOM_DEBUG > 2)
3613 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3614 getpid (), session_index, rv);
3617 case VPPCOM_ATTR_GET_FLAGS:
3618 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3620 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3621 *buflen = sizeof (*flags);
3622 if (VPPCOM_DEBUG > 2)
3623 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3624 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3625 session_index, *flags, session->is_nonblocking);
3631 case VPPCOM_ATTR_SET_FLAGS:
3632 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3634 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3635 if (VPPCOM_DEBUG > 2)
3636 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3637 "flags = 0x%08x, is_nonblocking = %u",
3638 getpid (), session_index, *flags,
3639 session->is_nonblocking);
3645 case VPPCOM_ATTR_GET_PEER_ADDR:
3646 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3648 ep->vrf = session->vrf;
3649 ep->is_ip4 = session->peer_addr.is_ip4;
3650 ep->port = session->peer_port;
3651 if (session->peer_addr.is_ip4)
3652 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3653 sizeof (ip4_address_t));
3655 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3656 sizeof (ip6_address_t));
3657 *buflen = sizeof (*ep);
3658 if (VPPCOM_DEBUG > 1)
3659 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
3660 "%u, addr = %U, port %u", getpid (),
3661 session_index, ep->is_ip4, format_ip46_address,
3662 &session->peer_addr.ip46, ep->is_ip4,
3663 clib_net_to_host_u16 (ep->port));
3669 case VPPCOM_ATTR_GET_LCL_ADDR:
3670 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3672 ep->vrf = session->vrf;
3673 ep->is_ip4 = session->lcl_addr.is_ip4;
3674 ep->port = session->lcl_port;
3675 if (session->lcl_addr.is_ip4)
3676 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3677 sizeof (ip4_address_t));
3679 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3680 sizeof (ip6_address_t));
3681 *buflen = sizeof (*ep);
3682 if (VPPCOM_DEBUG > 1)
3683 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
3684 "%u, addr = %U port %d", getpid (),
3685 session_index, ep->is_ip4, format_ip46_address,
3686 &session->lcl_addr.ip46, ep->is_ip4,
3687 clib_net_to_host_u16 (ep->port));
3693 case VPPCOM_ATTR_SET_REUSEADDR:
3696 case VPPCOM_ATTR_SET_BROADCAST:
3699 case VPPCOM_ATTR_SET_V6ONLY:
3702 case VPPCOM_ATTR_SET_KEEPALIVE:
3705 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3708 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3717 clib_spinlock_unlock (&vcm->sessions_lockp);
3722 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3723 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3726 session_t *session = 0;
3730 clib_spinlock_lock (&vcm->sessions_lockp);
3731 rv = vppcom_session_at_index (session_index, &session);
3732 if (PREDICT_FALSE (rv))
3734 clib_spinlock_unlock (&vcm->sessions_lockp);
3735 if (VPPCOM_DEBUG > 0)
3736 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3737 getpid (), session_index);
3739 clib_spinlock_unlock (&vcm->sessions_lockp);
3742 ep->vrf = session->vrf;
3743 ep->is_ip4 = session->peer_addr.is_ip4;
3744 ep->port = session->peer_port;
3745 if (session->peer_addr.is_ip4)
3746 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3747 sizeof (ip4_address_t));
3749 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3750 sizeof (ip6_address_t));
3751 clib_spinlock_unlock (&vcm->sessions_lockp);
3755 rv = vppcom_session_read (session_index, buffer, buflen);
3756 else if (flags & MSG_PEEK)
3757 rv = vppcom_session_peek (session_index, buffer, buflen);
3760 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3761 rv = VPPCOM_EAFNOSUPPORT;
3769 vppcom_session_sendto (uint32_t session_index, void *buffer,
3770 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3773 return VPPCOM_EINVAL;
3778 return VPPCOM_EINVAL;
3783 // TBD check the flags and do the right thing
3784 if (VPPCOM_DEBUG > 2)
3785 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3786 getpid (), flags, flags);
3789 return (vppcom_session_write (session_index, buffer, buflen));
3793 * fd.io coding-style-patch-verification: ON
3796 * eval: (c-set-style "gnu")