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>
26 #include <vppinfra/elog.h>
28 #define vl_typedefs /* define message structures */
29 #include <vpp/api/vpe_all_api_h.h>
32 /* declare message handlers for each api */
34 #define vl_endianfun /* define message structures */
35 #include <vpp/api/vpe_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
41 #include <vpp/api/vpe_all_api_h.h>
45 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
46 * 3 for read/write debug output
48 * export VCL_DEBUG=<#> to set dynamically.
50 #define VPPCOM_DEBUG_INIT 1
52 #define VPPCOM_DEBUG_INIT 0
55 #define VPPCOM_DEBUG vcm->debug
58 * VPPCOM Private definitions and functions.
74 STATE_CLOSE_ON_EMPTY = 0x10,
75 STATE_DISCONNECT = 0x20,
79 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
80 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
82 typedef struct epoll_event vppcom_epoll_event_t;
89 vppcom_epoll_event_t ev;
90 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
91 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
103 VCL_SESS_ATTR_SERVER,
104 VCL_SESS_ATTR_CUT_THRU,
106 VCL_SESS_ATTR_VEP_SESSION,
107 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
108 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
109 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
110 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
111 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
112 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
113 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
114 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
115 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
116 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
118 } vppcom_session_attr_t;
120 #define VCL_SESS_ATTR_SET(ATTR, VAL) \
122 (ATTR) |= 1 << (VAL); \
125 #define VCL_SESS_ATTR_CLR(ATTR, VAL) \
127 (ATTR) &= ~(1 << (VAL)); \
130 #define VCL_SESS_ATTR_TEST(ATTR, VAL) \
131 ((ATTR) & (1 << (VAL)) ? 1 : 0)
135 volatile session_state_t state;
137 svm_fifo_t *server_rx_fifo;
138 svm_fifo_t *server_tx_fifo;
139 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
140 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
141 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
146 svm_queue_t *vpp_event_queue;
148 /* Socket configuration state */
149 /* TBD: covert 'is_*' vars to bit in session->attr; */
160 vppcom_ip46_t lcl_addr;
161 vppcom_ip46_t peer_addr;
162 u16 lcl_port; // network order
163 u16 peer_port; // network order
165 u64 client_queue_address;
167 elog_track_t elog_track;
170 typedef struct vppcom_cfg_t_
173 u32 vpp_api_q_length;
176 u32 add_segment_size;
177 u32 preallocated_fifo_pairs;
180 u32 event_queue_size;
181 u32 listen_queue_size;
182 u8 app_proxy_transport_tcp;
183 u8 app_proxy_transport_udp;
187 u64 namespace_secret;
192 char *event_log_path;
193 u8 *vpp_api_filename;
196 typedef struct vppcom_main_t_
200 u32 *client_session_index_fifo;
203 /* vpp input queue */
204 svm_queue_t *vl_input_queue;
206 /* API client handle */
210 clib_spinlock_t sessions_lockp;
213 /* Hash table for disconnect processing */
214 uword *session_index_by_vpp_handles;
217 clib_bitmap_t *rd_bitmap;
218 clib_bitmap_t *wr_bitmap;
219 clib_bitmap_t *ex_bitmap;
221 /* Our event queue */
222 svm_queue_t *app_event_queue;
224 /* unique segment name counter */
225 u32 unique_segment_index;
227 /* For deadman timers */
228 clib_time_t clib_time;
230 /* State of the connection, shared between msg RX thread and main thread */
231 volatile app_state_t app_state;
236 elog_main_t elog_main;
237 elog_track_t elog_track;
239 /* VNET_API_ERROR_FOO -> "Foo" hash table */
240 uword *error_string_by_error_number;
243 /* NOTE: _vppcom_main is only used until the heap is allocated.
244 * Do not access it directly -- use vcm which will point to
245 * the heap allocated copy after init.
247 static vppcom_main_t _vppcom_main = {
248 .debug = VPPCOM_DEBUG_INIT,
249 .my_client_index = ~0
252 static vppcom_main_t *vcm = &_vppcom_main;
254 #define VCL_LOCK_AND_GET_SESSION(I, S) \
256 clib_spinlock_lock (&vcm->sessions_lockp); \
257 rv = vppcom_session_at_index (I, S); \
258 if (PREDICT_FALSE (rv)) \
260 clib_spinlock_unlock (&vcm->sessions_lockp); \
261 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
268 vppcom_app_state_str (app_state_t state)
274 case STATE_APP_START:
275 st = "STATE_APP_START";
278 case STATE_APP_CONN_VPP:
279 st = "STATE_APP_CONN_VPP";
282 case STATE_APP_ENABLED:
283 st = "STATE_APP_ENABLED";
286 case STATE_APP_ATTACHED:
287 st = "STATE_APP_ATTACHED";
291 st = "UNKNOWN_APP_STATE";
299 vppcom_session_state_str (session_state_t state)
310 st = "STATE_CONNECT";
321 case STATE_CLOSE_ON_EMPTY:
322 st = "STATE_CLOSE_ON_EMPTY";
325 case STATE_DISCONNECT:
326 st = "STATE_DISCONNECT";
334 st = "UNKNOWN_STATE";
342 * VPPCOM Utility Functions
345 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
347 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
348 if (PREDICT_FALSE ((session_index == ~0) ||
349 pool_is_free_index (vcm->sessions, session_index)))
351 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
352 getpid (), session_index);
353 return VPPCOM_EBADFD;
355 *sess = pool_elt_at_index (vcm->sessions, session_index);
360 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
362 /* Session and listener handles have different formats. The latter has
363 * the thread index in the upper 32 bits while the former has the session
364 * type. Knowing that, for listeners we just flip the MSB to 1 */
365 listener_handle |= 1ULL << 63;
366 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
369 static inline session_t *
370 vppcom_session_table_lookup_listener (u64 listener_handle)
373 u64 handle = listener_handle | (1ULL << 63);
376 p = hash_get (vcm->session_index_by_vpp_handles, handle);
379 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
380 "listener handle %llx", getpid (), listener_handle);
383 if (pool_is_free_index (vcm->sessions, p[0]))
385 if (VPPCOM_DEBUG > 1)
386 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
391 session = pool_elt_at_index (vcm->sessions, p[0]);
392 ASSERT (session->is_listen);
397 vppcom_session_table_del_listener (u64 listener_handle)
399 listener_handle |= 1ULL << 63;
400 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
406 elog_main_t *em = &vcm->elog_main;
408 clib_error_t *error = 0;
411 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
412 vcm->my_client_index, getpid (), 0);
413 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
416 clib_error_report (error);
418 if (VPPCOM_DEBUG > 0)
419 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
424 vppcom_connect_to_vpp (char *app_name)
426 api_main_t *am = &api_main;
427 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
430 if (!vcl_cfg->vpp_api_filename)
431 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
433 if (VPPCOM_DEBUG > 0)
434 clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
435 getpid (), app_name, vcl_cfg->vpp_api_filename);
437 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
438 vcm->cfg.vpp_api_q_length) < 0)
440 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
441 rv = VPPCOM_ECONNREFUSED;
445 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
446 vcm->my_client_index = am->my_client_index;
447 vcm->app_state = STATE_APP_CONN_VPP;
449 if (VPPCOM_DEBUG > 0)
450 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
451 getpid (), app_name);
454 if (VPPCOM_DEBUG > 0)
456 vcm->elog_main.lock =
457 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
458 vcm->elog_main.lock[0] = 0;
459 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
460 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
461 elog_enable_disable (&vcm->elog_main, 1);
463 vcm->elog_track.name =
464 (char *) format (0, "P:%d:C:%d%c", getpid (),
465 vcm->my_client_index, 0);
466 elog_track_register (&vcm->elog_main, &vcm->elog_track);
469 ELOG_TYPE_DECLARE (e) =
471 .format = "connect_vpp:rv:%d",
478 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
486 format_api_error (u8 * s, va_list * args)
488 i32 error = va_arg (*args, u32);
491 p = hash_get (vcm->error_string_by_error_number, -error);
494 s = format (s, "%s (%d)", p[0], error);
496 s = format (s, "%d", error);
501 vppcom_init_error_string_table (void)
503 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
505 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
506 foreach_vnet_api_error;
509 hash_set (vcm->error_string_by_error_number, 99, "Misc");
513 vppcom_wait_for_app_state_change (app_state_t app_state)
515 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
517 while (clib_time_now (&vcm->clib_time) < timeout)
519 if (vcm->app_state == app_state)
522 if (VPPCOM_DEBUG > 0)
523 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
524 vppcom_app_state_str (app_state), app_state);
526 if (VPPCOM_DEBUG > 0)
529 ELOG_TYPE_DECLARE (e) =
531 .format = "ERR: timeout state:%d",
539 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
541 ed->data = app_state;
545 return VPPCOM_ETIMEDOUT;
549 vppcom_wait_for_session_state_change (u32 session_index,
550 session_state_t state,
553 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
554 session_t *volatile session;
559 clib_spinlock_lock (&vcm->sessions_lockp);
560 rv = vppcom_session_at_index (session_index, &session);
561 if (PREDICT_FALSE (rv))
563 clib_spinlock_unlock (&vcm->sessions_lockp);
566 if (session->state & state)
568 clib_spinlock_unlock (&vcm->sessions_lockp);
571 if (session->state & STATE_FAILED)
573 clib_spinlock_unlock (&vcm->sessions_lockp);
574 return VPPCOM_ECONNREFUSED;
577 clib_spinlock_unlock (&vcm->sessions_lockp);
579 while (clib_time_now (&vcm->clib_time) < timeout);
581 if (VPPCOM_DEBUG > 0)
582 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
583 state, vppcom_session_state_str (state));
585 if (VPPCOM_DEBUG > 0)
588 ELOG_TYPE_DECLARE (e) =
590 .format = "ERR: timeout state:%d",
598 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
604 return VPPCOM_ETIMEDOUT;
608 vppcom_wait_for_client_session_index (f64 wait_for_time)
610 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
614 if (clib_fifo_elts (vcm->client_session_index_fifo))
617 while (clib_time_now (&vcm->clib_time) < timeout);
619 if (wait_for_time == 0)
620 return VPPCOM_EAGAIN;
622 if (VPPCOM_DEBUG > 0)
623 clib_warning ("VCL<%d>: timeout waiting for client_session_index",
626 if (VPPCOM_DEBUG > 0)
629 ELOG_TYPE_DECLARE (e) =
631 .format = "ERR: timeout waiting for session index :%d",
639 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
645 return VPPCOM_ETIMEDOUT;
649 * VPP-API message functions
652 vppcom_send_session_enable_disable (u8 is_enable)
654 vl_api_session_enable_disable_t *bmp;
655 bmp = vl_msg_api_alloc (sizeof (*bmp));
656 memset (bmp, 0, sizeof (*bmp));
658 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
659 bmp->client_index = vcm->my_client_index;
660 bmp->context = htonl (0xfeedface);
661 bmp->is_enable = is_enable;
662 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
666 vppcom_app_session_enable (void)
670 if (vcm->app_state != STATE_APP_ENABLED)
672 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
673 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
674 if (PREDICT_FALSE (rv))
676 if (VPPCOM_DEBUG > 0)
677 clib_warning ("VCL<%d>: application session enable timed out! "
679 getpid (), rv, vppcom_retval_str (rv));
687 vl_api_session_enable_disable_reply_t_handler
688 (vl_api_session_enable_disable_reply_t * mp)
692 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
693 format_api_error, ntohl (mp->retval));
696 vcm->app_state = STATE_APP_ENABLED;
700 vppcom_app_send_attach (void)
702 vl_api_application_attach_t *bmp;
703 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
704 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
705 vcm->cfg.app_proxy_transport_udp);
707 bmp = vl_msg_api_alloc (sizeof (*bmp));
708 memset (bmp, 0, sizeof (*bmp));
710 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
711 bmp->client_index = vcm->my_client_index;
712 bmp->context = htonl (0xfeedface);
713 bmp->options[APP_OPTIONS_FLAGS] =
714 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
715 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
716 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
717 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
718 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
719 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
720 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
721 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
722 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
723 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
724 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
725 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
726 vcm->cfg.preallocated_fifo_pairs;
727 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
730 bmp->namespace_id_len = nsid_len;
731 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
732 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
734 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
738 vppcom_app_attach (void)
742 vppcom_app_send_attach ();
743 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
744 if (PREDICT_FALSE (rv))
746 if (VPPCOM_DEBUG > 0)
747 clib_warning ("VCL<%d>: application attach timed out! "
749 getpid (), rv, vppcom_retval_str (rv));
756 vppcom_app_detach (void)
758 vl_api_application_detach_t *bmp;
759 bmp = vl_msg_api_alloc (sizeof (*bmp));
760 memset (bmp, 0, sizeof (*bmp));
762 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
763 bmp->client_index = vcm->my_client_index;
764 bmp->context = htonl (0xfeedface);
765 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
769 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
772 static svm_fifo_segment_create_args_t _a;
773 svm_fifo_segment_create_args_t *a = &_a;
776 memset (a, 0, sizeof (*a));
779 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
780 format_api_error, ntohl (mp->retval));
784 if (mp->segment_name_length == 0)
786 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
790 a->segment_name = (char *) mp->segment_name;
791 a->segment_size = mp->segment_size;
793 ASSERT (mp->app_event_queue_address);
795 /* Attach to the segment vpp created */
796 rv = svm_fifo_segment_attach (a);
797 vec_reset_length (a->new_segment_indices);
798 if (PREDICT_FALSE (rv))
800 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
801 getpid (), mp->segment_name);
805 vcm->app_event_queue =
806 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
808 vcm->app_state = STATE_APP_ATTACHED;
812 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
816 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
819 vcm->app_state = STATE_APP_ENABLED;
823 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
827 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
828 getpid (), mp->handle, format_api_error,
833 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
835 static svm_fifo_segment_create_args_t _a;
836 svm_fifo_segment_create_args_t *a = &_a;
839 memset (a, 0, sizeof (*a));
840 a->segment_name = (char *) mp->segment_name;
841 a->segment_size = mp->segment_size;
842 /* Attach to the segment vpp created */
843 rv = svm_fifo_segment_attach (a);
844 vec_reset_length (a->new_segment_indices);
845 if (PREDICT_FALSE (rv))
847 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
848 getpid (), mp->segment_name);
851 if (VPPCOM_DEBUG > 1)
852 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
853 mp->segment_name, mp->segment_size);
857 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
861 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
865 session_t *session = 0;
866 u32 session_index = p[0];
868 VCL_LOCK_AND_GET_SESSION (session_index, &session);
869 session->state = STATE_CLOSE_ON_EMPTY;
871 if (VPPCOM_DEBUG > 1)
872 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
873 "setting state to 0x%x (%s)",
874 getpid (), mp->handle, session_index, session->state,
875 vppcom_session_state_str (session->state));
876 clib_spinlock_unlock (&vcm->sessions_lockp);
880 if (VPPCOM_DEBUG > 1)
881 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
882 "session lookup failed!",
883 getpid (), mp->handle, session_index);
886 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
887 "handle failed!", getpid (), mp->handle);
891 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
893 session_t *session = 0;
894 vl_api_reset_session_reply_t *rmp;
898 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
902 clib_spinlock_lock (&vcm->sessions_lockp);
903 rval = vppcom_session_at_index (p[0], &session);
904 if (PREDICT_FALSE (rval))
906 rv = VNET_API_ERROR_INVALID_VALUE_2;
907 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
908 "session lookup failed! returning %d %U",
909 getpid (), mp->handle, p[0],
910 rv, format_api_error, rv);
914 /* TBD: should this disconnect immediately and
917 session->state = STATE_CLOSE_ON_EMPTY;
919 if (VPPCOM_DEBUG > 1)
920 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
921 "state set to %d (%s)!", getpid (),
922 mp->handle, p[0], session->state,
923 vppcom_session_state_str (session->state));
925 clib_spinlock_unlock (&vcm->sessions_lockp);
929 rv = VNET_API_ERROR_INVALID_VALUE;
930 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
931 "failed! returning %d %U",
932 getpid (), mp->handle, rv, format_api_error, rv);
935 rmp = vl_msg_api_alloc (sizeof (*rmp));
936 memset (rmp, 0, sizeof (*rmp));
937 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
938 rmp->retval = htonl (rv);
939 rmp->handle = mp->handle;
940 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
944 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
946 session_t *session = 0;
948 svm_fifo_t *rx_fifo, *tx_fifo;
952 session_index = mp->context;
953 VCL_LOCK_AND_GET_SESSION (session_index, &session);
957 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
958 "connect failed! %U",
959 getpid (), mp->handle, session_index,
960 format_api_error, ntohl (mp->retval));
963 session->state = STATE_FAILED;
964 session->vpp_handle = mp->handle;
968 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
969 "Invalid session index (%u)!",
970 getpid (), mp->handle, session_index);
978 /* We've been redirected */
979 if (mp->segment_name_length > 0)
981 static svm_fifo_segment_create_args_t _a;
982 svm_fifo_segment_create_args_t *a = &_a;
985 memset (a, 0, sizeof (*a));
986 a->segment_name = (char *) mp->segment_name;
987 if (VPPCOM_DEBUG > 1)
988 clib_warning ("VCL<%d>: cut-thru segment: %s\n",
989 getpid (), a->segment_name);
991 rv = svm_fifo_segment_attach (a);
992 vec_reset_length (a->new_segment_indices);
993 if (PREDICT_FALSE (rv))
995 clib_warning ("VCL<%d>: sm_fifo_segment_attach ('%s') failed",
996 getpid (), a->segment_name);
1004 session->is_cut_thru = is_cut_thru;
1005 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1008 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1009 rx_fifo->client_session_index = session_index;
1010 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1011 tx_fifo->client_session_index = session_index;
1013 session->server_rx_fifo = rx_fifo;
1014 session->server_tx_fifo = tx_fifo;
1015 session->vpp_handle = mp->handle;
1016 session->lcl_addr.is_ip4 = mp->is_ip4;
1017 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1018 sizeof (session->peer_addr.ip46));
1019 session->lcl_port = mp->lcl_port;
1020 session->state = STATE_CONNECT;
1022 /* Add it to lookup table */
1023 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1025 if (VPPCOM_DEBUG > 1)
1026 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1027 " session_rx_fifo %p, refcnt %d,"
1028 " session_tx_fifo %p, refcnt %d",
1029 getpid (), mp->handle, session_index,
1030 session->server_rx_fifo,
1031 session->server_rx_fifo->refcnt,
1032 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1034 clib_spinlock_unlock (&vcm->sessions_lockp);
1038 vppcom_send_connect_sock (session_t * session, u32 session_index)
1040 vl_api_connect_sock_t *cmp;
1042 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1043 session->is_server = 0;
1044 cmp = vl_msg_api_alloc (sizeof (*cmp));
1045 memset (cmp, 0, sizeof (*cmp));
1046 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1047 cmp->client_index = vcm->my_client_index;
1048 cmp->context = session_index;
1050 cmp->is_ip4 = session->peer_addr.is_ip4;
1051 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1052 cmp->port = session->peer_port;
1053 cmp->proto = session->proto;
1054 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1055 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1059 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1062 vl_api_disconnect_session_reply_t *rmp;
1064 if (VPPCOM_DEBUG > 1)
1065 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1066 "sending disconnect msg",
1067 getpid (), vpp_handle, session_index);
1069 rmp = vl_msg_api_alloc (sizeof (*rmp));
1070 memset (rmp, 0, sizeof (*rmp));
1072 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1073 rmp->retval = htonl (rv);
1074 rmp->handle = vpp_handle;
1075 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1079 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1081 vl_api_disconnect_session_t *dmp;
1083 if (VPPCOM_DEBUG > 1)
1084 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1085 "sending disconnect msg",
1086 getpid (), vpp_handle, session_index);
1088 dmp = vl_msg_api_alloc (sizeof (*dmp));
1089 memset (dmp, 0, sizeof (*dmp));
1090 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1091 dmp->client_index = vcm->my_client_index;
1092 dmp->handle = vpp_handle;
1093 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1097 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1099 session_t *session = 0;
1100 u32 session_index = mp->context;
1103 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1107 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1108 "sid %u: bind failed: %U",
1109 getpid (), mp->handle, session_index,
1110 format_api_error, ntohl (mp->retval));
1111 rv = vppcom_session_at_index (session_index, &session);
1112 if (rv == VPPCOM_OK)
1114 session->state = STATE_FAILED;
1115 session->vpp_handle = mp->handle;
1119 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1120 "Invalid session index (%u)!",
1121 getpid (), mp->handle, session_index);
1126 session->vpp_handle = mp->handle;
1127 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1128 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1129 sizeof (session->peer_addr.ip46));
1130 session->lcl_port = mp->lcl_port;
1131 vppcom_session_table_add_listener (mp->handle, session_index);
1132 session->is_listen = 1;
1133 session->state = STATE_LISTEN;
1135 if (VPPCOM_DEBUG > 1)
1136 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1137 getpid (), mp->handle, mp->context);
1139 clib_spinlock_unlock (&vcm->sessions_lockp);
1143 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1146 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1147 getpid (), mp->context, format_api_error,
1148 ntohl (mp->retval));
1150 else if (VPPCOM_DEBUG > 1)
1151 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1152 getpid (), mp->context);
1156 format_ip4_address (u8 * s, va_list * args)
1158 u8 *a = va_arg (*args, u8 *);
1159 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1163 format_ip6_address (u8 * s, va_list * args)
1165 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1166 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1168 i_max_n_zero = ARRAY_LEN (a->as_u16);
1170 i_first_zero = i_max_n_zero;
1172 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1174 u32 is_zero = a->as_u16[i] == 0;
1175 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1181 if ((!is_zero && n_zeros > max_n_zeros)
1182 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1184 i_max_n_zero = i_first_zero;
1185 max_n_zeros = n_zeros;
1186 i_first_zero = ARRAY_LEN (a->as_u16);
1191 last_double_colon = 0;
1192 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1194 if (i == i_max_n_zero && max_n_zeros > 1)
1196 s = format (s, "::");
1197 i += max_n_zeros - 1;
1198 last_double_colon = 1;
1202 s = format (s, "%s%x",
1203 (last_double_colon || i == 0) ? "" : ":",
1204 clib_net_to_host_u16 (a->as_u16[i]));
1205 last_double_colon = 0;
1212 /* Format an IP46 address. */
1214 format_ip46_address (u8 * s, va_list * args)
1216 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1217 ip46_type_t type = va_arg (*args, ip46_type_t);
1223 is_ip4 = ip46_address_is_ip4 (ip46);
1234 format (s, "%U", format_ip4_address, &ip46->ip4) :
1235 format (s, "%U", format_ip6_address, &ip46->ip6);
1239 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1241 vl_api_accept_session_reply_t *rmp;
1243 rmp = vl_msg_api_alloc (sizeof (*rmp));
1244 memset (rmp, 0, sizeof (*rmp));
1245 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1246 rmp->retval = htonl (retval);
1247 rmp->context = context;
1248 rmp->handle = handle;
1249 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1253 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1255 svm_fifo_t *rx_fifo, *tx_fifo;
1256 session_t *session, *listen_session;
1259 clib_spinlock_lock (&vcm->sessions_lockp);
1260 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1262 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1263 vppcom_send_accept_session_reply (mp->handle, mp->context,
1264 VNET_API_ERROR_QUEUE_FULL);
1265 clib_spinlock_unlock (&vcm->sessions_lockp);
1269 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1270 if (!listen_session)
1272 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1273 "unknown vpp listener handle %llx",
1274 getpid (), mp->listener_handle);
1275 clib_spinlock_unlock (&vcm->sessions_lockp);
1279 /* Allocate local session and set it up */
1280 pool_get (vcm->sessions, session);
1281 memset (session, 0, sizeof (*session));
1282 session_index = session - vcm->sessions;
1284 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1285 rx_fifo->client_session_index = session_index;
1286 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1287 tx_fifo->client_session_index = session_index;
1289 session->vpp_handle = mp->handle;
1290 session->client_context = mp->context;
1291 session->server_rx_fifo = rx_fifo;
1292 session->server_tx_fifo = tx_fifo;
1293 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1295 session->state = STATE_ACCEPT;
1296 session->is_cut_thru = 0;
1297 session->is_server = 1;
1298 session->peer_port = mp->port;
1299 session->peer_addr.is_ip4 = mp->is_ip4;
1300 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1301 sizeof (session->peer_addr.ip46));
1303 /* Add it to lookup table */
1304 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1305 session->lcl_port = listen_session->lcl_port;
1306 session->lcl_addr = listen_session->lcl_addr;
1308 /* TBD: move client_session_index_fifo into listener session */
1309 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1311 clib_spinlock_unlock (&vcm->sessions_lockp);
1313 if (VPPCOM_DEBUG > 1)
1314 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1315 "request from %s address %U port %d queue %p!", getpid (),
1316 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1317 format_ip46_address, &mp->ip, mp->is_ip4,
1318 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1320 if (VPPCOM_DEBUG > 0)
1322 session->elog_track.name =
1323 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1325 elog_track_register (&vcm->elog_main, &session->elog_track);
1327 if (session->peer_addr.is_ip4)
1330 ELOG_TYPE_DECLARE (e) =
1333 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1334 .format_args = "i8i1i1i1i1i2",
1337 CLIB_PACKED (struct {
1343 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1345 ed->handle = mp->handle;
1346 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1347 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1348 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1349 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1350 ed->port = clib_net_to_host_u16 (session->peer_port);
1355 clib_warning ("ip6");
1362 vppcom_send_connect_session_reply (session_t * session, u32 session_index,
1363 u64 vpp_handle, u32 context, int retval)
1365 vl_api_connect_session_reply_t *rmp;
1367 svm_queue_t *client_q;
1369 rmp = vl_msg_api_alloc (sizeof (*rmp));
1370 memset (rmp, 0, sizeof (*rmp));
1371 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1375 rmp->context = context;
1376 rmp->handle = vpp_handle;
1377 rmp->retval = htonl (retval);
1378 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1382 rmp->context = session->client_context;
1383 rmp->retval = htonl (retval);
1384 rmp->handle = session->vpp_handle;
1385 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1386 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1387 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1388 rmp->segment_size = vcm->cfg.segment_size;
1389 len = vec_len (session->segment_name);
1390 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1391 clib_memcpy (rmp->segment_name, session->segment_name,
1392 rmp->segment_name_length - 1);
1393 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1394 sizeof (rmp->lcl_ip));
1395 rmp->is_ip4 = session->peer_addr.is_ip4;
1396 rmp->lcl_port = session->peer_port;
1397 client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
1399 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1403 * Acting as server for redirected connect requests
1406 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1409 session_t *session = 0;
1411 clib_spinlock_lock (&vcm->sessions_lockp);
1412 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1414 clib_spinlock_unlock (&vcm->sessions_lockp);
1416 if (VPPCOM_DEBUG > 1)
1417 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1419 /* TBD: Fix api to include vpp handle */
1420 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1421 0 /* handle */ , mp->context,
1422 VNET_API_ERROR_QUEUE_FULL);
1426 pool_get (vcm->sessions, session);
1427 memset (session, 0, sizeof (*session));
1428 session_index = session - vcm->sessions;
1430 session->client_context = mp->context;
1431 session->vpp_handle = session_index;
1432 session->client_queue_address = mp->client_queue_address;
1433 session->is_cut_thru = 1;
1434 session->is_server = 1;
1435 session->lcl_port = mp->port;
1436 session->lcl_addr.is_ip4 = mp->is_ip4;
1437 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1438 sizeof (session->lcl_addr.ip46));
1440 /* TBD: missing peer info in api msg.
1442 session->peer_addr.is_ip4 = mp->is_ip4;
1443 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1445 session->state = STATE_ACCEPT;
1446 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1447 if (VPPCOM_DEBUG > 1)
1448 clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
1449 "clib_fifo_elts %u!\n", getpid (), session_index,
1450 clib_fifo_elts (vcm->client_session_index_fifo));
1452 if (VPPCOM_DEBUG > 0)
1454 session->elog_track.name =
1455 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1457 elog_track_register (&vcm->elog_main, &session->elog_track);
1460 ELOG_TYPE_DECLARE (e) =
1462 .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1463 .format_args = "i4i4",
1471 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1473 ed->data[0] = session_index;
1474 ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
1478 clib_spinlock_unlock (&vcm->sessions_lockp);
1482 vppcom_send_bind_sock (session_t * session, u32 session_index)
1484 vl_api_bind_sock_t *bmp;
1486 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1487 session->is_server = 1;
1488 bmp = vl_msg_api_alloc (sizeof (*bmp));
1489 memset (bmp, 0, sizeof (*bmp));
1491 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1492 bmp->client_index = vcm->my_client_index;
1493 bmp->context = session_index;
1494 bmp->is_ip4 = session->lcl_addr.is_ip4;
1495 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1496 bmp->port = session->lcl_port;
1497 bmp->proto = session->proto;
1498 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1499 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1503 vppcom_send_unbind_sock (u64 vpp_handle)
1505 vl_api_unbind_sock_t *ump;
1507 ump = vl_msg_api_alloc (sizeof (*ump));
1508 memset (ump, 0, sizeof (*ump));
1510 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1511 ump->client_index = vcm->my_client_index;
1512 ump->handle = vpp_handle;
1513 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1517 vppcom_session_unbind (u32 session_index)
1519 session_t *session = 0;
1523 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1525 vpp_handle = session->vpp_handle;
1526 vppcom_session_table_del_listener (vpp_handle);
1527 session->vpp_handle = ~0;
1528 session->state = STATE_DISCONNECT;
1530 clib_spinlock_unlock (&vcm->sessions_lockp);
1532 if (VPPCOM_DEBUG > 1)
1533 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1534 "sending unbind msg! new state 0x%x (%s)",
1535 getpid (), vpp_handle, session_index,
1536 session->state, vppcom_session_state_str (session->state));
1538 if (VPPCOM_DEBUG > 0)
1541 ELOG_TYPE_DECLARE (e) =
1543 .format = "unbind: handle:%x",
1544 .format_args = "i8",
1552 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1553 ed->handle = vpp_handle;
1556 vppcom_send_unbind_sock (vpp_handle);
1563 vppcom_session_disconnect (u32 session_index)
1567 u8 is_cut_thru, is_listen, is_server;
1569 session_state_t state;
1571 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1573 vpp_handle = session->vpp_handle;
1574 is_server = session->is_server;
1575 is_listen = session->is_listen;
1576 is_cut_thru = session->is_cut_thru;
1577 state = session->state;
1578 clib_spinlock_unlock (&vcm->sessions_lockp);
1580 if (VPPCOM_DEBUG > 1)
1582 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s "
1583 "state 0x%x (%s), is_cut_thru %d, is_listen %d",
1584 getpid (), vpp_handle, session_index,
1585 is_server ? "server" : "client",
1586 state, vppcom_session_state_str (state),
1587 is_cut_thru, is_listen);
1590 if (PREDICT_FALSE (is_listen))
1592 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1593 "Cannot disconnect a listen socket!",
1594 getpid (), vpp_handle, session_index);
1599 /* Through the VPP host stack...
1601 else if (!is_cut_thru)
1603 /* The peer has already initiated the close,
1604 * so send the disconnect session reply.
1606 if (state & STATE_CLOSE_ON_EMPTY)
1608 vppcom_send_disconnect_session_reply (vpp_handle,
1609 session_index, 0 /* rv */ );
1610 if (VPPCOM_DEBUG > 1)
1611 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1612 "sending disconnect REPLY...",
1613 getpid (), vpp_handle, session_index);
1616 /* Otherwise, send a disconnect session msg...
1620 if (VPPCOM_DEBUG > 1)
1621 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1622 "sending disconnect...",
1623 getpid (), vpp_handle, session_index);
1625 vppcom_send_disconnect_session (vpp_handle, session_index);
1629 /* Cut-thru connections...
1631 * server: free fifos and segment allocated during connect/redirect
1632 * client: no cleanup required
1638 svm_fifo_segment_private_t *seg;
1640 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1642 if (VPPCOM_DEBUG > 1)
1643 clib_warning ("VCL<%d>: sid %d: freeing cut-thru fifos in "
1645 " server_rx_fifo %p, refcnt = %d"
1646 " server_tx_fifo %p, refcnt = %d",
1647 getpid (), session_index, session->sm_seg_index,
1648 session->server_rx_fifo,
1649 session->server_rx_fifo->refcnt,
1650 session->server_tx_fifo,
1651 session->server_tx_fifo->refcnt);
1653 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1654 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1655 FIFO_SEGMENT_RX_FREELIST);
1656 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1657 FIFO_SEGMENT_TX_FREELIST);
1658 svm_fifo_segment_delete (seg);
1660 /* TBD: Send cut-thru disconnect event to client */
1662 clib_spinlock_unlock (&vcm->sessions_lockp);
1666 /* TBD: Send cut-thru disconnect event to server */
1674 #define foreach_sock_msg \
1675 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1676 _(BIND_SOCK_REPLY, bind_sock_reply) \
1677 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1678 _(ACCEPT_SESSION, accept_session) \
1679 _(CONNECT_SOCK, connect_sock) \
1680 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1681 _(DISCONNECT_SESSION, disconnect_session) \
1682 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1683 _(RESET_SESSION, reset_session) \
1684 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1685 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1686 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1689 vppcom_api_hookup (void)
1692 vl_msg_api_set_handlers(VL_API_##N, #n, \
1693 vl_api_##n##_t_handler, \
1695 vl_api_##n##_t_endian, \
1696 vl_api_##n##_t_print, \
1697 sizeof(vl_api_##n##_t), 1);
1703 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1707 vcl_cfg->heapsize = (256ULL << 20);
1708 vcl_cfg->vpp_api_q_length = 1024;
1709 vcl_cfg->segment_baseva = 0x200000000ULL;
1710 vcl_cfg->segment_size = (256 << 20);
1711 vcl_cfg->add_segment_size = (128 << 20);
1712 vcl_cfg->preallocated_fifo_pairs = 8;
1713 vcl_cfg->rx_fifo_size = (1 << 20);
1714 vcl_cfg->tx_fifo_size = (1 << 20);
1715 vcl_cfg->event_queue_size = 2048;
1716 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1717 vcl_cfg->app_timeout = 10 * 60.0;
1718 vcl_cfg->session_timeout = 10 * 60.0;
1719 vcl_cfg->accept_timeout = 60.0;
1720 vcl_cfg->event_ring_size = (128 << 10);
1721 vcl_cfg->event_log_path = "/dev/shm";
1725 vppcom_cfg_heapsize (char *conf_fname)
1727 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1740 fp = fopen (conf_fname, "r");
1743 if (VPPCOM_DEBUG > 0)
1744 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1745 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1749 argv = calloc (1, sizeof (char *));
1752 if (VPPCOM_DEBUG > 0)
1753 clib_warning ("VCL<%d>: calloc failed, using default "
1754 "heapsize %lld (0x%llx)",
1755 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1761 if (fgets (inbuf, 4096, fp) == 0)
1763 p = strtok (inbuf, " \t\n");
1769 char **tmp = realloc (argv, argc * sizeof (char *));
1772 if (VPPCOM_DEBUG > 0)
1773 clib_warning ("VCL<%d>: realloc failed, "
1774 "using default heapsize %lld (0x%llx)",
1775 getpid (), vcl_cfg->heapsize,
1780 arg = strndup (p, 1024);
1783 if (VPPCOM_DEBUG > 0)
1784 clib_warning ("VCL<%d>: strndup failed, "
1785 "using default heapsize %lld (0x%llx)",
1786 getpid (), vcl_cfg->heapsize,
1790 argv[argc - 1] = arg;
1791 p = strtok (NULL, " \t\n");
1798 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1801 if (VPPCOM_DEBUG > 0)
1802 clib_warning ("VCL<%d>: realloc failed, "
1803 "using default heapsize %lld (0x%llx)",
1804 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1811 * Look for and parse the "heapsize" config parameter.
1812 * Manual since none of the clib infra has been bootstrapped yet.
1814 * Format: heapsize <nn>[mM][gG]
1817 for (i = 1; i < (argc - 1); i++)
1819 if (!strncmp (argv[i], "heapsize", 8))
1821 sizep = (u8 *) argv[i + 1];
1823 while (*sizep >= '0' && *sizep <= '9')
1826 size += *sizep++ - '0';
1830 if (VPPCOM_DEBUG > 0)
1831 clib_warning ("VCL<%d>: parse error '%s %s', "
1832 "using default heapsize %lld (0x%llx)",
1833 getpid (), argv[i], argv[i + 1],
1834 vcl_cfg->heapsize, vcl_cfg->heapsize);
1838 if (*sizep == 'g' || *sizep == 'G')
1839 vcl_cfg->heapsize = size << 30;
1840 else if (*sizep == 'm' || *sizep == 'M')
1841 vcl_cfg->heapsize = size << 20;
1844 if (VPPCOM_DEBUG > 0)
1845 clib_warning ("VCL<%d>: parse error '%s %s', "
1846 "using default heapsize %lld (0x%llx)",
1847 getpid (), argv[i], argv[i + 1],
1848 vcl_cfg->heapsize, vcl_cfg->heapsize);
1860 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1861 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1862 if (vcl_mem == MAP_FAILED)
1864 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1865 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1867 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1868 ASSERT (vcl_mem != MAP_FAILED);
1871 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1874 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1878 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1881 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1886 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1889 if (VPPCOM_DEBUG > 0)
1890 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1891 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1895 vppcom_cfg_read (char *conf_fname)
1897 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1899 unformat_input_t _input, *input = &_input;
1900 unformat_input_t _line_input, *line_input = &_line_input;
1901 u8 vc_cfg_input = 0;
1904 u32 uid, gid, q_len;
1906 fd = open (conf_fname, O_RDONLY);
1909 if (VPPCOM_DEBUG > 0)
1910 clib_warning ("VCL<%d>: using default configuration.",
1911 getpid (), conf_fname);
1915 if (fstat (fd, &s) < 0)
1917 if (VPPCOM_DEBUG > 0)
1918 clib_warning ("VCL<%d>: failed to stat `%s', "
1919 "using default configuration", getpid (), conf_fname);
1923 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1925 if (VPPCOM_DEBUG > 0)
1926 clib_warning ("VCL<%d>: not a regular file `%s', "
1927 "using default configuration", getpid (), conf_fname);
1931 unformat_init_clib_file (input, fd);
1933 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1935 (void) unformat_user (input, unformat_line_input, line_input);
1936 unformat_skip_white_space (line_input);
1938 if (unformat (line_input, "vcl {"))
1946 if (unformat (line_input, "heapsize %s", &chroot_path))
1948 vec_terminate_c_string (chroot_path);
1949 if (VPPCOM_DEBUG > 0)
1950 clib_warning ("VCL<%d>: configured heapsize %s, "
1951 "actual heapsize %lld (0x%llx)",
1952 getpid (), chroot_path, vcl_cfg->heapsize,
1954 vec_free (chroot_path);
1956 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1958 vec_terminate_c_string (chroot_path);
1959 if (vcl_cfg->vpp_api_filename)
1960 vec_free (vcl_cfg->vpp_api_filename);
1961 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1963 vl_set_memory_root_path ((char *) chroot_path);
1965 if (VPPCOM_DEBUG > 0)
1966 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1967 "api filename (%s)", getpid (), chroot_path,
1968 vcl_cfg->vpp_api_filename);
1969 chroot_path = 0; /* Don't vec_free() it! */
1971 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1973 if (q_len < vcl_cfg->vpp_api_q_length)
1975 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1976 "(%u) is too small! Using default: %u ",
1977 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1981 vcl_cfg->vpp_api_q_length = q_len;
1983 if (VPPCOM_DEBUG > 0)
1984 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1985 getpid (), vcl_cfg->vpp_api_q_length);
1988 else if (unformat (line_input, "uid %d", &uid))
1990 vl_set_memory_uid (uid);
1991 if (VPPCOM_DEBUG > 0)
1992 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1994 else if (unformat (line_input, "gid %d", &gid))
1996 vl_set_memory_gid (gid);
1997 if (VPPCOM_DEBUG > 0)
1998 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
2000 else if (unformat (line_input, "segment-baseva 0x%lx",
2001 &vcl_cfg->segment_baseva))
2003 if (VPPCOM_DEBUG > 0)
2004 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
2005 getpid (), vcl_cfg->segment_baseva);
2007 else if (unformat (line_input, "segment-size 0x%lx",
2008 &vcl_cfg->segment_size))
2010 if (VPPCOM_DEBUG > 0)
2011 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
2012 getpid (), vcl_cfg->segment_size,
2013 vcl_cfg->segment_size);
2015 else if (unformat (line_input, "segment-size %ld",
2016 &vcl_cfg->segment_size))
2018 if (VPPCOM_DEBUG > 0)
2019 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
2020 getpid (), vcl_cfg->segment_size,
2021 vcl_cfg->segment_size);
2023 else if (unformat (line_input, "add-segment-size 0x%lx",
2024 &vcl_cfg->add_segment_size))
2026 if (VPPCOM_DEBUG > 0)
2028 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
2029 getpid (), vcl_cfg->add_segment_size,
2030 vcl_cfg->add_segment_size);
2032 else if (unformat (line_input, "add-segment-size %ld",
2033 &vcl_cfg->add_segment_size))
2035 if (VPPCOM_DEBUG > 0)
2037 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
2038 getpid (), vcl_cfg->add_segment_size,
2039 vcl_cfg->add_segment_size);
2041 else if (unformat (line_input, "preallocated-fifo-pairs %d",
2042 &vcl_cfg->preallocated_fifo_pairs))
2044 if (VPPCOM_DEBUG > 0)
2045 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
2046 "%d (0x%x)", getpid (),
2047 vcl_cfg->preallocated_fifo_pairs,
2048 vcl_cfg->preallocated_fifo_pairs);
2050 else if (unformat (line_input, "rx-fifo-size 0x%lx",
2051 &vcl_cfg->rx_fifo_size))
2053 if (VPPCOM_DEBUG > 0)
2054 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
2055 getpid (), vcl_cfg->rx_fifo_size,
2056 vcl_cfg->rx_fifo_size);
2058 else if (unformat (line_input, "rx-fifo-size %ld",
2059 &vcl_cfg->rx_fifo_size))
2061 if (VPPCOM_DEBUG > 0)
2062 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
2063 getpid (), vcl_cfg->rx_fifo_size,
2064 vcl_cfg->rx_fifo_size);
2066 else if (unformat (line_input, "tx-fifo-size 0x%lx",
2067 &vcl_cfg->tx_fifo_size))
2069 if (VPPCOM_DEBUG > 0)
2070 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
2071 getpid (), vcl_cfg->tx_fifo_size,
2072 vcl_cfg->tx_fifo_size);
2074 else if (unformat (line_input, "tx-fifo-size %ld",
2075 &vcl_cfg->tx_fifo_size))
2077 if (VPPCOM_DEBUG > 0)
2078 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
2079 getpid (), vcl_cfg->tx_fifo_size,
2080 vcl_cfg->tx_fifo_size);
2082 else if (unformat (line_input, "event-queue-size 0x%lx",
2083 &vcl_cfg->event_queue_size))
2085 if (VPPCOM_DEBUG > 0)
2086 clib_warning ("VCL<%d>: configured event_queue_size "
2088 getpid (), vcl_cfg->event_queue_size,
2089 vcl_cfg->event_queue_size);
2091 else if (unformat (line_input, "event-queue-size %ld",
2092 &vcl_cfg->event_queue_size))
2094 if (VPPCOM_DEBUG > 0)
2095 clib_warning ("VCL<%d>: configured event_queue_size "
2097 getpid (), vcl_cfg->event_queue_size,
2098 vcl_cfg->event_queue_size);
2100 else if (unformat (line_input, "listen-queue-size 0x%lx",
2101 &vcl_cfg->listen_queue_size))
2103 if (VPPCOM_DEBUG > 0)
2104 clib_warning ("VCL<%d>: configured listen_queue_size "
2106 getpid (), vcl_cfg->listen_queue_size,
2107 vcl_cfg->listen_queue_size);
2109 else if (unformat (line_input, "listen-queue-size %ld",
2110 &vcl_cfg->listen_queue_size))
2112 if (VPPCOM_DEBUG > 0)
2113 clib_warning ("VCL<%d>: configured listen_queue_size "
2115 getpid (), vcl_cfg->listen_queue_size,
2116 vcl_cfg->listen_queue_size);
2118 else if (unformat (line_input, "app-timeout %f",
2119 &vcl_cfg->app_timeout))
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("VCL<%d>: configured app_timeout %f",
2123 getpid (), vcl_cfg->app_timeout);
2125 else if (unformat (line_input, "session-timeout %f",
2126 &vcl_cfg->session_timeout))
2128 if (VPPCOM_DEBUG > 0)
2129 clib_warning ("VCL<%d>: configured session_timeout %f",
2130 getpid (), vcl_cfg->session_timeout);
2132 else if (unformat (line_input, "accept-timeout %f",
2133 &vcl_cfg->accept_timeout))
2135 if (VPPCOM_DEBUG > 0)
2136 clib_warning ("VCL<%d>: configured accept_timeout %f",
2137 getpid (), vcl_cfg->accept_timeout);
2139 else if (unformat (line_input, "app-proxy-transport-tcp"))
2141 vcl_cfg->app_proxy_transport_tcp = 1;
2142 if (VPPCOM_DEBUG > 0)
2143 clib_warning ("VCL<%d>: configured "
2144 "app_proxy_transport_tcp (%d)",
2145 getpid (), vcl_cfg->app_proxy_transport_tcp);
2147 else if (unformat (line_input, "app-proxy-transport-udp"))
2149 vcl_cfg->app_proxy_transport_udp = 1;
2150 if (VPPCOM_DEBUG > 0)
2151 clib_warning ("VCL<%d>: configured "
2152 "app_proxy_transport_udp (%d)",
2153 getpid (), vcl_cfg->app_proxy_transport_udp);
2155 else if (unformat (line_input, "app-scope-local"))
2157 vcl_cfg->app_scope_local = 1;
2158 if (VPPCOM_DEBUG > 0)
2159 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2160 getpid (), vcl_cfg->app_scope_local);
2162 else if (unformat (line_input, "app-scope-global"))
2164 vcl_cfg->app_scope_global = 1;
2165 if (VPPCOM_DEBUG > 0)
2166 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2167 getpid (), vcl_cfg->app_scope_global);
2169 else if (unformat (line_input, "namespace-secret %lu",
2170 &vcl_cfg->namespace_secret))
2172 if (VPPCOM_DEBUG > 0)
2174 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2175 getpid (), vcl_cfg->namespace_secret,
2176 vcl_cfg->namespace_secret);
2178 else if (unformat (line_input, "namespace-id %v",
2179 &vcl_cfg->namespace_id))
2181 vl_api_application_attach_t *mp;
2182 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2183 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2184 if (nsid_vec_len > max_nsid_vec_len)
2186 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2187 if (VPPCOM_DEBUG > 0)
2188 clib_warning ("VCL<%d>: configured namespace_id is "
2189 "too long, truncated to %d characters!",
2190 getpid (), max_nsid_vec_len);
2193 if (VPPCOM_DEBUG > 0)
2194 clib_warning ("VCL<%d>: configured namespace_id %v",
2195 getpid (), vcl_cfg->namespace_id);
2197 else if (unformat (line_input, "}"))
2200 if (VPPCOM_DEBUG > 0)
2201 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2207 if (line_input->buffer[line_input->index] != '#')
2209 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2211 &line_input->buffer[line_input->index]);
2218 unformat_free (input);
2226 * VPPCOM Public API functions
2229 vppcom_app_create (char *app_name)
2231 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2242 vppcom_cfg_init (vcl_cfg);
2243 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2247 if (sscanf (env_var_str, "%u", &tmp) != 1)
2248 clib_warning ("VCL<%d>: Invalid debug level specified in "
2249 "the environment variable "
2251 " (%s)!\n", getpid (), env_var_str);
2255 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2256 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2259 conf_fname = getenv (VPPCOM_ENV_CONF);
2261 conf_fname = VPPCOM_CONF_DEFAULT;
2262 vppcom_cfg_heapsize (conf_fname);
2263 vcl_cfg = &vcm->cfg;
2264 clib_fifo_validate (vcm->client_session_index_fifo,
2265 vcm->cfg.listen_queue_size);
2266 vppcom_cfg_read (conf_fname);
2268 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2271 if (vcl_cfg->vpp_api_filename)
2272 vec_free (vcl_cfg->vpp_api_filename);
2273 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2275 vl_set_memory_root_path ((char *) env_var_str);
2277 if (VPPCOM_DEBUG > 0)
2278 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2279 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2280 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2283 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2287 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2288 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2289 "the environment variable "
2290 VPPCOM_ENV_APP_NAMESPACE_SECRET
2291 " (%s)!\n", getpid (), env_var_str);
2294 vcm->cfg.namespace_secret = tmp;
2295 if (VPPCOM_DEBUG > 0)
2296 clib_warning ("VCL<%d>: configured namespace secret "
2297 "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2298 getpid (), vcm->cfg.namespace_secret);
2301 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2304 u32 ns_id_vec_len = strlen (env_var_str);
2306 vec_reset_length (vcm->cfg.namespace_id);
2307 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2308 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2310 if (VPPCOM_DEBUG > 0)
2311 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2312 VPPCOM_ENV_APP_NAMESPACE_ID
2313 "!", getpid (), vcm->cfg.namespace_id);
2315 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2319 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2320 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2321 "the environment variable "
2322 VPPCOM_ENV_APP_NAMESPACE_SECRET
2323 " (%s)!\n", getpid (), env_var_str);
2326 vcm->cfg.namespace_secret = tmp;
2327 if (VPPCOM_DEBUG > 0)
2328 clib_warning ("VCL<%d>: configured namespace secret "
2330 VPPCOM_ENV_APP_NAMESPACE_ID
2331 "!", getpid (), vcm->cfg.namespace_secret);
2334 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2336 vcm->cfg.app_proxy_transport_tcp = 1;
2337 if (VPPCOM_DEBUG > 0)
2338 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2340 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2341 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2343 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2345 vcm->cfg.app_proxy_transport_udp = 1;
2346 if (VPPCOM_DEBUG > 0)
2347 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2349 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2350 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2352 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2354 vcm->cfg.app_scope_local = 1;
2355 if (VPPCOM_DEBUG > 0)
2356 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2357 VPPCOM_ENV_APP_SCOPE_LOCAL
2358 "!", getpid (), vcm->cfg.app_scope_local);
2360 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2362 vcm->cfg.app_scope_global = 1;
2363 if (VPPCOM_DEBUG > 0)
2364 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2365 VPPCOM_ENV_APP_SCOPE_GLOBAL
2366 "!", getpid (), vcm->cfg.app_scope_global);
2369 vcm->main_cpu = os_get_thread_index ();
2370 heap = clib_mem_get_per_cpu_heap ();
2371 h = mheap_header (heap);
2373 /* make the main heap thread-safe */
2374 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2376 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2378 clib_time_init (&vcm->clib_time);
2379 vppcom_init_error_string_table ();
2380 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2381 20 /* timeout in secs */ );
2382 clib_spinlock_init (&vcm->sessions_lockp);
2385 if (vcm->my_client_index == ~0)
2387 vppcom_api_hookup ();
2388 vcm->app_state = STATE_APP_START;
2389 rv = vppcom_connect_to_vpp (app_name);
2392 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2397 if (VPPCOM_DEBUG > 0)
2398 clib_warning ("VCL<%d>: sending session enable", getpid ());
2400 rv = vppcom_app_session_enable ();
2403 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2404 "failed!", getpid ());
2408 if (VPPCOM_DEBUG > 0)
2409 clib_warning ("VCL<%d>: sending app attach", getpid ());
2411 rv = vppcom_app_attach ();
2414 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2419 if (VPPCOM_DEBUG > 0)
2420 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2421 getpid (), app_name, vcm->my_client_index,
2422 vcm->my_client_index);
2429 vppcom_app_destroy (void)
2433 if (vcm->my_client_index == ~0)
2436 if (VPPCOM_DEBUG > 0)
2437 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2438 getpid (), vcm->my_client_index, vcm->my_client_index);
2440 if (VPPCOM_DEBUG > 0)
2443 ELOG_TYPE_DECLARE (e) =
2445 .format = "app_detach:C:%d",
2446 .format_args = "i4",
2453 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2454 ed->data = vcm->my_client_index;
2458 vppcom_app_detach ();
2459 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2460 if (PREDICT_FALSE (rv))
2462 if (VPPCOM_DEBUG > 0)
2463 clib_warning ("VCL<%d>: application detach timed out! "
2464 "returning %d (%s)",
2465 getpid (), rv, vppcom_retval_str (rv));
2468 /* Finished with logging before client gets reset to ~0 */
2469 if (VPPCOM_DEBUG > 0)
2472 vl_client_disconnect_from_vlib ();
2473 vcm->my_client_index = ~0;
2474 vcm->app_state = STATE_APP_START;
2478 vppcom_session_create (u8 proto, u8 is_nonblocking)
2483 clib_spinlock_lock (&vcm->sessions_lockp);
2484 pool_get (vcm->sessions, session);
2485 memset (session, 0, sizeof (*session));
2486 session_index = session - vcm->sessions;
2488 session->proto = proto;
2489 session->state = STATE_START;
2490 session->is_nonblocking = is_nonblocking ? 1 : 0;
2491 session->vpp_handle = ~0;
2492 clib_spinlock_unlock (&vcm->sessions_lockp);
2494 if (VPPCOM_DEBUG > 0)
2495 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2497 if (VPPCOM_DEBUG > 0)
2499 session->elog_track.name =
2500 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2502 elog_track_register (&vcm->elog_main, &session->elog_track);
2505 ELOG_TYPE_DECLARE (e) =
2507 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2508 .format_args = "i4i4i4",
2516 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2517 ed->data[0] = session->proto;
2518 ed->data[1] = session->state;
2519 ed->data[2] = session->is_nonblocking;
2523 return (int) session_index;
2527 vppcom_session_close (uint32_t session_index)
2529 session_t *session = 0;
2538 session_state_t state;
2540 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2541 is_listen = session->is_listen;
2542 is_vep = session->is_vep;
2543 is_vep_session = session->is_vep_session;
2544 next_sid = session->vep.next_sid;
2545 vep_idx = session->vep.vep_idx;
2546 state = session->state;
2547 vpp_handle = session->vpp_handle;
2548 clib_spinlock_unlock (&vcm->sessions_lockp);
2550 if (VPPCOM_DEBUG > 0)
2553 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2554 "closing epoll session...",
2555 getpid (), session_index, session_index);
2557 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2558 "closing session...",
2559 getpid (), vpp_handle, session_index);
2564 while (next_sid != ~0)
2566 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2567 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2568 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2569 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2570 getpid (), vpp_handle, next_sid, vep_idx,
2571 rv, vppcom_retval_str (rv));
2573 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2574 next_sid = session->vep.next_sid;
2575 clib_spinlock_unlock (&vcm->sessions_lockp);
2582 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2583 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2584 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2585 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2586 getpid (), vpp_handle, session_index,
2587 vep_idx, rv, vppcom_retval_str (rv));
2592 if (state & STATE_LISTEN)
2594 rv = vppcom_session_unbind (session_index);
2595 if (PREDICT_FALSE (rv < 0))
2597 if (VPPCOM_DEBUG > 0)
2598 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2599 "listener unbind failed! rv %d (%s)",
2600 getpid (), vpp_handle, session_index,
2601 rv, vppcom_retval_str (rv));
2606 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2608 rv = vppcom_session_disconnect (session_index);
2609 if (PREDICT_FALSE (rv < 0))
2610 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2611 "session disconnect failed! rv %d (%s)",
2612 getpid (), vpp_handle, session_index,
2613 rv, vppcom_retval_str (rv));
2617 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2618 vpp_handle = session->vpp_handle;
2619 if (vpp_handle != ~0)
2621 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2623 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2625 pool_put_index (vcm->sessions, session_index);
2626 clib_spinlock_unlock (&vcm->sessions_lockp);
2628 if (VPPCOM_DEBUG > 0)
2631 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2632 getpid (), session_index, session_index);
2634 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2635 getpid (), vpp_handle, session_index);
2639 if (VPPCOM_DEBUG > 0)
2642 ELOG_TYPE_DECLARE (e) =
2644 .format = "session_close:rv:%d",
2645 .format_args = "i4",
2653 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2662 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2664 session_t *session = 0;
2668 return VPPCOM_EINVAL;
2670 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2672 if (session->is_vep)
2674 clib_spinlock_unlock (&vcm->sessions_lockp);
2675 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2676 "bind to an epoll session!", getpid (), session_index);
2681 session->lcl_addr.is_ip4 = ep->is_ip4;
2682 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2683 session->lcl_port = ep->port;
2685 if (VPPCOM_DEBUG > 0)
2686 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2687 "port %u, proto %s", getpid (), session_index,
2688 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2689 format_ip46_address, &session->lcl_addr.ip46,
2690 session->lcl_addr.is_ip4,
2691 clib_net_to_host_u16 (session->lcl_port),
2692 session->proto ? "UDP" : "TCP");
2694 if (VPPCOM_DEBUG > 0)
2696 if (session->lcl_addr.is_ip4)
2699 ELOG_TYPE_DECLARE (e) =
2701 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2702 .format_args = "t1i1i1i1i1i2",
2703 .n_enum_strings = 2,
2704 .enum_strings = {"TCP", "UDP",},
2707 CLIB_PACKED (struct {
2713 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2714 ed->proto = session->proto;
2715 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2716 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2717 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2718 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2719 ed->port = clib_net_to_host_u16 (session->lcl_port);
2724 clib_spinlock_unlock (&vcm->sessions_lockp);
2730 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2732 session_t *listen_session = 0;
2733 u64 listen_vpp_handle;
2736 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2738 if (listen_session->is_vep)
2740 clib_spinlock_unlock (&vcm->sessions_lockp);
2741 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2742 "epoll session!", getpid (), listen_session_index);
2747 listen_vpp_handle = listen_session->vpp_handle;
2748 if (listen_session->is_listen)
2750 clib_spinlock_unlock (&vcm->sessions_lockp);
2751 if (VPPCOM_DEBUG > 0)
2752 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2753 "already in listen state!",
2754 getpid (), listen_vpp_handle, listen_session_index);
2759 if (VPPCOM_DEBUG > 0)
2760 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2761 "sid %u: sending bind request...",
2762 getpid (), listen_vpp_handle, listen_session_index);
2764 vppcom_send_bind_sock (listen_session, listen_session_index);
2765 clib_spinlock_unlock (&vcm->sessions_lockp);
2767 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2768 vcm->cfg.session_timeout);
2770 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2771 if (PREDICT_FALSE (retval))
2773 if (VPPCOM_DEBUG > 0)
2774 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2775 "returning %d (%s)", getpid (),
2776 listen_session->vpp_handle, listen_session_index,
2777 retval, vppcom_retval_str (retval));
2778 clib_spinlock_unlock (&vcm->sessions_lockp);
2783 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2784 clib_spinlock_unlock (&vcm->sessions_lockp);
2790 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2793 session_t *listen_session = 0;
2794 session_t *client_session = 0;
2795 u32 client_session_index = ~0, n_fifos;
2799 u64 listen_vpp_handle;
2801 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2803 if (listen_session->is_vep)
2805 clib_spinlock_unlock (&vcm->sessions_lockp);
2806 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2807 "epoll session!", getpid (), listen_session_index);
2812 listen_vpp_handle = listen_session->vpp_handle;
2813 if (listen_session->state != STATE_LISTEN)
2815 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2816 "not in listen state! state 0x%x (%s)", getpid (),
2817 listen_vpp_handle, listen_session_index,
2818 listen_session->state,
2819 vppcom_session_state_str (listen_session->state));
2820 clib_spinlock_unlock (&vcm->sessions_lockp);
2824 wait_for = (listen_session->is_nonblocking) ? 0 : vcm->cfg.accept_timeout;
2826 clib_spinlock_unlock (&vcm->sessions_lockp);
2830 rv = vppcom_wait_for_client_session_index (wait_for);
2833 if ((VPPCOM_DEBUG > 0))
2834 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2835 "accept failed! returning %d (%s)", getpid (),
2836 listen_vpp_handle, listen_session_index,
2837 rv, vppcom_retval_str (rv));
2845 clib_spinlock_lock (&vcm->sessions_lockp);
2846 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2847 rv = vppcom_session_at_index (client_session_index, &client_session);
2848 if (PREDICT_FALSE (rv))
2850 rv = VPPCOM_ECONNABORTED;
2851 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2852 "lookup failed! returning %d (%s)", getpid (),
2853 listen_vpp_handle, listen_session_index,
2854 client_session_index, rv, vppcom_retval_str (rv));
2858 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2859 if (VPPCOM_DEBUG > 0)
2860 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2861 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2862 getpid (), listen_vpp_handle, listen_session_index,
2863 client_session->vpp_handle, client_session_index,
2864 flags, client_session->is_nonblocking);
2868 ep->is_cut_thru = client_session->is_cut_thru;
2869 ep->is_ip4 = client_session->peer_addr.is_ip4;
2870 ep->port = client_session->peer_port;
2871 if (client_session->peer_addr.is_ip4)
2872 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2873 sizeof (ip4_address_t));
2875 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2876 sizeof (ip6_address_t));
2879 if (client_session->is_server && client_session->is_cut_thru)
2881 static svm_fifo_segment_create_args_t _a;
2882 svm_fifo_segment_create_args_t *a = &_a;
2883 svm_fifo_segment_private_t *seg;
2885 cut_thru_str = " cut-thru ";
2887 /* Create the segment */
2888 memset (a, 0, sizeof (*a));
2889 a->segment_name = (char *)
2890 format ((u8 *) a->segment_name, "%d:segment%d%c",
2891 getpid (), vcm->unique_segment_index++, 0);
2892 a->segment_size = vcm->cfg.segment_size;
2894 rv = svm_fifo_segment_create (a);
2895 if (PREDICT_FALSE (rv))
2897 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2898 "client sid %u svm_fifo_segment_create ('%s') "
2899 "failed! rv %d", getpid (), listen_vpp_handle,
2900 listen_session_index, client_session_index,
2901 a->segment_name, rv);
2902 vec_reset_length (a->new_segment_indices);
2903 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2904 vppcom_send_connect_session_reply (client_session,
2905 client_session_index,
2906 client_session->vpp_handle,
2907 client_session->client_context,
2909 clib_spinlock_unlock (&vcm->sessions_lockp);
2914 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2915 client_session->sm_seg_index = a->new_segment_indices[0];
2916 vec_free (a->new_segment_indices);
2918 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2919 if (vcm->cfg.preallocated_fifo_pairs)
2921 n_fifos = vcm->cfg.preallocated_fifo_pairs;
2922 svm_fifo_segment_preallocate_fifo_pairs (seg, vcm->cfg.rx_fifo_size,
2923 vcm->cfg.tx_fifo_size,
2927 client_session->server_rx_fifo =
2928 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2929 FIFO_SEGMENT_RX_FREELIST);
2930 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2932 svm_fifo_segment_delete (seg);
2933 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2934 "client sid %u rx fifo alloc failed! "
2935 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2936 listen_session_index, client_session_index,
2937 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2938 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2939 vppcom_send_connect_session_reply (client_session,
2940 client_session_index,
2941 client_session->vpp_handle,
2942 client_session->client_context,
2944 clib_spinlock_unlock (&vcm->sessions_lockp);
2948 client_session->server_rx_fifo->master_session_index =
2949 client_session_index;
2951 client_session->server_tx_fifo =
2952 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2953 FIFO_SEGMENT_TX_FREELIST);
2954 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2956 svm_fifo_segment_delete (seg);
2957 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2958 "client sid %u tx fifo alloc failed! "
2959 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2960 listen_session_index, client_session_index,
2961 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2962 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2963 vppcom_send_connect_session_reply (client_session,
2964 client_session_index,
2965 client_session->vpp_handle,
2966 client_session->client_context,
2968 clib_spinlock_unlock (&vcm->sessions_lockp);
2972 client_session->server_tx_fifo->master_session_index =
2973 client_session_index;
2975 if (VPPCOM_DEBUG > 1)
2976 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2977 "created segment '%s', rx_fifo %p, tx_fifo %p",
2978 getpid (), listen_vpp_handle, listen_session_index,
2979 client_session_index, client_session->segment_name,
2980 client_session->server_rx_fifo,
2981 client_session->server_tx_fifo);
2983 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2986 ssvm_shared_header_t *sh = seg->ssvm.sh;
2988 ssvm_lock_non_recursive (sh, 1);
2989 oldheap = ssvm_push_heap (sh);
2990 event_q = client_session->vpp_event_queue =
2991 svm_queue_init (vcm->cfg.event_queue_size,
2992 sizeof (session_fifo_event_t),
2993 getpid (), 0 /* signal not sent */ );
2994 ssvm_pop_heap (oldheap);
2995 ssvm_unlock_non_recursive (sh);
2998 vppcom_send_connect_session_reply (client_session,
2999 client_session_index,
3000 client_session->vpp_handle,
3001 client_session->client_context,
3002 0 /* retval OK */ );
3007 vppcom_send_accept_session_reply (client_session->vpp_handle,
3008 client_session->client_context,
3009 0 /* retval OK */ );
3012 if (VPPCOM_DEBUG > 0)
3013 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
3014 "0x%llx, sid %u%sconnection to local %s address "
3015 "%U port %u", getpid (), listen_vpp_handle,
3016 listen_session_index, client_session->vpp_handle,
3017 client_session_index, cut_thru_str,
3018 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
3019 format_ip46_address, &client_session->lcl_addr.ip46,
3020 client_session->lcl_addr.is_ip4,
3021 clib_net_to_host_u16 (client_session->lcl_port));
3023 if (VPPCOM_DEBUG > 0)
3025 client_session->elog_track.name =
3026 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
3027 client_session_index, 0);
3028 elog_track_register (&vcm->elog_main, &client_session->elog_track);
3031 ELOG_TYPE_DECLARE (e) =
3033 .format = "accept cut-thru: listen_handle:%x from_handle:%x",
3034 .format_args = "i8i8",
3042 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
3043 ed->handle[0] = listen_vpp_handle;
3044 ed->handle[1] = client_session->vpp_handle;
3047 if (client_session->lcl_addr.is_ip4)
3050 ELOG_TYPE_DECLARE (e2) =
3052 .format = "accept cut-thru: S:%d %d.%d.%d.%d:%d ",
3053 .format_args = "i4i1i1i1i1i2",
3056 CLIB_PACKED (struct {
3063 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
3064 ed2->session = client_session_index;
3065 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
3066 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
3067 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
3068 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
3069 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
3074 clib_spinlock_unlock (&vcm->sessions_lockp);
3075 rv = (int) client_session_index;
3081 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
3083 session_t *session = 0;
3084 int rv, retval = VPPCOM_OK;
3085 u64 vpp_handle = ~0;
3087 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3089 if (PREDICT_FALSE (session->is_vep))
3091 clib_spinlock_unlock (&vcm->sessions_lockp);
3092 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3093 "connect on an epoll session!", getpid (), session_index);
3098 vpp_handle = session->vpp_handle;
3099 if (PREDICT_FALSE (session->is_server))
3101 clib_spinlock_unlock (&vcm->sessions_lockp);
3102 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: is in use "
3103 "as a server session!", getpid (), vpp_handle,
3109 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
3111 if (VPPCOM_DEBUG > 0)
3112 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
3113 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3114 getpid (), vpp_handle, session_index,
3115 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3116 format_ip46_address,
3117 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3118 clib_net_to_host_u16 (session->peer_port),
3119 session->proto ? "UDP" : "TCP", session->state,
3120 vppcom_session_state_str (session->state));
3122 clib_spinlock_unlock (&vcm->sessions_lockp);
3126 session->peer_addr.is_ip4 = server_ep->is_ip4;
3127 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
3128 session->peer_port = server_ep->port;
3130 if (VPPCOM_DEBUG > 0)
3131 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
3132 "%s %U port %d proto %s",
3133 getpid (), vpp_handle, session_index,
3134 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3135 format_ip46_address,
3136 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3137 clib_net_to_host_u16 (session->peer_port),
3138 session->proto ? "UDP" : "TCP");
3140 vppcom_send_connect_sock (session, session_index);
3141 clib_spinlock_unlock (&vcm->sessions_lockp);
3144 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3145 vcm->cfg.session_timeout);
3147 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3148 vpp_handle = session->vpp_handle;
3149 clib_spinlock_unlock (&vcm->sessions_lockp);
3152 if (PREDICT_FALSE (retval))
3155 if (VPPCOM_DEBUG > 0)
3156 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
3157 "returning %d (%s)", getpid (), vpp_handle,
3158 session_index, rv, vppcom_retval_str (rv));
3160 else if (VPPCOM_DEBUG > 0)
3161 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
3162 getpid (), vpp_handle, session_index);
3168 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3171 session_t *session = 0;
3172 svm_fifo_t *rx_fifo;
3177 session_state_t state;
3184 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3186 if (PREDICT_FALSE (session->is_vep))
3188 clib_spinlock_unlock (&vcm->sessions_lockp);
3189 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3190 "read from an epoll session!", getpid (), session_index);
3195 vpp_handle = session->vpp_handle;
3196 is_server = session->is_server;
3197 is_nonblocking = session->is_nonblocking;
3198 state = session->state;
3199 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3201 clib_spinlock_unlock (&vcm->sessions_lockp);
3202 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3204 if (VPPCOM_DEBUG > 0)
3205 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3206 "not open! state 0x%x (%s), returning %d (%s)",
3207 getpid (), vpp_handle, session_index,
3208 is_server ? "server" : "client", state,
3209 vppcom_session_state_str (state),
3210 rv, vppcom_retval_str (rv));
3214 rx_fifo = ((!session->is_cut_thru || is_server) ?
3215 session->server_rx_fifo : session->server_tx_fifo);
3216 fifo_str = ((!session->is_cut_thru || is_server) ?
3217 "server_rx_fifo" : "server_tx_fifo");
3218 clib_spinlock_unlock (&vcm->sessions_lockp);
3223 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3225 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3227 while (!is_nonblocking && (n_read <= 0));
3231 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3233 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3234 (EPOLLET | EPOLLIN));
3236 session->vep.et_mask |= EPOLLIN;
3238 if (state & STATE_CLOSE_ON_EMPTY)
3240 session_state_t new_state = STATE_DISCONNECT;
3241 rv = VPPCOM_ECONNRESET;
3243 if (VPPCOM_DEBUG > 1)
3245 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3246 "with %s session state 0x%x (%s)!"
3247 " Setting state to 0x%x (%s), returning %d (%s)",
3248 getpid (), vpp_handle, session_index,
3249 is_server ? "server" : "client",
3250 state, vppcom_session_state_str (state),
3251 new_state, vppcom_session_state_str (new_state),
3252 rv, vppcom_retval_str (rv));
3255 session->state = new_state;
3260 clib_spinlock_unlock (&vcm->sessions_lockp);
3265 if (VPPCOM_DEBUG > 2)
3268 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3269 "from %s (%p)", getpid (), vpp_handle,
3270 session_index, n_read, fifo_str, rx_fifo);
3272 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3273 "returning %d (%s)", getpid (), vpp_handle,
3274 session_index, rv, vppcom_retval_str (rv));
3281 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3283 return (vppcom_session_read_internal (session_index, buf, n, 0));
3287 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3289 return (vppcom_session_read_internal (session_index, buf, n, 1));
3293 vppcom_session_read_ready (session_t * session, u32 session_index)
3295 svm_fifo_t *rx_fifo = 0;
3299 u8 is_server = session->is_server;
3300 session_state_t state = session->state;
3301 u64 vpp_handle = session->vpp_handle;
3303 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3304 if (PREDICT_FALSE (session->is_vep))
3306 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3307 "epoll session!", getpid (), session_index);
3312 if (session->is_listen)
3313 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3316 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3318 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3321 if (VPPCOM_DEBUG > 1)
3322 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3323 "not open! state 0x%x (%s), returning %d (%s)",
3324 getpid (), vpp_handle, session_index,
3325 is_server ? "server" : "client",
3326 state, vppcom_session_state_str (state),
3327 rv, vppcom_retval_str (rv));
3331 rx_fifo = ((!session->is_cut_thru || is_server) ?
3332 session->server_rx_fifo : session->server_tx_fifo);
3334 ready = svm_fifo_max_dequeue (rx_fifo);
3340 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3342 session->vep.et_mask |= EPOLLIN;
3344 if (state & STATE_CLOSE_ON_EMPTY)
3346 rv = VPPCOM_ECONNRESET;
3347 session_state_t new_state = STATE_DISCONNECT;
3349 if (VPPCOM_DEBUG > 1)
3351 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3352 "sid %u: Empty fifo with"
3353 " %s session state 0x%x (%s)! Setting state to "
3354 "0x%x (%s), returning %d (%s)",
3355 getpid (), session_index, vpp_handle,
3356 is_server ? "server" : "client",
3357 state, vppcom_session_state_str (state),
3358 new_state, vppcom_session_state_str (new_state),
3359 rv, vppcom_retval_str (rv));
3361 session->state = new_state;
3367 if (vcm->app_event_queue->cursize &&
3368 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3370 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3371 session_fifo_event_t e;
3373 for (i = 0; i < n_to_dequeue; i++)
3374 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3376 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3383 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3385 session_t *session = 0;
3386 svm_fifo_t *tx_fifo;
3388 session_fifo_event_t evt;
3394 session_state_t state;
3399 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3401 if (PREDICT_FALSE (session->is_vep))
3403 clib_spinlock_unlock (&vcm->sessions_lockp);
3404 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3405 "cannot write to an epoll session!",
3406 getpid (), session->vpp_handle, session_index);
3412 is_server = session->is_server;
3413 is_nonblocking = session->is_nonblocking;
3414 vpp_handle = session->vpp_handle;
3415 state = session->state;
3416 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3418 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3420 clib_spinlock_unlock (&vcm->sessions_lockp);
3421 if (VPPCOM_DEBUG > 1)
3422 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3423 "%s session is not open! state 0x%x (%s)",
3424 getpid (), vpp_handle, session_index,
3425 is_server ? "server" : "client", state,
3426 vppcom_session_state_str (state));
3430 tx_fifo = ((!session->is_cut_thru || is_server) ?
3431 session->server_tx_fifo : session->server_rx_fifo);
3432 fifo_str = ((!session->is_cut_thru || is_server) ?
3433 "server_tx_fifo" : "server_rx_fifo");
3434 clib_spinlock_unlock (&vcm->sessions_lockp);
3438 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3440 while (!is_nonblocking && (n_write <= 0));
3442 /* If event wasn't set, add one */
3443 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
3445 /* Fabricate TX event, send to vpp */
3447 evt.event_type = FIFO_EVENT_APP_TX;
3449 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3450 q = session->vpp_event_queue;
3452 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3453 clib_spinlock_unlock (&vcm->sessions_lockp);
3454 if (VPPCOM_DEBUG > 1)
3455 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3456 "added FIFO_EVENT_APP_TX to "
3457 "vpp_event_q %p, n_write %d", getpid (),
3458 vpp_handle, session_index, q, n_write);
3463 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3465 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3466 (EPOLLET | EPOLLOUT));
3468 session->vep.et_mask |= EPOLLOUT;
3470 if (state & STATE_CLOSE_ON_EMPTY)
3472 session_state_t new_state = STATE_DISCONNECT;
3473 rv = VPPCOM_ECONNRESET;
3475 if (VPPCOM_DEBUG > 1)
3477 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3478 "Empty fifo with %s session state 0x%x (%s)!"
3479 " Setting state to 0x%x (%s), returning %d (%s)",
3480 getpid (), vpp_handle, session_index,
3481 is_server ? "server" : "client",
3482 state, vppcom_session_state_str (state),
3483 new_state, vppcom_session_state_str (new_state),
3484 rv, vppcom_retval_str (rv));
3487 session->state = new_state;
3492 clib_spinlock_unlock (&vcm->sessions_lockp);
3497 if (VPPCOM_DEBUG > 2)
3500 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3501 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
3502 session_index, fifo_str, tx_fifo);
3504 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3505 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
3506 session_index, n_write, fifo_str, tx_fifo);
3513 vppcom_session_write_ready (session_t * session, u32 session_index)
3515 svm_fifo_t *tx_fifo;
3520 u8 is_server = session->is_server;
3521 session_state_t state = session->state;
3523 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3524 if (PREDICT_FALSE (session->is_vep))
3526 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3527 "cannot write to an epoll session!",
3528 getpid (), session->vpp_handle, session_index);
3533 if (PREDICT_FALSE (session->is_listen))
3535 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3536 "cannot write to a listen session!",
3537 getpid (), session->vpp_handle, session_index);
3542 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3544 session_state_t state = session->state;
3546 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3548 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3549 "%s session is not open! state 0x%x (%s), "
3550 "returning %d (%s)", getpid (), session->vpp_handle,
3551 session_index, is_server ? "server" : "client",
3552 state, vppcom_session_state_str (state),
3553 rv, vppcom_retval_str (rv));
3557 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3558 session->server_tx_fifo : session->server_rx_fifo);
3559 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3560 "server_tx_fifo" : "server_rx_fifo");
3562 ready = svm_fifo_max_enqueue (tx_fifo);
3564 if (VPPCOM_DEBUG > 3)
3565 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3566 "peek %s (%p), ready = %d", getpid (),
3567 session->vpp_handle, session_index,
3568 fifo_str, tx_fifo, ready);
3572 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3573 (EPOLLET | EPOLLOUT));
3575 session->vep.et_mask |= EPOLLOUT;
3577 if (state & STATE_CLOSE_ON_EMPTY)
3579 rv = VPPCOM_ECONNRESET;
3580 session_state_t new_state = STATE_DISCONNECT;
3582 if (VPPCOM_DEBUG > 1)
3584 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3585 "Empty fifo with %s session "
3586 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3587 "returning %d (%s)", getpid (),
3588 session->vpp_handle, session_index,
3589 is_server ? "server" : "client",
3590 state, vppcom_session_state_str (state),
3591 new_state, vppcom_session_state_str (new_state),
3592 rv, vppcom_retval_str (rv));
3594 session->state = new_state;
3604 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3605 unsigned long *write_map, unsigned long *except_map,
3606 double time_to_wait)
3609 session_t *session = 0;
3610 int rv, bits_set = 0;
3611 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3612 u32 minbits = clib_max (n_bits, BITS (uword));
3614 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3616 if (n_bits && read_map)
3618 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3619 clib_memcpy (vcm->rd_bitmap, read_map,
3620 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3621 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3623 if (n_bits && write_map)
3625 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3626 clib_memcpy (vcm->wr_bitmap, write_map,
3627 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3628 memset (write_map, 0,
3629 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3631 if (n_bits && except_map)
3633 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3634 clib_memcpy (vcm->ex_bitmap, except_map,
3635 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3636 memset (except_map, 0,
3637 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3647 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3649 clib_spinlock_lock (&vcm->sessions_lockp);
3650 rv = vppcom_session_at_index (session_index, &session);
3653 clib_spinlock_unlock (&vcm->sessions_lockp);
3654 if (VPPCOM_DEBUG > 1)
3655 clib_warning ("VCL<%d>: session %d specified in "
3656 "read_map is closed.", getpid (),
3658 bits_set = VPPCOM_EBADFD;
3662 rv = vppcom_session_read_ready (session, session_index);
3663 clib_spinlock_unlock (&vcm->sessions_lockp);
3664 if (except_map && vcm->ex_bitmap &&
3665 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3668 clib_bitmap_set_no_check (except_map, session_index, 1);
3673 clib_bitmap_set_no_check (read_map, session_index, 1);
3681 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3683 clib_spinlock_lock (&vcm->sessions_lockp);
3684 rv = vppcom_session_at_index (session_index, &session);
3687 clib_spinlock_unlock (&vcm->sessions_lockp);
3688 if (VPPCOM_DEBUG > 0)
3689 clib_warning ("VCL<%d>: session %d specified in "
3690 "write_map is closed.", getpid (),
3692 bits_set = VPPCOM_EBADFD;
3696 rv = vppcom_session_write_ready (session, session_index);
3697 clib_spinlock_unlock (&vcm->sessions_lockp);
3698 if (write_map && (rv > 0))
3700 clib_bitmap_set_no_check (write_map, session_index, 1);
3708 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3710 clib_spinlock_lock (&vcm->sessions_lockp);
3711 rv = vppcom_session_at_index (session_index, &session);
3714 clib_spinlock_unlock (&vcm->sessions_lockp);
3715 if (VPPCOM_DEBUG > 1)
3716 clib_warning ("VCL<%d>: session %d specified in "
3717 "except_map is closed.", getpid (),
3719 bits_set = VPPCOM_EBADFD;
3723 rv = vppcom_session_read_ready (session, session_index);
3724 clib_spinlock_unlock (&vcm->sessions_lockp);
3727 clib_bitmap_set_no_check (except_map, session_index, 1);
3735 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3742 vep_verify_epoll_chain (u32 vep_idx)
3745 vppcom_epoll_t *vep;
3749 if (VPPCOM_DEBUG <= 1)
3752 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3753 rv = vppcom_session_at_index (vep_idx, &session);
3754 if (PREDICT_FALSE (rv))
3756 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3757 getpid (), vep_idx);
3760 if (PREDICT_FALSE (!session->is_vep))
3762 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3763 getpid (), vep_idx);
3766 vep = &session->vep;
3767 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3770 " is_vep_session = %u\n"
3771 " next_sid = 0x%x (%u)\n"
3772 " wait_cont_idx = 0x%x (%u)\n"
3773 "}\n", getpid (), vep_idx,
3774 session->is_vep, session->is_vep_session,
3775 vep->next_sid, vep->next_sid,
3776 session->wait_cont_idx, session->wait_cont_idx);
3778 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3780 rv = vppcom_session_at_index (sid, &session);
3781 if (PREDICT_FALSE (rv))
3783 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3786 if (PREDICT_FALSE (session->is_vep))
3787 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3788 getpid (), vep_idx);
3789 else if (PREDICT_FALSE (!session->is_vep_session))
3791 clib_warning ("VCL<%d>: ERROR: session (%u) "
3792 "is not a vep session!", getpid (), sid);
3795 vep = &session->vep;
3796 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3797 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3798 "vep_idx (%u)!", getpid (),
3799 sid, session->vep.vep_idx, vep_idx);
3800 if (session->is_vep_session)
3802 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3804 " next_sid = 0x%x (%u)\n"
3805 " prev_sid = 0x%x (%u)\n"
3806 " vep_idx = 0x%x (%u)\n"
3807 " ev.events = 0x%x\n"
3808 " ev.data.u64 = 0x%llx\n"
3812 vep->next_sid, vep->next_sid,
3813 vep->prev_sid, vep->prev_sid,
3814 vep->vep_idx, vep->vep_idx,
3815 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3820 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3821 getpid (), vep_idx);
3825 vppcom_epoll_create (void)
3827 session_t *vep_session;
3830 clib_spinlock_lock (&vcm->sessions_lockp);
3831 pool_get (vcm->sessions, vep_session);
3832 memset (vep_session, 0, sizeof (*vep_session));
3833 vep_idx = vep_session - vcm->sessions;
3835 vep_session->is_vep = 1;
3836 vep_session->vep.vep_idx = ~0;
3837 vep_session->vep.next_sid = ~0;
3838 vep_session->vep.prev_sid = ~0;
3839 vep_session->wait_cont_idx = ~0;
3840 vep_session->vpp_handle = ~0;
3841 clib_spinlock_unlock (&vcm->sessions_lockp);
3843 if (VPPCOM_DEBUG > 0)
3844 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3845 getpid (), vep_idx, vep_idx);
3847 if (VPPCOM_DEBUG > 0)
3849 vep_session->elog_track.name =
3850 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3852 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3855 ELOG_TYPE_DECLARE (e) =
3857 .format = "created epoll session:%d",
3858 .format_args = "i4",
3866 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
3875 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3876 struct epoll_event *event)
3878 session_t *vep_session;
3882 if (vep_idx == session_index)
3884 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3885 getpid (), vep_idx);
3886 return VPPCOM_EINVAL;
3889 clib_spinlock_lock (&vcm->sessions_lockp);
3890 rv = vppcom_session_at_index (vep_idx, &vep_session);
3891 if (PREDICT_FALSE (rv))
3893 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3896 if (PREDICT_FALSE (!vep_session->is_vep))
3898 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3899 getpid (), vep_idx);
3904 ASSERT (vep_session->vep.vep_idx == ~0);
3905 ASSERT (vep_session->vep.prev_sid == ~0);
3907 rv = vppcom_session_at_index (session_index, &session);
3908 if (PREDICT_FALSE (rv))
3910 if (VPPCOM_DEBUG > 0)
3911 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3912 getpid (), session_index);
3915 if (PREDICT_FALSE (session->is_vep))
3917 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3925 if (PREDICT_FALSE (!event))
3927 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3928 "epoll_event structure!", getpid ());
3932 if (vep_session->vep.next_sid != ~0)
3934 session_t *next_session;
3935 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3937 if (PREDICT_FALSE (rv))
3939 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3940 "vep.next_sid (%u) on vep_idx (%u)!",
3941 getpid (), vep_session->vep.next_sid, vep_idx);
3944 ASSERT (next_session->vep.prev_sid == vep_idx);
3945 next_session->vep.prev_sid = session_index;
3947 session->vep.next_sid = vep_session->vep.next_sid;
3948 session->vep.prev_sid = vep_idx;
3949 session->vep.vep_idx = vep_idx;
3950 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3951 session->vep.ev = *event;
3952 session->is_vep = 0;
3953 session->is_vep_session = 1;
3954 vep_session->vep.next_sid = session_index;
3955 if (VPPCOM_DEBUG > 1)
3956 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3957 "sid %u, events 0x%x, data 0x%llx!",
3958 getpid (), vep_idx, session_index,
3959 event->events, event->data.u64);
3960 if (VPPCOM_DEBUG > 0)
3963 ELOG_TYPE_DECLARE (e) =
3965 .format = "epoll_ctladd: events:%x data:%x",
3966 .format_args = "i4i4i8",
3974 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3976 ed->events = event->events;
3977 ed->event_data = event->data.u64;
3983 if (PREDICT_FALSE (!event))
3985 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3986 "epoll_event structure!", getpid ());
3990 else if (PREDICT_FALSE (!session->is_vep_session))
3992 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3993 "not a vep session!", getpid (), session_index);
3997 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3999 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
4000 "vep_idx (%u) != vep_idx (%u)!",
4001 getpid (), session_index,
4002 session->vep.vep_idx, vep_idx);
4006 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
4007 session->vep.ev = *event;
4008 if (VPPCOM_DEBUG > 1)
4010 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
4011 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
4016 if (PREDICT_FALSE (!session->is_vep_session))
4018 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
4019 "not a vep session!", getpid (), session_index);
4023 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
4025 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
4026 "vep_idx (%u) != vep_idx (%u)!",
4027 getpid (), session_index,
4028 session->vep.vep_idx, vep_idx);
4033 vep_session->wait_cont_idx =
4034 (vep_session->wait_cont_idx == session_index) ?
4035 session->vep.next_sid : vep_session->wait_cont_idx;
4037 if (session->vep.prev_sid == vep_idx)
4038 vep_session->vep.next_sid = session->vep.next_sid;
4041 session_t *prev_session;
4042 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
4043 if (PREDICT_FALSE (rv))
4045 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
4046 "vep.prev_sid (%u) on sid (%u)!",
4047 getpid (), session->vep.prev_sid, session_index);
4050 ASSERT (prev_session->vep.next_sid == session_index);
4051 prev_session->vep.next_sid = session->vep.next_sid;
4053 if (session->vep.next_sid != ~0)
4055 session_t *next_session;
4056 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
4057 if (PREDICT_FALSE (rv))
4059 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
4060 "vep.next_sid (%u) on sid (%u)!",
4061 getpid (), session->vep.next_sid, session_index);
4064 ASSERT (next_session->vep.prev_sid == session_index);
4065 next_session->vep.prev_sid = session->vep.prev_sid;
4068 memset (&session->vep, 0, sizeof (session->vep));
4069 session->vep.next_sid = ~0;
4070 session->vep.prev_sid = ~0;
4071 session->vep.vep_idx = ~0;
4072 session->is_vep_session = 0;
4073 if (VPPCOM_DEBUG > 1)
4074 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
4075 getpid (), vep_idx, session_index);
4076 if (VPPCOM_DEBUG > 0)
4079 ELOG_TYPE_DECLARE (e) =
4081 .format = "epoll_ctldel: vep:%d",
4082 .format_args = "i4",
4089 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4097 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
4101 vep_verify_epoll_chain (vep_idx);
4104 clib_spinlock_unlock (&vcm->sessions_lockp);
4109 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4110 int maxevents, double wait_for_time)
4112 session_t *vep_session;
4114 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4115 u32 keep_trying = 1;
4117 u32 vep_next_sid, wait_cont_idx;
4120 if (PREDICT_FALSE (maxevents <= 0))
4122 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
4123 getpid (), maxevents);
4124 return VPPCOM_EINVAL;
4126 memset (events, 0, sizeof (*events) * maxevents);
4128 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4129 vep_next_sid = vep_session->vep.next_sid;
4130 is_vep = vep_session->is_vep;
4131 wait_cont_idx = vep_session->wait_cont_idx;
4132 clib_spinlock_unlock (&vcm->sessions_lockp);
4134 if (PREDICT_FALSE (!is_vep))
4136 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
4137 getpid (), vep_idx);
4141 if (PREDICT_FALSE (vep_next_sid == ~0))
4143 if (VPPCOM_DEBUG > 0)
4144 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
4145 getpid (), vep_idx);
4146 if (VPPCOM_DEBUG > 0)
4149 ELOG_TYPE_DECLARE (e) =
4151 .format = "WRN: vep_idx:%d empty",
4152 .format_args = "i4",
4159 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
4173 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4174 sid != ~0; sid = next_sid)
4176 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4177 u8 add_event, is_vep_session;
4179 u64 session_ev_data;
4181 VCL_LOCK_AND_GET_SESSION (sid, &session);
4182 next_sid = session->vep.next_sid;
4183 session_events = session->vep.ev.events;
4184 et_mask = session->vep.et_mask;
4185 is_vep = session->is_vep;
4186 is_vep_session = session->is_vep_session;
4187 session_vep_idx = session->vep.vep_idx;
4188 session_ev_data = session->vep.ev.data.u64;
4189 clib_spinlock_unlock (&vcm->sessions_lockp);
4191 if (PREDICT_FALSE (is_vep))
4193 if (VPPCOM_DEBUG > 0)
4194 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4195 getpid (), vep_idx);
4196 if (VPPCOM_DEBUG > 0)
4199 ELOG_TYPE_DECLARE (e) =
4201 .format = "ERR:vep_idx:%d is vep",
4202 .format_args = "i4",
4209 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4218 if (PREDICT_FALSE (!is_vep_session))
4220 if (VPPCOM_DEBUG > 0)
4221 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4222 "a vep session!", getpid (), sid);
4223 if (VPPCOM_DEBUG > 0)
4226 ELOG_TYPE_DECLARE (e) =
4228 .format = "ERR:SID:%d not vep",
4229 .format_args = "i4",
4236 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4245 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4247 clib_warning ("VCL<%d>: ERROR: session (%u) "
4248 "vep_idx (%u) != vep_idx (%u)!",
4249 getpid (), sid, session->vep.vep_idx, vep_idx);
4254 add_event = clear_et_mask = 0;
4256 if (EPOLLIN & session_events)
4258 VCL_LOCK_AND_GET_SESSION (sid, &session);
4259 ready = vppcom_session_read_ready (session, sid);
4260 clib_spinlock_unlock (&vcm->sessions_lockp);
4261 if ((ready > 0) && (EPOLLIN & et_mask))
4264 events[num_ev].events |= EPOLLIN;
4265 if (((EPOLLET | EPOLLIN) & session_events) ==
4266 (EPOLLET | EPOLLIN))
4267 clear_et_mask |= EPOLLIN;
4274 case VPPCOM_ECONNRESET:
4275 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4279 events[num_ev].events |= EPOLLERR;
4285 if (EPOLLOUT & session_events)
4287 VCL_LOCK_AND_GET_SESSION (sid, &session);
4288 ready = vppcom_session_write_ready (session, sid);
4289 clib_spinlock_unlock (&vcm->sessions_lockp);
4290 if ((ready > 0) && (EPOLLOUT & et_mask))
4293 events[num_ev].events |= EPOLLOUT;
4294 if (((EPOLLET | EPOLLOUT) & session_events) ==
4295 (EPOLLET | EPOLLOUT))
4296 clear_et_mask |= EPOLLOUT;
4303 case VPPCOM_ECONNRESET:
4304 events[num_ev].events |= EPOLLHUP;
4308 events[num_ev].events |= EPOLLERR;
4316 events[num_ev].data.u64 = session_ev_data;
4317 if (EPOLLONESHOT & session_events)
4319 VCL_LOCK_AND_GET_SESSION (sid, &session);
4320 session->vep.ev.events = 0;
4321 clib_spinlock_unlock (&vcm->sessions_lockp);
4324 if (num_ev == maxevents)
4326 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4327 vep_session->wait_cont_idx = next_sid;
4328 clib_spinlock_unlock (&vcm->sessions_lockp);
4332 if (wait_cont_idx != ~0)
4335 next_sid = vep_next_sid;
4336 else if (next_sid == wait_cont_idx)
4340 if (wait_for_time != -1)
4341 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4343 while ((num_ev == 0) && keep_trying);
4345 if (wait_cont_idx != ~0)
4347 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4348 vep_session->wait_cont_idx = ~0;
4349 clib_spinlock_unlock (&vcm->sessions_lockp);
4352 return (rv != VPPCOM_OK) ? rv : num_ev;
4356 vppcom_session_attr (uint32_t session_index, uint32_t op,
4357 void *buffer, uint32_t * buflen)
4361 u32 *flags = buffer;
4362 vppcom_endpt_t *ep = buffer;
4364 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4367 case VPPCOM_ATTR_GET_NREAD:
4368 rv = vppcom_session_read_ready (session, session_index);
4369 if (VPPCOM_DEBUG > 2)
4370 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4372 if (VPPCOM_DEBUG > 0)
4375 ELOG_TYPE_DECLARE (e) =
4377 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4378 .format_args = "i4",
4385 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4393 case VPPCOM_ATTR_GET_NWRITE:
4394 rv = vppcom_session_write_ready (session, session_index);
4395 if (VPPCOM_DEBUG > 2)
4396 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4397 getpid (), session_index, rv);
4398 if (VPPCOM_DEBUG > 0)
4401 ELOG_TYPE_DECLARE (e) =
4403 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4404 .format_args = "i4",
4411 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4418 case VPPCOM_ATTR_GET_FLAGS:
4419 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4421 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
4422 *buflen = sizeof (*flags);
4423 if (VPPCOM_DEBUG > 2)
4424 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4425 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4426 session_index, *flags, session->is_nonblocking);
4427 if (VPPCOM_DEBUG > 0)
4430 ELOG_TYPE_DECLARE (e) =
4432 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4433 .format_args = "i4i4",
4441 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4444 ed->is_nonblk = session->is_nonblocking;
4453 case VPPCOM_ATTR_SET_FLAGS:
4454 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4456 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
4457 if (VPPCOM_DEBUG > 2)
4458 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4459 "flags = 0x%08x, is_nonblocking = %u",
4460 getpid (), session_index, *flags,
4461 session->is_nonblocking);
4462 if (VPPCOM_DEBUG > 0)
4465 ELOG_TYPE_DECLARE (e) =
4467 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4468 .format_args = "i4i4",
4476 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4479 ed->is_nonblk = session->is_nonblocking;
4487 case VPPCOM_ATTR_GET_PEER_ADDR:
4488 if (PREDICT_TRUE (buffer && buflen &&
4489 (*buflen >= sizeof (*ep)) && ep->ip))
4491 ep->is_ip4 = session->peer_addr.is_ip4;
4492 ep->port = session->peer_port;
4493 if (session->peer_addr.is_ip4)
4494 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4495 sizeof (ip4_address_t));
4497 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4498 sizeof (ip6_address_t));
4499 *buflen = sizeof (*ep);
4500 if (VPPCOM_DEBUG > 1)
4501 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4502 "is_ip4 = %u, addr = %U, port %u", getpid (),
4503 session_index, ep->is_ip4, format_ip46_address,
4504 &session->peer_addr.ip46, ep->is_ip4,
4505 clib_net_to_host_u16 (ep->port));
4506 if (VPPCOM_DEBUG > 0)
4511 ELOG_TYPE_DECLARE (e) =
4513 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4514 .format_args = "i1i1i1i1i2",
4516 CLIB_PACKED (struct {
4521 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4523 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4524 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4525 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4526 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4527 ed->port = clib_net_to_host_u16 (session->peer_port);
4533 ELOG_TYPE_DECLARE (e) =
4535 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4536 .format_args = "i2",
4538 CLIB_PACKED (struct {
4542 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4544 ed->port = clib_net_to_host_u16 (session->peer_port);
4553 case VPPCOM_ATTR_GET_LCL_ADDR:
4554 if (PREDICT_TRUE (buffer && buflen &&
4555 (*buflen >= sizeof (*ep)) && ep->ip))
4557 ep->is_ip4 = session->lcl_addr.is_ip4;
4558 ep->port = session->lcl_port;
4559 if (session->lcl_addr.is_ip4)
4560 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4561 sizeof (ip4_address_t));
4563 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4564 sizeof (ip6_address_t));
4565 *buflen = sizeof (*ep);
4566 if (VPPCOM_DEBUG > 1)
4567 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4568 "is_ip4 = %u, addr = %U port %d", getpid (),
4569 session_index, ep->is_ip4, format_ip46_address,
4570 &session->lcl_addr.ip46, ep->is_ip4,
4571 clib_net_to_host_u16 (ep->port));
4572 if (VPPCOM_DEBUG > 0)
4577 ELOG_TYPE_DECLARE (e) =
4579 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4580 .format_args = "i1i1i1i1i2",
4582 CLIB_PACKED (struct {
4587 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4589 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4590 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4591 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4592 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4593 ed->port = clib_net_to_host_u16 (session->peer_port);
4599 ELOG_TYPE_DECLARE (e) =
4601 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4602 .format_args = "i2",
4604 CLIB_PACKED (struct {
4608 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4610 ed->port = clib_net_to_host_u16 (session->peer_port);
4619 case VPPCOM_ATTR_GET_LIBC_EPFD:
4620 rv = session->libc_epfd;
4621 if (VPPCOM_DEBUG > 2)
4622 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4624 if (VPPCOM_DEBUG > 0)
4627 ELOG_TYPE_DECLARE (e) =
4629 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4630 .format_args = "i4",
4632 CLIB_PACKED (struct {
4636 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4637 ed->data = session->libc_epfd;
4643 case VPPCOM_ATTR_SET_LIBC_EPFD:
4644 if (PREDICT_TRUE (buffer && buflen &&
4645 (*buflen == sizeof (session->libc_epfd))))
4647 session->libc_epfd = *(int *) buffer;
4648 *buflen = sizeof (session->libc_epfd);
4650 if (VPPCOM_DEBUG > 2)
4651 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4652 "buflen %d", getpid (), session->libc_epfd,
4654 if (VPPCOM_DEBUG > 0)
4657 ELOG_TYPE_DECLARE (e) =
4659 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4660 .format_args = "t1i4i4",
4661 .n_enum_strings = 2,
4662 .enum_strings = {"", "-",},
4664 CLIB_PACKED (struct {
4669 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4671 ed->sign = (session->libc_epfd < 0);
4672 ed->data[0] = abs(session->libc_epfd);
4673 ed->data[1] = *buflen;
4681 case VPPCOM_ATTR_GET_PROTOCOL:
4682 if (buffer && buflen && (*buflen >= sizeof (int)))
4684 *(int *) buffer = session->proto;
4685 *buflen = sizeof (int);
4687 if (VPPCOM_DEBUG > 2)
4688 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4689 "buflen %d", getpid (), *(int *) buffer,
4690 *(int *) buffer ? "UDP" : "TCP", *buflen);
4691 if (VPPCOM_DEBUG > 0)
4694 ELOG_TYPE_DECLARE (e) =
4696 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4697 .format_args = "t1i4",
4698 .n_enum_strings = 2,
4699 .enum_strings = {"TCP", "UDP",},
4702 CLIB_PACKED (struct {
4707 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4708 ed->proto = session->proto;
4709 ed->buflen = *(int *) buffer;
4717 case VPPCOM_ATTR_GET_LISTEN:
4718 if (buffer && buflen && (*buflen >= sizeof (int)))
4720 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4721 VCL_SESS_ATTR_LISTEN);
4722 *buflen = sizeof (int);
4724 if (VPPCOM_DEBUG > 2)
4725 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4726 "buflen %d", getpid (), *(int *) buffer, *buflen);
4727 if (VPPCOM_DEBUG > 0)
4730 ELOG_TYPE_DECLARE (e) =
4732 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4733 .format_args = "i4i4",
4740 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4741 ed->data[0] = *(int *) buffer;
4742 ed->data[1] = *buflen;
4750 case VPPCOM_ATTR_GET_ERROR:
4751 if (buffer && buflen && (*buflen >= sizeof (int)))
4753 *(int *) buffer = 0;
4754 *buflen = sizeof (int);
4756 if (VPPCOM_DEBUG > 2)
4757 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4758 "buflen %d, #VPP-TBD#", getpid (),
4759 *(int *) buffer, *buflen);
4760 if (VPPCOM_DEBUG > 0)
4763 ELOG_TYPE_DECLARE (e) =
4765 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4766 .format_args = "i4i4",
4773 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4774 ed->data[0] = *(int *) buffer;
4775 ed->data[1] = *buflen;
4783 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4784 if (buffer && buflen && (*buflen >= sizeof (u32)))
4786 svm_fifo_t *tx_fifo;
4788 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
4789 session->server_tx_fifo : session->server_rx_fifo);
4792 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4793 tx_fifo ? tx_fifo->nitems :
4794 vcm->cfg.tx_fifo_size);
4795 *buflen = sizeof (u32);
4797 if (VPPCOM_DEBUG > 2)
4798 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4799 "buflen %d, #VPP-TBD#", getpid (),
4800 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4801 if (VPPCOM_DEBUG > 0)
4804 ELOG_TYPE_DECLARE (e) =
4806 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4807 .format_args = "i4i4",
4814 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4815 ed->data[0] = *(size_t *) buffer;
4816 ed->data[1] = *buflen;
4824 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4825 if (buffer && buflen && (*buflen == sizeof (u32)))
4828 session->sndbuf_size = *(u32 *) buffer;
4829 if (VPPCOM_DEBUG > 2)
4830 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4831 "buflen %d, #VPP-TBD#", getpid (),
4832 session->sndbuf_size, session->sndbuf_size,
4834 if (VPPCOM_DEBUG > 0)
4837 ELOG_TYPE_DECLARE (e) =
4839 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4840 .format_args = "i4i4",
4847 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4848 ed->data[0] = session->sndbuf_size;
4849 ed->data[1] = *buflen;
4857 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4858 if (buffer && buflen && (*buflen >= sizeof (u32)))
4860 svm_fifo_t *rx_fifo;
4862 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
4863 session->server_rx_fifo : session->server_tx_fifo);
4866 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4867 rx_fifo ? rx_fifo->nitems :
4868 vcm->cfg.rx_fifo_size);
4869 *buflen = sizeof (u32);
4871 if (VPPCOM_DEBUG > 2)
4872 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4873 "buflen %d, #VPP-TBD#", getpid (),
4874 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4875 if (VPPCOM_DEBUG > 0)
4878 ELOG_TYPE_DECLARE (e) =
4880 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4881 .format_args = "i4i4",
4888 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4889 ed->data[0] = *(size_t *) buffer;
4890 ed->data[1] = *buflen;
4898 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4899 if (buffer && buflen && (*buflen == sizeof (u32)))
4902 session->rcvbuf_size = *(u32 *) buffer;
4903 if (VPPCOM_DEBUG > 2)
4904 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4905 "buflen %d, #VPP-TBD#", getpid (),
4906 session->sndbuf_size, session->sndbuf_size,
4908 if (VPPCOM_DEBUG > 0)
4911 ELOG_TYPE_DECLARE (e) =
4913 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4914 .format_args = "i4i4",
4921 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4922 ed->data[0] = session->sndbuf_size;
4923 ed->data[1] = *buflen;
4931 case VPPCOM_ATTR_GET_REUSEADDR:
4932 if (buffer && buflen && (*buflen >= sizeof (int)))
4935 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4936 VCL_SESS_ATTR_REUSEADDR);
4937 *buflen = sizeof (int);
4939 if (VPPCOM_DEBUG > 2)
4940 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4941 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4943 if (VPPCOM_DEBUG > 0)
4946 ELOG_TYPE_DECLARE (e) =
4948 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4949 .format_args = "i4i4",
4956 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4957 ed->data[0] = *(int *) buffer;
4958 ed->data[1] = *buflen;
4966 case VPPCOM_ATTR_SET_REUSEADDR:
4967 if (buffer && buflen && (*buflen == sizeof (int)) &&
4968 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4971 if (*(int *) buffer)
4972 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4974 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4976 if (VPPCOM_DEBUG > 2)
4977 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4978 "buflen %d, #VPP-TBD#", getpid (),
4979 VCL_SESS_ATTR_TEST (session->attr,
4980 VCL_SESS_ATTR_REUSEADDR),
4982 if (VPPCOM_DEBUG > 0)
4985 ELOG_TYPE_DECLARE (e) =
4987 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4988 .format_args = "i4i4",
4995 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4996 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4997 VCL_SESS_ATTR_REUSEADDR);
4998 ed->data[1] = *buflen;
5006 case VPPCOM_ATTR_GET_REUSEPORT:
5007 if (buffer && buflen && (*buflen >= sizeof (int)))
5010 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5011 VCL_SESS_ATTR_REUSEPORT);
5012 *buflen = sizeof (int);
5014 if (VPPCOM_DEBUG > 2)
5015 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
5016 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5018 if (VPPCOM_DEBUG > 0)
5021 ELOG_TYPE_DECLARE (e) =
5023 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
5024 .format_args = "i4i4",
5031 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5032 ed->data[0] = *(int *) buffer;
5033 ed->data[1] = *buflen;
5041 case VPPCOM_ATTR_SET_REUSEPORT:
5042 if (buffer && buflen && (*buflen == sizeof (int)) &&
5043 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
5046 if (*(int *) buffer)
5047 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
5049 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
5051 if (VPPCOM_DEBUG > 2)
5052 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
5053 "buflen %d, #VPP-TBD#", getpid (),
5054 VCL_SESS_ATTR_TEST (session->attr,
5055 VCL_SESS_ATTR_REUSEPORT),
5057 if (VPPCOM_DEBUG > 0)
5060 ELOG_TYPE_DECLARE (e) =
5062 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5063 .format_args = "i4i4",
5070 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5071 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5072 VCL_SESS_ATTR_REUSEPORT);
5073 ed->data[1] = *buflen;
5081 case VPPCOM_ATTR_GET_BROADCAST:
5082 if (buffer && buflen && (*buflen >= sizeof (int)))
5085 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5086 VCL_SESS_ATTR_BROADCAST);
5087 *buflen = sizeof (int);
5089 if (VPPCOM_DEBUG > 2)
5090 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5091 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5093 if (VPPCOM_DEBUG > 0)
5096 ELOG_TYPE_DECLARE (e) =
5098 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5099 .format_args = "i4i4",
5106 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5107 ed->data[0] = *(int *) buffer;
5108 ed->data[1] = *buflen;
5116 case VPPCOM_ATTR_SET_BROADCAST:
5117 if (buffer && buflen && (*buflen == sizeof (int)))
5120 if (*(int *) buffer)
5121 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5123 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5125 if (VPPCOM_DEBUG > 2)
5126 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5127 "buflen %d, #VPP-TBD#", getpid (),
5128 VCL_SESS_ATTR_TEST (session->attr,
5129 VCL_SESS_ATTR_BROADCAST),
5131 if (VPPCOM_DEBUG > 0)
5134 ELOG_TYPE_DECLARE (e) =
5136 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5137 .format_args = "i4i4",
5144 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5145 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5146 VCL_SESS_ATTR_BROADCAST);
5147 ed->data[1] = *buflen;
5155 case VPPCOM_ATTR_GET_V6ONLY:
5156 if (buffer && buflen && (*buflen >= sizeof (int)))
5159 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5160 VCL_SESS_ATTR_V6ONLY);
5161 *buflen = sizeof (int);
5163 if (VPPCOM_DEBUG > 2)
5164 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5165 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5167 if (VPPCOM_DEBUG > 0)
5170 ELOG_TYPE_DECLARE (e) =
5172 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5173 .format_args = "i4i4",
5180 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5181 ed->data[0] = *(int *) buffer;
5182 ed->data[1] = *buflen;
5190 case VPPCOM_ATTR_SET_V6ONLY:
5191 if (buffer && buflen && (*buflen == sizeof (int)))
5194 if (*(int *) buffer)
5195 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5197 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5199 if (VPPCOM_DEBUG > 2)
5200 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5201 "buflen %d, #VPP-TBD#", getpid (),
5202 VCL_SESS_ATTR_TEST (session->attr,
5203 VCL_SESS_ATTR_V6ONLY), *buflen);
5204 if (VPPCOM_DEBUG > 0)
5207 ELOG_TYPE_DECLARE (e) =
5209 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5210 .format_args = "i4i4",
5217 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5218 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5219 VCL_SESS_ATTR_V6ONLY);
5220 ed->data[1] = *buflen;
5228 case VPPCOM_ATTR_GET_KEEPALIVE:
5229 if (buffer && buflen && (*buflen >= sizeof (int)))
5232 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5233 VCL_SESS_ATTR_KEEPALIVE);
5234 *buflen = sizeof (int);
5236 if (VPPCOM_DEBUG > 2)
5237 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5238 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5240 if (VPPCOM_DEBUG > 0)
5243 ELOG_TYPE_DECLARE (e) =
5245 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5246 .format_args = "i4i4",
5253 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5254 ed->data[0] = *(int *) buffer;
5255 ed->data[1] = *buflen;
5263 case VPPCOM_ATTR_SET_KEEPALIVE:
5264 if (buffer && buflen && (*buflen == sizeof (int)))
5267 if (*(int *) buffer)
5268 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5270 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5272 if (VPPCOM_DEBUG > 2)
5273 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5274 "buflen %d, #VPP-TBD#", getpid (),
5275 VCL_SESS_ATTR_TEST (session->attr,
5276 VCL_SESS_ATTR_KEEPALIVE),
5278 if (VPPCOM_DEBUG > 0)
5281 ELOG_TYPE_DECLARE (e) =
5283 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5284 .format_args = "i4i4",
5291 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5292 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5293 VCL_SESS_ATTR_KEEPALIVE);
5294 ed->data[1] = *buflen;
5302 case VPPCOM_ATTR_GET_TCP_NODELAY:
5303 if (buffer && buflen && (*buflen >= sizeof (int)))
5306 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5307 VCL_SESS_ATTR_TCP_NODELAY);
5308 *buflen = sizeof (int);
5310 if (VPPCOM_DEBUG > 2)
5311 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5312 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5314 if (VPPCOM_DEBUG > 0)
5317 ELOG_TYPE_DECLARE (e) =
5319 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5320 .format_args = "i4i4",
5327 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5328 ed->data[0] = *(int *) buffer;
5329 ed->data[1] = *buflen;
5337 case VPPCOM_ATTR_SET_TCP_NODELAY:
5338 if (buffer && buflen && (*buflen == sizeof (int)))
5341 if (*(int *) buffer)
5342 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5344 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5346 if (VPPCOM_DEBUG > 2)
5347 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5348 "buflen %d, #VPP-TBD#", getpid (),
5349 VCL_SESS_ATTR_TEST (session->attr,
5350 VCL_SESS_ATTR_TCP_NODELAY),
5352 if (VPPCOM_DEBUG > 0)
5355 ELOG_TYPE_DECLARE (e) =
5357 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5358 .format_args = "i4i4",
5365 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5366 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5367 VCL_SESS_ATTR_TCP_NODELAY);
5368 ed->data[1] = *buflen;
5376 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5377 if (buffer && buflen && (*buflen >= sizeof (int)))
5380 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5381 VCL_SESS_ATTR_TCP_KEEPIDLE);
5382 *buflen = sizeof (int);
5384 if (VPPCOM_DEBUG > 2)
5385 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5386 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5388 if (VPPCOM_DEBUG > 0)
5391 ELOG_TYPE_DECLARE (e) =
5393 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5394 .format_args = "i4i4",
5401 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5402 ed->data[0] = *(int *) buffer;
5403 ed->data[1] = *buflen;
5411 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5412 if (buffer && buflen && (*buflen == sizeof (int)))
5415 if (*(int *) buffer)
5416 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5418 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5420 if (VPPCOM_DEBUG > 2)
5421 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5422 "buflen %d, #VPP-TBD#", getpid (),
5423 VCL_SESS_ATTR_TEST (session->attr,
5424 VCL_SESS_ATTR_TCP_KEEPIDLE),
5426 if (VPPCOM_DEBUG > 0)
5429 ELOG_TYPE_DECLARE (e) =
5431 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5432 .format_args = "i4i4",
5439 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5440 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5441 VCL_SESS_ATTR_TCP_KEEPIDLE);
5442 ed->data[1] = *buflen;
5450 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5451 if (buffer && buflen && (*buflen >= sizeof (int)))
5454 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5455 VCL_SESS_ATTR_TCP_KEEPINTVL);
5456 *buflen = sizeof (int);
5458 if (VPPCOM_DEBUG > 2)
5459 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5460 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5462 if (VPPCOM_DEBUG > 0)
5465 ELOG_TYPE_DECLARE (e) =
5467 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5468 .format_args = "i4i4",
5475 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5476 ed->data[0] = *(int *) buffer;
5477 ed->data[1] = *buflen;
5485 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5486 if (buffer && buflen && (*buflen == sizeof (int)))
5489 if (*(int *) buffer)
5490 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5492 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5494 if (VPPCOM_DEBUG > 2)
5495 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5496 "buflen %d, #VPP-TBD#", getpid (),
5497 VCL_SESS_ATTR_TEST (session->attr,
5498 VCL_SESS_ATTR_TCP_KEEPINTVL),
5500 if (VPPCOM_DEBUG > 0)
5503 ELOG_TYPE_DECLARE (e) =
5505 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5506 .format_args = "i4i4",
5513 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5514 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5515 VCL_SESS_ATTR_TCP_KEEPINTVL);
5516 ed->data[1] = *buflen;
5524 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5525 if (buffer && buflen && (*buflen >= sizeof (u32)))
5528 *(u32 *) buffer = session->user_mss;
5529 *buflen = sizeof (int);
5531 if (VPPCOM_DEBUG > 2)
5532 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5533 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5535 if (VPPCOM_DEBUG > 0)
5538 ELOG_TYPE_DECLARE (e) =
5540 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5541 .format_args = "i4i4",
5548 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5549 ed->data[0] = *(int *) buffer;
5550 ed->data[1] = *buflen;
5558 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5559 if (buffer && buflen && (*buflen == sizeof (u32)))
5562 session->user_mss = *(u32 *) buffer;
5564 if (VPPCOM_DEBUG > 2)
5565 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5566 "buflen %d, #VPP-TBD#", getpid (),
5567 session->user_mss, *buflen);
5568 if (VPPCOM_DEBUG > 0)
5571 ELOG_TYPE_DECLARE (e) =
5573 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5574 .format_args = "i4i4",
5581 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5582 ed->data[0] = session->user_mss;
5583 ed->data[1] = *buflen;
5597 clib_spinlock_unlock (&vcm->sessions_lockp);
5602 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5603 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5606 session_t *session = 0;
5610 clib_spinlock_lock (&vcm->sessions_lockp);
5611 rv = vppcom_session_at_index (session_index, &session);
5612 if (PREDICT_FALSE (rv))
5614 clib_spinlock_unlock (&vcm->sessions_lockp);
5615 if (VPPCOM_DEBUG > 0)
5616 clib_warning ("VCL<%d>: invalid session, "
5617 "sid (%u) has been closed!",
5618 getpid (), session_index);
5619 if (VPPCOM_DEBUG > 0)
5622 ELOG_TYPE_DECLARE (e) =
5624 .format = "invalid session: %d closed",
5625 .format_args = "i4",
5632 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5633 ed->data = session_index;
5637 clib_spinlock_unlock (&vcm->sessions_lockp);
5640 ep->is_ip4 = session->peer_addr.is_ip4;
5641 ep->port = session->peer_port;
5642 if (session->peer_addr.is_ip4)
5643 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5644 sizeof (ip4_address_t));
5646 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5647 sizeof (ip6_address_t));
5648 clib_spinlock_unlock (&vcm->sessions_lockp);
5652 rv = vppcom_session_read (session_index, buffer, buflen);
5653 else if (flags & MSG_PEEK)
5654 rv = vppcom_session_peek (session_index, buffer, buflen);
5657 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5659 rv = VPPCOM_EAFNOSUPPORT;
5667 vppcom_session_sendto (uint32_t session_index, void *buffer,
5668 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5671 return VPPCOM_EINVAL;
5676 return VPPCOM_EINVAL;
5681 // TBD check the flags and do the right thing
5682 if (VPPCOM_DEBUG > 2)
5683 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5684 "not implemented yet.", getpid (), flags, flags);
5687 return (vppcom_session_write (session_index, buffer, buflen));
5691 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5693 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5694 u32 i, keep_trying = 1;
5697 if (VPPCOM_DEBUG > 3)
5698 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5699 getpid (), vp, n_sids, wait_for_time);
5702 return VPPCOM_EFAULT;
5708 for (i = 0; i < n_sids; i++)
5710 ASSERT (vp[i].revents);
5712 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5713 clib_spinlock_unlock (&vcm->sessions_lockp);
5718 if (POLLIN & vp[i].events)
5720 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5721 rv = vppcom_session_read_ready (session, vp[i].sid);
5722 clib_spinlock_unlock (&vcm->sessions_lockp);
5725 *vp[i].revents |= POLLIN;
5732 case VPPCOM_ECONNRESET:
5733 *vp[i].revents = POLLHUP;
5737 *vp[i].revents = POLLERR;
5744 if (POLLOUT & vp[i].events)
5746 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5747 rv = vppcom_session_write_ready (session, vp[i].sid);
5748 clib_spinlock_unlock (&vcm->sessions_lockp);
5751 *vp[i].revents |= POLLOUT;
5758 case VPPCOM_ECONNRESET:
5759 *vp[i].revents = POLLHUP;
5763 *vp[i].revents = POLLERR;
5773 *vp[i].revents = POLLNVAL;
5777 if (wait_for_time != -1)
5778 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5780 while ((num_ev == 0) && keep_trying);
5782 if (VPPCOM_DEBUG > 3)
5784 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5785 for (i = 0; i < n_sids; i++)
5787 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5788 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5789 vp[i].events, *vp[i].revents);
5796 * fd.io coding-style-patch-verification: ON
5799 * eval: (c-set-style "gnu")