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);
2340 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2341 if (VPPCOM_DEBUG > 0)
2342 clib_warning ("[%d] sid %u: Got a client request! flags %d, "
2343 "is_nonblocking %u", getpid (), client_session_index,
2344 flags, client_session->is_nonblocking);
2346 ep->vrf = client_session->vrf;
2347 ep->is_cut_thru = client_session->is_cut_thru;
2348 ep->is_ip4 = client_session->peer_addr.is_ip4;
2349 ep->port = client_session->peer_port;
2350 if (client_session->peer_addr.is_ip4)
2351 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2352 sizeof (ip4_address_t));
2354 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2355 sizeof (ip6_address_t));
2357 if (client_session->is_server && client_session->is_cut_thru)
2359 static svm_fifo_segment_create_args_t _a;
2360 svm_fifo_segment_create_args_t *a = &_a;
2361 svm_fifo_segment_private_t *seg;
2363 cut_thru_str = " cut-thru ";
2365 /* Create the segment */
2366 memset (a, 0, sizeof (*a));
2367 a->segment_name = (char *)
2368 format ((u8 *) a->segment_name, "%d:segment%d%c",
2369 getpid (), vcm->unique_segment_index++, 0);
2370 a->segment_size = vcm->cfg.segment_size;
2371 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2372 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2373 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2375 rv = svm_fifo_segment_create (a);
2376 if (PREDICT_FALSE (rv))
2378 clib_warning ("[%d] ERROR: sid %u: "
2379 "svm_fifo_segment_create ('%s') failed! rv %d",
2380 getpid (), client_session_index, a->segment_name, rv);
2381 vec_reset_length (a->new_segment_indices);
2382 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2383 vppcom_send_connect_session_reply (client_session, rv);
2384 clib_spinlock_unlock (&vcm->sessions_lockp);
2389 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2390 client_session->sm_seg_index = a->new_segment_indices[0];
2391 vec_free (a->new_segment_indices);
2393 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2394 client_session->server_rx_fifo =
2395 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2396 FIFO_SEGMENT_RX_FREELIST);
2397 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2399 svm_fifo_segment_delete (seg);
2400 clib_warning ("[%d] ERROR: sid %u: rx fifo alloc failed! "
2401 "size %ld (0x%lx)", getpid (),
2402 client_session_index,
2403 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2404 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2405 vppcom_send_connect_session_reply (client_session, rv);
2406 clib_spinlock_unlock (&vcm->sessions_lockp);
2410 client_session->server_rx_fifo->master_session_index =
2411 client_session_index;
2413 client_session->server_tx_fifo =
2414 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2415 FIFO_SEGMENT_TX_FREELIST);
2416 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2418 svm_fifo_segment_delete (seg);
2419 clib_warning ("[%d] ERROR: sid %u: tx fifo alloc failed! "
2421 getpid (), client_session_index,
2422 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2423 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2424 vppcom_send_connect_session_reply (client_session, rv);
2425 clib_spinlock_unlock (&vcm->sessions_lockp);
2429 client_session->server_tx_fifo->master_session_index =
2430 client_session_index;
2432 if (VPPCOM_DEBUG > 1)
2433 clib_warning ("[%d] sid %u: created segment '%s', "
2434 "rx_fifo %p, tx_fifo %p",
2435 getpid (), client_session_index,
2436 client_session->segment_name,
2437 client_session->server_rx_fifo,
2438 client_session->server_tx_fifo);
2440 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2443 ssvm_shared_header_t *sh = seg->ssvm.sh;
2445 ssvm_lock_non_recursive (sh, 1);
2446 oldheap = ssvm_push_heap (sh);
2447 event_q = client_session->vpp_event_queue =
2448 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2449 sizeof (session_fifo_event_t),
2450 getpid (), 0 /* signal not sent */ );
2451 ssvm_pop_heap (oldheap);
2452 ssvm_unlock_non_recursive (sh);
2455 vppcom_send_connect_session_reply (client_session, 0);
2460 vppcom_send_accept_session_reply (client_session->vpp_handle, 0);
2463 if (VPPCOM_DEBUG > 0)
2464 clib_warning ("[%d] sid %u: accepted%sconnection to local %s address "
2466 getpid (), client_session_index, cut_thru_str,
2467 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2468 format_ip46_address, &client_session->lcl_addr.ip46,
2469 client_session->lcl_addr.is_ip4,
2470 clib_net_to_host_u16 (client_session->lcl_port));
2472 clib_spinlock_unlock (&vcm->sessions_lockp);
2473 rv = (int) client_session_index;
2479 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2481 session_t *session = 0;
2484 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2486 if (PREDICT_FALSE (session->is_vep))
2488 clib_spinlock_unlock (&vcm->sessions_lockp);
2489 clib_warning ("[%d] ERROR: sid %u: cannot connect on an epoll session!",
2490 getpid (), session_index);
2495 if (PREDICT_FALSE (session->is_server))
2497 clib_spinlock_unlock (&vcm->sessions_lockp);
2498 clib_warning ("[%d] ERROR: sid %u: is in use as a server session!",
2499 getpid (), session_index);
2504 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2506 if (VPPCOM_DEBUG > 0)
2507 clib_warning ("[%d] sid %u: session already connected to "
2508 "%s %U port %d proto %s, state 0x%x (%s)",
2509 getpid (), session_index,
2510 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2511 format_ip46_address,
2512 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2513 clib_net_to_host_u16 (session->peer_port),
2514 session->proto ? "UDP" : "TCP", session->state,
2515 vppcom_session_state_str (session->state));
2517 clib_spinlock_unlock (&vcm->sessions_lockp);
2522 session->vrf = server_ep->vrf;
2523 session->peer_addr.is_ip4 = server_ep->is_ip4;
2524 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2525 session->peer_port = server_ep->port;
2527 if (VPPCOM_DEBUG > 0)
2528 clib_warning ("[%d] sid %u: connecting to server %s %U port %d proto %s",
2529 getpid (), session_index,
2530 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2531 format_ip46_address,
2532 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2533 clib_net_to_host_u16 (session->peer_port),
2534 session->proto ? "UDP" : "TCP");
2536 vppcom_send_connect_sock (session, session_index);
2537 clib_spinlock_unlock (&vcm->sessions_lockp);
2539 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2540 vcm->cfg.session_timeout);
2541 if (PREDICT_FALSE (rv))
2543 if (VPPCOM_DEBUG > 0)
2544 clib_warning ("[%d] sid %u: connect failed! returning %d (%s)",
2545 getpid (), session_index, rv, vppcom_retval_str (rv));
2547 else if (VPPCOM_DEBUG > 0)
2548 clib_warning ("[%d] sid %d: connected!", getpid (), session_index);
2555 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2558 session_t *session = 0;
2559 svm_fifo_t *rx_fifo;
2564 session_state_t state;
2570 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2572 if (PREDICT_FALSE (session->is_vep))
2574 clib_spinlock_unlock (&vcm->sessions_lockp);
2575 clib_warning ("[%d] ERROR: sid %u: cannot read from an epoll session!",
2576 getpid (), session_index);
2581 is_server = session->is_server;
2582 is_nonblocking = session->is_nonblocking;
2583 state = session->state;
2584 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
2586 clib_spinlock_unlock (&vcm->sessions_lockp);
2587 rv = ((state == STATE_DISCONNECT) ?
2588 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2590 if (VPPCOM_DEBUG > 0)
2591 clib_warning ("[%d] sid %u: %s session is not open! "
2592 "state 0x%x (%s), returning %d (%s)",
2593 getpid (), session_index, is_server ?
2594 "server" : "client", state,
2595 vppcom_session_state_str (state),
2596 rv, vppcom_retval_str (rv));
2600 rx_fifo = ((!session->is_cut_thru || is_server) ?
2601 session->server_rx_fifo : session->server_tx_fifo);
2602 fifo_str = ((!session->is_cut_thru || is_server) ?
2603 "server_rx_fifo" : "server_tx_fifo");
2604 clib_spinlock_unlock (&vcm->sessions_lockp);
2609 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2611 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2613 while (!is_nonblocking && (n_read <= 0));
2617 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2619 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2620 (EPOLLET | EPOLLIN));
2622 session->vep.et_mask |= EPOLLIN;
2624 if (state == STATE_CLOSE_ON_EMPTY)
2626 session_state_t new_state = STATE_DISCONNECT;
2627 rv = VPPCOM_ECONNRESET;
2629 if (VPPCOM_DEBUG > 1)
2631 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2632 "state 0x%x (%s)!\n"
2633 " Setting state to %d (%s), returning %d (%s)",
2634 getpid (), session_index,
2635 is_server ? "server" : "client",
2636 state, vppcom_session_state_str (state),
2637 new_state, vppcom_session_state_str (new_state),
2638 rv, vppcom_retval_str (rv));
2641 session->state = new_state;
2646 clib_spinlock_unlock (&vcm->sessions_lockp);
2651 if (VPPCOM_DEBUG > 2)
2654 clib_warning ("[%d] sid %u: read %d bytes from %s (%p)", getpid (),
2655 session_index, n_read, fifo_str, rx_fifo);
2657 clib_warning ("[%d] sid %u: nothing read, returning %d (%s)",
2658 getpid (), session_index, rv, vppcom_retval_str (rv));
2665 vppcom_session_read (uint32_t session_index, void *buf, int n)
2667 return (vppcom_session_read_internal (session_index, buf, n, 0));
2671 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2673 return (vppcom_session_read_internal (session_index, buf, n, 1));
2677 vppcom_session_read_ready (session_t * session, u32 session_index)
2679 svm_fifo_t *rx_fifo = 0;
2683 u8 is_server = session->is_server;
2684 session_state_t state = session->state;
2686 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2687 if (PREDICT_FALSE (session->is_vep))
2689 clib_warning ("[%d] ERROR: sid %u: cannot read from an "
2690 "epoll session!", getpid (), session_index);
2695 if (session->is_listen)
2696 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2699 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2701 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2704 if (VPPCOM_DEBUG > 1)
2705 clib_warning ("[%d] sid %u: %s session is not open! "
2706 "state 0x%x (%s), returning %d (%s)", getpid (),
2707 session_index, is_server ? "server" : "client",
2708 state, vppcom_session_state_str (state),
2709 rv, vppcom_retval_str (rv));
2713 rx_fifo = ((!session->is_cut_thru || is_server) ?
2714 session->server_rx_fifo : session->server_tx_fifo);
2716 ready = svm_fifo_max_dequeue (rx_fifo);
2722 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2724 session->vep.et_mask |= EPOLLIN;
2726 if (state == STATE_CLOSE_ON_EMPTY)
2728 rv = VPPCOM_ECONNRESET;
2729 session_state_t new_state = STATE_DISCONNECT;
2731 if (VPPCOM_DEBUG > 1)
2733 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2734 "state 0x%x (%s)!\n"
2735 " Setting state to %d (%s), returning %d (%s)",
2736 getpid (), session_index,
2737 is_server ? "server" : "client",
2738 state, vppcom_session_state_str (state),
2739 new_state, vppcom_session_state_str (new_state),
2740 rv, vppcom_retval_str (rv));
2742 session->state = new_state;
2752 vppcom_session_write (uint32_t session_index, void *buf, int n)
2754 session_t *session = 0;
2755 svm_fifo_t *tx_fifo;
2756 unix_shared_memory_queue_t *q;
2757 session_fifo_event_t evt;
2763 session_state_t state;
2767 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2769 if (PREDICT_FALSE (session->is_vep))
2771 clib_spinlock_unlock (&vcm->sessions_lockp);
2772 clib_warning ("[%d] ERROR: sid %u: cannot write to an epoll session!",
2773 getpid (), session_index);
2779 is_server = session->is_server;
2780 is_nonblocking = session->is_nonblocking;
2781 state = session->state;
2782 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2784 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2787 clib_spinlock_unlock (&vcm->sessions_lockp);
2788 if (VPPCOM_DEBUG > 1)
2789 clib_warning ("[%d] sid %u: %s session is not open! state 0x%x (%s)",
2790 getpid (), session_index, is_server ?
2791 "server" : "client", state,
2792 vppcom_session_state_str (state));
2796 tx_fifo = ((!session->is_cut_thru || is_server) ?
2797 session->server_tx_fifo : session->server_rx_fifo);
2798 fifo_str = ((!session->is_cut_thru || is_server) ?
2799 "server_tx_fifo" : "server_rx_fifo");
2800 clib_spinlock_unlock (&vcm->sessions_lockp);
2804 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2806 while (!is_nonblocking && (n_write <= 0));
2808 /* If event wasn't set, add one */
2809 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2811 /* Fabricate TX event, send to vpp */
2813 evt.event_type = FIFO_EVENT_APP_TX;
2815 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2816 q = session->vpp_event_queue;
2818 unix_shared_memory_queue_add (q, (u8 *) & evt,
2819 0 /* do wait for mutex */ );
2820 clib_spinlock_unlock (&vcm->sessions_lockp);
2821 if (VPPCOM_DEBUG > 1)
2822 clib_warning ("[%d] sid %u: added FIFO_EVENT_APP_TX to "
2823 "vpp_event_q %p, n_write %d", getpid (),
2824 session_index, q, n_write);
2829 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2831 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2832 (EPOLLET | EPOLLOUT));
2834 session->vep.et_mask |= EPOLLOUT;
2836 if (state == STATE_CLOSE_ON_EMPTY)
2838 session_state_t new_state = STATE_DISCONNECT;
2839 rv = VPPCOM_ECONNRESET;
2841 if (VPPCOM_DEBUG > 1)
2843 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2844 "state 0x%x (%s)!\n"
2845 " Setting state to %d (%s), returning %d (%s)",
2846 getpid (), session_index,
2847 is_server ? "server" : "client",
2848 state, vppcom_session_state_str (state),
2849 new_state, vppcom_session_state_str (new_state),
2850 rv, vppcom_retval_str (rv));
2853 session->state = new_state;
2858 clib_spinlock_unlock (&vcm->sessions_lockp);
2863 if (VPPCOM_DEBUG > 2)
2866 clib_warning ("[%d] sid %u: FIFO-FULL %s (%p)", getpid (),
2867 session_index, fifo_str, tx_fifo);
2869 clib_warning ("[%d] sid %u: wrote %d bytes to %s (%p)", getpid (),
2870 session_index, n_write, fifo_str, tx_fifo);
2877 vppcom_session_write_ready (session_t * session, u32 session_index)
2879 svm_fifo_t *tx_fifo;
2884 u8 is_server = session->is_server;
2885 session_state_t state = session->state;
2887 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2888 if (PREDICT_FALSE (session->is_vep))
2890 clib_warning ("[%d] ERROR: sid %u: cannot write to an "
2891 "epoll session!", getpid (), session_index);
2896 if (PREDICT_FALSE (session->is_listen))
2898 clib_warning ("[%d] ERROR: sid %u: cannot write to a listen session!",
2899 getpid (), session_index);
2904 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2906 session_state_t state = session->state;
2908 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2911 clib_warning ("[%d] ERROR: sid %u: %s session is not open! "
2912 "state 0x%x (%s), returning %d (%s)", getpid (),
2913 session_index, is_server ? "server" : "client",
2914 state, vppcom_session_state_str (state),
2915 rv, vppcom_retval_str (rv));
2919 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2920 session->server_tx_fifo : session->server_rx_fifo);
2921 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2922 "server_tx_fifo" : "server_rx_fifo");
2924 ready = svm_fifo_max_enqueue (tx_fifo);
2926 if (VPPCOM_DEBUG > 3)
2927 clib_warning ("[%d] sid %u: peek %s (%p), ready = %d", getpid (),
2928 session_index, fifo_str, tx_fifo, ready);
2932 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2933 (EPOLLET | EPOLLOUT));
2935 session->vep.et_mask |= EPOLLOUT;
2937 if (state == STATE_CLOSE_ON_EMPTY)
2939 rv = VPPCOM_ECONNRESET;
2940 session_state_t new_state = STATE_DISCONNECT;
2942 if (VPPCOM_DEBUG > 1)
2944 clib_warning ("[%d] sid %u: Empty fifo with %s session "
2945 "state 0x%x (%s)!\n"
2946 " Setting state to %d (%s), returning %d (%s)",
2947 getpid (), session_index,
2948 is_server ? "server" : "client",
2949 state, vppcom_session_state_str (state),
2950 new_state, vppcom_session_state_str (new_state),
2951 rv, vppcom_retval_str (rv));
2953 session->state = new_state;
2963 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2964 unsigned long *write_map, unsigned long *except_map,
2965 double time_to_wait)
2968 session_t *session = 0;
2969 int rv, bits_set = 0;
2970 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2971 u32 minbits = clib_max (n_bits, BITS (uword));
2973 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2975 if (n_bits && read_map)
2977 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2978 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2979 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2981 if (n_bits && write_map)
2983 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2984 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2985 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2987 if (n_bits && except_map)
2989 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2990 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2991 memset (except_map, 0, vec_len (vcm->ex_bitmap));
3001 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3003 clib_spinlock_lock (&vcm->sessions_lockp);
3004 rv = vppcom_session_at_index (session_index, &session);
3007 clib_spinlock_unlock (&vcm->sessions_lockp);
3008 if (VPPCOM_DEBUG > 1)
3009 clib_warning ("[%d] session %d specified in "
3010 "read_map is closed.", getpid (),
3012 bits_set = VPPCOM_EBADFD;
3016 rv = vppcom_session_read_ready (session, session_index);
3017 clib_spinlock_unlock (&vcm->sessions_lockp);
3018 if (except_map && vcm->ex_bitmap &&
3019 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3022 // TBD: clib_warning
3023 clib_bitmap_set_no_check (except_map, session_index, 1);
3028 // TBD: clib_warning
3029 clib_bitmap_set_no_check (read_map, session_index, 1);
3037 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3039 clib_spinlock_lock (&vcm->sessions_lockp);
3040 rv = vppcom_session_at_index (session_index, &session);
3043 clib_spinlock_unlock (&vcm->sessions_lockp);
3044 if (VPPCOM_DEBUG > 0)
3045 clib_warning ("[%d] session %d specified in "
3046 "write_map is closed.", getpid (),
3048 bits_set = VPPCOM_EBADFD;
3052 rv = vppcom_session_write_ready (session, session_index);
3053 clib_spinlock_unlock (&vcm->sessions_lockp);
3054 if (write_map && (rv > 0))
3056 // TBD: clib_warning
3057 clib_bitmap_set_no_check (write_map, session_index, 1);
3065 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3067 clib_spinlock_lock (&vcm->sessions_lockp);
3068 rv = vppcom_session_at_index (session_index, &session);
3071 clib_spinlock_unlock (&vcm->sessions_lockp);
3072 if (VPPCOM_DEBUG > 1)
3073 clib_warning ("[%d] session %d specified in "
3074 "except_map is closed.", getpid (),
3076 bits_set = VPPCOM_EBADFD;
3080 rv = vppcom_session_read_ready (session, session_index);
3081 clib_spinlock_unlock (&vcm->sessions_lockp);
3084 // TBD: clib_warning
3085 clib_bitmap_set_no_check (except_map, session_index, 1);
3093 while (clib_time_now (&vcm->clib_time) < timeout);
3100 vep_verify_epoll_chain (u32 vep_idx)
3103 vppcom_epoll_t *vep;
3107 if (VPPCOM_DEBUG <= 1)
3110 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3111 rv = vppcom_session_at_index (vep_idx, &session);
3112 if (PREDICT_FALSE (rv))
3114 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
3117 if (PREDICT_FALSE (!session->is_vep))
3119 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
3123 vep = &session->vep;
3124 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3127 " is_vep_session = %u\n"
3128 " next_sid = 0x%x (%u)\n"
3129 " wait_cont_idx = 0x%x (%u)\n"
3130 "}\n", getpid (), vep_idx,
3131 session->is_vep, session->is_vep_session,
3132 vep->next_sid, vep->next_sid,
3133 session->wait_cont_idx, session->wait_cont_idx);
3135 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3137 rv = vppcom_session_at_index (sid, &session);
3138 if (PREDICT_FALSE (rv))
3140 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
3143 if (PREDICT_FALSE (session->is_vep))
3144 clib_warning ("[%d] ERROR: sid (%u) is a vep!", getpid (), vep_idx);
3145 else if (PREDICT_FALSE (!session->is_vep_session))
3147 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
3151 vep = &session->vep;
3152 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3153 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
3154 "vep_idx (%u)!", getpid (),
3155 sid, session->vep.vep_idx, vep_idx);
3156 if (session->is_vep_session)
3158 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3160 " next_sid = 0x%x (%u)\n"
3161 " prev_sid = 0x%x (%u)\n"
3162 " vep_idx = 0x%x (%u)\n"
3163 " ev.events = 0x%x\n"
3164 " ev.data.u64 = 0x%llx\n"
3168 vep->next_sid, vep->next_sid,
3169 vep->prev_sid, vep->prev_sid,
3170 vep->vep_idx, vep->vep_idx,
3171 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3176 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
3180 vppcom_epoll_create (void)
3182 session_t *vep_session;
3185 clib_spinlock_lock (&vcm->sessions_lockp);
3186 pool_get (vcm->sessions, vep_session);
3187 memset (vep_session, 0, sizeof (*vep_session));
3188 vep_idx = vep_session - vcm->sessions;
3190 vep_session->is_vep = 1;
3191 vep_session->vep.vep_idx = ~0;
3192 vep_session->vep.next_sid = ~0;
3193 vep_session->vep.prev_sid = ~0;
3194 vep_session->wait_cont_idx = ~0;
3195 vep_session->vpp_handle = ~0;
3196 clib_spinlock_unlock (&vcm->sessions_lockp);
3198 if (VPPCOM_DEBUG > 0)
3199 clib_warning ("[%d] Created vep_idx %u!", getpid (), vep_idx);
3205 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3206 struct epoll_event *event)
3208 session_t *vep_session;
3212 if (vep_idx == session_index)
3214 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3215 getpid (), vep_idx);
3216 return VPPCOM_EINVAL;
3219 clib_spinlock_lock (&vcm->sessions_lockp);
3220 rv = vppcom_session_at_index (vep_idx, &vep_session);
3221 if (PREDICT_FALSE (rv))
3223 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3226 if (PREDICT_FALSE (!vep_session->is_vep))
3228 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3229 getpid (), vep_idx);
3234 ASSERT (vep_session->vep.vep_idx == ~0);
3235 ASSERT (vep_session->vep.prev_sid == ~0);
3237 rv = vppcom_session_at_index (session_index, &session);
3238 if (PREDICT_FALSE (rv))
3240 if (VPPCOM_DEBUG > 0)
3241 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3242 getpid (), session_index);
3245 if (PREDICT_FALSE (session->is_vep))
3247 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3255 if (PREDICT_FALSE (!event))
3257 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3258 "epoll_event structure!", getpid ());
3262 if (vep_session->vep.next_sid != ~0)
3264 session_t *next_session;
3265 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3267 if (PREDICT_FALSE (rv))
3269 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3270 "vep.next_sid (%u) on vep_idx (%u)!",
3271 getpid (), vep_session->vep.next_sid, vep_idx);
3274 ASSERT (next_session->vep.prev_sid == vep_idx);
3275 next_session->vep.prev_sid = session_index;
3277 session->vep.next_sid = vep_session->vep.next_sid;
3278 session->vep.prev_sid = vep_idx;
3279 session->vep.vep_idx = vep_idx;
3280 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3281 session->vep.ev = *event;
3282 session->is_vep = 0;
3283 session->is_vep_session = 1;
3284 vep_session->vep.next_sid = session_index;
3285 if (VPPCOM_DEBUG > 1)
3286 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3287 " data 0x%llx!", getpid (), vep_idx, session_index,
3288 event->events, event->data.u64);
3292 if (PREDICT_FALSE (!event))
3294 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3295 "epoll_event structure!", getpid ());
3299 else if (PREDICT_FALSE (!session->is_vep_session))
3301 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3302 "not a vep session!", getpid (), session_index);
3306 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3308 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3309 "vep_idx (%u) != vep_idx (%u)!",
3310 getpid (), session_index,
3311 session->vep.vep_idx, vep_idx);
3315 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3316 session->vep.ev = *event;
3317 if (VPPCOM_DEBUG > 1)
3318 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3319 " data 0x%llx!", getpid (), vep_idx, session_index,
3320 event->events, event->data.u64);
3324 if (PREDICT_FALSE (!session->is_vep_session))
3326 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3327 "not a vep session!", getpid (), session_index);
3331 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3333 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3334 "vep_idx (%u) != vep_idx (%u)!",
3335 getpid (), session_index,
3336 session->vep.vep_idx, vep_idx);
3341 vep_session->wait_cont_idx =
3342 (vep_session->wait_cont_idx == session_index) ?
3343 session->vep.next_sid : vep_session->wait_cont_idx;
3345 if (session->vep.prev_sid == vep_idx)
3346 vep_session->vep.next_sid = session->vep.next_sid;
3349 session_t *prev_session;
3350 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3351 if (PREDICT_FALSE (rv))
3353 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3354 "vep.prev_sid (%u) on sid (%u)!",
3355 getpid (), session->vep.prev_sid, session_index);
3358 ASSERT (prev_session->vep.next_sid == session_index);
3359 prev_session->vep.next_sid = session->vep.next_sid;
3361 if (session->vep.next_sid != ~0)
3363 session_t *next_session;
3364 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3365 if (PREDICT_FALSE (rv))
3367 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3368 "vep.next_sid (%u) on sid (%u)!",
3369 getpid (), session->vep.next_sid, session_index);
3372 ASSERT (next_session->vep.prev_sid == session_index);
3373 next_session->vep.prev_sid = session->vep.prev_sid;
3376 memset (&session->vep, 0, sizeof (session->vep));
3377 session->vep.next_sid = ~0;
3378 session->vep.prev_sid = ~0;
3379 session->vep.vep_idx = ~0;
3380 session->is_vep_session = 0;
3381 if (VPPCOM_DEBUG > 1)
3382 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3383 getpid (), vep_idx, session_index);
3387 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3391 vep_verify_epoll_chain (vep_idx);
3394 clib_spinlock_unlock (&vcm->sessions_lockp);
3399 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3400 int maxevents, double wait_for_time)
3402 session_t *vep_session;
3404 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3405 u32 keep_trying = 1;
3407 u32 vep_next_sid, wait_cont_idx;
3410 if (PREDICT_FALSE (maxevents <= 0))
3412 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3413 getpid (), maxevents);
3414 return VPPCOM_EINVAL;
3416 memset (events, 0, sizeof (*events) * maxevents);
3418 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3419 vep_next_sid = vep_session->vep.next_sid;
3420 is_vep = vep_session->is_vep;
3421 wait_cont_idx = vep_session->wait_cont_idx;
3422 clib_spinlock_unlock (&vcm->sessions_lockp);
3424 if (PREDICT_FALSE (!is_vep))
3426 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3427 getpid (), vep_idx);
3431 if (PREDICT_FALSE (vep_next_sid == ~0))
3433 if (VPPCOM_DEBUG > 0)
3434 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3435 getpid (), vep_idx);
3445 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3446 sid != ~0; sid = next_sid)
3448 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3449 u8 add_event, is_vep_session;
3451 u64 session_ev_data;
3453 VCL_LOCK_AND_GET_SESSION (sid, &session);
3454 next_sid = session->vep.next_sid;
3455 session_events = session->vep.ev.events;
3456 et_mask = session->vep.et_mask;
3457 is_vep = session->is_vep;
3458 is_vep_session = session->is_vep_session;
3459 session_vep_idx = session->vep.vep_idx;
3460 session_ev_data = session->vep.ev.data.u64;
3461 clib_spinlock_unlock (&vcm->sessions_lockp);
3463 if (PREDICT_FALSE (is_vep))
3465 if (VPPCOM_DEBUG > 0)
3466 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3467 getpid (), vep_idx);
3471 if (PREDICT_FALSE (!is_vep_session))
3473 if (VPPCOM_DEBUG > 0)
3474 clib_warning ("[%d] ERROR: session (%u) is not "
3475 "a vep session!", getpid (), sid);
3479 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3481 clib_warning ("[%d] ERROR: session (%u) "
3482 "vep_idx (%u) != vep_idx (%u)!",
3483 getpid (), sid, session->vep.vep_idx, vep_idx);
3488 add_event = clear_et_mask = 0;
3490 if (EPOLLIN & session_events)
3492 VCL_LOCK_AND_GET_SESSION (sid, &session);
3493 ready = vppcom_session_read_ready (session, sid);
3494 clib_spinlock_unlock (&vcm->sessions_lockp);
3495 if ((ready > 0) && (EPOLLIN & et_mask))
3498 events[num_ev].events |= EPOLLIN;
3499 if (((EPOLLET | EPOLLIN) & session_events) ==
3500 (EPOLLET | EPOLLIN))
3501 clear_et_mask |= EPOLLIN;
3508 case VPPCOM_ECONNRESET:
3509 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3513 events[num_ev].events |= EPOLLERR;
3519 if (EPOLLOUT & session_events)
3521 VCL_LOCK_AND_GET_SESSION (sid, &session);
3522 ready = vppcom_session_write_ready (session, sid);
3523 clib_spinlock_unlock (&vcm->sessions_lockp);
3524 if ((ready > 0) && (EPOLLOUT & et_mask))
3527 events[num_ev].events |= EPOLLOUT;
3528 if (((EPOLLET | EPOLLOUT) & session_events) ==
3529 (EPOLLET | EPOLLOUT))
3530 clear_et_mask |= EPOLLOUT;
3537 case VPPCOM_ECONNRESET:
3538 events[num_ev].events |= EPOLLHUP;
3542 events[num_ev].events |= EPOLLERR;
3550 events[num_ev].data.u64 = session_ev_data;
3551 if (EPOLLONESHOT & session_events)
3553 VCL_LOCK_AND_GET_SESSION (sid, &session);
3554 session->vep.ev.events = 0;
3555 clib_spinlock_unlock (&vcm->sessions_lockp);
3558 if (num_ev == maxevents)
3560 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3561 vep_session->wait_cont_idx = next_sid;
3562 clib_spinlock_unlock (&vcm->sessions_lockp);
3566 if (wait_cont_idx != ~0)
3569 next_sid = vep_next_sid;
3570 else if (next_sid == wait_cont_idx)
3574 if (wait_for_time != -1)
3575 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3577 while ((num_ev == 0) && keep_trying);
3579 if (wait_cont_idx != ~0)
3581 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3582 vep_session->wait_cont_idx = ~0;
3583 clib_spinlock_unlock (&vcm->sessions_lockp);
3586 return (rv != VPPCOM_OK) ? rv : num_ev;
3590 vppcom_session_attr (uint32_t session_index, uint32_t op,
3591 void *buffer, uint32_t * buflen)
3595 u32 *flags = buffer;
3596 vppcom_endpt_t *ep = buffer;
3598 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3601 case VPPCOM_ATTR_GET_NREAD:
3602 rv = vppcom_session_read_ready (session, session_index);
3603 if (VPPCOM_DEBUG > 2)
3604 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3608 case VPPCOM_ATTR_GET_NWRITE:
3609 rv = vppcom_session_write_ready (session, session_index);
3610 if (VPPCOM_DEBUG > 2)
3611 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3612 getpid (), session_index, rv);
3615 case VPPCOM_ATTR_GET_FLAGS:
3616 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3618 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3619 *buflen = sizeof (*flags);
3620 if (VPPCOM_DEBUG > 2)
3621 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3622 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3623 session_index, *flags, session->is_nonblocking);
3629 case VPPCOM_ATTR_SET_FLAGS:
3630 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3632 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3633 if (VPPCOM_DEBUG > 2)
3634 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3635 "flags = 0x%08x, is_nonblocking = %u",
3636 getpid (), session_index, *flags,
3637 session->is_nonblocking);
3643 case VPPCOM_ATTR_GET_PEER_ADDR:
3644 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3646 ep->vrf = session->vrf;
3647 ep->is_ip4 = session->peer_addr.is_ip4;
3648 ep->port = session->peer_port;
3649 if (session->peer_addr.is_ip4)
3650 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3651 sizeof (ip4_address_t));
3653 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3654 sizeof (ip6_address_t));
3655 *buflen = sizeof (*ep);
3656 if (VPPCOM_DEBUG > 1)
3657 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
3658 "%u, addr = %U, port %u", getpid (),
3659 session_index, ep->is_ip4, format_ip46_address,
3660 &session->peer_addr.ip46, ep->is_ip4,
3661 clib_net_to_host_u16 (ep->port));
3667 case VPPCOM_ATTR_GET_LCL_ADDR:
3668 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3670 ep->vrf = session->vrf;
3671 ep->is_ip4 = session->lcl_addr.is_ip4;
3672 ep->port = session->lcl_port;
3673 if (session->lcl_addr.is_ip4)
3674 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3675 sizeof (ip4_address_t));
3677 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3678 sizeof (ip6_address_t));
3679 *buflen = sizeof (*ep);
3680 if (VPPCOM_DEBUG > 1)
3681 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
3682 "%u, addr = %U port %d", getpid (),
3683 session_index, ep->is_ip4, format_ip46_address,
3684 &session->lcl_addr.ip46, ep->is_ip4,
3685 clib_net_to_host_u16 (ep->port));
3691 case VPPCOM_ATTR_SET_REUSEADDR:
3694 case VPPCOM_ATTR_SET_BROADCAST:
3697 case VPPCOM_ATTR_SET_V6ONLY:
3700 case VPPCOM_ATTR_SET_KEEPALIVE:
3703 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3706 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3715 clib_spinlock_unlock (&vcm->sessions_lockp);
3720 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3721 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3724 session_t *session = 0;
3728 clib_spinlock_lock (&vcm->sessions_lockp);
3729 rv = vppcom_session_at_index (session_index, &session);
3730 if (PREDICT_FALSE (rv))
3732 clib_spinlock_unlock (&vcm->sessions_lockp);
3733 if (VPPCOM_DEBUG > 0)
3734 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3735 getpid (), session_index);
3737 clib_spinlock_unlock (&vcm->sessions_lockp);
3740 ep->vrf = session->vrf;
3741 ep->is_ip4 = session->peer_addr.is_ip4;
3742 ep->port = session->peer_port;
3743 if (session->peer_addr.is_ip4)
3744 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3745 sizeof (ip4_address_t));
3747 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3748 sizeof (ip6_address_t));
3749 clib_spinlock_unlock (&vcm->sessions_lockp);
3753 rv = vppcom_session_read (session_index, buffer, buflen);
3754 else if (flags & MSG_PEEK)
3755 rv = vppcom_session_peek (session_index, buffer, buflen);
3758 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3759 rv = VPPCOM_EAFNOSUPPORT;
3767 vppcom_session_sendto (uint32_t session_index, void *buffer,
3768 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3771 return VPPCOM_EINVAL;
3776 return VPPCOM_EINVAL;
3781 // TBD check the flags and do the right thing
3782 if (VPPCOM_DEBUG > 2)
3783 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3784 getpid (), flags, flags);
3787 return (vppcom_session_write (session_index, buffer, buflen));
3791 * fd.io coding-style-patch-verification: ON
3794 * eval: (c-set-style "gnu")