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_
135 u32 vpp_api_q_length;
138 u32 add_segment_size;
139 u32 preallocated_fifo_pairs;
142 u32 event_queue_size;
143 u32 listen_queue_size;
144 u8 app_proxy_transport_tcp;
145 u8 app_proxy_transport_udp;
149 u64 namespace_secret;
155 typedef struct vppcom_main_t_
159 u32 *client_session_index_fifo;
162 /* vpe input queue */
163 unix_shared_memory_queue_t *vl_input_queue;
165 /* API client handle */
169 clib_spinlock_t sessions_lockp;
172 /* Hash table for disconnect processing */
173 uword *session_index_by_vpp_handles;
176 clib_bitmap_t *rd_bitmap;
177 clib_bitmap_t *wr_bitmap;
178 clib_bitmap_t *ex_bitmap;
180 /* Our event queue */
181 unix_shared_memory_queue_t *app_event_queue;
183 /* unique segment name counter */
184 u32 unique_segment_index;
186 /* For deadman timers */
187 clib_time_t clib_time;
189 /* State of the connection, shared between msg RX thread and main thread */
190 volatile app_state_t app_state;
194 /* VNET_API_ERROR_FOO -> "Foo" hash table */
195 uword *error_string_by_error_number;
198 /* NOTE: _vppcom_main is only used until the heap is allocated.
199 * Do not access it directly -- use vcm which will point to
200 * the heap allocated copy after init.
202 static vppcom_main_t _vppcom_main = {
203 .debug = VPPCOM_DEBUG_INIT,
204 .my_client_index = ~0
207 static vppcom_main_t *vcm = &_vppcom_main;
209 #define VCL_LOCK_AND_GET_SESSION(I, S) \
211 clib_spinlock_lock (&vcm->sessions_lockp); \
212 rv = vppcom_session_at_index (I, S); \
213 if (PREDICT_FALSE (rv)) \
215 clib_spinlock_unlock (&vcm->sessions_lockp); \
216 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
223 vppcom_app_state_str (app_state_t state)
229 case STATE_APP_START:
230 st = "STATE_APP_START";
233 case STATE_APP_CONN_VPP:
234 st = "STATE_APP_CONN_VPP";
237 case STATE_APP_ENABLED:
238 st = "STATE_APP_ENABLED";
241 case STATE_APP_ATTACHED:
242 st = "STATE_APP_ATTACHED";
246 st = "UNKNOWN_APP_STATE";
254 vppcom_session_state_str (session_state_t state)
265 st = "STATE_CONNECT";
276 case STATE_CLOSE_ON_EMPTY:
277 st = "STATE_CLOSE_ON_EMPTY";
280 case STATE_DISCONNECT:
281 st = "STATE_DISCONNECT";
289 st = "UNKNOWN_STATE";
297 * VPPCOM Utility Functions
300 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
302 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
303 if (PREDICT_FALSE ((session_index == ~0) ||
304 pool_is_free_index (vcm->sessions, session_index)))
306 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
307 getpid (), session_index);
308 return VPPCOM_EBADFD;
310 *sess = pool_elt_at_index (vcm->sessions, session_index);
315 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
317 /* Session and listener handles have different formats. The latter has
318 * the thread index in the upper 32 bits while the former has the session
319 * type. Knowing that, for listeners we just flip the MSB to 1 */
320 listener_handle |= 1ULL << 63;
321 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
324 static inline session_t *
325 vppcom_session_table_lookup_listener (u64 listener_handle)
328 u64 handle = listener_handle | (1ULL << 63);
331 p = hash_get (vcm->session_index_by_vpp_handles, handle);
334 clib_warning ("[%d] couldn't find listen session: unknown vpp "
335 "listener handle %llx", getpid (), listener_handle);
338 if (pool_is_free_index (vcm->sessions, p[0]))
340 if (VPPCOM_DEBUG > 1)
341 clib_warning ("[%d] invalid listen session, sid (%u)", getpid (),
346 session = pool_elt_at_index (vcm->sessions, p[0]);
347 ASSERT (session->is_listen);
352 vppcom_session_table_del_listener (u64 listener_handle)
354 listener_handle |= 1ULL << 63;
355 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
359 vppcom_connect_to_vpp (char *app_name)
361 api_main_t *am = &api_main;
363 if (VPPCOM_DEBUG > 0)
364 printf ("\nConnecting to VPP api...");
365 if (vl_client_connect_to_vlib ("/vpe-api", app_name,
366 vcm->cfg.vpp_api_q_length) < 0)
368 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
369 return VPPCOM_ECONNREFUSED;
372 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
373 vcm->my_client_index = am->my_client_index;
374 if (VPPCOM_DEBUG > 0)
375 printf (" connected!\n");
377 vcm->app_state = STATE_APP_CONN_VPP;
382 format_api_error (u8 * s, va_list * args)
384 i32 error = va_arg (*args, u32);
387 p = hash_get (vcm->error_string_by_error_number, -error);
390 s = format (s, "%s (%d)", p[0], error);
392 s = format (s, "%d", error);
397 vppcom_init_error_string_table (void)
399 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
401 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
402 foreach_vnet_api_error;
405 hash_set (vcm->error_string_by_error_number, 99, "Misc");
409 vppcom_wait_for_app_state_change (app_state_t app_state)
411 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
413 while (clib_time_now (&vcm->clib_time) < timeout)
415 if (vcm->app_state == app_state)
418 if (VPPCOM_DEBUG > 0)
419 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
420 vppcom_app_state_str (app_state), app_state);
421 return VPPCOM_ETIMEDOUT;
425 vppcom_wait_for_session_state_change (u32 session_index,
426 session_state_t state,
429 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
430 session_t *volatile session;
435 clib_spinlock_lock (&vcm->sessions_lockp);
436 rv = vppcom_session_at_index (session_index, &session);
437 if (PREDICT_FALSE (rv))
439 clib_spinlock_unlock (&vcm->sessions_lockp);
442 if (session->state == state)
444 clib_spinlock_unlock (&vcm->sessions_lockp);
447 if (session->state == STATE_FAILED)
449 clib_spinlock_unlock (&vcm->sessions_lockp);
450 return VPPCOM_ECONNREFUSED;
453 clib_spinlock_unlock (&vcm->sessions_lockp);
455 while (clib_time_now (&vcm->clib_time) < timeout);
457 if (VPPCOM_DEBUG > 0)
458 clib_warning ("[%d] timeout waiting for state 0x%x (%s)", getpid (),
459 state, vppcom_session_state_str (state));
460 return VPPCOM_ETIMEDOUT;
464 vppcom_wait_for_client_session_index (f64 wait_for_time)
466 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
470 if (clib_fifo_elts (vcm->client_session_index_fifo))
473 while (clib_time_now (&vcm->clib_time) < timeout);
475 if (wait_for_time == 0)
476 return VPPCOM_EAGAIN;
478 if (VPPCOM_DEBUG > 0)
479 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
480 return VPPCOM_ETIMEDOUT;
484 * VPP-API message functions
487 vppcom_send_session_enable_disable (u8 is_enable)
489 vl_api_session_enable_disable_t *bmp;
490 bmp = vl_msg_api_alloc (sizeof (*bmp));
491 memset (bmp, 0, sizeof (*bmp));
493 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
494 bmp->client_index = vcm->my_client_index;
495 bmp->context = htonl (0xfeedface);
496 bmp->is_enable = is_enable;
497 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
501 vppcom_app_session_enable (void)
505 if (vcm->app_state != STATE_APP_ENABLED)
507 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
508 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
509 if (PREDICT_FALSE (rv))
511 if (VPPCOM_DEBUG > 0)
512 clib_warning ("[%d] application session enable timed out! "
514 getpid (), rv, vppcom_retval_str (rv));
522 vl_api_session_enable_disable_reply_t_handler
523 (vl_api_session_enable_disable_reply_t * mp)
527 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
528 format_api_error, ntohl (mp->retval));
531 vcm->app_state = STATE_APP_ENABLED;
535 vppcom_app_send_attach (void)
537 vl_api_application_attach_t *bmp;
538 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
539 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
540 vcm->cfg.app_proxy_transport_udp);
542 bmp = vl_msg_api_alloc (sizeof (*bmp));
543 memset (bmp, 0, sizeof (*bmp));
545 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
546 bmp->client_index = vcm->my_client_index;
547 bmp->context = htonl (0xfeedface);
548 bmp->options[APP_OPTIONS_FLAGS] =
549 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
550 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
551 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
552 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
553 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
554 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
555 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
556 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
557 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
558 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
559 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
560 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
561 vcm->cfg.preallocated_fifo_pairs;
564 bmp->namespace_id_len = nsid_len;
565 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
566 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
568 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
572 vppcom_app_attach (void)
576 vppcom_app_send_attach ();
577 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
578 if (PREDICT_FALSE (rv))
580 if (VPPCOM_DEBUG > 0)
581 clib_warning ("[%d] application attach timed out! returning %d (%s)",
582 getpid (), rv, vppcom_retval_str (rv));
589 vppcom_app_detach (void)
591 vl_api_application_detach_t *bmp;
592 bmp = vl_msg_api_alloc (sizeof (*bmp));
593 memset (bmp, 0, sizeof (*bmp));
595 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
596 bmp->client_index = vcm->my_client_index;
597 bmp->context = htonl (0xfeedface);
598 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
602 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
605 static svm_fifo_segment_create_args_t _a;
606 svm_fifo_segment_create_args_t *a = &_a;
609 memset (a, 0, sizeof (*a));
612 clib_warning ("[%d] attach failed: %U", getpid (),
613 format_api_error, ntohl (mp->retval));
617 if (mp->segment_name_length == 0)
619 clib_warning ("[%d] segment_name_length zero", getpid ());
623 a->segment_name = (char *) mp->segment_name;
624 a->segment_size = mp->segment_size;
626 ASSERT (mp->app_event_queue_address);
628 /* Attach to the segment vpp created */
629 rv = svm_fifo_segment_attach (a);
630 vec_reset_length (a->new_segment_indices);
631 if (PREDICT_FALSE (rv))
633 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
638 vcm->app_event_queue =
639 uword_to_pointer (mp->app_event_queue_address,
640 unix_shared_memory_queue_t *);
642 vcm->app_state = STATE_APP_ATTACHED;
646 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
650 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
653 vcm->app_state = STATE_APP_ENABLED;
657 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
661 clib_warning ("[%d] vpp handle 0x%llx: disconnect session failed: %U",
662 getpid (), mp->handle, format_api_error,
667 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
669 static svm_fifo_segment_create_args_t _a;
670 svm_fifo_segment_create_args_t *a = &_a;
673 memset (a, 0, sizeof (*a));
674 a->segment_name = (char *) mp->segment_name;
675 a->segment_size = mp->segment_size;
676 /* Attach to the segment vpp created */
677 rv = svm_fifo_segment_attach (a);
678 vec_reset_length (a->new_segment_indices);
679 if (PREDICT_FALSE (rv))
681 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
682 getpid (), mp->segment_name);
685 if (VPPCOM_DEBUG > 1)
686 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
687 mp->segment_name, mp->segment_size);
691 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
695 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
699 session_t *session = 0;
700 u32 session_index = p[0];
702 VCL_LOCK_AND_GET_SESSION (session_index, &session);
703 session->state = STATE_CLOSE_ON_EMPTY;
705 if (VPPCOM_DEBUG > 1)
706 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
707 "setting state to 0x%x (%s)",
708 getpid (), mp->handle, session_index, session->state,
709 vppcom_session_state_str (session->state));
710 clib_spinlock_unlock (&vcm->sessions_lockp);
714 if (VPPCOM_DEBUG > 1)
715 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
716 "session lookup failed!",
717 getpid (), mp->handle, session_index);
720 clib_warning ("[%d] vpp handle 0x%llx: session lookup by "
721 "handle failed!", getpid (), mp->handle);
725 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
727 session_t *session = 0;
728 vl_api_reset_session_reply_t *rmp;
732 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
736 clib_spinlock_lock (&vcm->sessions_lockp);
737 rval = vppcom_session_at_index (p[0], &session);
738 if (PREDICT_FALSE (rval))
740 rv = VNET_API_ERROR_INVALID_VALUE_2;
741 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
742 "session lookup failed! returning %d %U",
743 getpid (), mp->handle, p[0],
744 rv, format_api_error, rv);
748 /* TBD: should this disconnect immediately and
751 session->state = STATE_CLOSE_ON_EMPTY;
753 if (VPPCOM_DEBUG > 1)
754 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
755 "state set to %d (%s)!", getpid (),
756 mp->handle, p[0], session->state,
757 vppcom_session_state_str (session->state));
759 clib_spinlock_unlock (&vcm->sessions_lockp);
763 rv = VNET_API_ERROR_INVALID_VALUE;
764 clib_warning ("[%d] ERROR: vpp handle 0x%llx: session lookup "
765 "failed! returning %d %U",
766 getpid (), mp->handle, rv, format_api_error, rv);
769 rmp = vl_msg_api_alloc (sizeof (*rmp));
770 memset (rmp, 0, sizeof (*rmp));
771 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
772 rmp->retval = htonl (rv);
773 rmp->handle = mp->handle;
774 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
778 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
780 session_t *session = 0;
782 svm_fifo_t *rx_fifo, *tx_fifo;
786 session_index = mp->context;
787 VCL_LOCK_AND_GET_SESSION (session_index, &session);
791 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
792 "connect failed! %U",
793 getpid (), mp->handle, session_index,
794 format_api_error, ntohl (mp->retval));
797 session->state = STATE_FAILED;
798 session->vpp_handle = mp->handle;
802 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
803 "Invalid session index (%u)!",
804 getpid (), mp->handle, session_index);
812 /* We've been redirected */
813 if (mp->segment_name_length > 0)
815 static svm_fifo_segment_create_args_t _a;
816 svm_fifo_segment_create_args_t *a = &_a;
819 memset (a, 0, sizeof (*a));
820 a->segment_name = (char *) mp->segment_name;
821 if (VPPCOM_DEBUG > 1)
822 clib_warning ("[%d] cut-thru segment: %s\n",
823 getpid (), a->segment_name);
825 rv = svm_fifo_segment_attach (a);
826 vec_reset_length (a->new_segment_indices);
827 if (PREDICT_FALSE (rv))
829 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
830 getpid (), a->segment_name);
838 session->is_cut_thru = is_cut_thru;
839 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
840 unix_shared_memory_queue_t *);
842 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
843 rx_fifo->client_session_index = session_index;
844 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
845 tx_fifo->client_session_index = session_index;
847 session->server_rx_fifo = rx_fifo;
848 session->server_tx_fifo = tx_fifo;
849 session->vpp_handle = mp->handle;
850 session->lcl_addr.is_ip4 = mp->is_ip4;
851 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
852 sizeof (session->peer_addr.ip46));
853 session->lcl_port = mp->lcl_port;
854 session->state = STATE_CONNECT;
856 /* Add it to lookup table */
857 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
859 if (VPPCOM_DEBUG > 1)
860 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connect succeeded!"
861 " session_rx_fifo %p, refcnt %d,"
862 " session_tx_fifo %p, refcnt %d",
863 getpid (), mp->handle, session_index,
864 session->server_rx_fifo,
865 session->server_rx_fifo->refcnt,
866 session->server_tx_fifo, session->server_tx_fifo->refcnt);
868 clib_spinlock_unlock (&vcm->sessions_lockp);
872 vppcom_send_connect_sock (session_t * session, u32 session_index)
874 vl_api_connect_sock_t *cmp;
876 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
877 session->is_server = 0;
878 cmp = vl_msg_api_alloc (sizeof (*cmp));
879 memset (cmp, 0, sizeof (*cmp));
880 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
881 cmp->client_index = vcm->my_client_index;
882 cmp->context = session_index;
884 cmp->vrf = session->vrf;
885 cmp->is_ip4 = session->peer_addr.is_ip4;
886 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
887 cmp->port = session->peer_port;
888 cmp->proto = session->proto;
889 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
890 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
894 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
897 vl_api_disconnect_session_reply_t *rmp;
899 if (VPPCOM_DEBUG > 1)
900 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
901 getpid (), vpp_handle, session_index);
903 rmp = vl_msg_api_alloc (sizeof (*rmp));
904 memset (rmp, 0, sizeof (*rmp));
906 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
907 rmp->retval = htonl (rv);
908 rmp->handle = vpp_handle;
909 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
913 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
915 vl_api_disconnect_session_t *dmp;
917 if (VPPCOM_DEBUG > 1)
918 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
919 getpid (), vpp_handle, session_index);
921 dmp = vl_msg_api_alloc (sizeof (*dmp));
922 memset (dmp, 0, sizeof (*dmp));
923 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
924 dmp->client_index = vcm->my_client_index;
925 dmp->handle = vpp_handle;
926 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
930 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
932 session_t *session = 0;
933 u32 session_index = mp->context;
936 VCL_LOCK_AND_GET_SESSION (session_index, &session);
940 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
941 getpid (), mp->handle, session_index, format_api_error,
943 rv = vppcom_session_at_index (session_index, &session);
946 session->state = STATE_FAILED;
947 session->vpp_handle = mp->handle;
951 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
952 "Invalid session index (%u)!",
953 getpid (), mp->handle, session_index);
958 session->vpp_handle = mp->handle;
959 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
960 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
961 sizeof (session->peer_addr.ip46));
962 session->lcl_port = mp->lcl_port;
963 vppcom_session_table_add_listener (mp->handle, session_index);
964 session->is_listen = 1;
965 session->state = STATE_LISTEN;
967 if (VPPCOM_DEBUG > 1)
968 clib_warning ("[%d] vpp handle 0x%llx, sid %u: bind succeeded!",
969 getpid (), mp->handle, mp->context);
971 clib_spinlock_unlock (&vcm->sessions_lockp);
975 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
978 clib_warning ("[%d] ERROR: sid %u: unbind failed: %U",
979 getpid (), mp->context, format_api_error,
982 else if (VPPCOM_DEBUG > 1)
983 clib_warning ("[%d] sid %u: unbind succeeded!", getpid (), mp->context);
987 format_ip4_address (u8 * s, va_list * args)
989 u8 *a = va_arg (*args, u8 *);
990 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
994 format_ip6_address (u8 * s, va_list * args)
996 ip6_address_t *a = va_arg (*args, ip6_address_t *);
997 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
999 i_max_n_zero = ARRAY_LEN (a->as_u16);
1001 i_first_zero = i_max_n_zero;
1003 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1005 u32 is_zero = a->as_u16[i] == 0;
1006 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1012 if ((!is_zero && n_zeros > max_n_zeros)
1013 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1015 i_max_n_zero = i_first_zero;
1016 max_n_zeros = n_zeros;
1017 i_first_zero = ARRAY_LEN (a->as_u16);
1022 last_double_colon = 0;
1023 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1025 if (i == i_max_n_zero && max_n_zeros > 1)
1027 s = format (s, "::");
1028 i += max_n_zeros - 1;
1029 last_double_colon = 1;
1033 s = format (s, "%s%x",
1034 (last_double_colon || i == 0) ? "" : ":",
1035 clib_net_to_host_u16 (a->as_u16[i]));
1036 last_double_colon = 0;
1043 /* Format an IP46 address. */
1045 format_ip46_address (u8 * s, va_list * args)
1047 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1048 ip46_type_t type = va_arg (*args, ip46_type_t);
1054 is_ip4 = ip46_address_is_ip4 (ip46);
1065 format (s, "%U", format_ip4_address, &ip46->ip4) :
1066 format (s, "%U", format_ip6_address, &ip46->ip6);
1070 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1072 vl_api_accept_session_reply_t *rmp;
1074 rmp = vl_msg_api_alloc (sizeof (*rmp));
1075 memset (rmp, 0, sizeof (*rmp));
1076 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1077 rmp->retval = htonl (retval);
1078 rmp->context = context;
1079 rmp->handle = handle;
1080 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1084 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1086 svm_fifo_t *rx_fifo, *tx_fifo;
1087 session_t *session, *listen_session;
1090 clib_spinlock_lock (&vcm->sessions_lockp);
1091 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1093 clib_warning ("[%d] client session queue is full!", getpid ());
1094 vppcom_send_accept_session_reply (mp->handle, mp->context,
1095 VNET_API_ERROR_QUEUE_FULL);
1096 clib_spinlock_unlock (&vcm->sessions_lockp);
1100 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1101 if (!listen_session)
1103 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1104 "listener handle %llx", getpid (), mp->listener_handle);
1105 clib_spinlock_unlock (&vcm->sessions_lockp);
1109 /* Allocate local session and set it up */
1110 pool_get (vcm->sessions, session);
1111 memset (session, 0, sizeof (*session));
1112 session_index = session - vcm->sessions;
1114 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1115 rx_fifo->client_session_index = session_index;
1116 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1117 tx_fifo->client_session_index = session_index;
1119 session->vpp_handle = mp->handle;
1120 session->client_context = mp->context;
1121 session->server_rx_fifo = rx_fifo;
1122 session->server_tx_fifo = tx_fifo;
1123 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1124 unix_shared_memory_queue_t *);
1125 session->state = STATE_ACCEPT;
1126 session->is_cut_thru = 0;
1127 session->is_server = 1;
1128 session->peer_port = mp->port;
1129 session->peer_addr.is_ip4 = mp->is_ip4;
1130 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1131 sizeof (session->peer_addr.ip46));
1133 /* Add it to lookup table */
1134 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1135 session->lcl_port = listen_session->lcl_port;
1136 session->lcl_addr = listen_session->lcl_addr;
1138 /* TBD: move client_session_index_fifo into listener session */
1139 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1141 clib_spinlock_unlock (&vcm->sessions_lockp);
1143 if (VPPCOM_DEBUG > 1)
1144 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client accept "
1145 "request from %s address %U port %d queue %p!", getpid (),
1146 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1147 format_ip46_address, &mp->ip, mp->is_ip4,
1148 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1152 vppcom_send_connect_session_reply (session_t * session, u32 session_index,
1153 u64 vpp_handle, u32 context, int retval)
1155 vl_api_connect_session_reply_t *rmp;
1157 unix_shared_memory_queue_t *client_q;
1159 rmp = vl_msg_api_alloc (sizeof (*rmp));
1160 memset (rmp, 0, sizeof (*rmp));
1161 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1165 rmp->context = context;
1166 rmp->handle = vpp_handle;
1167 rmp->retval = htonl (retval);
1168 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1172 rmp->context = session->client_context;
1173 rmp->retval = htonl (retval);
1174 rmp->handle = session->vpp_handle;
1175 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1176 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1177 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1178 rmp->segment_size = vcm->cfg.segment_size;
1179 len = vec_len (session->segment_name);
1180 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1181 clib_memcpy (rmp->segment_name, session->segment_name,
1182 rmp->segment_name_length - 1);
1183 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1184 sizeof (rmp->lcl_ip));
1185 rmp->is_ip4 = session->peer_addr.is_ip4;
1186 rmp->lcl_port = session->peer_port;
1187 client_q = uword_to_pointer (session->client_queue_address,
1188 unix_shared_memory_queue_t *);
1190 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1194 * Acting as server for redirected connect requests
1197 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1200 session_t *session = 0;
1202 clib_spinlock_lock (&vcm->sessions_lockp);
1203 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1205 clib_spinlock_unlock (&vcm->sessions_lockp);
1207 if (VPPCOM_DEBUG > 1)
1208 clib_warning ("[%d] client session queue is full!", getpid ());
1210 /* TBD: Fix api to include vpp handle */
1211 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1212 0 /* handle */ , mp->context,
1213 VNET_API_ERROR_QUEUE_FULL);
1217 pool_get (vcm->sessions, session);
1218 memset (session, 0, sizeof (*session));
1219 session_index = session - vcm->sessions;
1221 session->client_context = mp->context;
1222 session->vpp_handle = session_index;
1223 session->client_queue_address = mp->client_queue_address;
1224 session->is_cut_thru = 1;
1225 session->is_server = 1;
1226 session->lcl_port = mp->port;
1227 session->lcl_addr.is_ip4 = mp->is_ip4;
1228 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1229 sizeof (session->lcl_addr.ip46));
1231 /* TBD: missing peer info in api msg.
1233 session->peer_addr.is_ip4 = mp->is_ip4;
1234 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1236 session->state = STATE_ACCEPT;
1237 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1238 if (VPPCOM_DEBUG > 1)
1239 clib_warning ("[%d] sid %u: Got a cut-thru connect request! "
1240 "clib_fifo_elts %u!\n", getpid (), session_index,
1241 clib_fifo_elts (vcm->client_session_index_fifo));
1242 clib_spinlock_unlock (&vcm->sessions_lockp);
1246 vppcom_send_bind_sock (session_t * session, u32 session_index)
1248 vl_api_bind_sock_t *bmp;
1250 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1251 session->is_server = 1;
1252 bmp = vl_msg_api_alloc (sizeof (*bmp));
1253 memset (bmp, 0, sizeof (*bmp));
1255 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1256 bmp->client_index = vcm->my_client_index;
1257 bmp->context = session_index;
1258 bmp->vrf = session->vrf;
1259 bmp->is_ip4 = session->lcl_addr.is_ip4;
1260 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1261 bmp->port = session->lcl_port;
1262 bmp->proto = session->proto;
1263 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1264 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1268 vppcom_send_unbind_sock (u64 vpp_handle)
1270 vl_api_unbind_sock_t *ump;
1272 ump = vl_msg_api_alloc (sizeof (*ump));
1273 memset (ump, 0, sizeof (*ump));
1275 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1276 ump->client_index = vcm->my_client_index;
1277 ump->handle = vpp_handle;
1278 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1282 vppcom_session_unbind (u32 session_index)
1284 session_t *session = 0;
1288 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1290 vpp_handle = session->vpp_handle;
1291 vppcom_session_table_del_listener (vpp_handle);
1292 session->vpp_handle = ~0;
1293 session->state = STATE_DISCONNECT;
1295 clib_spinlock_unlock (&vcm->sessions_lockp);
1297 if (VPPCOM_DEBUG > 1)
1298 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1299 "sending unbind msg! new state 0x%x (%s)",
1300 getpid (), vpp_handle, session_index,
1301 session->state, vppcom_session_state_str (session->state));
1303 vppcom_send_unbind_sock (vpp_handle);
1310 vppcom_session_disconnect (u32 session_index)
1314 u8 is_cut_thru, is_listen, is_server;
1316 session_state_t state;
1318 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1320 vpp_handle = session->vpp_handle;
1321 is_server = session->is_server;
1322 is_listen = session->is_listen;
1323 is_cut_thru = session->is_cut_thru;
1324 state = session->state;
1325 clib_spinlock_unlock (&vcm->sessions_lockp);
1327 if (VPPCOM_DEBUG > 1)
1329 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s state 0x%x (%s), "
1330 "is_cut_thru %d, is_listen %d",
1331 getpid (), vpp_handle, session_index,
1332 is_server ? "server" : "client",
1333 state, vppcom_session_state_str (state),
1334 is_cut_thru, is_listen);
1337 if (PREDICT_FALSE (is_listen))
1339 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
1340 "Cannot disconnect a listen socket!",
1341 getpid (), vpp_handle, session_index);
1346 /* Through the VPP host stack...
1348 else if (!is_cut_thru)
1350 /* The peer has already initiated the close,
1351 * so send the disconnect session reply.
1353 if (state & STATE_CLOSE_ON_EMPTY)
1355 vppcom_send_disconnect_session_reply (vpp_handle,
1356 session_index, 0 /* rv */ );
1357 if (VPPCOM_DEBUG > 1)
1358 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1359 "sending disconnect REPLY...",
1360 getpid (), vpp_handle, session_index);
1363 /* Otherwise, send a disconnect session msg...
1367 if (VPPCOM_DEBUG > 1)
1368 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1369 "sending disconnect...",
1370 getpid (), vpp_handle, session_index);
1372 vppcom_send_disconnect_session (vpp_handle, session_index);
1376 /* Cut-thru connections...
1378 * server: free fifos and segment allocated during connect/redirect
1379 * client: no cleanup required
1385 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1386 svm_fifo_segment_private_t *seg;
1388 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1390 if (VPPCOM_DEBUG > 1)
1391 clib_warning ("[%d] sid %d: freeing cut-thru fifos in "
1393 " server_rx_fifo %p, refcnt = %d"
1394 " server_tx_fifo %p, refcnt = %d",
1395 getpid (), session_index, session->sm_seg_index,
1396 session->server_rx_fifo,
1397 session->server_rx_fifo->refcnt,
1398 session->server_tx_fifo,
1399 session->server_tx_fifo->refcnt);
1401 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1402 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1403 FIFO_SEGMENT_RX_FREELIST);
1404 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1405 FIFO_SEGMENT_TX_FREELIST);
1406 svm_fifo_segment_delete (seg);
1408 /* TBD: Send cut-thru disconnect event to client */
1410 clib_spinlock_unlock (&vcm->sessions_lockp);
1414 /* TBD: Send cut-thru disconnect event to server */
1422 #define foreach_sock_msg \
1423 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1424 _(BIND_SOCK_REPLY, bind_sock_reply) \
1425 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1426 _(ACCEPT_SESSION, accept_session) \
1427 _(CONNECT_SOCK, connect_sock) \
1428 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1429 _(DISCONNECT_SESSION, disconnect_session) \
1430 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1431 _(RESET_SESSION, reset_session) \
1432 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1433 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1434 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1437 vppcom_api_hookup (void)
1440 vl_msg_api_set_handlers(VL_API_##N, #n, \
1441 vl_api_##n##_t_handler, \
1443 vl_api_##n##_t_endian, \
1444 vl_api_##n##_t_print, \
1445 sizeof(vl_api_##n##_t), 1);
1451 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1455 vcl_cfg->heapsize = (256ULL << 20);
1456 vcl_cfg->vpp_api_q_length = 1024;
1457 vcl_cfg->segment_baseva = 0x200000000ULL;
1458 vcl_cfg->segment_size = (256 << 20);
1459 vcl_cfg->add_segment_size = (128 << 20);
1460 vcl_cfg->preallocated_fifo_pairs = 8;
1461 vcl_cfg->rx_fifo_size = (1 << 20);
1462 vcl_cfg->tx_fifo_size = (1 << 20);
1463 vcl_cfg->event_queue_size = 2048;
1464 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1465 vcl_cfg->app_timeout = 10 * 60.0;
1466 vcl_cfg->session_timeout = 10 * 60.0;
1467 vcl_cfg->accept_timeout = 60.0;
1471 vppcom_cfg_heapsize (char *conf_fname)
1473 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1486 fp = fopen (conf_fname, "r");
1489 if (VPPCOM_DEBUG > 0)
1490 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1493 argv = calloc (1, sizeof (char *));
1499 if (fgets (inbuf, 4096, fp) == 0)
1501 p = strtok (inbuf, " \t\n");
1507 char **tmp = realloc (argv, argc * sizeof (char *));
1511 arg = strndup (p, 1024);
1514 argv[argc - 1] = arg;
1515 p = strtok (NULL, " \t\n");
1522 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1529 * Look for and parse the "heapsize" config parameter.
1530 * Manual since none of the clib infra has been bootstrapped yet.
1532 * Format: heapsize <nn>[mM][gG]
1535 for (i = 1; i < (argc - 1); i++)
1537 if (!strncmp (argv[i], "heapsize", 8))
1539 sizep = (u8 *) argv[i + 1];
1541 while (*sizep >= '0' && *sizep <= '9')
1544 size += *sizep++ - '0';
1548 if (VPPCOM_DEBUG > 0)
1549 clib_warning ("[%d] parse error '%s %s', "
1550 "using default heapsize %lld (0x%llx)",
1551 getpid (), argv[i], argv[i + 1],
1552 vcl_cfg->heapsize, vcl_cfg->heapsize);
1556 if (*sizep == 'g' || *sizep == 'G')
1557 vcl_cfg->heapsize = size << 30;
1558 else if (*sizep == 'm' || *sizep == 'M')
1559 vcl_cfg->heapsize = size << 20;
1562 if (VPPCOM_DEBUG > 0)
1563 clib_warning ("[%d] parse error '%s %s', "
1564 "using default heapsize %lld (0x%llx)",
1565 getpid (), argv[i], argv[i + 1],
1566 vcl_cfg->heapsize, vcl_cfg->heapsize);
1578 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1579 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1580 if (vcl_mem == MAP_FAILED)
1582 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1583 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1585 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1588 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1591 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1594 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1597 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1601 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1604 if (VPPCOM_DEBUG > 0)
1605 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1606 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1610 vppcom_cfg_read (char *conf_fname)
1612 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1614 unformat_input_t _input, *input = &_input;
1615 unformat_input_t _line_input, *line_input = &_line_input;
1616 u8 vc_cfg_input = 0;
1619 u32 uid, gid, q_len;
1621 fd = open (conf_fname, O_RDONLY);
1624 if (VPPCOM_DEBUG > 0)
1625 clib_warning ("[%d] open configuration file '%s' failed!",
1626 getpid (), conf_fname);
1630 if (fstat (fd, &s) < 0)
1632 if (VPPCOM_DEBUG > 0)
1633 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1637 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1639 if (VPPCOM_DEBUG > 0)
1640 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1644 unformat_init_clib_file (input, fd);
1646 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1648 (void) unformat_user (input, unformat_line_input, line_input);
1649 unformat_skip_white_space (line_input);
1651 if (unformat (line_input, "vcl {"))
1659 if (unformat (line_input, "heapsize %s", &chroot_path))
1661 vec_terminate_c_string (chroot_path);
1662 if (VPPCOM_DEBUG > 0)
1663 clib_warning ("[%d] configured heapsize %s, "
1664 "actual heapsize %lld (0x%llx)",
1665 getpid (), chroot_path, vcl_cfg->heapsize,
1667 vec_free (chroot_path);
1669 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1671 vec_terminate_c_string (chroot_path);
1672 vl_set_memory_root_path ((char *) chroot_path);
1673 if (VPPCOM_DEBUG > 0)
1674 clib_warning ("[%d] configured api-prefix %s",
1675 getpid (), chroot_path);
1676 chroot_path = 0; /* Don't vec_free() it! */
1678 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1680 if (q_len < vcl_cfg->vpp_api_q_length)
1682 clib_warning ("[%d] ERROR: configured vpp-api-q-length "
1683 "(%u) is too small! Using default: %u ",
1684 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1688 vcl_cfg->vpp_api_q_length = q_len;
1690 if (VPPCOM_DEBUG > 0)
1691 clib_warning ("[%d] configured vpp-api-q-length %u",
1692 getpid (), vcl_cfg->vpp_api_q_length);
1695 else if (unformat (line_input, "uid %d", &uid))
1697 vl_set_memory_uid (uid);
1698 if (VPPCOM_DEBUG > 0)
1699 clib_warning ("[%d] configured uid %d", getpid (), uid);
1701 else if (unformat (line_input, "gid %d", &gid))
1703 vl_set_memory_gid (gid);
1704 if (VPPCOM_DEBUG > 0)
1705 clib_warning ("[%d] configured gid %d", getpid (), gid);
1707 else if (unformat (line_input, "segment-baseva 0x%lx",
1708 &vcl_cfg->segment_baseva))
1710 if (VPPCOM_DEBUG > 0)
1711 clib_warning ("[%d] configured segment_baseva 0x%lx",
1712 getpid (), vcl_cfg->segment_baseva);
1714 else if (unformat (line_input, "segment-size 0x%lx",
1715 &vcl_cfg->segment_size))
1717 if (VPPCOM_DEBUG > 0)
1718 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1719 getpid (), vcl_cfg->segment_size,
1720 vcl_cfg->segment_size);
1722 else if (unformat (line_input, "segment-size %ld",
1723 &vcl_cfg->segment_size))
1725 if (VPPCOM_DEBUG > 0)
1726 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1727 getpid (), vcl_cfg->segment_size,
1728 vcl_cfg->segment_size);
1730 else if (unformat (line_input, "add-segment-size 0x%lx",
1731 &vcl_cfg->add_segment_size))
1733 if (VPPCOM_DEBUG > 0)
1735 ("[%d] configured add_segment_size 0x%lx (%ld)",
1736 getpid (), vcl_cfg->add_segment_size,
1737 vcl_cfg->add_segment_size);
1739 else if (unformat (line_input, "add-segment-size %ld",
1740 &vcl_cfg->add_segment_size))
1742 if (VPPCOM_DEBUG > 0)
1744 ("[%d] configured add_segment_size %ld (0x%lx)",
1745 getpid (), vcl_cfg->add_segment_size,
1746 vcl_cfg->add_segment_size);
1748 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1749 &vcl_cfg->preallocated_fifo_pairs))
1751 if (VPPCOM_DEBUG > 0)
1752 clib_warning ("[%d] configured preallocated_fifo_pairs "
1753 "%d (0x%x)", getpid (),
1754 vcl_cfg->preallocated_fifo_pairs,
1755 vcl_cfg->preallocated_fifo_pairs);
1757 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1758 &vcl_cfg->rx_fifo_size))
1760 if (VPPCOM_DEBUG > 0)
1761 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1762 getpid (), vcl_cfg->rx_fifo_size,
1763 vcl_cfg->rx_fifo_size);
1765 else if (unformat (line_input, "rx-fifo-size %ld",
1766 &vcl_cfg->rx_fifo_size))
1768 if (VPPCOM_DEBUG > 0)
1769 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1770 getpid (), vcl_cfg->rx_fifo_size,
1771 vcl_cfg->rx_fifo_size);
1773 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1774 &vcl_cfg->tx_fifo_size))
1776 if (VPPCOM_DEBUG > 0)
1777 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1778 getpid (), vcl_cfg->tx_fifo_size,
1779 vcl_cfg->tx_fifo_size);
1781 else if (unformat (line_input, "tx-fifo-size %ld",
1782 &vcl_cfg->tx_fifo_size))
1784 if (VPPCOM_DEBUG > 0)
1785 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1786 getpid (), vcl_cfg->tx_fifo_size,
1787 vcl_cfg->tx_fifo_size);
1789 else if (unformat (line_input, "event-queue-size 0x%lx",
1790 &vcl_cfg->event_queue_size))
1792 if (VPPCOM_DEBUG > 0)
1793 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1794 getpid (), vcl_cfg->event_queue_size,
1795 vcl_cfg->event_queue_size);
1797 else if (unformat (line_input, "event-queue-size %ld",
1798 &vcl_cfg->event_queue_size))
1800 if (VPPCOM_DEBUG > 0)
1801 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1802 getpid (), vcl_cfg->event_queue_size,
1803 vcl_cfg->event_queue_size);
1805 else if (unformat (line_input, "listen-queue-size 0x%lx",
1806 &vcl_cfg->listen_queue_size))
1808 if (VPPCOM_DEBUG > 0)
1809 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1810 getpid (), vcl_cfg->listen_queue_size,
1811 vcl_cfg->listen_queue_size);
1813 else if (unformat (line_input, "listen-queue-size %ld",
1814 &vcl_cfg->listen_queue_size))
1816 if (VPPCOM_DEBUG > 0)
1817 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1818 getpid (), vcl_cfg->listen_queue_size,
1819 vcl_cfg->listen_queue_size);
1821 else if (unformat (line_input, "app-timeout %f",
1822 &vcl_cfg->app_timeout))
1824 if (VPPCOM_DEBUG > 0)
1825 clib_warning ("[%d] configured app_timeout %f",
1826 getpid (), vcl_cfg->app_timeout);
1828 else if (unformat (line_input, "session-timeout %f",
1829 &vcl_cfg->session_timeout))
1831 if (VPPCOM_DEBUG > 0)
1832 clib_warning ("[%d] configured session_timeout %f",
1833 getpid (), vcl_cfg->session_timeout);
1835 else if (unformat (line_input, "accept-timeout %f",
1836 &vcl_cfg->accept_timeout))
1838 if (VPPCOM_DEBUG > 0)
1839 clib_warning ("[%d] configured accept_timeout %f",
1840 getpid (), vcl_cfg->accept_timeout);
1842 else if (unformat (line_input, "app-proxy-transport-tcp"))
1844 vcl_cfg->app_proxy_transport_tcp = 1;
1845 if (VPPCOM_DEBUG > 0)
1846 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1847 getpid (), vcl_cfg->app_proxy_transport_tcp);
1849 else if (unformat (line_input, "app-proxy-transport-udp"))
1851 vcl_cfg->app_proxy_transport_udp = 1;
1852 if (VPPCOM_DEBUG > 0)
1853 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1854 getpid (), vcl_cfg->app_proxy_transport_udp);
1856 else if (unformat (line_input, "app-scope-local"))
1858 vcl_cfg->app_scope_local = 1;
1859 if (VPPCOM_DEBUG > 0)
1860 clib_warning ("[%d] configured app_scope_local (%d)",
1861 getpid (), vcl_cfg->app_scope_local);
1863 else if (unformat (line_input, "app-scope-global"))
1865 vcl_cfg->app_scope_global = 1;
1866 if (VPPCOM_DEBUG > 0)
1867 clib_warning ("[%d] configured app_scope_global (%d)",
1868 getpid (), vcl_cfg->app_scope_global);
1870 else if (unformat (line_input, "namespace-secret %lu",
1871 &vcl_cfg->namespace_secret))
1873 if (VPPCOM_DEBUG > 0)
1875 ("[%d] configured namespace_secret %lu (0x%lx)",
1876 getpid (), vcl_cfg->namespace_secret,
1877 vcl_cfg->namespace_secret);
1879 else if (unformat (line_input, "namespace-id %v",
1880 &vcl_cfg->namespace_id))
1882 vl_api_application_attach_t *mp;
1883 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1884 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1885 if (nsid_vec_len > max_nsid_vec_len)
1887 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1888 if (VPPCOM_DEBUG > 0)
1889 clib_warning ("[%d] configured namespace_id is too long,"
1890 " truncated to %d characters!", getpid (),
1894 if (VPPCOM_DEBUG > 0)
1895 clib_warning ("[%d] configured namespace_id %v",
1896 getpid (), vcl_cfg->namespace_id);
1898 else if (unformat (line_input, "}"))
1901 if (VPPCOM_DEBUG > 0)
1902 clib_warning ("[%d] completed parsing vppcom config!",
1908 if (line_input->buffer[line_input->index] != '#')
1910 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1912 &line_input->buffer[line_input->index]);
1919 unformat_free (input);
1927 * VPPCOM Public API functions
1930 vppcom_app_create (char *app_name)
1932 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1943 vppcom_cfg_init (vcl_cfg);
1944 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1948 if (sscanf (env_var_str, "%u", &tmp) != 1)
1949 clib_warning ("[%d] Invalid debug level specified in "
1950 "the environment variable "
1952 " (%s)!\n", getpid (), env_var_str);
1956 clib_warning ("[%d] configured debug level (%u) from "
1957 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1960 conf_fname = getenv (VPPCOM_ENV_CONF);
1963 conf_fname = VPPCOM_CONF_DEFAULT;
1964 if (VPPCOM_DEBUG > 0)
1965 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1968 vppcom_cfg_heapsize (conf_fname);
1969 clib_fifo_validate (vcm->client_session_index_fifo,
1970 vcm->cfg.listen_queue_size);
1971 vppcom_cfg_read (conf_fname);
1972 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1975 u32 ns_id_vec_len = strlen (env_var_str);
1977 vec_reset_length (vcm->cfg.namespace_id);
1978 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1979 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1981 if (VPPCOM_DEBUG > 0)
1982 clib_warning ("[%d] configured namespace_id (%v) from "
1983 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1984 vcm->cfg.namespace_id);
1986 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1990 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1991 clib_warning ("[%d] Invalid namespace secret specified in "
1992 "the environment variable "
1993 VPPCOM_ENV_APP_NAMESPACE_SECRET
1994 " (%s)!\n", getpid (), env_var_str);
1997 vcm->cfg.namespace_secret = tmp;
1998 if (VPPCOM_DEBUG > 0)
1999 clib_warning ("[%d] configured namespace secret (%lu) from "
2000 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
2001 vcm->cfg.namespace_secret);
2004 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2006 vcm->cfg.app_proxy_transport_tcp = 1;
2007 if (VPPCOM_DEBUG > 0)
2008 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
2009 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
2010 vcm->cfg.app_proxy_transport_tcp);
2012 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2014 vcm->cfg.app_proxy_transport_udp = 1;
2015 if (VPPCOM_DEBUG > 0)
2016 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
2017 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
2018 vcm->cfg.app_proxy_transport_udp);
2020 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2022 vcm->cfg.app_scope_local = 1;
2023 if (VPPCOM_DEBUG > 0)
2024 clib_warning ("[%d] configured app_scope_local (%u) from "
2025 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
2026 vcm->cfg.app_scope_local);
2028 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2030 vcm->cfg.app_scope_global = 1;
2031 if (VPPCOM_DEBUG > 0)
2032 clib_warning ("[%d] configured app_scope_global (%u) from "
2033 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
2034 vcm->cfg.app_scope_global);
2037 vcm->main_cpu = os_get_thread_index ();
2038 heap = clib_mem_get_per_cpu_heap ();
2039 h = mheap_header (heap);
2041 /* make the main heap thread-safe */
2042 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2044 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2046 clib_time_init (&vcm->clib_time);
2047 vppcom_init_error_string_table ();
2048 svm_fifo_segment_init (vcl_cfg->segment_baseva,
2049 20 /* timeout in secs */ );
2050 clib_spinlock_init (&vcm->sessions_lockp);
2051 vppcom_api_hookup ();
2054 if (vcm->my_client_index == ~0)
2056 vcm->app_state = STATE_APP_START;
2057 rv = vppcom_connect_to_vpp (app_name);
2060 clib_warning ("[%d] ERROR: couldn't connect to VPP!", getpid ());
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("[%d] sending session enable", getpid ());
2067 rv = vppcom_app_session_enable ();
2070 clib_warning ("[%d] ERROR: vppcom_app_session_enable() failed!",
2075 if (VPPCOM_DEBUG > 0)
2076 clib_warning ("[%d] sending app attach", getpid ());
2078 rv = vppcom_app_attach ();
2081 clib_warning ("[%d] ERROR: vppcom_app_attach() failed!", getpid ());
2085 if (VPPCOM_DEBUG > 0)
2086 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
2087 getpid (), app_name, vcm->my_client_index,
2088 vcm->my_client_index);
2095 vppcom_app_destroy (void)
2099 if (vcm->my_client_index == ~0)
2102 if (VPPCOM_DEBUG > 0)
2103 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
2104 getpid (), vcm->my_client_index, vcm->my_client_index);
2106 vppcom_app_detach ();
2107 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2108 if (PREDICT_FALSE (rv))
2110 if (VPPCOM_DEBUG > 0)
2111 clib_warning ("[%d] application detach timed out! returning %d (%s)",
2112 getpid (), rv, vppcom_retval_str (rv));
2114 vl_client_disconnect_from_vlib ();
2115 vcm->my_client_index = ~0;
2116 vcm->app_state = STATE_APP_START;
2120 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2125 clib_spinlock_lock (&vcm->sessions_lockp);
2126 pool_get (vcm->sessions, session);
2127 memset (session, 0, sizeof (*session));
2128 session_index = session - vcm->sessions;
2131 session->proto = proto;
2132 session->state = STATE_START;
2133 session->is_nonblocking = is_nonblocking ? 1 : 0;
2134 session->vpp_handle = ~0;
2135 clib_spinlock_unlock (&vcm->sessions_lockp);
2137 if (VPPCOM_DEBUG > 0)
2138 clib_warning ("[%d] sid %u", getpid (), session_index);
2140 return (int) session_index;
2144 vppcom_session_close (uint32_t session_index)
2146 session_t *session = 0;
2155 session_state_t state;
2157 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2158 is_listen = session->is_listen;
2159 is_vep = session->is_vep;
2160 is_vep_session = session->is_vep_session;
2161 next_sid = session->vep.next_sid;
2162 vep_idx = session->vep.vep_idx;
2163 state = session->state;
2164 vpp_handle = session->vpp_handle;
2165 clib_spinlock_unlock (&vcm->sessions_lockp);
2167 if (VPPCOM_DEBUG > 0)
2170 clib_warning ("[%d] vep_idx %u / sid %u: closing epoll session...",
2171 getpid (), session_index, session_index);
2173 clib_warning ("[%d] vpp handle 0x%llx, sid %d: closing session...",
2174 getpid (), vpp_handle, session_index);
2179 while (next_sid != ~0)
2181 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2182 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2183 clib_warning ("[%d] vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2184 "vep_idx %u failed! rv %d (%s)", getpid (),
2185 vpp_handle, next_sid, vep_idx,
2186 rv, vppcom_retval_str (rv));
2188 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2189 next_sid = session->vep.next_sid;
2190 clib_spinlock_unlock (&vcm->sessions_lockp);
2197 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2198 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2199 clib_warning ("[%d] vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2200 "vep_idx %u failed! rv %d (%s)",
2201 getpid (), vpp_handle, session_index,
2202 vep_idx, rv, vppcom_retval_str (rv));
2207 if (state == STATE_LISTEN)
2209 rv = vppcom_session_unbind (session_index);
2210 if (PREDICT_FALSE (rv < 0))
2212 if (VPPCOM_DEBUG > 0)
2213 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2214 "listener unbind failed! rv %d (%s)",
2215 getpid (), vpp_handle, session_index,
2216 rv, vppcom_retval_str (rv));
2221 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2223 rv = vppcom_session_disconnect (session_index);
2224 if (PREDICT_FALSE (rv < 0))
2225 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2226 "session disconnect failed! rv %d (%s)",
2227 getpid (), vpp_handle, session_index,
2228 rv, vppcom_retval_str (rv));
2232 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2233 vpp_handle = session->vpp_handle;
2234 if (vpp_handle != ~0)
2236 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2238 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2240 pool_put_index (vcm->sessions, session_index);
2241 clib_spinlock_unlock (&vcm->sessions_lockp);
2243 if (VPPCOM_DEBUG > 0)
2246 clib_warning ("[%d] vep_idx %u / sid %u: epoll session removed.",
2247 getpid (), session_index, session_index);
2249 clib_warning ("[%d] vpp handle 0x%llx, sid %u: session removed.",
2250 getpid (), vpp_handle, session_index);
2257 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2259 session_t *session = 0;
2263 return VPPCOM_EINVAL;
2265 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2267 if (session->is_vep)
2269 clib_spinlock_unlock (&vcm->sessions_lockp);
2270 clib_warning ("[%d] ERROR: sid %u: cannot bind to an epoll session!",
2271 getpid (), session_index);
2276 session->vrf = ep->vrf;
2277 session->lcl_addr.is_ip4 = ep->is_ip4;
2278 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2279 session->lcl_port = ep->port;
2281 if (VPPCOM_DEBUG > 0)
2282 clib_warning ("[%d] sid %u: binding to local %s address %U "
2283 "port %u, proto %s", getpid (), session_index,
2284 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2285 format_ip46_address, &session->lcl_addr.ip46,
2286 session->lcl_addr.is_ip4,
2287 clib_net_to_host_u16 (session->lcl_port),
2288 session->proto ? "UDP" : "TCP");
2290 clib_spinlock_unlock (&vcm->sessions_lockp);
2296 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2298 session_t *listen_session = 0;
2299 u64 listen_vpp_handle;
2302 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2304 if (listen_session->is_vep)
2306 clib_spinlock_unlock (&vcm->sessions_lockp);
2307 clib_warning ("[%d] ERROR: sid %u: cannot listen on an "
2308 "epoll session!", getpid (), listen_session_index);
2313 listen_vpp_handle = listen_session->vpp_handle;
2314 if (listen_session->is_listen)
2316 clib_spinlock_unlock (&vcm->sessions_lockp);
2317 if (VPPCOM_DEBUG > 0)
2318 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2319 "already in listen state!",
2320 getpid (), listen_vpp_handle, listen_session_index);
2325 if (VPPCOM_DEBUG > 0)
2326 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending bind request...",
2327 getpid (), listen_vpp_handle, listen_session_index);
2329 vppcom_send_bind_sock (listen_session, listen_session_index);
2330 clib_spinlock_unlock (&vcm->sessions_lockp);
2332 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2333 vcm->cfg.session_timeout);
2335 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2336 if (PREDICT_FALSE (retval))
2338 if (VPPCOM_DEBUG > 0)
2339 clib_warning ("[%d] vpp handle 0x%llx, sid %u: bind failed! "
2340 "returning %d (%s)", getpid (),
2341 listen_session->vpp_handle, listen_session_index,
2342 retval, vppcom_retval_str (retval));
2343 clib_spinlock_unlock (&vcm->sessions_lockp);
2348 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2349 clib_spinlock_unlock (&vcm->sessions_lockp);
2355 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2356 uint32_t flags, double wait_for_time)
2358 session_t *listen_session = 0;
2359 session_t *client_session = 0;
2360 u32 client_session_index = ~0;
2364 u64 listen_vpp_handle;
2366 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2368 if (listen_session->is_vep)
2370 clib_spinlock_unlock (&vcm->sessions_lockp);
2371 clib_warning ("[%d] ERROR: sid %u: cannot accept on an "
2372 "epoll session!", getpid (), listen_session_index);
2377 listen_vpp_handle = listen_session->vpp_handle;
2378 if (listen_session->state != STATE_LISTEN)
2380 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2381 "not in listen state! state 0x%x (%s)", getpid (),
2382 listen_vpp_handle, listen_session_index,
2383 listen_session->state,
2384 vppcom_session_state_str (listen_session->state));
2385 clib_spinlock_unlock (&vcm->sessions_lockp);
2389 wait_for = ((listen_session->is_nonblocking) ? 0 :
2390 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time);
2392 clib_spinlock_unlock (&vcm->sessions_lockp);
2396 rv = vppcom_wait_for_client_session_index (wait_for);
2399 if ((VPPCOM_DEBUG > 0))
2400 clib_warning ("[%d] vpp handle 0x%llx, sid %u: accept failed! "
2401 "returning %d (%s)", getpid (),
2402 listen_vpp_handle, listen_session_index,
2403 rv, vppcom_retval_str (rv));
2404 if ((wait_for == 0) || (wait_for_time > 0))
2411 clib_spinlock_lock (&vcm->sessions_lockp);
2412 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2413 rv = vppcom_session_at_index (client_session_index, &client_session);
2414 if (PREDICT_FALSE (rv))
2416 rv = VPPCOM_ECONNABORTED;
2417 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client sid %u "
2418 "lookup failed! returning %d (%s)", getpid (),
2419 listen_vpp_handle, listen_session_index,
2420 client_session_index, rv, vppcom_retval_str (rv));
2424 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2425 if (VPPCOM_DEBUG > 0)
2426 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Got a client request! "
2427 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2428 getpid (), listen_vpp_handle, listen_session_index,
2429 client_session->vpp_handle, client_session_index,
2430 flags, client_session->is_nonblocking);
2432 ep->vrf = client_session->vrf;
2433 ep->is_cut_thru = client_session->is_cut_thru;
2434 ep->is_ip4 = client_session->peer_addr.is_ip4;
2435 ep->port = client_session->peer_port;
2436 if (client_session->peer_addr.is_ip4)
2437 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2438 sizeof (ip4_address_t));
2440 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2441 sizeof (ip6_address_t));
2443 if (client_session->is_server && client_session->is_cut_thru)
2445 static svm_fifo_segment_create_args_t _a;
2446 svm_fifo_segment_create_args_t *a = &_a;
2447 svm_fifo_segment_private_t *seg;
2449 cut_thru_str = " cut-thru ";
2451 /* Create the segment */
2452 memset (a, 0, sizeof (*a));
2453 a->segment_name = (char *)
2454 format ((u8 *) a->segment_name, "%d:segment%d%c",
2455 getpid (), vcm->unique_segment_index++, 0);
2456 a->segment_size = vcm->cfg.segment_size;
2457 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2458 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2459 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2461 rv = svm_fifo_segment_create (a);
2462 if (PREDICT_FALSE (rv))
2464 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2465 "client sid %u svm_fifo_segment_create ('%s') "
2466 "failed! rv %d", getpid (), listen_vpp_handle,
2467 listen_session_index, client_session_index,
2468 a->segment_name, rv);
2469 vec_reset_length (a->new_segment_indices);
2470 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2471 vppcom_send_connect_session_reply (client_session,
2472 client_session_index,
2473 client_session->vpp_handle,
2474 client_session->client_context,
2476 clib_spinlock_unlock (&vcm->sessions_lockp);
2481 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2482 client_session->sm_seg_index = a->new_segment_indices[0];
2483 vec_free (a->new_segment_indices);
2485 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2486 client_session->server_rx_fifo =
2487 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2488 FIFO_SEGMENT_RX_FREELIST);
2489 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2491 svm_fifo_segment_delete (seg);
2492 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2493 "client sid %u rx fifo alloc failed! "
2494 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2495 listen_session_index, client_session_index,
2496 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2497 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2498 vppcom_send_connect_session_reply (client_session,
2499 client_session_index,
2500 client_session->vpp_handle,
2501 client_session->client_context,
2503 clib_spinlock_unlock (&vcm->sessions_lockp);
2507 client_session->server_rx_fifo->master_session_index =
2508 client_session_index;
2510 client_session->server_tx_fifo =
2511 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2512 FIFO_SEGMENT_TX_FREELIST);
2513 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2515 svm_fifo_segment_delete (seg);
2516 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2517 "client sid %u tx fifo alloc failed! "
2518 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2519 listen_session_index, client_session_index,
2520 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2521 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2522 vppcom_send_connect_session_reply (client_session,
2523 client_session_index,
2524 client_session->vpp_handle,
2525 client_session->client_context,
2527 clib_spinlock_unlock (&vcm->sessions_lockp);
2531 client_session->server_tx_fifo->master_session_index =
2532 client_session_index;
2534 if (VPPCOM_DEBUG > 1)
2535 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client sid %u "
2536 "created segment '%s', rx_fifo %p, tx_fifo %p",
2537 getpid (), listen_vpp_handle, listen_session_index,
2538 client_session_index, client_session->segment_name,
2539 client_session->server_rx_fifo,
2540 client_session->server_tx_fifo);
2542 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2545 ssvm_shared_header_t *sh = seg->ssvm.sh;
2547 ssvm_lock_non_recursive (sh, 1);
2548 oldheap = ssvm_push_heap (sh);
2549 event_q = client_session->vpp_event_queue =
2550 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2551 sizeof (session_fifo_event_t),
2552 getpid (), 0 /* signal not sent */ );
2553 ssvm_pop_heap (oldheap);
2554 ssvm_unlock_non_recursive (sh);
2557 vppcom_send_connect_session_reply (client_session,
2558 client_session_index,
2559 client_session->vpp_handle,
2560 client_session->client_context,
2561 0 /* retval OK */ );
2566 vppcom_send_accept_session_reply (client_session->vpp_handle,
2567 client_session->client_context,
2568 0 /* retval OK */ );
2571 if (VPPCOM_DEBUG > 0)
2572 clib_warning ("[%d] vpp handle 0x%llx, sid %u: accepted vpp handle "
2573 "0x%llx, sid %u%sconnection to local %s address "
2574 "%U port %u", getpid (), listen_vpp_handle,
2575 listen_session_index, client_session->vpp_handle,
2576 client_session_index, cut_thru_str,
2577 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2578 format_ip46_address, &client_session->lcl_addr.ip46,
2579 client_session->lcl_addr.is_ip4,
2580 clib_net_to_host_u16 (client_session->lcl_port));
2582 clib_spinlock_unlock (&vcm->sessions_lockp);
2583 rv = (int) client_session_index;
2589 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2591 session_t *session = 0;
2592 int rv, retval = VPPCOM_OK;
2593 u64 vpp_handle = ~0;
2595 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2597 if (PREDICT_FALSE (session->is_vep))
2599 clib_spinlock_unlock (&vcm->sessions_lockp);
2600 clib_warning ("[%d] ERROR: sid %u: cannot connect on an epoll session!",
2601 getpid (), session_index);
2606 vpp_handle = session->vpp_handle;
2607 if (PREDICT_FALSE (session->is_server))
2609 clib_spinlock_unlock (&vcm->sessions_lockp);
2610 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: is in use "
2611 "as a server session!", getpid (), vpp_handle,
2617 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2619 if (VPPCOM_DEBUG > 0)
2620 clib_warning ("[%d] vpp handle 0x%llx, sid %u: session already "
2621 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2622 getpid (), vpp_handle, session_index,
2623 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2624 format_ip46_address,
2625 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2626 clib_net_to_host_u16 (session->peer_port),
2627 session->proto ? "UDP" : "TCP", session->state,
2628 vppcom_session_state_str (session->state));
2630 clib_spinlock_unlock (&vcm->sessions_lockp);
2634 session->vrf = server_ep->vrf;
2635 session->peer_addr.is_ip4 = server_ep->is_ip4;
2636 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2637 session->peer_port = server_ep->port;
2639 if (VPPCOM_DEBUG > 0)
2640 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connecting to server "
2641 "%s %U port %d proto %s",
2642 getpid (), vpp_handle, session_index,
2643 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2644 format_ip46_address,
2645 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2646 clib_net_to_host_u16 (session->peer_port),
2647 session->proto ? "UDP" : "TCP");
2649 vppcom_send_connect_sock (session, session_index);
2650 clib_spinlock_unlock (&vcm->sessions_lockp);
2653 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2654 vcm->cfg.session_timeout);
2656 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2657 vpp_handle = session->vpp_handle;
2658 clib_spinlock_unlock (&vcm->sessions_lockp);
2661 if (PREDICT_FALSE (retval))
2664 if (VPPCOM_DEBUG > 0)
2665 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connect failed! "
2666 "returning %d (%s)", getpid (), vpp_handle,
2667 session_index, rv, vppcom_retval_str (rv));
2669 else if (VPPCOM_DEBUG > 0)
2670 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connected!",
2671 getpid (), vpp_handle, session_index);
2677 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2680 session_t *session = 0;
2681 svm_fifo_t *rx_fifo;
2686 session_state_t state;
2693 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2695 if (PREDICT_FALSE (session->is_vep))
2697 clib_spinlock_unlock (&vcm->sessions_lockp);
2698 clib_warning ("[%d] ERROR: sid %u: cannot read from an epoll session!",
2699 getpid (), session_index);
2704 vpp_handle = session->vpp_handle;
2705 is_server = session->is_server;
2706 is_nonblocking = session->is_nonblocking;
2707 state = session->state;
2708 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
2710 clib_spinlock_unlock (&vcm->sessions_lockp);
2711 rv = ((state == STATE_DISCONNECT) ?
2712 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2714 if (VPPCOM_DEBUG > 0)
2715 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s session is "
2716 "not open! state 0x%x (%s), returning %d (%s)",
2717 getpid (), vpp_handle, session_index,
2718 is_server ? "server" : "client", state,
2719 vppcom_session_state_str (state),
2720 rv, vppcom_retval_str (rv));
2724 rx_fifo = ((!session->is_cut_thru || is_server) ?
2725 session->server_rx_fifo : session->server_tx_fifo);
2726 fifo_str = ((!session->is_cut_thru || is_server) ?
2727 "server_rx_fifo" : "server_tx_fifo");
2728 clib_spinlock_unlock (&vcm->sessions_lockp);
2733 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2735 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2737 while (!is_nonblocking && (n_read <= 0));
2741 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2743 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2744 (EPOLLET | EPOLLIN));
2746 session->vep.et_mask |= EPOLLIN;
2748 if (state == STATE_CLOSE_ON_EMPTY)
2750 session_state_t new_state = STATE_DISCONNECT;
2751 rv = VPPCOM_ECONNRESET;
2753 if (VPPCOM_DEBUG > 1)
2755 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Empty fifo "
2756 "with %s session state 0x%x (%s)!"
2757 " Setting state to 0x%x (%s), returning %d (%s)",
2758 getpid (), vpp_handle, session_index,
2759 is_server ? "server" : "client",
2760 state, vppcom_session_state_str (state),
2761 new_state, vppcom_session_state_str (new_state),
2762 rv, vppcom_retval_str (rv));
2765 session->state = new_state;
2770 clib_spinlock_unlock (&vcm->sessions_lockp);
2775 if (VPPCOM_DEBUG > 2)
2778 clib_warning ("[%d] vpp handle 0x%llx, sid %u: read %d bytes "
2779 "from %s (%p)", getpid (), vpp_handle,
2780 session_index, n_read, fifo_str, rx_fifo);
2782 clib_warning ("[%d] vpp handle 0x%llx, sid %u: nothing read! "
2783 "returning %d (%s)", getpid (), vpp_handle,
2784 session_index, rv, vppcom_retval_str (rv));
2791 vppcom_session_read (uint32_t session_index, void *buf, int n)
2793 return (vppcom_session_read_internal (session_index, buf, n, 0));
2797 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2799 return (vppcom_session_read_internal (session_index, buf, n, 1));
2803 vppcom_session_read_ready (session_t * session, u32 session_index)
2805 svm_fifo_t *rx_fifo = 0;
2809 u8 is_server = session->is_server;
2810 session_state_t state = session->state;
2811 u64 vpp_handle = session->vpp_handle;
2813 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2814 if (PREDICT_FALSE (session->is_vep))
2816 clib_warning ("[%d] ERROR: sid %u: cannot read from an "
2817 "epoll session!", getpid (), session_index);
2822 if (session->is_listen)
2823 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2826 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2828 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2831 if (VPPCOM_DEBUG > 1)
2832 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s session is "
2833 "not open! state 0x%x (%s), returning %d (%s)",
2834 getpid (), vpp_handle, session_index,
2835 is_server ? "server" : "client",
2836 state, vppcom_session_state_str (state),
2837 rv, vppcom_retval_str (rv));
2841 rx_fifo = ((!session->is_cut_thru || is_server) ?
2842 session->server_rx_fifo : session->server_tx_fifo);
2844 ready = svm_fifo_max_dequeue (rx_fifo);
2850 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2852 session->vep.et_mask |= EPOLLIN;
2854 if (state == STATE_CLOSE_ON_EMPTY)
2856 rv = VPPCOM_ECONNRESET;
2857 session_state_t new_state = STATE_DISCONNECT;
2859 if (VPPCOM_DEBUG > 1)
2861 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Empty fifo with"
2862 " %s session state 0x%x (%s)! Setting state to "
2863 "0x%x (%s), returning %d (%s)",
2864 getpid (), session_index, vpp_handle,
2865 is_server ? "server" : "client",
2866 state, vppcom_session_state_str (state),
2867 new_state, vppcom_session_state_str (new_state),
2868 rv, vppcom_retval_str (rv));
2870 session->state = new_state;
2876 if (vcm->app_event_queue->cursize &&
2877 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
2879 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
2880 session_fifo_event_t e;
2882 for (i = 0; i < n_to_dequeue; i++)
2883 unix_shared_memory_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
2885 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
2893 vppcom_session_write (uint32_t session_index, void *buf, int n)
2895 session_t *session = 0;
2896 svm_fifo_t *tx_fifo;
2897 unix_shared_memory_queue_t *q;
2898 session_fifo_event_t evt;
2904 session_state_t state;
2909 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2911 if (PREDICT_FALSE (session->is_vep))
2913 clib_spinlock_unlock (&vcm->sessions_lockp);
2914 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2915 "cannot write to an epoll session!",
2916 getpid (), session->vpp_handle, session_index);
2922 is_server = session->is_server;
2923 is_nonblocking = session->is_nonblocking;
2924 vpp_handle = session->vpp_handle;
2925 state = session->state;
2926 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2928 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2931 clib_spinlock_unlock (&vcm->sessions_lockp);
2932 if (VPPCOM_DEBUG > 1)
2933 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2934 "%s session is not open! state 0x%x (%s)",
2935 getpid (), vpp_handle, session_index,
2936 is_server ? "server" : "client", state,
2937 vppcom_session_state_str (state));
2941 tx_fifo = ((!session->is_cut_thru || is_server) ?
2942 session->server_tx_fifo : session->server_rx_fifo);
2943 fifo_str = ((!session->is_cut_thru || is_server) ?
2944 "server_tx_fifo" : "server_rx_fifo");
2945 clib_spinlock_unlock (&vcm->sessions_lockp);
2949 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2951 while (!is_nonblocking && (n_write <= 0));
2953 /* If event wasn't set, add one */
2954 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2956 /* Fabricate TX event, send to vpp */
2958 evt.event_type = FIFO_EVENT_APP_TX;
2960 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2961 q = session->vpp_event_queue;
2963 unix_shared_memory_queue_add (q, (u8 *) & evt,
2964 0 /* do wait for mutex */ );
2965 clib_spinlock_unlock (&vcm->sessions_lockp);
2966 if (VPPCOM_DEBUG > 1)
2967 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2968 "added FIFO_EVENT_APP_TX to "
2969 "vpp_event_q %p, n_write %d", getpid (),
2970 vpp_handle, session_index, q, n_write);
2975 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2977 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2978 (EPOLLET | EPOLLOUT));
2980 session->vep.et_mask |= EPOLLOUT;
2982 if (state == STATE_CLOSE_ON_EMPTY)
2984 session_state_t new_state = STATE_DISCONNECT;
2985 rv = VPPCOM_ECONNRESET;
2987 if (VPPCOM_DEBUG > 1)
2989 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2990 "Empty fifo with %s session state 0x%x (%s)!"
2991 " Setting state to 0x%x (%s), returning %d (%s)",
2992 getpid (), vpp_handle, session_index,
2993 is_server ? "server" : "client",
2994 state, vppcom_session_state_str (state),
2995 new_state, vppcom_session_state_str (new_state),
2996 rv, vppcom_retval_str (rv));
2999 session->state = new_state;
3004 clib_spinlock_unlock (&vcm->sessions_lockp);
3009 if (VPPCOM_DEBUG > 2)
3012 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3013 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
3014 session_index, fifo_str, tx_fifo);
3016 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3017 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
3018 session_index, n_write, fifo_str, tx_fifo);
3025 vppcom_session_write_ready (session_t * session, u32 session_index)
3027 svm_fifo_t *tx_fifo;
3032 u8 is_server = session->is_server;
3033 session_state_t state = session->state;
3035 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3036 if (PREDICT_FALSE (session->is_vep))
3038 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3039 "cannot write to an epoll session!",
3040 getpid (), session->vpp_handle, session_index);
3045 if (PREDICT_FALSE (session->is_listen))
3047 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3048 "cannot write to a listen session!",
3049 getpid (), session->vpp_handle, session_index);
3054 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3056 session_state_t state = session->state;
3058 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3061 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3062 "%s session is not open! state 0x%x (%s), "
3063 "returning %d (%s)", getpid (), session->vpp_handle,
3064 session_index, is_server ? "server" : "client",
3065 state, vppcom_session_state_str (state),
3066 rv, vppcom_retval_str (rv));
3070 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3071 session->server_tx_fifo : session->server_rx_fifo);
3072 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3073 "server_tx_fifo" : "server_rx_fifo");
3075 ready = svm_fifo_max_enqueue (tx_fifo);
3077 if (VPPCOM_DEBUG > 3)
3078 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3079 "peek %s (%p), ready = %d", getpid (),
3080 session->vpp_handle, session_index,
3081 fifo_str, tx_fifo, ready);
3085 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3086 (EPOLLET | EPOLLOUT));
3088 session->vep.et_mask |= EPOLLOUT;
3090 if (state == STATE_CLOSE_ON_EMPTY)
3092 rv = VPPCOM_ECONNRESET;
3093 session_state_t new_state = STATE_DISCONNECT;
3095 if (VPPCOM_DEBUG > 1)
3097 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3098 "Empty fifo with %s session "
3099 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3100 "returning %d (%s)", getpid (),
3101 session->vpp_handle, session_index,
3102 is_server ? "server" : "client",
3103 state, vppcom_session_state_str (state),
3104 new_state, vppcom_session_state_str (new_state),
3105 rv, vppcom_retval_str (rv));
3107 session->state = new_state;
3117 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3118 unsigned long *write_map, unsigned long *except_map,
3119 double time_to_wait)
3122 session_t *session = 0;
3123 int rv, bits_set = 0;
3124 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3125 u32 minbits = clib_max (n_bits, BITS (uword));
3127 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3129 if (n_bits && read_map)
3131 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3132 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
3133 memset (read_map, 0, vec_len (vcm->rd_bitmap));
3135 if (n_bits && write_map)
3137 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3138 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
3139 memset (write_map, 0, vec_len (vcm->wr_bitmap));
3141 if (n_bits && except_map)
3143 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3144 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
3145 memset (except_map, 0, vec_len (vcm->ex_bitmap));
3155 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3157 clib_spinlock_lock (&vcm->sessions_lockp);
3158 rv = vppcom_session_at_index (session_index, &session);
3161 clib_spinlock_unlock (&vcm->sessions_lockp);
3162 if (VPPCOM_DEBUG > 1)
3163 clib_warning ("[%d] session %d specified in "
3164 "read_map is closed.", getpid (),
3166 bits_set = VPPCOM_EBADFD;
3170 rv = vppcom_session_read_ready (session, session_index);
3171 clib_spinlock_unlock (&vcm->sessions_lockp);
3172 if (except_map && vcm->ex_bitmap &&
3173 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3176 // TBD: clib_warning
3177 clib_bitmap_set_no_check (except_map, session_index, 1);
3182 // TBD: clib_warning
3183 clib_bitmap_set_no_check (read_map, session_index, 1);
3191 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3193 clib_spinlock_lock (&vcm->sessions_lockp);
3194 rv = vppcom_session_at_index (session_index, &session);
3197 clib_spinlock_unlock (&vcm->sessions_lockp);
3198 if (VPPCOM_DEBUG > 0)
3199 clib_warning ("[%d] session %d specified in "
3200 "write_map is closed.", getpid (),
3202 bits_set = VPPCOM_EBADFD;
3206 rv = vppcom_session_write_ready (session, session_index);
3207 clib_spinlock_unlock (&vcm->sessions_lockp);
3208 if (write_map && (rv > 0))
3210 // TBD: clib_warning
3211 clib_bitmap_set_no_check (write_map, session_index, 1);
3219 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3221 clib_spinlock_lock (&vcm->sessions_lockp);
3222 rv = vppcom_session_at_index (session_index, &session);
3225 clib_spinlock_unlock (&vcm->sessions_lockp);
3226 if (VPPCOM_DEBUG > 1)
3227 clib_warning ("[%d] session %d specified in "
3228 "except_map is closed.", getpid (),
3230 bits_set = VPPCOM_EBADFD;
3234 rv = vppcom_session_read_ready (session, session_index);
3235 clib_spinlock_unlock (&vcm->sessions_lockp);
3238 // TBD: clib_warning
3239 clib_bitmap_set_no_check (except_map, session_index, 1);
3247 while (clib_time_now (&vcm->clib_time) < timeout);
3254 vep_verify_epoll_chain (u32 vep_idx)
3257 vppcom_epoll_t *vep;
3261 if (VPPCOM_DEBUG <= 1)
3264 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3265 rv = vppcom_session_at_index (vep_idx, &session);
3266 if (PREDICT_FALSE (rv))
3268 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
3271 if (PREDICT_FALSE (!session->is_vep))
3273 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
3277 vep = &session->vep;
3278 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3281 " is_vep_session = %u\n"
3282 " next_sid = 0x%x (%u)\n"
3283 " wait_cont_idx = 0x%x (%u)\n"
3284 "}\n", getpid (), vep_idx,
3285 session->is_vep, session->is_vep_session,
3286 vep->next_sid, vep->next_sid,
3287 session->wait_cont_idx, session->wait_cont_idx);
3289 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3291 rv = vppcom_session_at_index (sid, &session);
3292 if (PREDICT_FALSE (rv))
3294 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
3297 if (PREDICT_FALSE (session->is_vep))
3298 clib_warning ("[%d] ERROR: sid (%u) is a vep!", getpid (), vep_idx);
3299 else if (PREDICT_FALSE (!session->is_vep_session))
3301 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
3305 vep = &session->vep;
3306 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3307 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
3308 "vep_idx (%u)!", getpid (),
3309 sid, session->vep.vep_idx, vep_idx);
3310 if (session->is_vep_session)
3312 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3314 " next_sid = 0x%x (%u)\n"
3315 " prev_sid = 0x%x (%u)\n"
3316 " vep_idx = 0x%x (%u)\n"
3317 " ev.events = 0x%x\n"
3318 " ev.data.u64 = 0x%llx\n"
3322 vep->next_sid, vep->next_sid,
3323 vep->prev_sid, vep->prev_sid,
3324 vep->vep_idx, vep->vep_idx,
3325 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3330 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
3334 vppcom_epoll_create (void)
3336 session_t *vep_session;
3339 clib_spinlock_lock (&vcm->sessions_lockp);
3340 pool_get (vcm->sessions, vep_session);
3341 memset (vep_session, 0, sizeof (*vep_session));
3342 vep_idx = vep_session - vcm->sessions;
3344 vep_session->is_vep = 1;
3345 vep_session->vep.vep_idx = ~0;
3346 vep_session->vep.next_sid = ~0;
3347 vep_session->vep.prev_sid = ~0;
3348 vep_session->wait_cont_idx = ~0;
3349 vep_session->vpp_handle = ~0;
3350 clib_spinlock_unlock (&vcm->sessions_lockp);
3352 if (VPPCOM_DEBUG > 0)
3353 clib_warning ("[%d] Created vep_idx %u / sid %u!",
3354 getpid (), vep_idx, vep_idx);
3360 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3361 struct epoll_event *event)
3363 session_t *vep_session;
3367 if (vep_idx == session_index)
3369 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3370 getpid (), vep_idx);
3371 return VPPCOM_EINVAL;
3374 clib_spinlock_lock (&vcm->sessions_lockp);
3375 rv = vppcom_session_at_index (vep_idx, &vep_session);
3376 if (PREDICT_FALSE (rv))
3378 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3381 if (PREDICT_FALSE (!vep_session->is_vep))
3383 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3384 getpid (), vep_idx);
3389 ASSERT (vep_session->vep.vep_idx == ~0);
3390 ASSERT (vep_session->vep.prev_sid == ~0);
3392 rv = vppcom_session_at_index (session_index, &session);
3393 if (PREDICT_FALSE (rv))
3395 if (VPPCOM_DEBUG > 0)
3396 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3397 getpid (), session_index);
3400 if (PREDICT_FALSE (session->is_vep))
3402 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3410 if (PREDICT_FALSE (!event))
3412 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3413 "epoll_event structure!", getpid ());
3417 if (vep_session->vep.next_sid != ~0)
3419 session_t *next_session;
3420 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3422 if (PREDICT_FALSE (rv))
3424 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3425 "vep.next_sid (%u) on vep_idx (%u)!",
3426 getpid (), vep_session->vep.next_sid, vep_idx);
3429 ASSERT (next_session->vep.prev_sid == vep_idx);
3430 next_session->vep.prev_sid = session_index;
3432 session->vep.next_sid = vep_session->vep.next_sid;
3433 session->vep.prev_sid = vep_idx;
3434 session->vep.vep_idx = vep_idx;
3435 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3436 session->vep.ev = *event;
3437 session->is_vep = 0;
3438 session->is_vep_session = 1;
3439 vep_session->vep.next_sid = session_index;
3440 if (VPPCOM_DEBUG > 1)
3441 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3442 " data 0x%llx!", getpid (), vep_idx, session_index,
3443 event->events, event->data.u64);
3447 if (PREDICT_FALSE (!event))
3449 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3450 "epoll_event structure!", getpid ());
3454 else if (PREDICT_FALSE (!session->is_vep_session))
3456 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3457 "not a vep session!", getpid (), session_index);
3461 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3463 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3464 "vep_idx (%u) != vep_idx (%u)!",
3465 getpid (), session_index,
3466 session->vep.vep_idx, vep_idx);
3470 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3471 session->vep.ev = *event;
3472 if (VPPCOM_DEBUG > 1)
3473 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3474 " data 0x%llx!", getpid (), vep_idx, session_index,
3475 event->events, event->data.u64);
3479 if (PREDICT_FALSE (!session->is_vep_session))
3481 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3482 "not a vep session!", getpid (), session_index);
3486 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3488 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3489 "vep_idx (%u) != vep_idx (%u)!",
3490 getpid (), session_index,
3491 session->vep.vep_idx, vep_idx);
3496 vep_session->wait_cont_idx =
3497 (vep_session->wait_cont_idx == session_index) ?
3498 session->vep.next_sid : vep_session->wait_cont_idx;
3500 if (session->vep.prev_sid == vep_idx)
3501 vep_session->vep.next_sid = session->vep.next_sid;
3504 session_t *prev_session;
3505 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3506 if (PREDICT_FALSE (rv))
3508 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3509 "vep.prev_sid (%u) on sid (%u)!",
3510 getpid (), session->vep.prev_sid, session_index);
3513 ASSERT (prev_session->vep.next_sid == session_index);
3514 prev_session->vep.next_sid = session->vep.next_sid;
3516 if (session->vep.next_sid != ~0)
3518 session_t *next_session;
3519 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3520 if (PREDICT_FALSE (rv))
3522 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3523 "vep.next_sid (%u) on sid (%u)!",
3524 getpid (), session->vep.next_sid, session_index);
3527 ASSERT (next_session->vep.prev_sid == session_index);
3528 next_session->vep.prev_sid = session->vep.prev_sid;
3531 memset (&session->vep, 0, sizeof (session->vep));
3532 session->vep.next_sid = ~0;
3533 session->vep.prev_sid = ~0;
3534 session->vep.vep_idx = ~0;
3535 session->is_vep_session = 0;
3536 if (VPPCOM_DEBUG > 1)
3537 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3538 getpid (), vep_idx, session_index);
3542 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3546 vep_verify_epoll_chain (vep_idx);
3549 clib_spinlock_unlock (&vcm->sessions_lockp);
3554 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3555 int maxevents, double wait_for_time)
3557 session_t *vep_session;
3559 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3560 u32 keep_trying = 1;
3562 u32 vep_next_sid, wait_cont_idx;
3565 if (PREDICT_FALSE (maxevents <= 0))
3567 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3568 getpid (), maxevents);
3569 return VPPCOM_EINVAL;
3571 memset (events, 0, sizeof (*events) * maxevents);
3573 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3574 vep_next_sid = vep_session->vep.next_sid;
3575 is_vep = vep_session->is_vep;
3576 wait_cont_idx = vep_session->wait_cont_idx;
3577 clib_spinlock_unlock (&vcm->sessions_lockp);
3579 if (PREDICT_FALSE (!is_vep))
3581 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3582 getpid (), vep_idx);
3586 if (PREDICT_FALSE (vep_next_sid == ~0))
3588 if (VPPCOM_DEBUG > 0)
3589 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3590 getpid (), vep_idx);
3600 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3601 sid != ~0; sid = next_sid)
3603 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3604 u8 add_event, is_vep_session;
3606 u64 session_ev_data;
3608 VCL_LOCK_AND_GET_SESSION (sid, &session);
3609 next_sid = session->vep.next_sid;
3610 session_events = session->vep.ev.events;
3611 et_mask = session->vep.et_mask;
3612 is_vep = session->is_vep;
3613 is_vep_session = session->is_vep_session;
3614 session_vep_idx = session->vep.vep_idx;
3615 session_ev_data = session->vep.ev.data.u64;
3616 clib_spinlock_unlock (&vcm->sessions_lockp);
3618 if (PREDICT_FALSE (is_vep))
3620 if (VPPCOM_DEBUG > 0)
3621 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3622 getpid (), vep_idx);
3626 if (PREDICT_FALSE (!is_vep_session))
3628 if (VPPCOM_DEBUG > 0)
3629 clib_warning ("[%d] ERROR: session (%u) is not "
3630 "a vep session!", getpid (), sid);
3634 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3636 clib_warning ("[%d] ERROR: session (%u) "
3637 "vep_idx (%u) != vep_idx (%u)!",
3638 getpid (), sid, session->vep.vep_idx, vep_idx);
3643 add_event = clear_et_mask = 0;
3645 if (EPOLLIN & session_events)
3647 VCL_LOCK_AND_GET_SESSION (sid, &session);
3648 ready = vppcom_session_read_ready (session, sid);
3649 clib_spinlock_unlock (&vcm->sessions_lockp);
3650 if ((ready > 0) && (EPOLLIN & et_mask))
3653 events[num_ev].events |= EPOLLIN;
3654 if (((EPOLLET | EPOLLIN) & session_events) ==
3655 (EPOLLET | EPOLLIN))
3656 clear_et_mask |= EPOLLIN;
3663 case VPPCOM_ECONNRESET:
3664 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3668 events[num_ev].events |= EPOLLERR;
3674 if (EPOLLOUT & session_events)
3676 VCL_LOCK_AND_GET_SESSION (sid, &session);
3677 ready = vppcom_session_write_ready (session, sid);
3678 clib_spinlock_unlock (&vcm->sessions_lockp);
3679 if ((ready > 0) && (EPOLLOUT & et_mask))
3682 events[num_ev].events |= EPOLLOUT;
3683 if (((EPOLLET | EPOLLOUT) & session_events) ==
3684 (EPOLLET | EPOLLOUT))
3685 clear_et_mask |= EPOLLOUT;
3692 case VPPCOM_ECONNRESET:
3693 events[num_ev].events |= EPOLLHUP;
3697 events[num_ev].events |= EPOLLERR;
3705 events[num_ev].data.u64 = session_ev_data;
3706 if (EPOLLONESHOT & session_events)
3708 VCL_LOCK_AND_GET_SESSION (sid, &session);
3709 session->vep.ev.events = 0;
3710 clib_spinlock_unlock (&vcm->sessions_lockp);
3713 if (num_ev == maxevents)
3715 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3716 vep_session->wait_cont_idx = next_sid;
3717 clib_spinlock_unlock (&vcm->sessions_lockp);
3721 if (wait_cont_idx != ~0)
3724 next_sid = vep_next_sid;
3725 else if (next_sid == wait_cont_idx)
3729 if (wait_for_time != -1)
3730 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3732 while ((num_ev == 0) && keep_trying);
3734 if (wait_cont_idx != ~0)
3736 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3737 vep_session->wait_cont_idx = ~0;
3738 clib_spinlock_unlock (&vcm->sessions_lockp);
3741 return (rv != VPPCOM_OK) ? rv : num_ev;
3745 vppcom_session_attr (uint32_t session_index, uint32_t op,
3746 void *buffer, uint32_t * buflen)
3750 u32 *flags = buffer;
3751 vppcom_endpt_t *ep = buffer;
3753 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3756 case VPPCOM_ATTR_GET_NREAD:
3757 rv = vppcom_session_read_ready (session, session_index);
3758 if (VPPCOM_DEBUG > 2)
3759 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3763 case VPPCOM_ATTR_GET_NWRITE:
3764 rv = vppcom_session_write_ready (session, session_index);
3765 if (VPPCOM_DEBUG > 2)
3766 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3767 getpid (), session_index, rv);
3770 case VPPCOM_ATTR_GET_FLAGS:
3771 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3773 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3774 *buflen = sizeof (*flags);
3775 if (VPPCOM_DEBUG > 2)
3776 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3777 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3778 session_index, *flags, session->is_nonblocking);
3784 case VPPCOM_ATTR_SET_FLAGS:
3785 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3787 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3788 if (VPPCOM_DEBUG > 2)
3789 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3790 "flags = 0x%08x, is_nonblocking = %u",
3791 getpid (), session_index, *flags,
3792 session->is_nonblocking);
3798 case VPPCOM_ATTR_GET_PEER_ADDR:
3799 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3801 ep->vrf = session->vrf;
3802 ep->is_ip4 = session->peer_addr.is_ip4;
3803 ep->port = session->peer_port;
3804 if (session->peer_addr.is_ip4)
3805 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3806 sizeof (ip4_address_t));
3808 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3809 sizeof (ip6_address_t));
3810 *buflen = sizeof (*ep);
3811 if (VPPCOM_DEBUG > 1)
3812 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
3813 "%u, addr = %U, port %u", getpid (),
3814 session_index, ep->is_ip4, format_ip46_address,
3815 &session->peer_addr.ip46, ep->is_ip4,
3816 clib_net_to_host_u16 (ep->port));
3822 case VPPCOM_ATTR_GET_LCL_ADDR:
3823 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3825 ep->vrf = session->vrf;
3826 ep->is_ip4 = session->lcl_addr.is_ip4;
3827 ep->port = session->lcl_port;
3828 if (session->lcl_addr.is_ip4)
3829 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3830 sizeof (ip4_address_t));
3832 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3833 sizeof (ip6_address_t));
3834 *buflen = sizeof (*ep);
3835 if (VPPCOM_DEBUG > 1)
3836 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
3837 "%u, addr = %U port %d", getpid (),
3838 session_index, ep->is_ip4, format_ip46_address,
3839 &session->lcl_addr.ip46, ep->is_ip4,
3840 clib_net_to_host_u16 (ep->port));
3846 case VPPCOM_ATTR_SET_REUSEADDR:
3849 case VPPCOM_ATTR_SET_BROADCAST:
3852 case VPPCOM_ATTR_SET_V6ONLY:
3855 case VPPCOM_ATTR_SET_KEEPALIVE:
3858 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3861 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3870 clib_spinlock_unlock (&vcm->sessions_lockp);
3875 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3876 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3879 session_t *session = 0;
3883 clib_spinlock_lock (&vcm->sessions_lockp);
3884 rv = vppcom_session_at_index (session_index, &session);
3885 if (PREDICT_FALSE (rv))
3887 clib_spinlock_unlock (&vcm->sessions_lockp);
3888 if (VPPCOM_DEBUG > 0)
3889 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3890 getpid (), session_index);
3892 clib_spinlock_unlock (&vcm->sessions_lockp);
3895 ep->vrf = session->vrf;
3896 ep->is_ip4 = session->peer_addr.is_ip4;
3897 ep->port = session->peer_port;
3898 if (session->peer_addr.is_ip4)
3899 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3900 sizeof (ip4_address_t));
3902 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3903 sizeof (ip6_address_t));
3904 clib_spinlock_unlock (&vcm->sessions_lockp);
3908 rv = vppcom_session_read (session_index, buffer, buflen);
3909 else if (flags & MSG_PEEK)
3910 rv = vppcom_session_peek (session_index, buffer, buflen);
3913 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3914 rv = VPPCOM_EAFNOSUPPORT;
3922 vppcom_session_sendto (uint32_t session_index, void *buffer,
3923 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3926 return VPPCOM_EINVAL;
3931 return VPPCOM_EINVAL;
3936 // TBD check the flags and do the right thing
3937 if (VPPCOM_DEBUG > 2)
3938 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3939 getpid (), flags, flags);
3942 return (vppcom_session_write (session_index, buffer, buflen));
3946 * fd.io coding-style-patch-verification: ON
3949 * eval: (c-set-style "gnu")