2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #include <svm/svm_fifo_segment.h>
20 #include <vlibmemory/api.h>
21 #include <vpp/api/vpe_msg_enum.h>
22 #include <vnet/session/application_interface.h>
23 #include <vcl/vppcom.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
27 #define vl_typedefs /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
31 /* declare message handlers for each api */
33 #define vl_endianfun /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
40 #include <vpp/api/vpe_all_api_h.h>
44 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
45 * 3 for read/write debug output
47 * export VCL_DEBUG=<#> to set dynamically.
49 #define VPPCOM_DEBUG_INIT 1
51 #define VPPCOM_DEBUG_INIT 0
54 #define VPPCOM_DEBUG vcm->debug
57 * VPPCOM Private definitions and functions.
73 STATE_CLOSE_ON_EMPTY = 0x10,
74 STATE_DISCONNECT = 0x20,
78 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
79 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
81 typedef struct epoll_event vppcom_epoll_event_t;
88 vppcom_epoll_event_t ev;
89 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
90 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
102 volatile session_state_t state;
104 svm_fifo_t *server_rx_fifo;
105 svm_fifo_t *server_tx_fifo;
110 unix_shared_memory_queue_t *vpp_event_queue;
112 /* Socket configuration state */
113 /* TBD: covert 'is_*' vars to bit in u8 flags; */
123 vppcom_ip46_t lcl_addr;
124 vppcom_ip46_t peer_addr;
125 u16 lcl_port; // network order
126 u16 peer_port; // network order
128 u64 client_queue_address;
132 typedef struct vppcom_cfg_t_
137 u32 add_segment_size;
138 u32 preallocated_fifo_pairs;
141 u32 event_queue_size;
142 u32 listen_queue_size;
143 u8 app_proxy_transport_tcp;
144 u8 app_proxy_transport_udp;
148 u64 namespace_secret;
154 typedef struct vppcom_main_t_
158 u32 *client_session_index_fifo;
161 /* vpe input queue */
162 unix_shared_memory_queue_t *vl_input_queue;
164 /* API client handle */
168 clib_spinlock_t sessions_lockp;
171 /* Hash table for disconnect processing */
172 uword *session_index_by_vpp_handles;
175 clib_bitmap_t *rd_bitmap;
176 clib_bitmap_t *wr_bitmap;
177 clib_bitmap_t *ex_bitmap;
179 /* Our event queue */
180 unix_shared_memory_queue_t *app_event_queue;
182 /* unique segment name counter */
183 u32 unique_segment_index;
185 /* For deadman timers */
186 clib_time_t clib_time;
188 /* State of the connection, shared between msg RX thread and main thread */
189 volatile app_state_t app_state;
193 /* VNET_API_ERROR_FOO -> "Foo" hash table */
194 uword *error_string_by_error_number;
197 /* NOTE: _vppcom_main is only used until the heap is allocated.
198 * Do not access it directly -- use vcm which will point to
199 * the heap allocated copy after init.
201 static vppcom_main_t _vppcom_main = {
202 .debug = VPPCOM_DEBUG_INIT,
203 .my_client_index = ~0
206 static vppcom_main_t *vcm = &_vppcom_main;
208 #define VCL_LOCK_AND_GET_SESSION(I, S) \
210 clib_spinlock_lock (&vcm->sessions_lockp); \
211 rv = vppcom_session_at_index (I, S); \
212 if (PREDICT_FALSE (rv)) \
214 clib_spinlock_unlock (&vcm->sessions_lockp); \
215 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
222 vppcom_app_state_str (app_state_t state)
228 case STATE_APP_START:
229 st = "STATE_APP_START";
232 case STATE_APP_CONN_VPP:
233 st = "STATE_APP_CONN_VPP";
236 case STATE_APP_ENABLED:
237 st = "STATE_APP_ENABLED";
240 case STATE_APP_ATTACHED:
241 st = "STATE_APP_ATTACHED";
245 st = "UNKNOWN_APP_STATE";
253 vppcom_session_state_str (session_state_t state)
264 st = "STATE_CONNECT";
275 case STATE_CLOSE_ON_EMPTY:
276 st = "STATE_CLOSE_ON_EMPTY";
279 case STATE_DISCONNECT:
280 st = "STATE_DISCONNECT";
288 st = "UNKNOWN_STATE";
296 * VPPCOM Utility Functions
299 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
301 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
302 if (PREDICT_FALSE ((session_index == ~0) ||
303 pool_is_free_index (vcm->sessions, session_index)))
305 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
306 getpid (), session_index);
307 return VPPCOM_EBADFD;
309 *sess = pool_elt_at_index (vcm->sessions, session_index);
314 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
316 /* Session and listener handles have different formats. The latter has
317 * the thread index in the upper 32 bits while the former has the session
318 * type. Knowing that, for listeners we just flip the MSB to 1 */
319 listener_handle |= 1ULL << 63;
320 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
323 static inline session_t *
324 vppcom_session_table_lookup_listener (u64 listener_handle)
327 u64 handle = listener_handle | (1ULL << 63);
330 p = hash_get (vcm->session_index_by_vpp_handles, handle);
333 clib_warning ("[%d] couldn't find listen session: unknown vpp "
334 "listener handle %llx", getpid (), listener_handle);
337 if (pool_is_free_index (vcm->sessions, p[0]))
339 if (VPPCOM_DEBUG > 1)
340 clib_warning ("[%d] invalid listen session, sid (%u)", getpid (),
345 session = pool_elt_at_index (vcm->sessions, p[0]);
346 ASSERT (session->is_listen);
351 vppcom_session_table_del_listener (u64 listener_handle)
353 listener_handle |= 1ULL << 63;
354 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
358 vppcom_connect_to_vpp (char *app_name)
360 api_main_t *am = &api_main;
362 if (VPPCOM_DEBUG > 0)
363 printf ("\nConnecting to VPP api...");
364 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
366 clib_warning ("[%d] connect to vpp (%s) failed!", getpid (), app_name);
367 return VPPCOM_ECONNREFUSED;
370 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
371 vcm->my_client_index = am->my_client_index;
372 if (VPPCOM_DEBUG > 0)
373 printf (" connected!\n");
375 vcm->app_state = STATE_APP_CONN_VPP;
380 format_api_error (u8 * s, va_list * args)
382 i32 error = va_arg (*args, u32);
385 p = hash_get (vcm->error_string_by_error_number, -error);
388 s = format (s, "%s (%d)", p[0], error);
390 s = format (s, "%d", error);
395 vppcom_init_error_string_table (void)
397 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
399 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
400 foreach_vnet_api_error;
403 hash_set (vcm->error_string_by_error_number, 99, "Misc");
407 vppcom_wait_for_app_state_change (app_state_t app_state)
409 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
411 while (clib_time_now (&vcm->clib_time) < timeout)
413 if (vcm->app_state == app_state)
416 if (VPPCOM_DEBUG > 0)
417 clib_warning ("[%d] timeout waiting for state %s (%d)", getpid (),
418 vppcom_app_state_str (app_state), app_state);
419 return VPPCOM_ETIMEDOUT;
423 vppcom_wait_for_session_state_change (u32 session_index,
424 session_state_t state,
427 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
428 session_t *volatile session;
433 clib_spinlock_lock (&vcm->sessions_lockp);
434 rv = vppcom_session_at_index (session_index, &session);
435 if (PREDICT_FALSE (rv))
437 clib_spinlock_unlock (&vcm->sessions_lockp);
440 if (session->state == state)
442 clib_spinlock_unlock (&vcm->sessions_lockp);
445 if (session->state == STATE_FAILED)
447 clib_spinlock_unlock (&vcm->sessions_lockp);
448 return VPPCOM_ECONNREFUSED;
451 clib_spinlock_unlock (&vcm->sessions_lockp);
453 while (clib_time_now (&vcm->clib_time) < timeout);
455 if (VPPCOM_DEBUG > 0)
456 clib_warning ("[%d] timeout waiting for state 0x%x (%s)", getpid (),
457 state, vppcom_session_state_str (state));
458 return VPPCOM_ETIMEDOUT;
462 vppcom_wait_for_client_session_index (f64 wait_for_time)
464 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
468 if (clib_fifo_elts (vcm->client_session_index_fifo))
471 while (clib_time_now (&vcm->clib_time) < timeout);
473 if (wait_for_time == 0)
474 return VPPCOM_EAGAIN;
476 if (VPPCOM_DEBUG > 0)
477 clib_warning ("[%d] timeout waiting for client_session_index", getpid ());
478 return VPPCOM_ETIMEDOUT;
482 * VPP-API message functions
485 vppcom_send_session_enable_disable (u8 is_enable)
487 vl_api_session_enable_disable_t *bmp;
488 bmp = vl_msg_api_alloc (sizeof (*bmp));
489 memset (bmp, 0, sizeof (*bmp));
491 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
492 bmp->client_index = vcm->my_client_index;
493 bmp->context = htonl (0xfeedface);
494 bmp->is_enable = is_enable;
495 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
499 vppcom_app_session_enable (void)
503 if (vcm->app_state != STATE_APP_ENABLED)
505 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
506 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
507 if (PREDICT_FALSE (rv))
509 if (VPPCOM_DEBUG > 0)
510 clib_warning ("[%d] application session enable timed out! "
512 getpid (), rv, vppcom_retval_str (rv));
520 vl_api_session_enable_disable_reply_t_handler
521 (vl_api_session_enable_disable_reply_t * mp)
525 clib_warning ("[%d] session_enable_disable failed: %U", getpid (),
526 format_api_error, ntohl (mp->retval));
529 vcm->app_state = STATE_APP_ENABLED;
533 vppcom_app_send_attach (void)
535 vl_api_application_attach_t *bmp;
536 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
537 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
538 vcm->cfg.app_proxy_transport_udp);
540 bmp = vl_msg_api_alloc (sizeof (*bmp));
541 memset (bmp, 0, sizeof (*bmp));
543 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
544 bmp->client_index = vcm->my_client_index;
545 bmp->context = htonl (0xfeedface);
546 bmp->options[APP_OPTIONS_FLAGS] =
547 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
548 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
549 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
550 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
551 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
552 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
553 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
554 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
555 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
556 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
557 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
560 bmp->namespace_id_len = nsid_len;
561 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
562 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
564 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
568 vppcom_app_attach (void)
572 vppcom_app_send_attach ();
573 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
574 if (PREDICT_FALSE (rv))
576 if (VPPCOM_DEBUG > 0)
577 clib_warning ("[%d] application attach timed out! returning %d (%s)",
578 getpid (), rv, vppcom_retval_str (rv));
585 vppcom_app_detach (void)
587 vl_api_application_detach_t *bmp;
588 bmp = vl_msg_api_alloc (sizeof (*bmp));
589 memset (bmp, 0, sizeof (*bmp));
591 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
592 bmp->client_index = vcm->my_client_index;
593 bmp->context = htonl (0xfeedface);
594 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
598 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
601 static svm_fifo_segment_create_args_t _a;
602 svm_fifo_segment_create_args_t *a = &_a;
605 memset (a, 0, sizeof (*a));
608 clib_warning ("[%d] attach failed: %U", getpid (),
609 format_api_error, ntohl (mp->retval));
613 if (mp->segment_name_length == 0)
615 clib_warning ("[%d] segment_name_length zero", getpid ());
619 a->segment_name = (char *) mp->segment_name;
620 a->segment_size = mp->segment_size;
622 ASSERT (mp->app_event_queue_address);
624 /* Attach to the segment vpp created */
625 rv = svm_fifo_segment_attach (a);
626 vec_reset_length (a->new_segment_indices);
627 if (PREDICT_FALSE (rv))
629 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", getpid (),
634 vcm->app_event_queue =
635 uword_to_pointer (mp->app_event_queue_address,
636 unix_shared_memory_queue_t *);
638 vcm->app_state = STATE_APP_ATTACHED;
642 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
646 clib_warning ("[%d] detach failed: %U", getpid (), format_api_error,
649 vcm->app_state = STATE_APP_ENABLED;
653 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
657 clib_warning ("[%d] vpp handle 0x%llx: disconnect session failed: %U",
658 getpid (), mp->handle, format_api_error,
663 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
665 static svm_fifo_segment_create_args_t _a;
666 svm_fifo_segment_create_args_t *a = &_a;
669 memset (a, 0, sizeof (*a));
670 a->segment_name = (char *) mp->segment_name;
671 a->segment_size = mp->segment_size;
672 /* Attach to the segment vpp created */
673 rv = svm_fifo_segment_attach (a);
674 vec_reset_length (a->new_segment_indices);
675 if (PREDICT_FALSE (rv))
677 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
678 getpid (), mp->segment_name);
681 if (VPPCOM_DEBUG > 1)
682 clib_warning ("[%d] mapped new segment '%s' size %d", getpid (),
683 mp->segment_name, mp->segment_size);
687 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
691 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
695 session_t *session = 0;
696 u32 session_index = p[0];
698 VCL_LOCK_AND_GET_SESSION (session_index, &session);
699 session->state = STATE_CLOSE_ON_EMPTY;
701 if (VPPCOM_DEBUG > 1)
702 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
703 "setting state to 0x%x (%s)",
704 getpid (), mp->handle, session_index, session->state,
705 vppcom_session_state_str (session->state));
706 clib_spinlock_unlock (&vcm->sessions_lockp);
710 if (VPPCOM_DEBUG > 1)
711 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
712 "session lookup failed!",
713 getpid (), mp->handle, session_index);
716 clib_warning ("[%d] vpp handle 0x%llx: session lookup by "
717 "handle failed!", getpid (), mp->handle);
721 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
723 session_t *session = 0;
724 vl_api_reset_session_reply_t *rmp;
728 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
732 clib_spinlock_lock (&vcm->sessions_lockp);
733 rval = vppcom_session_at_index (p[0], &session);
734 if (PREDICT_FALSE (rval))
736 rv = VNET_API_ERROR_INVALID_VALUE_2;
737 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
738 "session lookup failed! returning %d %U",
739 getpid (), mp->handle, p[0],
740 rv, format_api_error, rv);
744 /* TBD: should this disconnect immediately and
747 session->state = STATE_CLOSE_ON_EMPTY;
749 if (VPPCOM_DEBUG > 1)
750 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
751 "state set to %d (%s)!", getpid (),
752 mp->handle, p[0], session->state,
753 vppcom_session_state_str (session->state));
755 clib_spinlock_unlock (&vcm->sessions_lockp);
759 rv = VNET_API_ERROR_INVALID_VALUE;
760 clib_warning ("[%d] ERROR: vpp handle 0x%llx: session lookup "
761 "failed! returning %d %U",
762 getpid (), mp->handle, rv, format_api_error, rv);
765 rmp = vl_msg_api_alloc (sizeof (*rmp));
766 memset (rmp, 0, sizeof (*rmp));
767 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
768 rmp->retval = htonl (rv);
769 rmp->handle = mp->handle;
770 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
774 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
776 session_t *session = 0;
778 svm_fifo_t *rx_fifo, *tx_fifo;
782 session_index = mp->context;
783 VCL_LOCK_AND_GET_SESSION (session_index, &session);
787 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
788 "connect failed! %U",
789 getpid (), mp->handle, session_index,
790 format_api_error, ntohl (mp->retval));
793 session->state = STATE_FAILED;
794 session->vpp_handle = mp->handle;
798 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
799 "Invalid session index (%u)!",
800 getpid (), mp->handle, session_index);
808 /* We've been redirected */
809 if (mp->segment_name_length > 0)
811 static svm_fifo_segment_create_args_t _a;
812 svm_fifo_segment_create_args_t *a = &_a;
815 memset (a, 0, sizeof (*a));
816 a->segment_name = (char *) mp->segment_name;
817 if (VPPCOM_DEBUG > 1)
818 clib_warning ("[%d] cut-thru segment: %s\n",
819 getpid (), a->segment_name);
821 rv = svm_fifo_segment_attach (a);
822 vec_reset_length (a->new_segment_indices);
823 if (PREDICT_FALSE (rv))
825 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
826 getpid (), a->segment_name);
834 session->is_cut_thru = is_cut_thru;
835 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
836 unix_shared_memory_queue_t *);
838 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
839 rx_fifo->client_session_index = session_index;
840 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
841 tx_fifo->client_session_index = session_index;
843 session->server_rx_fifo = rx_fifo;
844 session->server_tx_fifo = tx_fifo;
845 session->vpp_handle = mp->handle;
846 session->lcl_addr.is_ip4 = mp->is_ip4;
847 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
848 sizeof (session->peer_addr.ip46));
849 session->lcl_port = mp->lcl_port;
850 session->state = STATE_CONNECT;
852 /* Add it to lookup table */
853 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
855 if (VPPCOM_DEBUG > 1)
856 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connect succeeded!"
857 " session_rx_fifo %p, refcnt %d,"
858 " session_tx_fifo %p, refcnt %d",
859 getpid (), mp->handle, session_index,
860 session->server_rx_fifo,
861 session->server_rx_fifo->refcnt,
862 session->server_tx_fifo, session->server_tx_fifo->refcnt);
864 clib_spinlock_unlock (&vcm->sessions_lockp);
868 vppcom_send_connect_sock (session_t * session, u32 session_index)
870 vl_api_connect_sock_t *cmp;
872 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
873 session->is_server = 0;
874 cmp = vl_msg_api_alloc (sizeof (*cmp));
875 memset (cmp, 0, sizeof (*cmp));
876 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
877 cmp->client_index = vcm->my_client_index;
878 cmp->context = session_index;
880 cmp->vrf = session->vrf;
881 cmp->is_ip4 = session->peer_addr.is_ip4;
882 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
883 cmp->port = session->peer_port;
884 cmp->proto = session->proto;
885 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
886 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
890 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
893 vl_api_disconnect_session_reply_t *rmp;
895 if (VPPCOM_DEBUG > 1)
896 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
897 getpid (), vpp_handle, session_index);
899 rmp = vl_msg_api_alloc (sizeof (*rmp));
900 memset (rmp, 0, sizeof (*rmp));
902 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
903 rmp->retval = htonl (rv);
904 rmp->handle = vpp_handle;
905 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
909 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
911 vl_api_disconnect_session_t *dmp;
913 if (VPPCOM_DEBUG > 1)
914 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending disconnect msg",
915 getpid (), vpp_handle, session_index);
917 dmp = vl_msg_api_alloc (sizeof (*dmp));
918 memset (dmp, 0, sizeof (*dmp));
919 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
920 dmp->client_index = vcm->my_client_index;
921 dmp->handle = vpp_handle;
922 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
926 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
928 session_t *session = 0;
929 u32 session_index = mp->context;
932 VCL_LOCK_AND_GET_SESSION (session_index, &session);
936 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
937 getpid (), mp->handle, session_index, format_api_error,
939 rv = vppcom_session_at_index (session_index, &session);
942 session->state = STATE_FAILED;
943 session->vpp_handle = mp->handle;
947 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
948 "Invalid session index (%u)!",
949 getpid (), mp->handle, session_index);
954 session->vpp_handle = mp->handle;
955 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
956 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
957 sizeof (session->peer_addr.ip46));
958 session->lcl_port = mp->lcl_port;
959 vppcom_session_table_add_listener (mp->handle, session_index);
960 session->is_listen = 1;
961 session->state = STATE_LISTEN;
963 if (VPPCOM_DEBUG > 1)
964 clib_warning ("[%d] vpp handle 0x%llx, sid %u: bind succeeded!",
965 getpid (), mp->handle, mp->context);
967 clib_spinlock_unlock (&vcm->sessions_lockp);
971 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
974 clib_warning ("[%d] ERROR: sid %u: unbind failed: %U",
975 getpid (), mp->context, format_api_error,
978 else if (VPPCOM_DEBUG > 1)
979 clib_warning ("[%d] sid %u: unbind succeeded!", getpid (), mp->context);
983 format_ip4_address (u8 * s, va_list * args)
985 u8 *a = va_arg (*args, u8 *);
986 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
990 format_ip6_address (u8 * s, va_list * args)
992 ip6_address_t *a = va_arg (*args, ip6_address_t *);
993 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
995 i_max_n_zero = ARRAY_LEN (a->as_u16);
997 i_first_zero = i_max_n_zero;
999 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1001 u32 is_zero = a->as_u16[i] == 0;
1002 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1008 if ((!is_zero && n_zeros > max_n_zeros)
1009 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1011 i_max_n_zero = i_first_zero;
1012 max_n_zeros = n_zeros;
1013 i_first_zero = ARRAY_LEN (a->as_u16);
1018 last_double_colon = 0;
1019 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1021 if (i == i_max_n_zero && max_n_zeros > 1)
1023 s = format (s, "::");
1024 i += max_n_zeros - 1;
1025 last_double_colon = 1;
1029 s = format (s, "%s%x",
1030 (last_double_colon || i == 0) ? "" : ":",
1031 clib_net_to_host_u16 (a->as_u16[i]));
1032 last_double_colon = 0;
1039 /* Format an IP46 address. */
1041 format_ip46_address (u8 * s, va_list * args)
1043 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1044 ip46_type_t type = va_arg (*args, ip46_type_t);
1050 is_ip4 = ip46_address_is_ip4 (ip46);
1061 format (s, "%U", format_ip4_address, &ip46->ip4) :
1062 format (s, "%U", format_ip6_address, &ip46->ip6);
1066 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1068 vl_api_accept_session_reply_t *rmp;
1070 rmp = vl_msg_api_alloc (sizeof (*rmp));
1071 memset (rmp, 0, sizeof (*rmp));
1072 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1073 rmp->retval = htonl (retval);
1074 rmp->context = context;
1075 rmp->handle = handle;
1076 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1080 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1082 svm_fifo_t *rx_fifo, *tx_fifo;
1083 session_t *session, *listen_session;
1086 clib_spinlock_lock (&vcm->sessions_lockp);
1087 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1089 clib_warning ("[%d] client session queue is full!", getpid ());
1090 vppcom_send_accept_session_reply (mp->handle, mp->context,
1091 VNET_API_ERROR_QUEUE_FULL);
1092 clib_spinlock_unlock (&vcm->sessions_lockp);
1096 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1097 if (!listen_session)
1099 clib_warning ("[%d] ERROR: couldn't find listen session: unknown vpp "
1100 "listener handle %llx", getpid (), mp->listener_handle);
1101 clib_spinlock_unlock (&vcm->sessions_lockp);
1105 /* Allocate local session and set it up */
1106 pool_get (vcm->sessions, session);
1107 memset (session, 0, sizeof (*session));
1108 session_index = session - vcm->sessions;
1110 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1111 rx_fifo->client_session_index = session_index;
1112 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1113 tx_fifo->client_session_index = session_index;
1115 session->vpp_handle = mp->handle;
1116 session->client_context = mp->context;
1117 session->server_rx_fifo = rx_fifo;
1118 session->server_tx_fifo = tx_fifo;
1119 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1120 unix_shared_memory_queue_t *);
1121 session->state = STATE_ACCEPT;
1122 session->is_cut_thru = 0;
1123 session->is_server = 1;
1124 session->peer_port = mp->port;
1125 session->peer_addr.is_ip4 = mp->is_ip4;
1126 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1127 sizeof (session->peer_addr.ip46));
1129 /* Add it to lookup table */
1130 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1131 session->lcl_port = listen_session->lcl_port;
1132 session->lcl_addr = listen_session->lcl_addr;
1134 /* TBD: move client_session_index_fifo into listener session */
1135 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1137 clib_spinlock_unlock (&vcm->sessions_lockp);
1139 if (VPPCOM_DEBUG > 1)
1140 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client accept "
1141 "request from %s address %U port %d queue %p!", getpid (),
1142 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1143 format_ip46_address, &mp->ip, mp->is_ip4,
1144 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1148 vppcom_send_connect_session_reply (session_t * session, u32 session_index,
1149 u64 vpp_handle, u32 context, int retval)
1151 vl_api_connect_session_reply_t *rmp;
1153 unix_shared_memory_queue_t *client_q;
1155 rmp = vl_msg_api_alloc (sizeof (*rmp));
1156 memset (rmp, 0, sizeof (*rmp));
1157 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1161 rmp->context = context;
1162 rmp->handle = vpp_handle;
1163 rmp->retval = htonl (retval);
1164 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1168 rmp->context = session->client_context;
1169 rmp->retval = htonl (retval);
1170 rmp->handle = session->vpp_handle;
1171 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1172 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1173 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1174 rmp->segment_size = vcm->cfg.segment_size;
1175 len = vec_len (session->segment_name);
1176 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1177 clib_memcpy (rmp->segment_name, session->segment_name,
1178 rmp->segment_name_length - 1);
1179 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1180 sizeof (rmp->lcl_ip));
1181 rmp->is_ip4 = session->peer_addr.is_ip4;
1182 rmp->lcl_port = session->peer_port;
1183 client_q = uword_to_pointer (session->client_queue_address,
1184 unix_shared_memory_queue_t *);
1186 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1190 * Acting as server for redirected connect requests
1193 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1196 session_t *session = 0;
1198 clib_spinlock_lock (&vcm->sessions_lockp);
1199 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1201 clib_spinlock_unlock (&vcm->sessions_lockp);
1203 if (VPPCOM_DEBUG > 1)
1204 clib_warning ("[%d] client session queue is full!", getpid ());
1206 /* TBD: Fix api to include vpp handle */
1207 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1208 0 /* handle */ , mp->context,
1209 VNET_API_ERROR_QUEUE_FULL);
1213 pool_get (vcm->sessions, session);
1214 memset (session, 0, sizeof (*session));
1215 session_index = session - vcm->sessions;
1217 session->client_context = mp->context;
1218 session->vpp_handle = session_index;
1219 session->client_queue_address = mp->client_queue_address;
1220 session->is_cut_thru = 1;
1221 session->is_server = 1;
1222 session->lcl_port = mp->port;
1223 session->lcl_addr.is_ip4 = mp->is_ip4;
1224 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1225 sizeof (session->lcl_addr.ip46));
1227 /* TBD: missing peer info in api msg.
1229 session->peer_addr.is_ip4 = mp->is_ip4;
1230 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1232 session->state = STATE_ACCEPT;
1233 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1234 if (VPPCOM_DEBUG > 1)
1235 clib_warning ("[%d] sid %u: Got a cut-thru connect request! "
1236 "clib_fifo_elts %u!\n", getpid (), session_index,
1237 clib_fifo_elts (vcm->client_session_index_fifo));
1238 clib_spinlock_unlock (&vcm->sessions_lockp);
1242 vppcom_send_bind_sock (session_t * session, u32 session_index)
1244 vl_api_bind_sock_t *bmp;
1246 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1247 session->is_server = 1;
1248 bmp = vl_msg_api_alloc (sizeof (*bmp));
1249 memset (bmp, 0, sizeof (*bmp));
1251 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1252 bmp->client_index = vcm->my_client_index;
1253 bmp->context = session_index;
1254 bmp->vrf = session->vrf;
1255 bmp->is_ip4 = session->lcl_addr.is_ip4;
1256 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1257 bmp->port = session->lcl_port;
1258 bmp->proto = session->proto;
1259 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1260 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1264 vppcom_send_unbind_sock (u64 vpp_handle)
1266 vl_api_unbind_sock_t *ump;
1268 ump = vl_msg_api_alloc (sizeof (*ump));
1269 memset (ump, 0, sizeof (*ump));
1271 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1272 ump->client_index = vcm->my_client_index;
1273 ump->handle = vpp_handle;
1274 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1278 vppcom_session_unbind (u32 session_index)
1280 session_t *session = 0;
1284 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1286 vpp_handle = session->vpp_handle;
1287 vppcom_session_table_del_listener (vpp_handle);
1288 session->vpp_handle = ~0;
1289 session->state = STATE_DISCONNECT;
1291 clib_spinlock_unlock (&vcm->sessions_lockp);
1293 if (VPPCOM_DEBUG > 1)
1294 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1295 "sending unbind msg! new state 0x%x (%s)",
1296 getpid (), vpp_handle, session_index,
1297 session->state, vppcom_session_state_str (session->state));
1299 vppcom_send_unbind_sock (vpp_handle);
1306 vppcom_session_disconnect (u32 session_index)
1310 u8 is_cut_thru, is_listen, is_server;
1312 session_state_t state;
1314 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1316 vpp_handle = session->vpp_handle;
1317 is_server = session->is_server;
1318 is_listen = session->is_listen;
1319 is_cut_thru = session->is_cut_thru;
1320 state = session->state;
1321 clib_spinlock_unlock (&vcm->sessions_lockp);
1323 if (VPPCOM_DEBUG > 1)
1325 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s state 0x%x (%s), "
1326 "is_cut_thru %d, is_listen %d",
1327 getpid (), vpp_handle, session_index,
1328 is_server ? "server" : "client",
1329 state, vppcom_session_state_str (state),
1330 is_cut_thru, is_listen);
1333 if (PREDICT_FALSE (is_listen))
1335 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
1336 "Cannot disconnect a listen socket!",
1337 getpid (), vpp_handle, session_index);
1342 /* Through the VPP host stack...
1344 else if (!is_cut_thru)
1346 /* The peer has already initiated the close,
1347 * so send the disconnect session reply.
1349 if (state & STATE_CLOSE_ON_EMPTY)
1351 vppcom_send_disconnect_session_reply (vpp_handle,
1352 session_index, 0 /* rv */ );
1353 if (VPPCOM_DEBUG > 1)
1354 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1355 "sending disconnect REPLY...",
1356 getpid (), vpp_handle, session_index);
1359 /* Otherwise, send a disconnect session msg...
1363 if (VPPCOM_DEBUG > 1)
1364 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
1365 "sending disconnect...",
1366 getpid (), vpp_handle, session_index);
1368 vppcom_send_disconnect_session (vpp_handle, session_index);
1372 /* Cut-thru connections...
1374 * server: free fifos and segment allocated during connect/redirect
1375 * client: no cleanup required
1381 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1382 svm_fifo_segment_private_t *seg;
1384 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1386 if (VPPCOM_DEBUG > 1)
1387 clib_warning ("[%d] sid %d: freeing cut-thru fifos in "
1389 " server_rx_fifo %p, refcnt = %d"
1390 " server_tx_fifo %p, refcnt = %d",
1391 getpid (), session_index, session->sm_seg_index,
1392 session->server_rx_fifo,
1393 session->server_rx_fifo->refcnt,
1394 session->server_tx_fifo,
1395 session->server_tx_fifo->refcnt);
1397 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1398 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1399 FIFO_SEGMENT_RX_FREELIST);
1400 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1401 FIFO_SEGMENT_TX_FREELIST);
1402 svm_fifo_segment_delete (seg);
1404 /* TBD: Send cut-thru disconnect event to client */
1406 clib_spinlock_unlock (&vcm->sessions_lockp);
1410 /* TBD: Send cut-thru disconnect event to server */
1418 #define foreach_sock_msg \
1419 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1420 _(BIND_SOCK_REPLY, bind_sock_reply) \
1421 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1422 _(ACCEPT_SESSION, accept_session) \
1423 _(CONNECT_SOCK, connect_sock) \
1424 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1425 _(DISCONNECT_SESSION, disconnect_session) \
1426 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1427 _(RESET_SESSION, reset_session) \
1428 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1429 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1430 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1433 vppcom_api_hookup (void)
1436 vl_msg_api_set_handlers(VL_API_##N, #n, \
1437 vl_api_##n##_t_handler, \
1439 vl_api_##n##_t_endian, \
1440 vl_api_##n##_t_print, \
1441 sizeof(vl_api_##n##_t), 1);
1447 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1451 vcl_cfg->heapsize = (256ULL << 20);
1452 vcl_cfg->segment_baseva = 0x200000000ULL;
1453 vcl_cfg->segment_size = (256 << 20);
1454 vcl_cfg->add_segment_size = (128 << 20);
1455 vcl_cfg->preallocated_fifo_pairs = 8;
1456 vcl_cfg->rx_fifo_size = (1 << 20);
1457 vcl_cfg->tx_fifo_size = (1 << 20);
1458 vcl_cfg->event_queue_size = 2048;
1459 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1460 vcl_cfg->app_timeout = 10 * 60.0;
1461 vcl_cfg->session_timeout = 10 * 60.0;
1462 vcl_cfg->accept_timeout = 60.0;
1466 vppcom_cfg_heapsize (char *conf_fname)
1468 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1481 fp = fopen (conf_fname, "r");
1484 if (VPPCOM_DEBUG > 0)
1485 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1488 argv = calloc (1, sizeof (char *));
1494 if (fgets (inbuf, 4096, fp) == 0)
1496 p = strtok (inbuf, " \t\n");
1502 char **tmp = realloc (argv, argc * sizeof (char *));
1506 arg = strndup (p, 1024);
1509 argv[argc - 1] = arg;
1510 p = strtok (NULL, " \t\n");
1517 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1524 * Look for and parse the "heapsize" config parameter.
1525 * Manual since none of the clib infra has been bootstrapped yet.
1527 * Format: heapsize <nn>[mM][gG]
1530 for (i = 1; i < (argc - 1); i++)
1532 if (!strncmp (argv[i], "heapsize", 8))
1534 sizep = (u8 *) argv[i + 1];
1536 while (*sizep >= '0' && *sizep <= '9')
1539 size += *sizep++ - '0';
1543 if (VPPCOM_DEBUG > 0)
1544 clib_warning ("[%d] parse error '%s %s', "
1545 "using default heapsize %lld (0x%llx)",
1546 getpid (), argv[i], argv[i + 1],
1547 vcl_cfg->heapsize, vcl_cfg->heapsize);
1551 if (*sizep == 'g' || *sizep == 'G')
1552 vcl_cfg->heapsize = size << 30;
1553 else if (*sizep == 'm' || *sizep == 'M')
1554 vcl_cfg->heapsize = size << 20;
1557 if (VPPCOM_DEBUG > 0)
1558 clib_warning ("[%d] parse error '%s %s', "
1559 "using default heapsize %lld (0x%llx)",
1560 getpid (), argv[i], argv[i + 1],
1561 vcl_cfg->heapsize, vcl_cfg->heapsize);
1573 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1574 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1575 if (vcl_mem == MAP_FAILED)
1577 clib_unix_error ("[%d] ERROR: mmap(0, %lld == 0x%llx, "
1578 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1580 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1583 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1586 clib_warning ("[%d] ERROR: clib_mem_init() failed!", getpid ());
1589 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1592 clib_warning ("[%d] ERROR: clib_mem_alloc() failed!", getpid ());
1596 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1599 if (VPPCOM_DEBUG > 0)
1600 clib_warning ("[%d] allocated VCL heap = %p, size %lld (0x%llx)",
1601 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1605 vppcom_cfg_read (char *conf_fname)
1607 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1609 unformat_input_t _input, *input = &_input;
1610 unformat_input_t _line_input, *line_input = &_line_input;
1611 u8 vc_cfg_input = 0;
1616 fd = open (conf_fname, O_RDONLY);
1619 if (VPPCOM_DEBUG > 0)
1620 clib_warning ("[%d] open configuration file '%s' failed!",
1621 getpid (), conf_fname);
1625 if (fstat (fd, &s) < 0)
1627 if (VPPCOM_DEBUG > 0)
1628 clib_warning ("[%d] failed to stat `%s'", getpid (), conf_fname);
1632 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1634 if (VPPCOM_DEBUG > 0)
1635 clib_warning ("[%d] not a regular file `%s'", getpid (), conf_fname);
1639 unformat_init_clib_file (input, fd);
1641 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1643 (void) unformat_user (input, unformat_line_input, line_input);
1644 unformat_skip_white_space (line_input);
1646 if (unformat (line_input, "vcl {"))
1654 if (unformat (line_input, "heapsize %s", &chroot_path))
1656 vec_terminate_c_string (chroot_path);
1657 if (VPPCOM_DEBUG > 0)
1658 clib_warning ("[%d] configured heapsize %s, "
1659 "actual heapsize %lld (0x%llx)",
1660 getpid (), chroot_path, vcl_cfg->heapsize,
1662 vec_free (chroot_path);
1664 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1666 vec_terminate_c_string (chroot_path);
1667 vl_set_memory_root_path ((char *) chroot_path);
1668 if (VPPCOM_DEBUG > 0)
1669 clib_warning ("[%d] configured api-prefix %s",
1670 getpid (), chroot_path);
1671 chroot_path = 0; /* Don't vec_free() it! */
1673 else if (unformat (line_input, "uid %d", &uid))
1675 vl_set_memory_uid (uid);
1676 if (VPPCOM_DEBUG > 0)
1677 clib_warning ("[%d] configured uid %d", getpid (), uid);
1679 else if (unformat (line_input, "gid %d", &gid))
1681 vl_set_memory_gid (gid);
1682 if (VPPCOM_DEBUG > 0)
1683 clib_warning ("[%d] configured gid %d", getpid (), gid);
1685 else if (unformat (line_input, "segment-baseva 0x%lx",
1686 &vcl_cfg->segment_baseva))
1688 if (VPPCOM_DEBUG > 0)
1689 clib_warning ("[%d] configured segment_baseva 0x%lx",
1690 getpid (), vcl_cfg->segment_baseva);
1692 else if (unformat (line_input, "segment-size 0x%lx",
1693 &vcl_cfg->segment_size))
1695 if (VPPCOM_DEBUG > 0)
1696 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1697 getpid (), vcl_cfg->segment_size,
1698 vcl_cfg->segment_size);
1700 else if (unformat (line_input, "segment-size %ld",
1701 &vcl_cfg->segment_size))
1703 if (VPPCOM_DEBUG > 0)
1704 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1705 getpid (), vcl_cfg->segment_size,
1706 vcl_cfg->segment_size);
1708 else if (unformat (line_input, "add-segment-size 0x%lx",
1709 &vcl_cfg->add_segment_size))
1711 if (VPPCOM_DEBUG > 0)
1713 ("[%d] configured add_segment_size 0x%lx (%ld)",
1714 getpid (), vcl_cfg->add_segment_size,
1715 vcl_cfg->add_segment_size);
1717 else if (unformat (line_input, "add-segment-size %ld",
1718 &vcl_cfg->add_segment_size))
1720 if (VPPCOM_DEBUG > 0)
1722 ("[%d] configured add_segment_size %ld (0x%lx)",
1723 getpid (), vcl_cfg->add_segment_size,
1724 vcl_cfg->add_segment_size);
1726 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1727 &vcl_cfg->preallocated_fifo_pairs))
1729 if (VPPCOM_DEBUG > 0)
1730 clib_warning ("[%d] configured preallocated_fifo_pairs "
1731 "%d (0x%x)", getpid (),
1732 vcl_cfg->preallocated_fifo_pairs,
1733 vcl_cfg->preallocated_fifo_pairs);
1735 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1736 &vcl_cfg->rx_fifo_size))
1738 if (VPPCOM_DEBUG > 0)
1739 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1740 getpid (), vcl_cfg->rx_fifo_size,
1741 vcl_cfg->rx_fifo_size);
1743 else if (unformat (line_input, "rx-fifo-size %ld",
1744 &vcl_cfg->rx_fifo_size))
1746 if (VPPCOM_DEBUG > 0)
1747 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1748 getpid (), vcl_cfg->rx_fifo_size,
1749 vcl_cfg->rx_fifo_size);
1751 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1752 &vcl_cfg->tx_fifo_size))
1754 if (VPPCOM_DEBUG > 0)
1755 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1756 getpid (), vcl_cfg->tx_fifo_size,
1757 vcl_cfg->tx_fifo_size);
1759 else if (unformat (line_input, "tx-fifo-size %ld",
1760 &vcl_cfg->tx_fifo_size))
1762 if (VPPCOM_DEBUG > 0)
1763 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1764 getpid (), vcl_cfg->tx_fifo_size,
1765 vcl_cfg->tx_fifo_size);
1767 else if (unformat (line_input, "event-queue-size 0x%lx",
1768 &vcl_cfg->event_queue_size))
1770 if (VPPCOM_DEBUG > 0)
1771 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1772 getpid (), vcl_cfg->event_queue_size,
1773 vcl_cfg->event_queue_size);
1775 else if (unformat (line_input, "event-queue-size %ld",
1776 &vcl_cfg->event_queue_size))
1778 if (VPPCOM_DEBUG > 0)
1779 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1780 getpid (), vcl_cfg->event_queue_size,
1781 vcl_cfg->event_queue_size);
1783 else if (unformat (line_input, "listen-queue-size 0x%lx",
1784 &vcl_cfg->listen_queue_size))
1786 if (VPPCOM_DEBUG > 0)
1787 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1788 getpid (), vcl_cfg->listen_queue_size,
1789 vcl_cfg->listen_queue_size);
1791 else if (unformat (line_input, "listen-queue-size %ld",
1792 &vcl_cfg->listen_queue_size))
1794 if (VPPCOM_DEBUG > 0)
1795 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1796 getpid (), vcl_cfg->listen_queue_size,
1797 vcl_cfg->listen_queue_size);
1799 else if (unformat (line_input, "app-timeout %f",
1800 &vcl_cfg->app_timeout))
1802 if (VPPCOM_DEBUG > 0)
1803 clib_warning ("[%d] configured app_timeout %f",
1804 getpid (), vcl_cfg->app_timeout);
1806 else if (unformat (line_input, "session-timeout %f",
1807 &vcl_cfg->session_timeout))
1809 if (VPPCOM_DEBUG > 0)
1810 clib_warning ("[%d] configured session_timeout %f",
1811 getpid (), vcl_cfg->session_timeout);
1813 else if (unformat (line_input, "accept-timeout %f",
1814 &vcl_cfg->accept_timeout))
1816 if (VPPCOM_DEBUG > 0)
1817 clib_warning ("[%d] configured accept_timeout %f",
1818 getpid (), vcl_cfg->accept_timeout);
1820 else if (unformat (line_input, "app-proxy-transport-tcp"))
1822 vcl_cfg->app_proxy_transport_tcp = 1;
1823 if (VPPCOM_DEBUG > 0)
1824 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1825 getpid (), vcl_cfg->app_proxy_transport_tcp);
1827 else if (unformat (line_input, "app-proxy-transport-udp"))
1829 vcl_cfg->app_proxy_transport_udp = 1;
1830 if (VPPCOM_DEBUG > 0)
1831 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1832 getpid (), vcl_cfg->app_proxy_transport_udp);
1834 else if (unformat (line_input, "app-scope-local"))
1836 vcl_cfg->app_scope_local = 1;
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("[%d] configured app_scope_local (%d)",
1839 getpid (), vcl_cfg->app_scope_local);
1841 else if (unformat (line_input, "app-scope-global"))
1843 vcl_cfg->app_scope_global = 1;
1844 if (VPPCOM_DEBUG > 0)
1845 clib_warning ("[%d] configured app_scope_global (%d)",
1846 getpid (), vcl_cfg->app_scope_global);
1848 else if (unformat (line_input, "namespace-secret %lu",
1849 &vcl_cfg->namespace_secret))
1851 if (VPPCOM_DEBUG > 0)
1853 ("[%d] configured namespace_secret %lu (0x%lx)",
1854 getpid (), vcl_cfg->namespace_secret,
1855 vcl_cfg->namespace_secret);
1857 else if (unformat (line_input, "namespace-id %v",
1858 &vcl_cfg->namespace_id))
1860 vl_api_application_attach_t *mp;
1861 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1862 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1863 if (nsid_vec_len > max_nsid_vec_len)
1865 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1866 if (VPPCOM_DEBUG > 0)
1867 clib_warning ("[%d] configured namespace_id is too long,"
1868 " truncated to %d characters!", getpid (),
1872 if (VPPCOM_DEBUG > 0)
1873 clib_warning ("[%d] configured namespace_id %v",
1874 getpid (), vcl_cfg->namespace_id);
1876 else if (unformat (line_input, "}"))
1879 if (VPPCOM_DEBUG > 0)
1880 clib_warning ("[%d] completed parsing vppcom config!",
1886 if (line_input->buffer[line_input->index] != '#')
1888 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1890 &line_input->buffer[line_input->index]);
1897 unformat_free (input);
1905 * VPPCOM Public API functions
1908 vppcom_app_create (char *app_name)
1910 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1921 vppcom_cfg_init (vcl_cfg);
1922 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1926 if (sscanf (env_var_str, "%u", &tmp) != 1)
1927 clib_warning ("[%d] Invalid debug level specified in "
1928 "the environment variable "
1930 " (%s)!\n", getpid (), env_var_str);
1934 clib_warning ("[%d] configured debug level (%u) from "
1935 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1938 conf_fname = getenv (VPPCOM_ENV_CONF);
1941 conf_fname = VPPCOM_CONF_DEFAULT;
1942 if (VPPCOM_DEBUG > 0)
1943 clib_warning ("[%d] getenv '%s' failed!", getpid (),
1946 vppcom_cfg_heapsize (conf_fname);
1947 clib_fifo_validate (vcm->client_session_index_fifo,
1948 vcm->cfg.listen_queue_size);
1949 vppcom_cfg_read (conf_fname);
1950 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1953 u32 ns_id_vec_len = strlen (env_var_str);
1955 vec_reset_length (vcm->cfg.namespace_id);
1956 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1957 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1959 if (VPPCOM_DEBUG > 0)
1960 clib_warning ("[%d] configured namespace_id (%v) from "
1961 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1962 vcm->cfg.namespace_id);
1964 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1968 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1969 clib_warning ("[%d] Invalid namespace secret specified in "
1970 "the environment variable "
1971 VPPCOM_ENV_APP_NAMESPACE_SECRET
1972 " (%s)!\n", getpid (), env_var_str);
1975 vcm->cfg.namespace_secret = tmp;
1976 if (VPPCOM_DEBUG > 0)
1977 clib_warning ("[%d] configured namespace secret (%lu) from "
1978 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1979 vcm->cfg.namespace_secret);
1982 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1984 vcm->cfg.app_proxy_transport_tcp = 1;
1985 if (VPPCOM_DEBUG > 0)
1986 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1987 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1988 vcm->cfg.app_proxy_transport_tcp);
1990 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1992 vcm->cfg.app_proxy_transport_udp = 1;
1993 if (VPPCOM_DEBUG > 0)
1994 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1995 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1996 vcm->cfg.app_proxy_transport_udp);
1998 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2000 vcm->cfg.app_scope_local = 1;
2001 if (VPPCOM_DEBUG > 0)
2002 clib_warning ("[%d] configured app_scope_local (%u) from "
2003 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
2004 vcm->cfg.app_scope_local);
2006 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2008 vcm->cfg.app_scope_global = 1;
2009 if (VPPCOM_DEBUG > 0)
2010 clib_warning ("[%d] configured app_scope_global (%u) from "
2011 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
2012 vcm->cfg.app_scope_global);
2015 vcm->main_cpu = os_get_thread_index ();
2016 heap = clib_mem_get_per_cpu_heap ();
2017 h = mheap_header (heap);
2019 /* make the main heap thread-safe */
2020 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2022 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2024 clib_time_init (&vcm->clib_time);
2025 vppcom_init_error_string_table ();
2026 svm_fifo_segment_init (vcl_cfg->segment_baseva,
2027 20 /* timeout in secs */ );
2028 clib_spinlock_init (&vcm->sessions_lockp);
2029 vppcom_api_hookup ();
2032 if (vcm->my_client_index == ~0)
2034 vcm->app_state = STATE_APP_START;
2035 rv = vppcom_connect_to_vpp (app_name);
2038 clib_warning ("[%d] ERROR: couldn't connect to VPP!", getpid ());
2042 if (VPPCOM_DEBUG > 0)
2043 clib_warning ("[%d] sending session enable", getpid ());
2045 rv = vppcom_app_session_enable ();
2048 clib_warning ("[%d] ERROR: vppcom_app_session_enable() failed!",
2053 if (VPPCOM_DEBUG > 0)
2054 clib_warning ("[%d] sending app attach", getpid ());
2056 rv = vppcom_app_attach ();
2059 clib_warning ("[%d] ERROR: vppcom_app_attach() failed!", getpid ());
2063 if (VPPCOM_DEBUG > 0)
2064 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
2065 getpid (), app_name, vcm->my_client_index,
2066 vcm->my_client_index);
2073 vppcom_app_destroy (void)
2077 if (vcm->my_client_index == ~0)
2080 if (VPPCOM_DEBUG > 0)
2081 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
2082 getpid (), vcm->my_client_index, vcm->my_client_index);
2084 vppcom_app_detach ();
2085 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2086 if (PREDICT_FALSE (rv))
2088 if (VPPCOM_DEBUG > 0)
2089 clib_warning ("[%d] application detach timed out! returning %d (%s)",
2090 getpid (), rv, vppcom_retval_str (rv));
2092 vl_client_disconnect_from_vlib ();
2093 vcm->my_client_index = ~0;
2094 vcm->app_state = STATE_APP_START;
2098 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2103 clib_spinlock_lock (&vcm->sessions_lockp);
2104 pool_get (vcm->sessions, session);
2105 memset (session, 0, sizeof (*session));
2106 session_index = session - vcm->sessions;
2109 session->proto = proto;
2110 session->state = STATE_START;
2111 session->is_nonblocking = is_nonblocking ? 1 : 0;
2112 session->vpp_handle = ~0;
2113 clib_spinlock_unlock (&vcm->sessions_lockp);
2115 if (VPPCOM_DEBUG > 0)
2116 clib_warning ("[%d] sid %u", getpid (), session_index);
2118 return (int) session_index;
2122 vppcom_session_close (uint32_t session_index)
2124 session_t *session = 0;
2133 session_state_t state;
2135 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2136 is_listen = session->is_listen;
2137 is_vep = session->is_vep;
2138 is_vep_session = session->is_vep_session;
2139 next_sid = session->vep.next_sid;
2140 vep_idx = session->vep.vep_idx;
2141 state = session->state;
2142 vpp_handle = session->vpp_handle;
2143 clib_spinlock_unlock (&vcm->sessions_lockp);
2145 if (VPPCOM_DEBUG > 0)
2148 clib_warning ("[%d] vep_idx %u / sid %u: closing epoll session...",
2149 getpid (), session_index, session_index);
2151 clib_warning ("[%d] vpp handle 0x%llx, sid %d: closing session...",
2152 getpid (), vpp_handle, session_index);
2157 while (next_sid != ~0)
2159 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2160 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2161 clib_warning ("[%d] vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2162 "vep_idx %u failed! rv %d (%s)", getpid (),
2163 vpp_handle, next_sid, vep_idx,
2164 rv, vppcom_retval_str (rv));
2166 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2167 next_sid = session->vep.next_sid;
2168 clib_spinlock_unlock (&vcm->sessions_lockp);
2175 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2176 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2177 clib_warning ("[%d] vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2178 "vep_idx %u failed! rv %d (%s)",
2179 getpid (), vpp_handle, session_index,
2180 vep_idx, rv, vppcom_retval_str (rv));
2185 if (state == STATE_LISTEN)
2187 rv = vppcom_session_unbind (session_index);
2188 if (PREDICT_FALSE (rv < 0))
2190 if (VPPCOM_DEBUG > 0)
2191 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2192 "listener unbind failed! rv %d (%s)",
2193 getpid (), vpp_handle, session_index,
2194 rv, vppcom_retval_str (rv));
2199 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2201 rv = vppcom_session_disconnect (session_index);
2202 if (PREDICT_FALSE (rv < 0))
2203 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2204 "session disconnect failed! rv %d (%s)",
2205 getpid (), vpp_handle, session_index,
2206 rv, vppcom_retval_str (rv));
2210 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2211 vpp_handle = session->vpp_handle;
2212 if (vpp_handle != ~0)
2214 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2216 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2218 pool_put_index (vcm->sessions, session_index);
2219 clib_spinlock_unlock (&vcm->sessions_lockp);
2221 if (VPPCOM_DEBUG > 0)
2224 clib_warning ("[%d] vep_idx %u / sid %u: epoll session removed.",
2225 getpid (), session_index, session_index);
2227 clib_warning ("[%d] vpp handle 0x%llx, sid %u: session removed.",
2228 getpid (), vpp_handle, session_index);
2235 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2237 session_t *session = 0;
2241 return VPPCOM_EINVAL;
2243 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2245 if (session->is_vep)
2247 clib_spinlock_unlock (&vcm->sessions_lockp);
2248 clib_warning ("[%d] ERROR: sid %u: cannot bind to an epoll session!",
2249 getpid (), session_index);
2254 session->vrf = ep->vrf;
2255 session->lcl_addr.is_ip4 = ep->is_ip4;
2256 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2257 session->lcl_port = ep->port;
2259 if (VPPCOM_DEBUG > 0)
2260 clib_warning ("[%d] sid %u: binding to local %s address %U "
2261 "port %u, proto %s", getpid (), session_index,
2262 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2263 format_ip46_address, &session->lcl_addr.ip46,
2264 session->lcl_addr.is_ip4,
2265 clib_net_to_host_u16 (session->lcl_port),
2266 session->proto ? "UDP" : "TCP");
2268 clib_spinlock_unlock (&vcm->sessions_lockp);
2274 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2276 session_t *listen_session = 0;
2277 u64 listen_vpp_handle;
2280 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2282 if (listen_session->is_vep)
2284 clib_spinlock_unlock (&vcm->sessions_lockp);
2285 clib_warning ("[%d] ERROR: sid %u: cannot listen on an "
2286 "epoll session!", getpid (), listen_session_index);
2291 listen_vpp_handle = listen_session->vpp_handle;
2292 if (listen_session->is_listen)
2294 clib_spinlock_unlock (&vcm->sessions_lockp);
2295 if (VPPCOM_DEBUG > 0)
2296 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2297 "already in listen state!",
2298 getpid (), listen_vpp_handle, listen_session_index);
2303 if (VPPCOM_DEBUG > 0)
2304 clib_warning ("[%d] vpp handle 0x%llx, sid %u: sending bind request...",
2305 getpid (), listen_vpp_handle, listen_session_index);
2307 vppcom_send_bind_sock (listen_session, listen_session_index);
2308 clib_spinlock_unlock (&vcm->sessions_lockp);
2310 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2311 vcm->cfg.session_timeout);
2313 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2314 if (PREDICT_FALSE (retval))
2316 if (VPPCOM_DEBUG > 0)
2317 clib_warning ("[%d] vpp handle 0x%llx, sid %u: bind failed! "
2318 "returning %d (%s)", getpid (),
2319 listen_session->vpp_handle, listen_session_index,
2320 retval, vppcom_retval_str (retval));
2321 clib_spinlock_unlock (&vcm->sessions_lockp);
2326 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2327 clib_spinlock_unlock (&vcm->sessions_lockp);
2333 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2334 uint32_t flags, double wait_for_time)
2336 session_t *listen_session = 0;
2337 session_t *client_session = 0;
2338 u32 client_session_index = ~0;
2342 u64 listen_vpp_handle;
2344 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2346 if (listen_session->is_vep)
2348 clib_spinlock_unlock (&vcm->sessions_lockp);
2349 clib_warning ("[%d] ERROR: sid %u: cannot accept on an "
2350 "epoll session!", getpid (), listen_session_index);
2355 listen_vpp_handle = listen_session->vpp_handle;
2356 if (listen_session->state != STATE_LISTEN)
2358 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2359 "not in listen state! state 0x%x (%s)", getpid (),
2360 listen_vpp_handle, listen_session_index,
2361 listen_session->state,
2362 vppcom_session_state_str (listen_session->state));
2363 clib_spinlock_unlock (&vcm->sessions_lockp);
2367 wait_for = ((listen_session->is_nonblocking) ? 0 :
2368 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time);
2370 clib_spinlock_unlock (&vcm->sessions_lockp);
2374 rv = vppcom_wait_for_client_session_index (wait_for);
2377 if ((VPPCOM_DEBUG > 0))
2378 clib_warning ("[%d] vpp handle 0x%llx, sid %u: accept failed! "
2379 "returning %d (%s)", getpid (),
2380 listen_vpp_handle, listen_session_index,
2381 rv, vppcom_retval_str (rv));
2382 if ((wait_for == 0) || (wait_for_time > 0))
2389 clib_spinlock_lock (&vcm->sessions_lockp);
2390 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2391 rv = vppcom_session_at_index (client_session_index, &client_session);
2392 if (PREDICT_FALSE (rv))
2394 rv = VPPCOM_ECONNABORTED;
2395 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client sid %u "
2396 "lookup failed! returning %d (%s)", getpid (),
2397 listen_vpp_handle, listen_session_index,
2398 client_session_index, rv, vppcom_retval_str (rv));
2402 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2403 if (VPPCOM_DEBUG > 0)
2404 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Got a client request! "
2405 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2406 getpid (), listen_vpp_handle, listen_session_index,
2407 client_session->vpp_handle, client_session_index,
2408 flags, client_session->is_nonblocking);
2410 ep->vrf = client_session->vrf;
2411 ep->is_cut_thru = client_session->is_cut_thru;
2412 ep->is_ip4 = client_session->peer_addr.is_ip4;
2413 ep->port = client_session->peer_port;
2414 if (client_session->peer_addr.is_ip4)
2415 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2416 sizeof (ip4_address_t));
2418 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2419 sizeof (ip6_address_t));
2421 if (client_session->is_server && client_session->is_cut_thru)
2423 static svm_fifo_segment_create_args_t _a;
2424 svm_fifo_segment_create_args_t *a = &_a;
2425 svm_fifo_segment_private_t *seg;
2427 cut_thru_str = " cut-thru ";
2429 /* Create the segment */
2430 memset (a, 0, sizeof (*a));
2431 a->segment_name = (char *)
2432 format ((u8 *) a->segment_name, "%d:segment%d%c",
2433 getpid (), vcm->unique_segment_index++, 0);
2434 a->segment_size = vcm->cfg.segment_size;
2435 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2436 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2437 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2439 rv = svm_fifo_segment_create (a);
2440 if (PREDICT_FALSE (rv))
2442 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2443 "client sid %u svm_fifo_segment_create ('%s') "
2444 "failed! rv %d", getpid (), listen_vpp_handle,
2445 listen_session_index, client_session_index,
2446 a->segment_name, rv);
2447 vec_reset_length (a->new_segment_indices);
2448 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2449 vppcom_send_connect_session_reply (client_session,
2450 client_session_index,
2451 client_session->vpp_handle,
2452 client_session->client_context,
2454 clib_spinlock_unlock (&vcm->sessions_lockp);
2459 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2460 client_session->sm_seg_index = a->new_segment_indices[0];
2461 vec_free (a->new_segment_indices);
2463 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2464 client_session->server_rx_fifo =
2465 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2466 FIFO_SEGMENT_RX_FREELIST);
2467 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2469 svm_fifo_segment_delete (seg);
2470 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2471 "client sid %u rx fifo alloc failed! "
2472 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2473 listen_session_index, client_session_index,
2474 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2475 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2476 vppcom_send_connect_session_reply (client_session,
2477 client_session_index,
2478 client_session->vpp_handle,
2479 client_session->client_context,
2481 clib_spinlock_unlock (&vcm->sessions_lockp);
2485 client_session->server_rx_fifo->master_session_index =
2486 client_session_index;
2488 client_session->server_tx_fifo =
2489 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2490 FIFO_SEGMENT_TX_FREELIST);
2491 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2493 svm_fifo_segment_delete (seg);
2494 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2495 "client sid %u tx fifo alloc failed! "
2496 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2497 listen_session_index, client_session_index,
2498 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2499 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2500 vppcom_send_connect_session_reply (client_session,
2501 client_session_index,
2502 client_session->vpp_handle,
2503 client_session->client_context,
2505 clib_spinlock_unlock (&vcm->sessions_lockp);
2509 client_session->server_tx_fifo->master_session_index =
2510 client_session_index;
2512 if (VPPCOM_DEBUG > 1)
2513 clib_warning ("[%d] vpp handle 0x%llx, sid %u: client sid %u "
2514 "created segment '%s', rx_fifo %p, tx_fifo %p",
2515 getpid (), listen_vpp_handle, listen_session_index,
2516 client_session_index, client_session->segment_name,
2517 client_session->server_rx_fifo,
2518 client_session->server_tx_fifo);
2520 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2523 ssvm_shared_header_t *sh = seg->ssvm.sh;
2525 ssvm_lock_non_recursive (sh, 1);
2526 oldheap = ssvm_push_heap (sh);
2527 event_q = client_session->vpp_event_queue =
2528 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
2529 sizeof (session_fifo_event_t),
2530 getpid (), 0 /* signal not sent */ );
2531 ssvm_pop_heap (oldheap);
2532 ssvm_unlock_non_recursive (sh);
2535 vppcom_send_connect_session_reply (client_session,
2536 client_session_index,
2537 client_session->vpp_handle,
2538 client_session->client_context,
2539 0 /* retval OK */ );
2544 vppcom_send_accept_session_reply (client_session->vpp_handle,
2545 client_session->client_context,
2546 0 /* retval OK */ );
2549 if (VPPCOM_DEBUG > 0)
2550 clib_warning ("[%d] vpp handle 0x%llx, sid %u: accepted vpp handle "
2551 "0x%llx, sid %u%sconnection to local %s address "
2552 "%U port %u", getpid (), listen_vpp_handle,
2553 listen_session_index, client_session->vpp_handle,
2554 client_session_index, cut_thru_str,
2555 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2556 format_ip46_address, &client_session->lcl_addr.ip46,
2557 client_session->lcl_addr.is_ip4,
2558 clib_net_to_host_u16 (client_session->lcl_port));
2560 clib_spinlock_unlock (&vcm->sessions_lockp);
2561 rv = (int) client_session_index;
2567 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2569 session_t *session = 0;
2570 int rv, retval = VPPCOM_OK;
2571 u64 vpp_handle = ~0;
2573 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2575 if (PREDICT_FALSE (session->is_vep))
2577 clib_spinlock_unlock (&vcm->sessions_lockp);
2578 clib_warning ("[%d] ERROR: sid %u: cannot connect on an epoll session!",
2579 getpid (), session_index);
2584 vpp_handle = session->vpp_handle;
2585 if (PREDICT_FALSE (session->is_server))
2587 clib_spinlock_unlock (&vcm->sessions_lockp);
2588 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: is in use "
2589 "as a server session!", getpid (), vpp_handle,
2595 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2597 if (VPPCOM_DEBUG > 0)
2598 clib_warning ("[%d] vpp handle 0x%llx, sid %u: session already "
2599 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2600 getpid (), vpp_handle, session_index,
2601 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2602 format_ip46_address,
2603 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2604 clib_net_to_host_u16 (session->peer_port),
2605 session->proto ? "UDP" : "TCP", session->state,
2606 vppcom_session_state_str (session->state));
2608 clib_spinlock_unlock (&vcm->sessions_lockp);
2612 session->vrf = server_ep->vrf;
2613 session->peer_addr.is_ip4 = server_ep->is_ip4;
2614 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2615 session->peer_port = server_ep->port;
2617 if (VPPCOM_DEBUG > 0)
2618 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connecting to server "
2619 "%s %U port %d proto %s",
2620 getpid (), vpp_handle, session_index,
2621 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2622 format_ip46_address,
2623 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2624 clib_net_to_host_u16 (session->peer_port),
2625 session->proto ? "UDP" : "TCP");
2627 vppcom_send_connect_sock (session, session_index);
2628 clib_spinlock_unlock (&vcm->sessions_lockp);
2631 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2632 vcm->cfg.session_timeout);
2634 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2635 vpp_handle = session->vpp_handle;
2636 clib_spinlock_unlock (&vcm->sessions_lockp);
2639 if (PREDICT_FALSE (retval))
2642 if (VPPCOM_DEBUG > 0)
2643 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connect failed! "
2644 "returning %d (%s)", getpid (), vpp_handle,
2645 session_index, rv, vppcom_retval_str (rv));
2647 else if (VPPCOM_DEBUG > 0)
2648 clib_warning ("[%d] vpp handle 0x%llx, sid %u: connected!",
2649 getpid (), vpp_handle, session_index);
2655 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2658 session_t *session = 0;
2659 svm_fifo_t *rx_fifo;
2664 session_state_t state;
2671 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2673 if (PREDICT_FALSE (session->is_vep))
2675 clib_spinlock_unlock (&vcm->sessions_lockp);
2676 clib_warning ("[%d] ERROR: sid %u: cannot read from an epoll session!",
2677 getpid (), session_index);
2682 vpp_handle = session->vpp_handle;
2683 is_server = session->is_server;
2684 is_nonblocking = session->is_nonblocking;
2685 state = session->state;
2686 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
2688 clib_spinlock_unlock (&vcm->sessions_lockp);
2689 rv = ((state == STATE_DISCONNECT) ?
2690 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2692 if (VPPCOM_DEBUG > 0)
2693 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s session is "
2694 "not open! state 0x%x (%s), returning %d (%s)",
2695 getpid (), vpp_handle, session_index,
2696 is_server ? "server" : "client", state,
2697 vppcom_session_state_str (state),
2698 rv, vppcom_retval_str (rv));
2702 rx_fifo = ((!session->is_cut_thru || is_server) ?
2703 session->server_rx_fifo : session->server_tx_fifo);
2704 fifo_str = ((!session->is_cut_thru || is_server) ?
2705 "server_rx_fifo" : "server_tx_fifo");
2706 clib_spinlock_unlock (&vcm->sessions_lockp);
2711 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2713 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2715 while (!is_nonblocking && (n_read <= 0));
2719 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2721 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2722 (EPOLLET | EPOLLIN));
2724 session->vep.et_mask |= EPOLLIN;
2726 if (state == STATE_CLOSE_ON_EMPTY)
2728 session_state_t new_state = STATE_DISCONNECT;
2729 rv = VPPCOM_ECONNRESET;
2731 if (VPPCOM_DEBUG > 1)
2733 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Empty fifo "
2734 "with %s session state 0x%x (%s)!"
2735 " Setting state to 0x%x (%s), returning %d (%s)",
2736 getpid (), vpp_handle, session_index,
2737 is_server ? "server" : "client",
2738 state, vppcom_session_state_str (state),
2739 new_state, vppcom_session_state_str (new_state),
2740 rv, vppcom_retval_str (rv));
2743 session->state = new_state;
2748 clib_spinlock_unlock (&vcm->sessions_lockp);
2753 if (VPPCOM_DEBUG > 2)
2756 clib_warning ("[%d] vpp handle 0x%llx, sid %u: read %d bytes "
2757 "from %s (%p)", getpid (), vpp_handle,
2758 session_index, n_read, fifo_str, rx_fifo);
2760 clib_warning ("[%d] vpp handle 0x%llx, sid %u: nothing read! "
2761 "returning %d (%s)", getpid (), vpp_handle,
2762 session_index, rv, vppcom_retval_str (rv));
2769 vppcom_session_read (uint32_t session_index, void *buf, int n)
2771 return (vppcom_session_read_internal (session_index, buf, n, 0));
2775 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2777 return (vppcom_session_read_internal (session_index, buf, n, 1));
2781 vppcom_session_read_ready (session_t * session, u32 session_index)
2783 svm_fifo_t *rx_fifo = 0;
2787 u8 is_server = session->is_server;
2788 session_state_t state = session->state;
2789 u64 vpp_handle = session->vpp_handle;
2791 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2792 if (PREDICT_FALSE (session->is_vep))
2794 clib_warning ("[%d] ERROR: sid %u: cannot read from an "
2795 "epoll session!", getpid (), session_index);
2800 if (session->is_listen)
2801 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2804 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2806 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2809 if (VPPCOM_DEBUG > 1)
2810 clib_warning ("[%d] vpp handle 0x%llx, sid %u: %s session is "
2811 "not open! state 0x%x (%s), returning %d (%s)",
2812 getpid (), vpp_handle, session_index,
2813 is_server ? "server" : "client",
2814 state, vppcom_session_state_str (state),
2815 rv, vppcom_retval_str (rv));
2819 rx_fifo = ((!session->is_cut_thru || is_server) ?
2820 session->server_rx_fifo : session->server_tx_fifo);
2822 ready = svm_fifo_max_dequeue (rx_fifo);
2828 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2830 session->vep.et_mask |= EPOLLIN;
2832 if (state == STATE_CLOSE_ON_EMPTY)
2834 rv = VPPCOM_ECONNRESET;
2835 session_state_t new_state = STATE_DISCONNECT;
2837 if (VPPCOM_DEBUG > 1)
2839 clib_warning ("[%d] vpp handle 0x%llx, sid %u: Empty fifo with"
2840 " %s session state 0x%x (%s)! Setting state to "
2841 "0x%x (%s), returning %d (%s)",
2842 getpid (), session_index, vpp_handle,
2843 is_server ? "server" : "client",
2844 state, vppcom_session_state_str (state),
2845 new_state, vppcom_session_state_str (new_state),
2846 rv, vppcom_retval_str (rv));
2848 session->state = new_state;
2858 vppcom_session_write (uint32_t session_index, void *buf, int n)
2860 session_t *session = 0;
2861 svm_fifo_t *tx_fifo;
2862 unix_shared_memory_queue_t *q;
2863 session_fifo_event_t evt;
2869 session_state_t state;
2874 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2876 if (PREDICT_FALSE (session->is_vep))
2878 clib_spinlock_unlock (&vcm->sessions_lockp);
2879 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
2880 "cannot write to an epoll session!",
2881 getpid (), session->vpp_handle, session_index);
2887 is_server = session->is_server;
2888 is_nonblocking = session->is_nonblocking;
2889 vpp_handle = session->vpp_handle;
2890 state = session->state;
2891 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2893 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2896 clib_spinlock_unlock (&vcm->sessions_lockp);
2897 if (VPPCOM_DEBUG > 1)
2898 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2899 "%s session is not open! state 0x%x (%s)",
2900 getpid (), vpp_handle, session_index,
2901 is_server ? "server" : "client", state,
2902 vppcom_session_state_str (state));
2906 tx_fifo = ((!session->is_cut_thru || is_server) ?
2907 session->server_tx_fifo : session->server_rx_fifo);
2908 fifo_str = ((!session->is_cut_thru || is_server) ?
2909 "server_tx_fifo" : "server_rx_fifo");
2910 clib_spinlock_unlock (&vcm->sessions_lockp);
2914 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2916 while (!is_nonblocking && (n_write <= 0));
2918 /* If event wasn't set, add one */
2919 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2921 /* Fabricate TX event, send to vpp */
2923 evt.event_type = FIFO_EVENT_APP_TX;
2925 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2926 q = session->vpp_event_queue;
2928 unix_shared_memory_queue_add (q, (u8 *) & evt,
2929 0 /* do wait for mutex */ );
2930 clib_spinlock_unlock (&vcm->sessions_lockp);
2931 if (VPPCOM_DEBUG > 1)
2932 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2933 "added FIFO_EVENT_APP_TX to "
2934 "vpp_event_q %p, n_write %d", getpid (),
2935 vpp_handle, session_index, q, n_write);
2940 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2942 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2943 (EPOLLET | EPOLLOUT));
2945 session->vep.et_mask |= EPOLLOUT;
2947 if (state == STATE_CLOSE_ON_EMPTY)
2949 session_state_t new_state = STATE_DISCONNECT;
2950 rv = VPPCOM_ECONNRESET;
2952 if (VPPCOM_DEBUG > 1)
2954 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2955 "Empty fifo with %s session state 0x%x (%s)!"
2956 " Setting state to 0x%x (%s), returning %d (%s)",
2957 getpid (), vpp_handle, session_index,
2958 is_server ? "server" : "client",
2959 state, vppcom_session_state_str (state),
2960 new_state, vppcom_session_state_str (new_state),
2961 rv, vppcom_retval_str (rv));
2964 session->state = new_state;
2969 clib_spinlock_unlock (&vcm->sessions_lockp);
2974 if (VPPCOM_DEBUG > 2)
2977 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2978 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
2979 session_index, fifo_str, tx_fifo);
2981 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
2982 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
2983 session_index, n_write, fifo_str, tx_fifo);
2990 vppcom_session_write_ready (session_t * session, u32 session_index)
2992 svm_fifo_t *tx_fifo;
2997 u8 is_server = session->is_server;
2998 session_state_t state = session->state;
3000 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3001 if (PREDICT_FALSE (session->is_vep))
3003 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3004 "cannot write to an epoll session!",
3005 getpid (), session->vpp_handle, session_index);
3010 if (PREDICT_FALSE (session->is_listen))
3012 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3013 "cannot write to a listen session!",
3014 getpid (), session->vpp_handle, session_index);
3019 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3021 session_state_t state = session->state;
3023 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3026 clib_warning ("[%d] ERROR: vpp handle 0x%llx, sid %u: "
3027 "%s session is not open! state 0x%x (%s), "
3028 "returning %d (%s)", getpid (), session->vpp_handle,
3029 session_index, is_server ? "server" : "client",
3030 state, vppcom_session_state_str (state),
3031 rv, vppcom_retval_str (rv));
3035 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3036 session->server_tx_fifo : session->server_rx_fifo);
3037 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3038 "server_tx_fifo" : "server_rx_fifo");
3040 ready = svm_fifo_max_enqueue (tx_fifo);
3042 if (VPPCOM_DEBUG > 3)
3043 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3044 "peek %s (%p), ready = %d", getpid (),
3045 session->vpp_handle, session_index,
3046 fifo_str, tx_fifo, ready);
3050 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3051 (EPOLLET | EPOLLOUT));
3053 session->vep.et_mask |= EPOLLOUT;
3055 if (state == STATE_CLOSE_ON_EMPTY)
3057 rv = VPPCOM_ECONNRESET;
3058 session_state_t new_state = STATE_DISCONNECT;
3060 if (VPPCOM_DEBUG > 1)
3062 clib_warning ("[%d] vpp handle 0x%llx, sid %u: "
3063 "Empty fifo with %s session "
3064 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3065 "returning %d (%s)", getpid (),
3066 session->vpp_handle, session_index,
3067 is_server ? "server" : "client",
3068 state, vppcom_session_state_str (state),
3069 new_state, vppcom_session_state_str (new_state),
3070 rv, vppcom_retval_str (rv));
3072 session->state = new_state;
3082 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3083 unsigned long *write_map, unsigned long *except_map,
3084 double time_to_wait)
3087 session_t *session = 0;
3088 int rv, bits_set = 0;
3089 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3090 u32 minbits = clib_max (n_bits, BITS (uword));
3092 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3094 if (n_bits && read_map)
3096 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3097 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
3098 memset (read_map, 0, vec_len (vcm->rd_bitmap));
3100 if (n_bits && write_map)
3102 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3103 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
3104 memset (write_map, 0, vec_len (vcm->wr_bitmap));
3106 if (n_bits && except_map)
3108 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3109 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
3110 memset (except_map, 0, vec_len (vcm->ex_bitmap));
3120 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3122 clib_spinlock_lock (&vcm->sessions_lockp);
3123 rv = vppcom_session_at_index (session_index, &session);
3126 clib_spinlock_unlock (&vcm->sessions_lockp);
3127 if (VPPCOM_DEBUG > 1)
3128 clib_warning ("[%d] session %d specified in "
3129 "read_map is closed.", getpid (),
3131 bits_set = VPPCOM_EBADFD;
3135 rv = vppcom_session_read_ready (session, session_index);
3136 clib_spinlock_unlock (&vcm->sessions_lockp);
3137 if (except_map && vcm->ex_bitmap &&
3138 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3141 // TBD: clib_warning
3142 clib_bitmap_set_no_check (except_map, session_index, 1);
3147 // TBD: clib_warning
3148 clib_bitmap_set_no_check (read_map, session_index, 1);
3156 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3158 clib_spinlock_lock (&vcm->sessions_lockp);
3159 rv = vppcom_session_at_index (session_index, &session);
3162 clib_spinlock_unlock (&vcm->sessions_lockp);
3163 if (VPPCOM_DEBUG > 0)
3164 clib_warning ("[%d] session %d specified in "
3165 "write_map is closed.", getpid (),
3167 bits_set = VPPCOM_EBADFD;
3171 rv = vppcom_session_write_ready (session, session_index);
3172 clib_spinlock_unlock (&vcm->sessions_lockp);
3173 if (write_map && (rv > 0))
3175 // TBD: clib_warning
3176 clib_bitmap_set_no_check (write_map, session_index, 1);
3184 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3186 clib_spinlock_lock (&vcm->sessions_lockp);
3187 rv = vppcom_session_at_index (session_index, &session);
3190 clib_spinlock_unlock (&vcm->sessions_lockp);
3191 if (VPPCOM_DEBUG > 1)
3192 clib_warning ("[%d] session %d specified in "
3193 "except_map is closed.", getpid (),
3195 bits_set = VPPCOM_EBADFD;
3199 rv = vppcom_session_read_ready (session, session_index);
3200 clib_spinlock_unlock (&vcm->sessions_lockp);
3203 // TBD: clib_warning
3204 clib_bitmap_set_no_check (except_map, session_index, 1);
3212 while (clib_time_now (&vcm->clib_time) < timeout);
3219 vep_verify_epoll_chain (u32 vep_idx)
3222 vppcom_epoll_t *vep;
3226 if (VPPCOM_DEBUG <= 1)
3229 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3230 rv = vppcom_session_at_index (vep_idx, &session);
3231 if (PREDICT_FALSE (rv))
3233 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", getpid (), vep_idx);
3236 if (PREDICT_FALSE (!session->is_vep))
3238 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", getpid (),
3242 vep = &session->vep;
3243 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
3246 " is_vep_session = %u\n"
3247 " next_sid = 0x%x (%u)\n"
3248 " wait_cont_idx = 0x%x (%u)\n"
3249 "}\n", getpid (), vep_idx,
3250 session->is_vep, session->is_vep_session,
3251 vep->next_sid, vep->next_sid,
3252 session->wait_cont_idx, session->wait_cont_idx);
3254 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3256 rv = vppcom_session_at_index (sid, &session);
3257 if (PREDICT_FALSE (rv))
3259 clib_warning ("[%d] ERROR: Invalid sid (%u)!", getpid (), sid);
3262 if (PREDICT_FALSE (session->is_vep))
3263 clib_warning ("[%d] ERROR: sid (%u) is a vep!", getpid (), vep_idx);
3264 else if (PREDICT_FALSE (!session->is_vep_session))
3266 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
3270 vep = &session->vep;
3271 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3272 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
3273 "vep_idx (%u)!", getpid (),
3274 sid, session->vep.vep_idx, vep_idx);
3275 if (session->is_vep_session)
3277 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3279 " next_sid = 0x%x (%u)\n"
3280 " prev_sid = 0x%x (%u)\n"
3281 " vep_idx = 0x%x (%u)\n"
3282 " ev.events = 0x%x\n"
3283 " ev.data.u64 = 0x%llx\n"
3287 vep->next_sid, vep->next_sid,
3288 vep->prev_sid, vep->prev_sid,
3289 vep->vep_idx, vep->vep_idx,
3290 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3295 clib_warning ("[%d] vep_idx (%u): Dump complete!\n", getpid (), vep_idx);
3299 vppcom_epoll_create (void)
3301 session_t *vep_session;
3304 clib_spinlock_lock (&vcm->sessions_lockp);
3305 pool_get (vcm->sessions, vep_session);
3306 memset (vep_session, 0, sizeof (*vep_session));
3307 vep_idx = vep_session - vcm->sessions;
3309 vep_session->is_vep = 1;
3310 vep_session->vep.vep_idx = ~0;
3311 vep_session->vep.next_sid = ~0;
3312 vep_session->vep.prev_sid = ~0;
3313 vep_session->wait_cont_idx = ~0;
3314 vep_session->vpp_handle = ~0;
3315 clib_spinlock_unlock (&vcm->sessions_lockp);
3317 if (VPPCOM_DEBUG > 0)
3318 clib_warning ("[%d] Created vep_idx %u / sid %u!",
3319 getpid (), vep_idx, vep_idx);
3325 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3326 struct epoll_event *event)
3328 session_t *vep_session;
3332 if (vep_idx == session_index)
3334 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
3335 getpid (), vep_idx);
3336 return VPPCOM_EINVAL;
3339 clib_spinlock_lock (&vcm->sessions_lockp);
3340 rv = vppcom_session_at_index (vep_idx, &vep_session);
3341 if (PREDICT_FALSE (rv))
3343 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
3346 if (PREDICT_FALSE (!vep_session->is_vep))
3348 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3349 getpid (), vep_idx);
3354 ASSERT (vep_session->vep.vep_idx == ~0);
3355 ASSERT (vep_session->vep.prev_sid == ~0);
3357 rv = vppcom_session_at_index (session_index, &session);
3358 if (PREDICT_FALSE (rv))
3360 if (VPPCOM_DEBUG > 0)
3361 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
3362 getpid (), session_index);
3365 if (PREDICT_FALSE (session->is_vep))
3367 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3375 if (PREDICT_FALSE (!event))
3377 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
3378 "epoll_event structure!", getpid ());
3382 if (vep_session->vep.next_sid != ~0)
3384 session_t *next_session;
3385 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3387 if (PREDICT_FALSE (rv))
3389 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
3390 "vep.next_sid (%u) on vep_idx (%u)!",
3391 getpid (), vep_session->vep.next_sid, vep_idx);
3394 ASSERT (next_session->vep.prev_sid == vep_idx);
3395 next_session->vep.prev_sid = session_index;
3397 session->vep.next_sid = vep_session->vep.next_sid;
3398 session->vep.prev_sid = vep_idx;
3399 session->vep.vep_idx = vep_idx;
3400 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3401 session->vep.ev = *event;
3402 session->is_vep = 0;
3403 session->is_vep_session = 1;
3404 vep_session->vep.next_sid = session_index;
3405 if (VPPCOM_DEBUG > 1)
3406 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
3407 " data 0x%llx!", getpid (), vep_idx, session_index,
3408 event->events, event->data.u64);
3412 if (PREDICT_FALSE (!event))
3414 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
3415 "epoll_event structure!", getpid ());
3419 else if (PREDICT_FALSE (!session->is_vep_session))
3421 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3422 "not a vep session!", getpid (), session_index);
3426 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3428 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_MOD: "
3429 "vep_idx (%u) != vep_idx (%u)!",
3430 getpid (), session_index,
3431 session->vep.vep_idx, vep_idx);
3435 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3436 session->vep.ev = *event;
3437 if (VPPCOM_DEBUG > 1)
3438 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3439 " data 0x%llx!", getpid (), vep_idx, session_index,
3440 event->events, event->data.u64);
3444 if (PREDICT_FALSE (!session->is_vep_session))
3446 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3447 "not a vep session!", getpid (), session_index);
3451 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3453 clib_warning ("[%d] ERROR: sid %u EPOLL_CTL_DEL: "
3454 "vep_idx (%u) != vep_idx (%u)!",
3455 getpid (), session_index,
3456 session->vep.vep_idx, vep_idx);
3461 vep_session->wait_cont_idx =
3462 (vep_session->wait_cont_idx == session_index) ?
3463 session->vep.next_sid : vep_session->wait_cont_idx;
3465 if (session->vep.prev_sid == vep_idx)
3466 vep_session->vep.next_sid = session->vep.next_sid;
3469 session_t *prev_session;
3470 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3471 if (PREDICT_FALSE (rv))
3473 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3474 "vep.prev_sid (%u) on sid (%u)!",
3475 getpid (), session->vep.prev_sid, session_index);
3478 ASSERT (prev_session->vep.next_sid == session_index);
3479 prev_session->vep.next_sid = session->vep.next_sid;
3481 if (session->vep.next_sid != ~0)
3483 session_t *next_session;
3484 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3485 if (PREDICT_FALSE (rv))
3487 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3488 "vep.next_sid (%u) on sid (%u)!",
3489 getpid (), session->vep.next_sid, session_index);
3492 ASSERT (next_session->vep.prev_sid == session_index);
3493 next_session->vep.prev_sid = session->vep.prev_sid;
3496 memset (&session->vep, 0, sizeof (session->vep));
3497 session->vep.next_sid = ~0;
3498 session->vep.prev_sid = ~0;
3499 session->vep.vep_idx = ~0;
3500 session->is_vep_session = 0;
3501 if (VPPCOM_DEBUG > 1)
3502 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3503 getpid (), vep_idx, session_index);
3507 clib_warning ("[%d] ERROR: Invalid operation (%d)!", getpid (), op);
3511 vep_verify_epoll_chain (vep_idx);
3514 clib_spinlock_unlock (&vcm->sessions_lockp);
3519 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3520 int maxevents, double wait_for_time)
3522 session_t *vep_session;
3524 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3525 u32 keep_trying = 1;
3527 u32 vep_next_sid, wait_cont_idx;
3530 if (PREDICT_FALSE (maxevents <= 0))
3532 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3533 getpid (), maxevents);
3534 return VPPCOM_EINVAL;
3536 memset (events, 0, sizeof (*events) * maxevents);
3538 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3539 vep_next_sid = vep_session->vep.next_sid;
3540 is_vep = vep_session->is_vep;
3541 wait_cont_idx = vep_session->wait_cont_idx;
3542 clib_spinlock_unlock (&vcm->sessions_lockp);
3544 if (PREDICT_FALSE (!is_vep))
3546 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3547 getpid (), vep_idx);
3551 if (PREDICT_FALSE (vep_next_sid == ~0))
3553 if (VPPCOM_DEBUG > 0)
3554 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3555 getpid (), vep_idx);
3565 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3566 sid != ~0; sid = next_sid)
3568 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3569 u8 add_event, is_vep_session;
3571 u64 session_ev_data;
3573 VCL_LOCK_AND_GET_SESSION (sid, &session);
3574 next_sid = session->vep.next_sid;
3575 session_events = session->vep.ev.events;
3576 et_mask = session->vep.et_mask;
3577 is_vep = session->is_vep;
3578 is_vep_session = session->is_vep_session;
3579 session_vep_idx = session->vep.vep_idx;
3580 session_ev_data = session->vep.ev.data.u64;
3581 clib_spinlock_unlock (&vcm->sessions_lockp);
3583 if (PREDICT_FALSE (is_vep))
3585 if (VPPCOM_DEBUG > 0)
3586 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3587 getpid (), vep_idx);
3591 if (PREDICT_FALSE (!is_vep_session))
3593 if (VPPCOM_DEBUG > 0)
3594 clib_warning ("[%d] ERROR: session (%u) is not "
3595 "a vep session!", getpid (), sid);
3599 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3601 clib_warning ("[%d] ERROR: session (%u) "
3602 "vep_idx (%u) != vep_idx (%u)!",
3603 getpid (), sid, session->vep.vep_idx, vep_idx);
3608 add_event = clear_et_mask = 0;
3610 if (EPOLLIN & session_events)
3612 VCL_LOCK_AND_GET_SESSION (sid, &session);
3613 ready = vppcom_session_read_ready (session, sid);
3614 clib_spinlock_unlock (&vcm->sessions_lockp);
3615 if ((ready > 0) && (EPOLLIN & et_mask))
3618 events[num_ev].events |= EPOLLIN;
3619 if (((EPOLLET | EPOLLIN) & session_events) ==
3620 (EPOLLET | EPOLLIN))
3621 clear_et_mask |= EPOLLIN;
3628 case VPPCOM_ECONNRESET:
3629 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3633 events[num_ev].events |= EPOLLERR;
3639 if (EPOLLOUT & session_events)
3641 VCL_LOCK_AND_GET_SESSION (sid, &session);
3642 ready = vppcom_session_write_ready (session, sid);
3643 clib_spinlock_unlock (&vcm->sessions_lockp);
3644 if ((ready > 0) && (EPOLLOUT & et_mask))
3647 events[num_ev].events |= EPOLLOUT;
3648 if (((EPOLLET | EPOLLOUT) & session_events) ==
3649 (EPOLLET | EPOLLOUT))
3650 clear_et_mask |= EPOLLOUT;
3657 case VPPCOM_ECONNRESET:
3658 events[num_ev].events |= EPOLLHUP;
3662 events[num_ev].events |= EPOLLERR;
3670 events[num_ev].data.u64 = session_ev_data;
3671 if (EPOLLONESHOT & session_events)
3673 VCL_LOCK_AND_GET_SESSION (sid, &session);
3674 session->vep.ev.events = 0;
3675 clib_spinlock_unlock (&vcm->sessions_lockp);
3678 if (num_ev == maxevents)
3680 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3681 vep_session->wait_cont_idx = next_sid;
3682 clib_spinlock_unlock (&vcm->sessions_lockp);
3686 if (wait_cont_idx != ~0)
3689 next_sid = vep_next_sid;
3690 else if (next_sid == wait_cont_idx)
3694 if (wait_for_time != -1)
3695 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3697 while ((num_ev == 0) && keep_trying);
3699 if (wait_cont_idx != ~0)
3701 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3702 vep_session->wait_cont_idx = ~0;
3703 clib_spinlock_unlock (&vcm->sessions_lockp);
3706 return (rv != VPPCOM_OK) ? rv : num_ev;
3710 vppcom_session_attr (uint32_t session_index, uint32_t op,
3711 void *buffer, uint32_t * buflen)
3715 u32 *flags = buffer;
3716 vppcom_endpt_t *ep = buffer;
3718 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3721 case VPPCOM_ATTR_GET_NREAD:
3722 rv = vppcom_session_read_ready (session, session_index);
3723 if (VPPCOM_DEBUG > 2)
3724 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3728 case VPPCOM_ATTR_GET_NWRITE:
3729 rv = vppcom_session_write_ready (session, session_index);
3730 if (VPPCOM_DEBUG > 2)
3731 clib_warning ("[%d] VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3732 getpid (), session_index, rv);
3735 case VPPCOM_ATTR_GET_FLAGS:
3736 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3738 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3739 *buflen = sizeof (*flags);
3740 if (VPPCOM_DEBUG > 2)
3741 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: sid %u, "
3742 "flags = 0x%08x, is_nonblocking = %u", getpid (),
3743 session_index, *flags, session->is_nonblocking);
3749 case VPPCOM_ATTR_SET_FLAGS:
3750 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3752 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3753 if (VPPCOM_DEBUG > 2)
3754 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: sid %u, "
3755 "flags = 0x%08x, is_nonblocking = %u",
3756 getpid (), session_index, *flags,
3757 session->is_nonblocking);
3763 case VPPCOM_ATTR_GET_PEER_ADDR:
3764 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3766 ep->vrf = session->vrf;
3767 ep->is_ip4 = session->peer_addr.is_ip4;
3768 ep->port = session->peer_port;
3769 if (session->peer_addr.is_ip4)
3770 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3771 sizeof (ip4_address_t));
3773 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3774 sizeof (ip6_address_t));
3775 *buflen = sizeof (*ep);
3776 if (VPPCOM_DEBUG > 1)
3777 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = "
3778 "%u, addr = %U, port %u", getpid (),
3779 session_index, ep->is_ip4, format_ip46_address,
3780 &session->peer_addr.ip46, ep->is_ip4,
3781 clib_net_to_host_u16 (ep->port));
3787 case VPPCOM_ATTR_GET_LCL_ADDR:
3788 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3790 ep->vrf = session->vrf;
3791 ep->is_ip4 = session->lcl_addr.is_ip4;
3792 ep->port = session->lcl_port;
3793 if (session->lcl_addr.is_ip4)
3794 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3795 sizeof (ip4_address_t));
3797 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3798 sizeof (ip6_address_t));
3799 *buflen = sizeof (*ep);
3800 if (VPPCOM_DEBUG > 1)
3801 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = "
3802 "%u, addr = %U port %d", getpid (),
3803 session_index, ep->is_ip4, format_ip46_address,
3804 &session->lcl_addr.ip46, ep->is_ip4,
3805 clib_net_to_host_u16 (ep->port));
3811 case VPPCOM_ATTR_SET_REUSEADDR:
3814 case VPPCOM_ATTR_SET_BROADCAST:
3817 case VPPCOM_ATTR_SET_V6ONLY:
3820 case VPPCOM_ATTR_SET_KEEPALIVE:
3823 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3826 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3835 clib_spinlock_unlock (&vcm->sessions_lockp);
3840 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3841 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3844 session_t *session = 0;
3848 clib_spinlock_lock (&vcm->sessions_lockp);
3849 rv = vppcom_session_at_index (session_index, &session);
3850 if (PREDICT_FALSE (rv))
3852 clib_spinlock_unlock (&vcm->sessions_lockp);
3853 if (VPPCOM_DEBUG > 0)
3854 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3855 getpid (), session_index);
3857 clib_spinlock_unlock (&vcm->sessions_lockp);
3860 ep->vrf = session->vrf;
3861 ep->is_ip4 = session->peer_addr.is_ip4;
3862 ep->port = session->peer_port;
3863 if (session->peer_addr.is_ip4)
3864 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3865 sizeof (ip4_address_t));
3867 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3868 sizeof (ip6_address_t));
3869 clib_spinlock_unlock (&vcm->sessions_lockp);
3873 rv = vppcom_session_read (session_index, buffer, buflen);
3874 else if (flags & MSG_PEEK)
3875 rv = vppcom_session_peek (session_index, buffer, buflen);
3878 clib_warning ("[%d] Unsupport flags for recvfrom %d", getpid (), flags);
3879 rv = VPPCOM_EAFNOSUPPORT;
3887 vppcom_session_sendto (uint32_t session_index, void *buffer,
3888 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3891 return VPPCOM_EINVAL;
3896 return VPPCOM_EINVAL;
3901 // TBD check the flags and do the right thing
3902 if (VPPCOM_DEBUG > 2)
3903 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3904 getpid (), flags, flags);
3907 return (vppcom_session_write (session_index, buffer, buflen));
3911 * fd.io coding-style-patch-verification: ON
3914 * eval: (c-set-style "gnu")