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; */
161 vppcom_ip46_t lcl_addr;
162 vppcom_ip46_t peer_addr;
163 u16 lcl_port; // network order
164 u16 peer_port; // network order
166 u64 client_queue_address;
168 elog_track_t elog_track;
171 typedef struct vppcom_cfg_t_
174 u32 vpp_api_q_length;
177 u32 add_segment_size;
178 u32 preallocated_fifo_pairs;
181 u32 event_queue_size;
182 u32 listen_queue_size;
183 u8 app_proxy_transport_tcp;
184 u8 app_proxy_transport_udp;
188 u64 namespace_secret;
193 char *event_log_path;
196 typedef struct vppcom_main_t_
200 u32 *client_session_index_fifo;
203 /* vpe 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;
429 if (VPPCOM_DEBUG > 0)
430 clib_warning ("VCL<%d>: app (%s) connecting to VPP api...",
431 getpid (), app_name);
433 if (vl_client_connect_to_vlib ("/vpe-api", app_name,
434 vcm->cfg.vpp_api_q_length) < 0)
436 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
437 rv = VPPCOM_ECONNREFUSED;
441 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
442 vcm->my_client_index = am->my_client_index;
443 vcm->app_state = STATE_APP_CONN_VPP;
445 if (VPPCOM_DEBUG > 0)
446 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
447 getpid (), app_name);
450 if (VPPCOM_DEBUG > 0)
452 vcm->elog_main.lock =
453 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
454 vcm->elog_main.lock[0] = 0;
455 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
456 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
457 elog_enable_disable (&vcm->elog_main, 1);
459 vcm->elog_track.name =
460 (char *) format (0, "P:%d:C:%d%c", getpid (),
461 vcm->my_client_index, 0);
462 elog_track_register (&vcm->elog_main, &vcm->elog_track);
465 ELOG_TYPE_DECLARE (e) =
467 .format = "connect_vpp:rv:%d",
474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
482 format_api_error (u8 * s, va_list * args)
484 i32 error = va_arg (*args, u32);
487 p = hash_get (vcm->error_string_by_error_number, -error);
490 s = format (s, "%s (%d)", p[0], error);
492 s = format (s, "%d", error);
497 vppcom_init_error_string_table (void)
499 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
501 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
502 foreach_vnet_api_error;
505 hash_set (vcm->error_string_by_error_number, 99, "Misc");
509 vppcom_wait_for_app_state_change (app_state_t app_state)
511 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
513 while (clib_time_now (&vcm->clib_time) < timeout)
515 if (vcm->app_state == app_state)
518 if (VPPCOM_DEBUG > 0)
519 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
520 vppcom_app_state_str (app_state), app_state);
522 if (VPPCOM_DEBUG > 0)
525 ELOG_TYPE_DECLARE (e) =
527 .format = "ERR: timeout state:%d",
535 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
537 ed->data = app_state;
541 return VPPCOM_ETIMEDOUT;
545 vppcom_wait_for_session_state_change (u32 session_index,
546 session_state_t state,
549 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
550 session_t *volatile session;
555 clib_spinlock_lock (&vcm->sessions_lockp);
556 rv = vppcom_session_at_index (session_index, &session);
557 if (PREDICT_FALSE (rv))
559 clib_spinlock_unlock (&vcm->sessions_lockp);
562 if (session->state == state)
564 clib_spinlock_unlock (&vcm->sessions_lockp);
567 if (session->state == STATE_FAILED)
569 clib_spinlock_unlock (&vcm->sessions_lockp);
570 return VPPCOM_ECONNREFUSED;
573 clib_spinlock_unlock (&vcm->sessions_lockp);
575 while (clib_time_now (&vcm->clib_time) < timeout);
577 if (VPPCOM_DEBUG > 0)
578 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
579 state, vppcom_session_state_str (state));
581 if (VPPCOM_DEBUG > 0)
584 ELOG_TYPE_DECLARE (e) =
586 .format = "ERR: timeout state:%d",
594 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
600 return VPPCOM_ETIMEDOUT;
604 vppcom_wait_for_client_session_index (f64 wait_for_time)
606 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
610 if (clib_fifo_elts (vcm->client_session_index_fifo))
613 while (clib_time_now (&vcm->clib_time) < timeout);
615 if (wait_for_time == 0)
616 return VPPCOM_EAGAIN;
618 if (VPPCOM_DEBUG > 0)
619 clib_warning ("VCL<%d>: timeout waiting for client_session_index",
622 if (VPPCOM_DEBUG > 0)
625 ELOG_TYPE_DECLARE (e) =
627 .format = "ERR: timeout waiting for session index :%d",
635 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
641 return VPPCOM_ETIMEDOUT;
645 * VPP-API message functions
648 vppcom_send_session_enable_disable (u8 is_enable)
650 vl_api_session_enable_disable_t *bmp;
651 bmp = vl_msg_api_alloc (sizeof (*bmp));
652 memset (bmp, 0, sizeof (*bmp));
654 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
655 bmp->client_index = vcm->my_client_index;
656 bmp->context = htonl (0xfeedface);
657 bmp->is_enable = is_enable;
658 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
662 vppcom_app_session_enable (void)
666 if (vcm->app_state != STATE_APP_ENABLED)
668 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
669 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
670 if (PREDICT_FALSE (rv))
672 if (VPPCOM_DEBUG > 0)
673 clib_warning ("VCL<%d>: application session enable timed out! "
675 getpid (), rv, vppcom_retval_str (rv));
683 vl_api_session_enable_disable_reply_t_handler
684 (vl_api_session_enable_disable_reply_t * mp)
688 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
689 format_api_error, ntohl (mp->retval));
692 vcm->app_state = STATE_APP_ENABLED;
696 vppcom_app_send_attach (void)
698 vl_api_application_attach_t *bmp;
699 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
700 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
701 vcm->cfg.app_proxy_transport_udp);
703 bmp = vl_msg_api_alloc (sizeof (*bmp));
704 memset (bmp, 0, sizeof (*bmp));
706 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
707 bmp->client_index = vcm->my_client_index;
708 bmp->context = htonl (0xfeedface);
709 bmp->options[APP_OPTIONS_FLAGS] =
710 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
711 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
712 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
713 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
714 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
715 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
716 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
717 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
718 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
719 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
720 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
721 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
722 vcm->cfg.preallocated_fifo_pairs;
723 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
726 bmp->namespace_id_len = nsid_len;
727 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
728 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
730 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
734 vppcom_app_attach (void)
738 vppcom_app_send_attach ();
739 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
740 if (PREDICT_FALSE (rv))
742 if (VPPCOM_DEBUG > 0)
743 clib_warning ("VCL<%d>: application attach timed out! "
745 getpid (), rv, vppcom_retval_str (rv));
752 vppcom_app_detach (void)
754 vl_api_application_detach_t *bmp;
755 bmp = vl_msg_api_alloc (sizeof (*bmp));
756 memset (bmp, 0, sizeof (*bmp));
758 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
759 bmp->client_index = vcm->my_client_index;
760 bmp->context = htonl (0xfeedface);
761 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
765 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
768 static svm_fifo_segment_create_args_t _a;
769 svm_fifo_segment_create_args_t *a = &_a;
772 memset (a, 0, sizeof (*a));
775 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
776 format_api_error, ntohl (mp->retval));
780 if (mp->segment_name_length == 0)
782 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
786 a->segment_name = (char *) mp->segment_name;
787 a->segment_size = mp->segment_size;
789 ASSERT (mp->app_event_queue_address);
791 /* Attach to the segment vpp created */
792 rv = svm_fifo_segment_attach (a);
793 vec_reset_length (a->new_segment_indices);
794 if (PREDICT_FALSE (rv))
796 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
797 getpid (), mp->segment_name);
801 vcm->app_event_queue =
802 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
804 vcm->app_state = STATE_APP_ATTACHED;
808 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
812 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
815 vcm->app_state = STATE_APP_ENABLED;
819 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
823 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
824 getpid (), mp->handle, format_api_error,
829 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
831 static svm_fifo_segment_create_args_t _a;
832 svm_fifo_segment_create_args_t *a = &_a;
835 memset (a, 0, sizeof (*a));
836 a->segment_name = (char *) mp->segment_name;
837 a->segment_size = mp->segment_size;
838 /* Attach to the segment vpp created */
839 rv = svm_fifo_segment_attach (a);
840 vec_reset_length (a->new_segment_indices);
841 if (PREDICT_FALSE (rv))
843 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
844 getpid (), mp->segment_name);
847 if (VPPCOM_DEBUG > 1)
848 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
849 mp->segment_name, mp->segment_size);
853 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
857 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
861 session_t *session = 0;
862 u32 session_index = p[0];
864 VCL_LOCK_AND_GET_SESSION (session_index, &session);
865 session->state = STATE_CLOSE_ON_EMPTY;
867 if (VPPCOM_DEBUG > 1)
868 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
869 "setting state to 0x%x (%s)",
870 getpid (), mp->handle, session_index, session->state,
871 vppcom_session_state_str (session->state));
872 clib_spinlock_unlock (&vcm->sessions_lockp);
876 if (VPPCOM_DEBUG > 1)
877 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
878 "session lookup failed!",
879 getpid (), mp->handle, session_index);
882 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
883 "handle failed!", getpid (), mp->handle);
887 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
889 session_t *session = 0;
890 vl_api_reset_session_reply_t *rmp;
894 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
898 clib_spinlock_lock (&vcm->sessions_lockp);
899 rval = vppcom_session_at_index (p[0], &session);
900 if (PREDICT_FALSE (rval))
902 rv = VNET_API_ERROR_INVALID_VALUE_2;
903 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
904 "session lookup failed! returning %d %U",
905 getpid (), mp->handle, p[0],
906 rv, format_api_error, rv);
910 /* TBD: should this disconnect immediately and
913 session->state = STATE_CLOSE_ON_EMPTY;
915 if (VPPCOM_DEBUG > 1)
916 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
917 "state set to %d (%s)!", getpid (),
918 mp->handle, p[0], session->state,
919 vppcom_session_state_str (session->state));
921 clib_spinlock_unlock (&vcm->sessions_lockp);
925 rv = VNET_API_ERROR_INVALID_VALUE;
926 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
927 "failed! returning %d %U",
928 getpid (), mp->handle, rv, format_api_error, rv);
931 rmp = vl_msg_api_alloc (sizeof (*rmp));
932 memset (rmp, 0, sizeof (*rmp));
933 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
934 rmp->retval = htonl (rv);
935 rmp->handle = mp->handle;
936 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
940 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
942 session_t *session = 0;
944 svm_fifo_t *rx_fifo, *tx_fifo;
948 session_index = mp->context;
949 VCL_LOCK_AND_GET_SESSION (session_index, &session);
953 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
954 "connect failed! %U",
955 getpid (), mp->handle, session_index,
956 format_api_error, ntohl (mp->retval));
959 session->state = STATE_FAILED;
960 session->vpp_handle = mp->handle;
964 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
965 "Invalid session index (%u)!",
966 getpid (), mp->handle, session_index);
974 /* We've been redirected */
975 if (mp->segment_name_length > 0)
977 static svm_fifo_segment_create_args_t _a;
978 svm_fifo_segment_create_args_t *a = &_a;
981 memset (a, 0, sizeof (*a));
982 a->segment_name = (char *) mp->segment_name;
983 if (VPPCOM_DEBUG > 1)
984 clib_warning ("VCL<%d>: cut-thru segment: %s\n",
985 getpid (), a->segment_name);
987 rv = svm_fifo_segment_attach (a);
988 vec_reset_length (a->new_segment_indices);
989 if (PREDICT_FALSE (rv))
991 clib_warning ("VCL<%d>: sm_fifo_segment_attach ('%s') failed",
992 getpid (), a->segment_name);
1000 session->is_cut_thru = is_cut_thru;
1001 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1004 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1005 rx_fifo->client_session_index = session_index;
1006 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1007 tx_fifo->client_session_index = session_index;
1009 session->server_rx_fifo = rx_fifo;
1010 session->server_tx_fifo = tx_fifo;
1011 session->vpp_handle = mp->handle;
1012 session->lcl_addr.is_ip4 = mp->is_ip4;
1013 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1014 sizeof (session->peer_addr.ip46));
1015 session->lcl_port = mp->lcl_port;
1016 session->state = STATE_CONNECT;
1018 /* Add it to lookup table */
1019 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1021 if (VPPCOM_DEBUG > 1)
1022 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1023 " session_rx_fifo %p, refcnt %d,"
1024 " session_tx_fifo %p, refcnt %d",
1025 getpid (), mp->handle, session_index,
1026 session->server_rx_fifo,
1027 session->server_rx_fifo->refcnt,
1028 session->server_tx_fifo, session->server_tx_fifo->refcnt);
1030 clib_spinlock_unlock (&vcm->sessions_lockp);
1034 vppcom_send_connect_sock (session_t * session, u32 session_index)
1036 vl_api_connect_sock_t *cmp;
1038 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1039 session->is_server = 0;
1040 cmp = vl_msg_api_alloc (sizeof (*cmp));
1041 memset (cmp, 0, sizeof (*cmp));
1042 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1043 cmp->client_index = vcm->my_client_index;
1044 cmp->context = session_index;
1046 cmp->vrf = session->vrf;
1047 cmp->is_ip4 = session->peer_addr.is_ip4;
1048 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1049 cmp->port = session->peer_port;
1050 cmp->proto = session->proto;
1051 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1052 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1056 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1059 vl_api_disconnect_session_reply_t *rmp;
1061 if (VPPCOM_DEBUG > 1)
1062 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1063 "sending disconnect msg",
1064 getpid (), vpp_handle, session_index);
1066 rmp = vl_msg_api_alloc (sizeof (*rmp));
1067 memset (rmp, 0, sizeof (*rmp));
1069 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1070 rmp->retval = htonl (rv);
1071 rmp->handle = vpp_handle;
1072 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1076 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1078 vl_api_disconnect_session_t *dmp;
1080 if (VPPCOM_DEBUG > 1)
1081 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1082 "sending disconnect msg",
1083 getpid (), vpp_handle, session_index);
1085 dmp = vl_msg_api_alloc (sizeof (*dmp));
1086 memset (dmp, 0, sizeof (*dmp));
1087 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1088 dmp->client_index = vcm->my_client_index;
1089 dmp->handle = vpp_handle;
1090 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1094 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1096 session_t *session = 0;
1097 u32 session_index = mp->context;
1100 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1104 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1105 "sid %u: bind failed: %U",
1106 getpid (), mp->handle, session_index,
1107 format_api_error, ntohl (mp->retval));
1108 rv = vppcom_session_at_index (session_index, &session);
1109 if (rv == VPPCOM_OK)
1111 session->state = STATE_FAILED;
1112 session->vpp_handle = mp->handle;
1116 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1117 "Invalid session index (%u)!",
1118 getpid (), mp->handle, session_index);
1123 session->vpp_handle = mp->handle;
1124 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1125 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1126 sizeof (session->peer_addr.ip46));
1127 session->lcl_port = mp->lcl_port;
1128 vppcom_session_table_add_listener (mp->handle, session_index);
1129 session->is_listen = 1;
1130 session->state = STATE_LISTEN;
1132 if (VPPCOM_DEBUG > 1)
1133 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1134 getpid (), mp->handle, mp->context);
1136 clib_spinlock_unlock (&vcm->sessions_lockp);
1140 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1143 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1144 getpid (), mp->context, format_api_error,
1145 ntohl (mp->retval));
1147 else if (VPPCOM_DEBUG > 1)
1148 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1149 getpid (), mp->context);
1153 format_ip4_address (u8 * s, va_list * args)
1155 u8 *a = va_arg (*args, u8 *);
1156 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1160 format_ip6_address (u8 * s, va_list * args)
1162 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1163 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1165 i_max_n_zero = ARRAY_LEN (a->as_u16);
1167 i_first_zero = i_max_n_zero;
1169 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1171 u32 is_zero = a->as_u16[i] == 0;
1172 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1178 if ((!is_zero && n_zeros > max_n_zeros)
1179 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1181 i_max_n_zero = i_first_zero;
1182 max_n_zeros = n_zeros;
1183 i_first_zero = ARRAY_LEN (a->as_u16);
1188 last_double_colon = 0;
1189 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1191 if (i == i_max_n_zero && max_n_zeros > 1)
1193 s = format (s, "::");
1194 i += max_n_zeros - 1;
1195 last_double_colon = 1;
1199 s = format (s, "%s%x",
1200 (last_double_colon || i == 0) ? "" : ":",
1201 clib_net_to_host_u16 (a->as_u16[i]));
1202 last_double_colon = 0;
1209 /* Format an IP46 address. */
1211 format_ip46_address (u8 * s, va_list * args)
1213 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1214 ip46_type_t type = va_arg (*args, ip46_type_t);
1220 is_ip4 = ip46_address_is_ip4 (ip46);
1231 format (s, "%U", format_ip4_address, &ip46->ip4) :
1232 format (s, "%U", format_ip6_address, &ip46->ip6);
1236 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1238 vl_api_accept_session_reply_t *rmp;
1240 rmp = vl_msg_api_alloc (sizeof (*rmp));
1241 memset (rmp, 0, sizeof (*rmp));
1242 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1243 rmp->retval = htonl (retval);
1244 rmp->context = context;
1245 rmp->handle = handle;
1246 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1250 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1252 svm_fifo_t *rx_fifo, *tx_fifo;
1253 session_t *session, *listen_session;
1256 clib_spinlock_lock (&vcm->sessions_lockp);
1257 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1259 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1260 vppcom_send_accept_session_reply (mp->handle, mp->context,
1261 VNET_API_ERROR_QUEUE_FULL);
1262 clib_spinlock_unlock (&vcm->sessions_lockp);
1266 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1267 if (!listen_session)
1269 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1270 "unknown vpp listener handle %llx",
1271 getpid (), mp->listener_handle);
1272 clib_spinlock_unlock (&vcm->sessions_lockp);
1276 /* Allocate local session and set it up */
1277 pool_get (vcm->sessions, session);
1278 memset (session, 0, sizeof (*session));
1279 session_index = session - vcm->sessions;
1281 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1282 rx_fifo->client_session_index = session_index;
1283 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1284 tx_fifo->client_session_index = session_index;
1286 session->vpp_handle = mp->handle;
1287 session->client_context = mp->context;
1288 session->server_rx_fifo = rx_fifo;
1289 session->server_tx_fifo = tx_fifo;
1290 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1292 session->state = STATE_ACCEPT;
1293 session->is_cut_thru = 0;
1294 session->is_server = 1;
1295 session->peer_port = mp->port;
1296 session->peer_addr.is_ip4 = mp->is_ip4;
1297 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1298 sizeof (session->peer_addr.ip46));
1300 /* Add it to lookup table */
1301 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1302 session->lcl_port = listen_session->lcl_port;
1303 session->lcl_addr = listen_session->lcl_addr;
1305 /* TBD: move client_session_index_fifo into listener session */
1306 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1308 clib_spinlock_unlock (&vcm->sessions_lockp);
1310 if (VPPCOM_DEBUG > 1)
1311 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1312 "request from %s address %U port %d queue %p!", getpid (),
1313 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1314 format_ip46_address, &mp->ip, mp->is_ip4,
1315 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1317 if (VPPCOM_DEBUG > 0)
1319 session->elog_track.name =
1320 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1322 elog_track_register (&vcm->elog_main, &session->elog_track);
1324 if (session->peer_addr.is_ip4)
1327 ELOG_TYPE_DECLARE (e) =
1330 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1331 .format_args = "i8i1i1i1i1i2",
1334 CLIB_PACKED (struct {
1340 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1342 ed->handle = mp->handle;
1343 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1344 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1345 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1346 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1347 ed->port = clib_net_to_host_u16 (session->peer_port);
1352 clib_warning ("ip6");
1359 vppcom_send_connect_session_reply (session_t * session, u32 session_index,
1360 u64 vpp_handle, u32 context, int retval)
1362 vl_api_connect_session_reply_t *rmp;
1364 svm_queue_t *client_q;
1366 rmp = vl_msg_api_alloc (sizeof (*rmp));
1367 memset (rmp, 0, sizeof (*rmp));
1368 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1372 rmp->context = context;
1373 rmp->handle = vpp_handle;
1374 rmp->retval = htonl (retval);
1375 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1379 rmp->context = session->client_context;
1380 rmp->retval = htonl (retval);
1381 rmp->handle = session->vpp_handle;
1382 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1383 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1384 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1385 rmp->segment_size = vcm->cfg.segment_size;
1386 len = vec_len (session->segment_name);
1387 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1388 clib_memcpy (rmp->segment_name, session->segment_name,
1389 rmp->segment_name_length - 1);
1390 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
1391 sizeof (rmp->lcl_ip));
1392 rmp->is_ip4 = session->peer_addr.is_ip4;
1393 rmp->lcl_port = session->peer_port;
1394 client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
1396 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1400 * Acting as server for redirected connect requests
1403 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1406 session_t *session = 0;
1408 clib_spinlock_lock (&vcm->sessions_lockp);
1409 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1411 clib_spinlock_unlock (&vcm->sessions_lockp);
1413 if (VPPCOM_DEBUG > 1)
1414 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1416 /* TBD: Fix api to include vpp handle */
1417 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1418 0 /* handle */ , mp->context,
1419 VNET_API_ERROR_QUEUE_FULL);
1423 pool_get (vcm->sessions, session);
1424 memset (session, 0, sizeof (*session));
1425 session_index = session - vcm->sessions;
1427 session->client_context = mp->context;
1428 session->vpp_handle = session_index;
1429 session->client_queue_address = mp->client_queue_address;
1430 session->is_cut_thru = 1;
1431 session->is_server = 1;
1432 session->lcl_port = mp->port;
1433 session->lcl_addr.is_ip4 = mp->is_ip4;
1434 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1435 sizeof (session->lcl_addr.ip46));
1437 /* TBD: missing peer info in api msg.
1439 session->peer_addr.is_ip4 = mp->is_ip4;
1440 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
1442 session->state = STATE_ACCEPT;
1443 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1444 if (VPPCOM_DEBUG > 1)
1445 clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
1446 "clib_fifo_elts %u!\n", getpid (), session_index,
1447 clib_fifo_elts (vcm->client_session_index_fifo));
1449 if (VPPCOM_DEBUG > 0)
1451 session->elog_track.name =
1452 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1454 elog_track_register (&vcm->elog_main, &session->elog_track);
1457 ELOG_TYPE_DECLARE (e) =
1459 .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1460 .format_args = "i4i4",
1468 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1470 ed->data[0] = session_index;
1471 ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
1475 clib_spinlock_unlock (&vcm->sessions_lockp);
1479 vppcom_send_bind_sock (session_t * session, u32 session_index)
1481 vl_api_bind_sock_t *bmp;
1483 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1484 session->is_server = 1;
1485 bmp = vl_msg_api_alloc (sizeof (*bmp));
1486 memset (bmp, 0, sizeof (*bmp));
1488 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1489 bmp->client_index = vcm->my_client_index;
1490 bmp->context = session_index;
1491 bmp->vrf = session->vrf;
1492 bmp->is_ip4 = session->lcl_addr.is_ip4;
1493 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1494 bmp->port = session->lcl_port;
1495 bmp->proto = session->proto;
1496 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1497 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1501 vppcom_send_unbind_sock (u64 vpp_handle)
1503 vl_api_unbind_sock_t *ump;
1505 ump = vl_msg_api_alloc (sizeof (*ump));
1506 memset (ump, 0, sizeof (*ump));
1508 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1509 ump->client_index = vcm->my_client_index;
1510 ump->handle = vpp_handle;
1511 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1515 vppcom_session_unbind (u32 session_index)
1517 session_t *session = 0;
1521 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1523 vpp_handle = session->vpp_handle;
1524 vppcom_session_table_del_listener (vpp_handle);
1525 session->vpp_handle = ~0;
1526 session->state = STATE_DISCONNECT;
1528 clib_spinlock_unlock (&vcm->sessions_lockp);
1530 if (VPPCOM_DEBUG > 1)
1531 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1532 "sending unbind msg! new state 0x%x (%s)",
1533 getpid (), vpp_handle, session_index,
1534 session->state, vppcom_session_state_str (session->state));
1536 if (VPPCOM_DEBUG > 0)
1539 ELOG_TYPE_DECLARE (e) =
1541 .format = "unbind: handle:%x",
1542 .format_args = "i8",
1550 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1551 ed->handle = vpp_handle;
1554 vppcom_send_unbind_sock (vpp_handle);
1561 vppcom_session_disconnect (u32 session_index)
1565 u8 is_cut_thru, is_listen, is_server;
1567 session_state_t state;
1569 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1571 vpp_handle = session->vpp_handle;
1572 is_server = session->is_server;
1573 is_listen = session->is_listen;
1574 is_cut_thru = session->is_cut_thru;
1575 state = session->state;
1576 clib_spinlock_unlock (&vcm->sessions_lockp);
1578 if (VPPCOM_DEBUG > 1)
1580 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s "
1581 "state 0x%x (%s), is_cut_thru %d, is_listen %d",
1582 getpid (), vpp_handle, session_index,
1583 is_server ? "server" : "client",
1584 state, vppcom_session_state_str (state),
1585 is_cut_thru, is_listen);
1588 if (PREDICT_FALSE (is_listen))
1590 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1591 "Cannot disconnect a listen socket!",
1592 getpid (), vpp_handle, session_index);
1597 /* Through the VPP host stack...
1599 else if (!is_cut_thru)
1601 /* The peer has already initiated the close,
1602 * so send the disconnect session reply.
1604 if (state & STATE_CLOSE_ON_EMPTY)
1606 vppcom_send_disconnect_session_reply (vpp_handle,
1607 session_index, 0 /* rv */ );
1608 if (VPPCOM_DEBUG > 1)
1609 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1610 "sending disconnect REPLY...",
1611 getpid (), vpp_handle, session_index);
1614 /* Otherwise, send a disconnect session msg...
1618 if (VPPCOM_DEBUG > 1)
1619 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1620 "sending disconnect...",
1621 getpid (), vpp_handle, session_index);
1623 vppcom_send_disconnect_session (vpp_handle, session_index);
1627 /* Cut-thru connections...
1629 * server: free fifos and segment allocated during connect/redirect
1630 * client: no cleanup required
1636 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1637 svm_fifo_segment_private_t *seg;
1639 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1641 if (VPPCOM_DEBUG > 1)
1642 clib_warning ("VCL<%d>: sid %d: freeing cut-thru fifos in "
1644 " server_rx_fifo %p, refcnt = %d"
1645 " server_tx_fifo %p, refcnt = %d",
1646 getpid (), session_index, session->sm_seg_index,
1647 session->server_rx_fifo,
1648 session->server_rx_fifo->refcnt,
1649 session->server_tx_fifo,
1650 session->server_tx_fifo->refcnt);
1652 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1653 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1654 FIFO_SEGMENT_RX_FREELIST);
1655 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1656 FIFO_SEGMENT_TX_FREELIST);
1657 svm_fifo_segment_delete (seg);
1659 /* TBD: Send cut-thru disconnect event to client */
1661 clib_spinlock_unlock (&vcm->sessions_lockp);
1665 /* TBD: Send cut-thru disconnect event to server */
1673 #define foreach_sock_msg \
1674 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1675 _(BIND_SOCK_REPLY, bind_sock_reply) \
1676 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1677 _(ACCEPT_SESSION, accept_session) \
1678 _(CONNECT_SOCK, connect_sock) \
1679 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1680 _(DISCONNECT_SESSION, disconnect_session) \
1681 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1682 _(RESET_SESSION, reset_session) \
1683 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1684 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1685 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1688 vppcom_api_hookup (void)
1691 vl_msg_api_set_handlers(VL_API_##N, #n, \
1692 vl_api_##n##_t_handler, \
1694 vl_api_##n##_t_endian, \
1695 vl_api_##n##_t_print, \
1696 sizeof(vl_api_##n##_t), 1);
1702 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1706 vcl_cfg->heapsize = (256ULL << 20);
1707 vcl_cfg->vpp_api_q_length = 1024;
1708 vcl_cfg->segment_baseva = 0x200000000ULL;
1709 vcl_cfg->segment_size = (256 << 20);
1710 vcl_cfg->add_segment_size = (128 << 20);
1711 vcl_cfg->preallocated_fifo_pairs = 8;
1712 vcl_cfg->rx_fifo_size = (1 << 20);
1713 vcl_cfg->tx_fifo_size = (1 << 20);
1714 vcl_cfg->event_queue_size = 2048;
1715 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1716 vcl_cfg->app_timeout = 10 * 60.0;
1717 vcl_cfg->session_timeout = 10 * 60.0;
1718 vcl_cfg->accept_timeout = 60.0;
1719 vcl_cfg->event_ring_size = (128 << 10);
1720 vcl_cfg->event_log_path = "/dev/shm";
1724 vppcom_cfg_heapsize (char *conf_fname)
1726 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1739 fp = fopen (conf_fname, "r");
1742 if (VPPCOM_DEBUG > 0)
1743 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1746 argv = calloc (1, sizeof (char *));
1752 if (fgets (inbuf, 4096, fp) == 0)
1754 p = strtok (inbuf, " \t\n");
1760 char **tmp = realloc (argv, argc * sizeof (char *));
1764 arg = strndup (p, 1024);
1767 argv[argc - 1] = arg;
1768 p = strtok (NULL, " \t\n");
1775 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1782 * Look for and parse the "heapsize" config parameter.
1783 * Manual since none of the clib infra has been bootstrapped yet.
1785 * Format: heapsize <nn>[mM][gG]
1788 for (i = 1; i < (argc - 1); i++)
1790 if (!strncmp (argv[i], "heapsize", 8))
1792 sizep = (u8 *) argv[i + 1];
1794 while (*sizep >= '0' && *sizep <= '9')
1797 size += *sizep++ - '0';
1801 if (VPPCOM_DEBUG > 0)
1802 clib_warning ("VCL<%d>: parse error '%s %s', "
1803 "using default heapsize %lld (0x%llx)",
1804 getpid (), argv[i], argv[i + 1],
1805 vcl_cfg->heapsize, vcl_cfg->heapsize);
1809 if (*sizep == 'g' || *sizep == 'G')
1810 vcl_cfg->heapsize = size << 30;
1811 else if (*sizep == 'm' || *sizep == 'M')
1812 vcl_cfg->heapsize = size << 20;
1815 if (VPPCOM_DEBUG > 0)
1816 clib_warning ("VCL<%d>: parse error '%s %s', "
1817 "using default heapsize %lld (0x%llx)",
1818 getpid (), argv[i], argv[i + 1],
1819 vcl_cfg->heapsize, vcl_cfg->heapsize);
1831 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1832 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1833 if (vcl_mem == MAP_FAILED)
1835 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1836 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1838 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1841 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1844 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1847 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1850 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1854 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1857 if (VPPCOM_DEBUG > 0)
1858 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1859 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1863 vppcom_cfg_read (char *conf_fname)
1865 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1867 unformat_input_t _input, *input = &_input;
1868 unformat_input_t _line_input, *line_input = &_line_input;
1869 u8 vc_cfg_input = 0;
1872 u32 uid, gid, q_len;
1874 fd = open (conf_fname, O_RDONLY);
1877 if (VPPCOM_DEBUG > 0)
1878 clib_warning ("VCL<%d>: open configuration file '%s' failed!",
1879 getpid (), conf_fname);
1883 if (fstat (fd, &s) < 0)
1885 if (VPPCOM_DEBUG > 0)
1886 clib_warning ("VCL<%d>: failed to stat `%s'", getpid (), conf_fname);
1890 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1892 if (VPPCOM_DEBUG > 0)
1893 clib_warning ("VCL<%d>: not a regular file `%s'",
1894 getpid (), conf_fname);
1898 unformat_init_clib_file (input, fd);
1900 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1902 (void) unformat_user (input, unformat_line_input, line_input);
1903 unformat_skip_white_space (line_input);
1905 if (unformat (line_input, "vcl {"))
1913 if (unformat (line_input, "heapsize %s", &chroot_path))
1915 vec_terminate_c_string (chroot_path);
1916 if (VPPCOM_DEBUG > 0)
1917 clib_warning ("VCL<%d>: configured heapsize %s, "
1918 "actual heapsize %lld (0x%llx)",
1919 getpid (), chroot_path, vcl_cfg->heapsize,
1921 vec_free (chroot_path);
1923 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1925 vec_terminate_c_string (chroot_path);
1926 vl_set_memory_root_path ((char *) chroot_path);
1927 if (VPPCOM_DEBUG > 0)
1928 clib_warning ("VCL<%d>: configured api-prefix %s",
1929 getpid (), chroot_path);
1930 chroot_path = 0; /* Don't vec_free() it! */
1932 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1934 if (q_len < vcl_cfg->vpp_api_q_length)
1936 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1937 "(%u) is too small! Using default: %u ",
1938 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1942 vcl_cfg->vpp_api_q_length = q_len;
1944 if (VPPCOM_DEBUG > 0)
1945 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1946 getpid (), vcl_cfg->vpp_api_q_length);
1949 else if (unformat (line_input, "uid %d", &uid))
1951 vl_set_memory_uid (uid);
1952 if (VPPCOM_DEBUG > 0)
1953 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1955 else if (unformat (line_input, "gid %d", &gid))
1957 vl_set_memory_gid (gid);
1958 if (VPPCOM_DEBUG > 0)
1959 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1961 else if (unformat (line_input, "segment-baseva 0x%lx",
1962 &vcl_cfg->segment_baseva))
1964 if (VPPCOM_DEBUG > 0)
1965 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1966 getpid (), vcl_cfg->segment_baseva);
1968 else if (unformat (line_input, "segment-size 0x%lx",
1969 &vcl_cfg->segment_size))
1971 if (VPPCOM_DEBUG > 0)
1972 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1973 getpid (), vcl_cfg->segment_size,
1974 vcl_cfg->segment_size);
1976 else if (unformat (line_input, "segment-size %ld",
1977 &vcl_cfg->segment_size))
1979 if (VPPCOM_DEBUG > 0)
1980 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1981 getpid (), vcl_cfg->segment_size,
1982 vcl_cfg->segment_size);
1984 else if (unformat (line_input, "add-segment-size 0x%lx",
1985 &vcl_cfg->add_segment_size))
1987 if (VPPCOM_DEBUG > 0)
1989 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1990 getpid (), vcl_cfg->add_segment_size,
1991 vcl_cfg->add_segment_size);
1993 else if (unformat (line_input, "add-segment-size %ld",
1994 &vcl_cfg->add_segment_size))
1996 if (VPPCOM_DEBUG > 0)
1998 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1999 getpid (), vcl_cfg->add_segment_size,
2000 vcl_cfg->add_segment_size);
2002 else if (unformat (line_input, "preallocated-fifo-pairs %d",
2003 &vcl_cfg->preallocated_fifo_pairs))
2005 if (VPPCOM_DEBUG > 0)
2006 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
2007 "%d (0x%x)", getpid (),
2008 vcl_cfg->preallocated_fifo_pairs,
2009 vcl_cfg->preallocated_fifo_pairs);
2011 else if (unformat (line_input, "rx-fifo-size 0x%lx",
2012 &vcl_cfg->rx_fifo_size))
2014 if (VPPCOM_DEBUG > 0)
2015 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
2016 getpid (), vcl_cfg->rx_fifo_size,
2017 vcl_cfg->rx_fifo_size);
2019 else if (unformat (line_input, "rx-fifo-size %ld",
2020 &vcl_cfg->rx_fifo_size))
2022 if (VPPCOM_DEBUG > 0)
2023 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
2024 getpid (), vcl_cfg->rx_fifo_size,
2025 vcl_cfg->rx_fifo_size);
2027 else if (unformat (line_input, "tx-fifo-size 0x%lx",
2028 &vcl_cfg->tx_fifo_size))
2030 if (VPPCOM_DEBUG > 0)
2031 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
2032 getpid (), vcl_cfg->tx_fifo_size,
2033 vcl_cfg->tx_fifo_size);
2035 else if (unformat (line_input, "tx-fifo-size %ld",
2036 &vcl_cfg->tx_fifo_size))
2038 if (VPPCOM_DEBUG > 0)
2039 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
2040 getpid (), vcl_cfg->tx_fifo_size,
2041 vcl_cfg->tx_fifo_size);
2043 else if (unformat (line_input, "event-queue-size 0x%lx",
2044 &vcl_cfg->event_queue_size))
2046 if (VPPCOM_DEBUG > 0)
2047 clib_warning ("VCL<%d>: configured event_queue_size "
2049 getpid (), vcl_cfg->event_queue_size,
2050 vcl_cfg->event_queue_size);
2052 else if (unformat (line_input, "event-queue-size %ld",
2053 &vcl_cfg->event_queue_size))
2055 if (VPPCOM_DEBUG > 0)
2056 clib_warning ("VCL<%d>: configured event_queue_size "
2058 getpid (), vcl_cfg->event_queue_size,
2059 vcl_cfg->event_queue_size);
2061 else if (unformat (line_input, "listen-queue-size 0x%lx",
2062 &vcl_cfg->listen_queue_size))
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("VCL<%d>: configured listen_queue_size "
2067 getpid (), vcl_cfg->listen_queue_size,
2068 vcl_cfg->listen_queue_size);
2070 else if (unformat (line_input, "listen-queue-size %ld",
2071 &vcl_cfg->listen_queue_size))
2073 if (VPPCOM_DEBUG > 0)
2074 clib_warning ("VCL<%d>: configured listen_queue_size "
2076 getpid (), vcl_cfg->listen_queue_size,
2077 vcl_cfg->listen_queue_size);
2079 else if (unformat (line_input, "app-timeout %f",
2080 &vcl_cfg->app_timeout))
2082 if (VPPCOM_DEBUG > 0)
2083 clib_warning ("VCL<%d>: configured app_timeout %f",
2084 getpid (), vcl_cfg->app_timeout);
2086 else if (unformat (line_input, "session-timeout %f",
2087 &vcl_cfg->session_timeout))
2089 if (VPPCOM_DEBUG > 0)
2090 clib_warning ("VCL<%d>: configured session_timeout %f",
2091 getpid (), vcl_cfg->session_timeout);
2093 else if (unformat (line_input, "accept-timeout %f",
2094 &vcl_cfg->accept_timeout))
2096 if (VPPCOM_DEBUG > 0)
2097 clib_warning ("VCL<%d>: configured accept_timeout %f",
2098 getpid (), vcl_cfg->accept_timeout);
2100 else if (unformat (line_input, "app-proxy-transport-tcp"))
2102 vcl_cfg->app_proxy_transport_tcp = 1;
2103 if (VPPCOM_DEBUG > 0)
2104 clib_warning ("VCL<%d>: configured "
2105 "app_proxy_transport_tcp (%d)",
2106 getpid (), vcl_cfg->app_proxy_transport_tcp);
2108 else if (unformat (line_input, "app-proxy-transport-udp"))
2110 vcl_cfg->app_proxy_transport_udp = 1;
2111 if (VPPCOM_DEBUG > 0)
2112 clib_warning ("VCL<%d>: configured "
2113 "app_proxy_transport_udp (%d)",
2114 getpid (), vcl_cfg->app_proxy_transport_udp);
2116 else if (unformat (line_input, "app-scope-local"))
2118 vcl_cfg->app_scope_local = 1;
2119 if (VPPCOM_DEBUG > 0)
2120 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2121 getpid (), vcl_cfg->app_scope_local);
2123 else if (unformat (line_input, "app-scope-global"))
2125 vcl_cfg->app_scope_global = 1;
2126 if (VPPCOM_DEBUG > 0)
2127 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2128 getpid (), vcl_cfg->app_scope_global);
2130 else if (unformat (line_input, "namespace-secret %lu",
2131 &vcl_cfg->namespace_secret))
2133 if (VPPCOM_DEBUG > 0)
2135 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2136 getpid (), vcl_cfg->namespace_secret,
2137 vcl_cfg->namespace_secret);
2139 else if (unformat (line_input, "namespace-id %v",
2140 &vcl_cfg->namespace_id))
2142 vl_api_application_attach_t *mp;
2143 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2144 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2145 if (nsid_vec_len > max_nsid_vec_len)
2147 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2148 if (VPPCOM_DEBUG > 0)
2149 clib_warning ("VCL<%d>: configured namespace_id is "
2150 "too long, truncated to %d characters!",
2151 getpid (), max_nsid_vec_len);
2154 if (VPPCOM_DEBUG > 0)
2155 clib_warning ("VCL<%d>: configured namespace_id %v",
2156 getpid (), vcl_cfg->namespace_id);
2158 else if (unformat (line_input, "}"))
2161 if (VPPCOM_DEBUG > 0)
2162 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2168 if (line_input->buffer[line_input->index] != '#')
2170 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2172 &line_input->buffer[line_input->index]);
2179 unformat_free (input);
2187 * VPPCOM Public API functions
2190 vppcom_app_create (char *app_name)
2192 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2203 vppcom_cfg_init (vcl_cfg);
2204 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2208 if (sscanf (env_var_str, "%u", &tmp) != 1)
2209 clib_warning ("VCL<%d>: Invalid debug level specified in "
2210 "the environment variable "
2212 " (%s)!\n", getpid (), env_var_str);
2216 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2217 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2220 conf_fname = getenv (VPPCOM_ENV_CONF);
2223 conf_fname = VPPCOM_CONF_DEFAULT;
2224 if (VPPCOM_DEBUG > 0)
2225 clib_warning ("VCL<%d>: getenv '%s' failed!", getpid (),
2228 vppcom_cfg_heapsize (conf_fname);
2229 clib_fifo_validate (vcm->client_session_index_fifo,
2230 vcm->cfg.listen_queue_size);
2231 vppcom_cfg_read (conf_fname);
2232 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2235 u32 ns_id_vec_len = strlen (env_var_str);
2237 vec_reset_length (vcm->cfg.namespace_id);
2238 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2239 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2241 if (VPPCOM_DEBUG > 0)
2242 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2243 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
2244 vcm->cfg.namespace_id);
2246 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2250 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2251 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2252 "the environment variable "
2253 VPPCOM_ENV_APP_NAMESPACE_SECRET
2254 " (%s)!\n", getpid (), env_var_str);
2257 vcm->cfg.namespace_secret = tmp;
2258 if (VPPCOM_DEBUG > 0)
2259 clib_warning ("VCL<%d>: configured namespace secret "
2260 "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2261 getpid (), vcm->cfg.namespace_secret);
2264 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2266 vcm->cfg.app_proxy_transport_tcp = 1;
2267 if (VPPCOM_DEBUG > 0)
2268 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2269 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2270 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2272 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2274 vcm->cfg.app_proxy_transport_udp = 1;
2275 if (VPPCOM_DEBUG > 0)
2276 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2277 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2278 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2280 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2282 vcm->cfg.app_scope_local = 1;
2283 if (VPPCOM_DEBUG > 0)
2284 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2285 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
2286 vcm->cfg.app_scope_local);
2288 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2290 vcm->cfg.app_scope_global = 1;
2291 if (VPPCOM_DEBUG > 0)
2292 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2293 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
2294 vcm->cfg.app_scope_global);
2297 vcm->main_cpu = os_get_thread_index ();
2298 heap = clib_mem_get_per_cpu_heap ();
2299 h = mheap_header (heap);
2301 /* make the main heap thread-safe */
2302 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2304 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2306 clib_time_init (&vcm->clib_time);
2307 vppcom_init_error_string_table ();
2308 svm_fifo_segment_init (vcl_cfg->segment_baseva,
2309 20 /* timeout in secs */ );
2310 clib_spinlock_init (&vcm->sessions_lockp);
2313 if (vcm->my_client_index == ~0)
2315 vppcom_api_hookup ();
2316 vcm->app_state = STATE_APP_START;
2317 rv = vppcom_connect_to_vpp (app_name);
2320 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2325 if (VPPCOM_DEBUG > 0)
2326 clib_warning ("VCL<%d>: sending session enable", getpid ());
2328 rv = vppcom_app_session_enable ();
2331 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2332 "failed!", getpid ());
2336 if (VPPCOM_DEBUG > 0)
2337 clib_warning ("VCL<%d>: sending app attach", getpid ());
2339 rv = vppcom_app_attach ();
2342 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2347 if (VPPCOM_DEBUG > 0)
2348 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2349 getpid (), app_name, vcm->my_client_index,
2350 vcm->my_client_index);
2357 vppcom_app_destroy (void)
2361 if (vcm->my_client_index == ~0)
2364 if (VPPCOM_DEBUG > 0)
2365 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2366 getpid (), vcm->my_client_index, vcm->my_client_index);
2368 if (VPPCOM_DEBUG > 0)
2371 ELOG_TYPE_DECLARE (e) =
2373 .format = "app_detach:C:%d",
2374 .format_args = "i4",
2381 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2382 ed->data = vcm->my_client_index;
2386 vppcom_app_detach ();
2387 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2388 if (PREDICT_FALSE (rv))
2390 if (VPPCOM_DEBUG > 0)
2391 clib_warning ("VCL<%d>: application detach timed out! "
2392 "returning %d (%s)",
2393 getpid (), rv, vppcom_retval_str (rv));
2396 /* Finished with logging before client gets reset to ~0 */
2397 if (VPPCOM_DEBUG > 0)
2400 vl_client_disconnect_from_vlib ();
2401 vcm->my_client_index = ~0;
2402 vcm->app_state = STATE_APP_START;
2406 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2411 clib_spinlock_lock (&vcm->sessions_lockp);
2412 pool_get (vcm->sessions, session);
2413 memset (session, 0, sizeof (*session));
2414 session_index = session - vcm->sessions;
2417 session->proto = proto;
2418 session->state = STATE_START;
2419 session->is_nonblocking = is_nonblocking ? 1 : 0;
2420 session->vpp_handle = ~0;
2421 clib_spinlock_unlock (&vcm->sessions_lockp);
2423 if (VPPCOM_DEBUG > 0)
2424 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2426 if (VPPCOM_DEBUG > 0)
2428 session->elog_track.name =
2429 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2431 elog_track_register (&vcm->elog_main, &session->elog_track);
2434 ELOG_TYPE_DECLARE (e) =
2436 .format = "session_create:vrf:%d proto:%d state:%d is_nonblocking:%d",
2437 .format_args = "i4i4i4i4",
2445 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2446 ed->data[0] = session->vrf;
2447 ed->data[1] = session->proto;
2448 ed->data[2] = session->state;
2449 ed->data[3] = session->is_nonblocking;
2453 return (int) session_index;
2457 vppcom_session_close (uint32_t session_index)
2459 session_t *session = 0;
2468 session_state_t state;
2470 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2471 is_listen = session->is_listen;
2472 is_vep = session->is_vep;
2473 is_vep_session = session->is_vep_session;
2474 next_sid = session->vep.next_sid;
2475 vep_idx = session->vep.vep_idx;
2476 state = session->state;
2477 vpp_handle = session->vpp_handle;
2478 clib_spinlock_unlock (&vcm->sessions_lockp);
2480 if (VPPCOM_DEBUG > 0)
2483 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2484 "closing epoll session...",
2485 getpid (), session_index, session_index);
2487 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2488 "closing session...",
2489 getpid (), vpp_handle, session_index);
2494 while (next_sid != ~0)
2496 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2497 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2498 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2499 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2500 getpid (), vpp_handle, next_sid, vep_idx,
2501 rv, vppcom_retval_str (rv));
2503 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2504 next_sid = session->vep.next_sid;
2505 clib_spinlock_unlock (&vcm->sessions_lockp);
2512 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2513 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2514 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2515 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2516 getpid (), vpp_handle, session_index,
2517 vep_idx, rv, vppcom_retval_str (rv));
2522 if (state == STATE_LISTEN)
2524 rv = vppcom_session_unbind (session_index);
2525 if (PREDICT_FALSE (rv < 0))
2527 if (VPPCOM_DEBUG > 0)
2528 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2529 "listener unbind failed! rv %d (%s)",
2530 getpid (), vpp_handle, session_index,
2531 rv, vppcom_retval_str (rv));
2536 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2538 rv = vppcom_session_disconnect (session_index);
2539 if (PREDICT_FALSE (rv < 0))
2540 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2541 "session disconnect failed! rv %d (%s)",
2542 getpid (), vpp_handle, session_index,
2543 rv, vppcom_retval_str (rv));
2547 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2548 vpp_handle = session->vpp_handle;
2549 if (vpp_handle != ~0)
2551 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2553 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2555 pool_put_index (vcm->sessions, session_index);
2556 clib_spinlock_unlock (&vcm->sessions_lockp);
2558 if (VPPCOM_DEBUG > 0)
2561 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2562 getpid (), session_index, session_index);
2564 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2565 getpid (), vpp_handle, session_index);
2569 if (VPPCOM_DEBUG > 0)
2572 ELOG_TYPE_DECLARE (e) =
2574 .format = "session_close:rv:%d",
2575 .format_args = "i4",
2583 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2592 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2594 session_t *session = 0;
2598 return VPPCOM_EINVAL;
2600 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2602 if (session->is_vep)
2604 clib_spinlock_unlock (&vcm->sessions_lockp);
2605 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2606 "bind to an epoll session!", getpid (), session_index);
2611 session->vrf = ep->vrf;
2612 session->lcl_addr.is_ip4 = ep->is_ip4;
2613 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2614 session->lcl_port = ep->port;
2616 if (VPPCOM_DEBUG > 0)
2617 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2618 "port %u, proto %s", getpid (), session_index,
2619 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2620 format_ip46_address, &session->lcl_addr.ip46,
2621 session->lcl_addr.is_ip4,
2622 clib_net_to_host_u16 (session->lcl_port),
2623 session->proto ? "UDP" : "TCP");
2625 if (VPPCOM_DEBUG > 0)
2627 if (session->lcl_addr.is_ip4)
2630 ELOG_TYPE_DECLARE (e) =
2632 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2633 .format_args = "t1i1i1i1i1i2",
2634 .n_enum_strings = 2,
2635 .enum_strings = {"TCP", "UDP",},
2638 CLIB_PACKED (struct {
2644 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2645 ed->proto = session->proto;
2646 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2647 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2648 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2649 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2650 ed->port = clib_net_to_host_u16 (session->lcl_port);
2655 clib_spinlock_unlock (&vcm->sessions_lockp);
2661 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2663 session_t *listen_session = 0;
2664 u64 listen_vpp_handle;
2667 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2669 if (listen_session->is_vep)
2671 clib_spinlock_unlock (&vcm->sessions_lockp);
2672 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2673 "epoll session!", getpid (), listen_session_index);
2678 listen_vpp_handle = listen_session->vpp_handle;
2679 if (listen_session->is_listen)
2681 clib_spinlock_unlock (&vcm->sessions_lockp);
2682 if (VPPCOM_DEBUG > 0)
2683 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2684 "already in listen state!",
2685 getpid (), listen_vpp_handle, listen_session_index);
2690 if (VPPCOM_DEBUG > 0)
2691 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2692 "sid %u: sending bind request...",
2693 getpid (), listen_vpp_handle, listen_session_index);
2695 vppcom_send_bind_sock (listen_session, listen_session_index);
2696 clib_spinlock_unlock (&vcm->sessions_lockp);
2698 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2699 vcm->cfg.session_timeout);
2701 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2702 if (PREDICT_FALSE (retval))
2704 if (VPPCOM_DEBUG > 0)
2705 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2706 "returning %d (%s)", getpid (),
2707 listen_session->vpp_handle, listen_session_index,
2708 retval, vppcom_retval_str (retval));
2709 clib_spinlock_unlock (&vcm->sessions_lockp);
2714 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2715 clib_spinlock_unlock (&vcm->sessions_lockp);
2721 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2724 session_t *listen_session = 0;
2725 session_t *client_session = 0;
2726 u32 client_session_index = ~0;
2730 u64 listen_vpp_handle;
2732 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2734 if (listen_session->is_vep)
2736 clib_spinlock_unlock (&vcm->sessions_lockp);
2737 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2738 "epoll session!", getpid (), listen_session_index);
2743 listen_vpp_handle = listen_session->vpp_handle;
2744 if (listen_session->state != STATE_LISTEN)
2746 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2747 "not in listen state! state 0x%x (%s)", getpid (),
2748 listen_vpp_handle, listen_session_index,
2749 listen_session->state,
2750 vppcom_session_state_str (listen_session->state));
2751 clib_spinlock_unlock (&vcm->sessions_lockp);
2755 wait_for = (listen_session->is_nonblocking) ? 0 : vcm->cfg.accept_timeout;
2757 clib_spinlock_unlock (&vcm->sessions_lockp);
2761 rv = vppcom_wait_for_client_session_index (wait_for);
2764 if ((VPPCOM_DEBUG > 0))
2765 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2766 "accept failed! returning %d (%s)", getpid (),
2767 listen_vpp_handle, listen_session_index,
2768 rv, vppcom_retval_str (rv));
2776 clib_spinlock_lock (&vcm->sessions_lockp);
2777 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2778 rv = vppcom_session_at_index (client_session_index, &client_session);
2779 if (PREDICT_FALSE (rv))
2781 rv = VPPCOM_ECONNABORTED;
2782 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2783 "lookup failed! returning %d (%s)", getpid (),
2784 listen_vpp_handle, listen_session_index,
2785 client_session_index, rv, vppcom_retval_str (rv));
2789 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
2790 if (VPPCOM_DEBUG > 0)
2791 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2792 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2793 getpid (), listen_vpp_handle, listen_session_index,
2794 client_session->vpp_handle, client_session_index,
2795 flags, client_session->is_nonblocking);
2799 ep->vrf = client_session->vrf;
2800 ep->is_cut_thru = client_session->is_cut_thru;
2801 ep->is_ip4 = client_session->peer_addr.is_ip4;
2802 ep->port = client_session->peer_port;
2803 if (client_session->peer_addr.is_ip4)
2804 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2805 sizeof (ip4_address_t));
2807 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2808 sizeof (ip6_address_t));
2811 if (client_session->is_server && client_session->is_cut_thru)
2813 static svm_fifo_segment_create_args_t _a;
2814 svm_fifo_segment_create_args_t *a = &_a;
2815 svm_fifo_segment_private_t *seg;
2817 cut_thru_str = " cut-thru ";
2819 /* Create the segment */
2820 memset (a, 0, sizeof (*a));
2821 a->segment_name = (char *)
2822 format ((u8 *) a->segment_name, "%d:segment%d%c",
2823 getpid (), vcm->unique_segment_index++, 0);
2824 a->segment_size = vcm->cfg.segment_size;
2825 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
2826 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
2827 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
2829 rv = svm_fifo_segment_create (a);
2830 if (PREDICT_FALSE (rv))
2832 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2833 "client sid %u svm_fifo_segment_create ('%s') "
2834 "failed! rv %d", getpid (), listen_vpp_handle,
2835 listen_session_index, client_session_index,
2836 a->segment_name, rv);
2837 vec_reset_length (a->new_segment_indices);
2838 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2839 vppcom_send_connect_session_reply (client_session,
2840 client_session_index,
2841 client_session->vpp_handle,
2842 client_session->client_context,
2844 clib_spinlock_unlock (&vcm->sessions_lockp);
2849 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2850 client_session->sm_seg_index = a->new_segment_indices[0];
2851 vec_free (a->new_segment_indices);
2853 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
2854 client_session->server_rx_fifo =
2855 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2856 FIFO_SEGMENT_RX_FREELIST);
2857 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2859 svm_fifo_segment_delete (seg);
2860 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2861 "client sid %u rx fifo alloc failed! "
2862 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2863 listen_session_index, client_session_index,
2864 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
2865 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2866 vppcom_send_connect_session_reply (client_session,
2867 client_session_index,
2868 client_session->vpp_handle,
2869 client_session->client_context,
2871 clib_spinlock_unlock (&vcm->sessions_lockp);
2875 client_session->server_rx_fifo->master_session_index =
2876 client_session_index;
2878 client_session->server_tx_fifo =
2879 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2880 FIFO_SEGMENT_TX_FREELIST);
2881 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2883 svm_fifo_segment_delete (seg);
2884 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2885 "client sid %u tx fifo alloc failed! "
2886 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2887 listen_session_index, client_session_index,
2888 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
2889 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
2890 vppcom_send_connect_session_reply (client_session,
2891 client_session_index,
2892 client_session->vpp_handle,
2893 client_session->client_context,
2895 clib_spinlock_unlock (&vcm->sessions_lockp);
2899 client_session->server_tx_fifo->master_session_index =
2900 client_session_index;
2902 if (VPPCOM_DEBUG > 1)
2903 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2904 "created segment '%s', rx_fifo %p, tx_fifo %p",
2905 getpid (), listen_vpp_handle, listen_session_index,
2906 client_session_index, client_session->segment_name,
2907 client_session->server_rx_fifo,
2908 client_session->server_tx_fifo);
2910 #ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2913 ssvm_shared_header_t *sh = seg->ssvm.sh;
2915 ssvm_lock_non_recursive (sh, 1);
2916 oldheap = ssvm_push_heap (sh);
2917 event_q = client_session->vpp_event_queue =
2918 svm_queue_init (vcm->cfg.event_queue_size,
2919 sizeof (session_fifo_event_t),
2920 getpid (), 0 /* signal not sent */ );
2921 ssvm_pop_heap (oldheap);
2922 ssvm_unlock_non_recursive (sh);
2925 vppcom_send_connect_session_reply (client_session,
2926 client_session_index,
2927 client_session->vpp_handle,
2928 client_session->client_context,
2929 0 /* retval OK */ );
2934 vppcom_send_accept_session_reply (client_session->vpp_handle,
2935 client_session->client_context,
2936 0 /* retval OK */ );
2939 if (VPPCOM_DEBUG > 0)
2940 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2941 "0x%llx, sid %u%sconnection to local %s address "
2942 "%U port %u", getpid (), listen_vpp_handle,
2943 listen_session_index, client_session->vpp_handle,
2944 client_session_index, cut_thru_str,
2945 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2946 format_ip46_address, &client_session->lcl_addr.ip46,
2947 client_session->lcl_addr.is_ip4,
2948 clib_net_to_host_u16 (client_session->lcl_port));
2950 if (VPPCOM_DEBUG > 0)
2952 client_session->elog_track.name =
2953 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2954 client_session_index, 0);
2955 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2958 ELOG_TYPE_DECLARE (e) =
2960 .format = "accept cut-thru: listen_handle:%x from_handle:%x",
2961 .format_args = "i8i8",
2969 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2970 ed->handle[0] = listen_vpp_handle;
2971 ed->handle[1] = client_session->vpp_handle;
2974 if (client_session->lcl_addr.is_ip4)
2977 ELOG_TYPE_DECLARE (e2) =
2979 .format = "accept cut-thru: S:%d %d.%d.%d.%d:%d ",
2980 .format_args = "i4i1i1i1i1i2",
2983 CLIB_PACKED (struct {
2990 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2991 ed2->session = client_session_index;
2992 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2993 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2994 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2995 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2996 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
3001 clib_spinlock_unlock (&vcm->sessions_lockp);
3002 rv = (int) client_session_index;
3008 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
3010 session_t *session = 0;
3011 int rv, retval = VPPCOM_OK;
3012 u64 vpp_handle = ~0;
3014 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3016 if (PREDICT_FALSE (session->is_vep))
3018 clib_spinlock_unlock (&vcm->sessions_lockp);
3019 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3020 "connect on an epoll session!", getpid (), session_index);
3025 vpp_handle = session->vpp_handle;
3026 if (PREDICT_FALSE (session->is_server))
3028 clib_spinlock_unlock (&vcm->sessions_lockp);
3029 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: is in use "
3030 "as a server session!", getpid (), vpp_handle,
3036 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
3038 if (VPPCOM_DEBUG > 0)
3039 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
3040 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3041 getpid (), vpp_handle, session_index,
3042 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3043 format_ip46_address,
3044 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3045 clib_net_to_host_u16 (session->peer_port),
3046 session->proto ? "UDP" : "TCP", session->state,
3047 vppcom_session_state_str (session->state));
3049 clib_spinlock_unlock (&vcm->sessions_lockp);
3053 session->vrf = server_ep->vrf;
3054 session->peer_addr.is_ip4 = server_ep->is_ip4;
3055 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
3056 session->peer_port = server_ep->port;
3058 if (VPPCOM_DEBUG > 0)
3059 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
3060 "%s %U port %d proto %s",
3061 getpid (), vpp_handle, session_index,
3062 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3063 format_ip46_address,
3064 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3065 clib_net_to_host_u16 (session->peer_port),
3066 session->proto ? "UDP" : "TCP");
3068 vppcom_send_connect_sock (session, session_index);
3069 clib_spinlock_unlock (&vcm->sessions_lockp);
3072 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3073 vcm->cfg.session_timeout);
3075 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3076 vpp_handle = session->vpp_handle;
3077 clib_spinlock_unlock (&vcm->sessions_lockp);
3080 if (PREDICT_FALSE (retval))
3083 if (VPPCOM_DEBUG > 0)
3084 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
3085 "returning %d (%s)", getpid (), vpp_handle,
3086 session_index, rv, vppcom_retval_str (rv));
3088 else if (VPPCOM_DEBUG > 0)
3089 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
3090 getpid (), vpp_handle, session_index);
3096 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3099 session_t *session = 0;
3100 svm_fifo_t *rx_fifo;
3105 session_state_t state;
3112 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3114 if (PREDICT_FALSE (session->is_vep))
3116 clib_spinlock_unlock (&vcm->sessions_lockp);
3117 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3118 "read from an epoll session!", getpid (), session_index);
3123 vpp_handle = session->vpp_handle;
3124 is_server = session->is_server;
3125 is_nonblocking = session->is_nonblocking;
3126 state = session->state;
3127 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3129 clib_spinlock_unlock (&vcm->sessions_lockp);
3130 rv = ((state == STATE_DISCONNECT) ?
3131 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3133 if (VPPCOM_DEBUG > 0)
3134 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3135 "not open! state 0x%x (%s), returning %d (%s)",
3136 getpid (), vpp_handle, session_index,
3137 is_server ? "server" : "client", state,
3138 vppcom_session_state_str (state),
3139 rv, vppcom_retval_str (rv));
3143 rx_fifo = ((!session->is_cut_thru || is_server) ?
3144 session->server_rx_fifo : session->server_tx_fifo);
3145 fifo_str = ((!session->is_cut_thru || is_server) ?
3146 "server_rx_fifo" : "server_tx_fifo");
3147 clib_spinlock_unlock (&vcm->sessions_lockp);
3152 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3154 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3156 while (!is_nonblocking && (n_read <= 0));
3160 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3162 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3163 (EPOLLET | EPOLLIN));
3165 session->vep.et_mask |= EPOLLIN;
3167 if (state == STATE_CLOSE_ON_EMPTY)
3169 session_state_t new_state = STATE_DISCONNECT;
3170 rv = VPPCOM_ECONNRESET;
3172 if (VPPCOM_DEBUG > 1)
3174 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3175 "with %s session state 0x%x (%s)!"
3176 " Setting state to 0x%x (%s), returning %d (%s)",
3177 getpid (), vpp_handle, session_index,
3178 is_server ? "server" : "client",
3179 state, vppcom_session_state_str (state),
3180 new_state, vppcom_session_state_str (new_state),
3181 rv, vppcom_retval_str (rv));
3184 session->state = new_state;
3189 clib_spinlock_unlock (&vcm->sessions_lockp);
3194 if (VPPCOM_DEBUG > 2)
3197 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3198 "from %s (%p)", getpid (), vpp_handle,
3199 session_index, n_read, fifo_str, rx_fifo);
3201 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3202 "returning %d (%s)", getpid (), vpp_handle,
3203 session_index, rv, vppcom_retval_str (rv));
3210 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3212 return (vppcom_session_read_internal (session_index, buf, n, 0));
3216 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3218 return (vppcom_session_read_internal (session_index, buf, n, 1));
3222 vppcom_session_read_ready (session_t * session, u32 session_index)
3224 svm_fifo_t *rx_fifo = 0;
3228 u8 is_server = session->is_server;
3229 session_state_t state = session->state;
3230 u64 vpp_handle = session->vpp_handle;
3232 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3233 if (PREDICT_FALSE (session->is_vep))
3235 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3236 "epoll session!", getpid (), session_index);
3241 if (session->is_listen)
3242 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3245 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3247 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3250 if (VPPCOM_DEBUG > 1)
3251 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3252 "not open! state 0x%x (%s), returning %d (%s)",
3253 getpid (), vpp_handle, session_index,
3254 is_server ? "server" : "client",
3255 state, vppcom_session_state_str (state),
3256 rv, vppcom_retval_str (rv));
3260 rx_fifo = ((!session->is_cut_thru || is_server) ?
3261 session->server_rx_fifo : session->server_tx_fifo);
3263 ready = svm_fifo_max_dequeue (rx_fifo);
3269 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3271 session->vep.et_mask |= EPOLLIN;
3273 if (state == STATE_CLOSE_ON_EMPTY)
3275 rv = VPPCOM_ECONNRESET;
3276 session_state_t new_state = STATE_DISCONNECT;
3278 if (VPPCOM_DEBUG > 1)
3280 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3281 "sid %u: Empty fifo with"
3282 " %s session state 0x%x (%s)! Setting state to "
3283 "0x%x (%s), returning %d (%s)",
3284 getpid (), session_index, vpp_handle,
3285 is_server ? "server" : "client",
3286 state, vppcom_session_state_str (state),
3287 new_state, vppcom_session_state_str (new_state),
3288 rv, vppcom_retval_str (rv));
3290 session->state = new_state;
3296 if (vcm->app_event_queue->cursize &&
3297 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3299 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3300 session_fifo_event_t e;
3302 for (i = 0; i < n_to_dequeue; i++)
3303 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3305 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3312 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3314 session_t *session = 0;
3315 svm_fifo_t *tx_fifo;
3317 session_fifo_event_t evt;
3323 session_state_t state;
3328 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3330 if (PREDICT_FALSE (session->is_vep))
3332 clib_spinlock_unlock (&vcm->sessions_lockp);
3333 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3334 "cannot write to an epoll session!",
3335 getpid (), session->vpp_handle, session_index);
3341 is_server = session->is_server;
3342 is_nonblocking = session->is_nonblocking;
3343 vpp_handle = session->vpp_handle;
3344 state = session->state;
3345 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3347 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3350 clib_spinlock_unlock (&vcm->sessions_lockp);
3351 if (VPPCOM_DEBUG > 1)
3352 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3353 "%s session is not open! state 0x%x (%s)",
3354 getpid (), vpp_handle, session_index,
3355 is_server ? "server" : "client", state,
3356 vppcom_session_state_str (state));
3360 tx_fifo = ((!session->is_cut_thru || is_server) ?
3361 session->server_tx_fifo : session->server_rx_fifo);
3362 fifo_str = ((!session->is_cut_thru || is_server) ?
3363 "server_tx_fifo" : "server_rx_fifo");
3364 clib_spinlock_unlock (&vcm->sessions_lockp);
3368 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3370 while (!is_nonblocking && (n_write <= 0));
3372 /* If event wasn't set, add one */
3373 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
3375 /* Fabricate TX event, send to vpp */
3377 evt.event_type = FIFO_EVENT_APP_TX;
3379 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3380 q = session->vpp_event_queue;
3382 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3383 clib_spinlock_unlock (&vcm->sessions_lockp);
3384 if (VPPCOM_DEBUG > 1)
3385 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3386 "added FIFO_EVENT_APP_TX to "
3387 "vpp_event_q %p, n_write %d", getpid (),
3388 vpp_handle, session_index, q, n_write);
3393 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3395 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3396 (EPOLLET | EPOLLOUT));
3398 session->vep.et_mask |= EPOLLOUT;
3400 if (state == STATE_CLOSE_ON_EMPTY)
3402 session_state_t new_state = STATE_DISCONNECT;
3403 rv = VPPCOM_ECONNRESET;
3405 if (VPPCOM_DEBUG > 1)
3407 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3408 "Empty fifo with %s session state 0x%x (%s)!"
3409 " Setting state to 0x%x (%s), returning %d (%s)",
3410 getpid (), vpp_handle, session_index,
3411 is_server ? "server" : "client",
3412 state, vppcom_session_state_str (state),
3413 new_state, vppcom_session_state_str (new_state),
3414 rv, vppcom_retval_str (rv));
3417 session->state = new_state;
3422 clib_spinlock_unlock (&vcm->sessions_lockp);
3427 if (VPPCOM_DEBUG > 2)
3430 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3431 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
3432 session_index, fifo_str, tx_fifo);
3434 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3435 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
3436 session_index, n_write, fifo_str, tx_fifo);
3443 vppcom_session_write_ready (session_t * session, u32 session_index)
3445 svm_fifo_t *tx_fifo;
3450 u8 is_server = session->is_server;
3451 session_state_t state = session->state;
3453 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3454 if (PREDICT_FALSE (session->is_vep))
3456 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3457 "cannot write to an epoll session!",
3458 getpid (), session->vpp_handle, session_index);
3463 if (PREDICT_FALSE (session->is_listen))
3465 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3466 "cannot write to a listen session!",
3467 getpid (), session->vpp_handle, session_index);
3472 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3474 session_state_t state = session->state;
3476 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3479 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3480 "%s session is not open! state 0x%x (%s), "
3481 "returning %d (%s)", getpid (), session->vpp_handle,
3482 session_index, is_server ? "server" : "client",
3483 state, vppcom_session_state_str (state),
3484 rv, vppcom_retval_str (rv));
3488 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3489 session->server_tx_fifo : session->server_rx_fifo);
3490 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3491 "server_tx_fifo" : "server_rx_fifo");
3493 ready = svm_fifo_max_enqueue (tx_fifo);
3495 if (VPPCOM_DEBUG > 3)
3496 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3497 "peek %s (%p), ready = %d", getpid (),
3498 session->vpp_handle, session_index,
3499 fifo_str, tx_fifo, ready);
3503 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3504 (EPOLLET | EPOLLOUT));
3506 session->vep.et_mask |= EPOLLOUT;
3508 if (state == STATE_CLOSE_ON_EMPTY)
3510 rv = VPPCOM_ECONNRESET;
3511 session_state_t new_state = STATE_DISCONNECT;
3513 if (VPPCOM_DEBUG > 1)
3515 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3516 "Empty fifo with %s session "
3517 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3518 "returning %d (%s)", getpid (),
3519 session->vpp_handle, session_index,
3520 is_server ? "server" : "client",
3521 state, vppcom_session_state_str (state),
3522 new_state, vppcom_session_state_str (new_state),
3523 rv, vppcom_retval_str (rv));
3525 session->state = new_state;
3535 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3536 unsigned long *write_map, unsigned long *except_map,
3537 double time_to_wait)
3540 session_t *session = 0;
3541 int rv, bits_set = 0;
3542 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3543 u32 minbits = clib_max (n_bits, BITS (uword));
3545 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3547 if (n_bits && read_map)
3549 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3550 clib_memcpy (vcm->rd_bitmap, read_map,
3551 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3552 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3554 if (n_bits && write_map)
3556 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3557 clib_memcpy (vcm->wr_bitmap, write_map,
3558 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3559 memset (write_map, 0,
3560 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3562 if (n_bits && except_map)
3564 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3565 clib_memcpy (vcm->ex_bitmap, except_map,
3566 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3567 memset (except_map, 0,
3568 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3578 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3580 clib_spinlock_lock (&vcm->sessions_lockp);
3581 rv = vppcom_session_at_index (session_index, &session);
3584 clib_spinlock_unlock (&vcm->sessions_lockp);
3585 if (VPPCOM_DEBUG > 1)
3586 clib_warning ("VCL<%d>: session %d specified in "
3587 "read_map is closed.", getpid (),
3589 bits_set = VPPCOM_EBADFD;
3593 rv = vppcom_session_read_ready (session, session_index);
3594 clib_spinlock_unlock (&vcm->sessions_lockp);
3595 if (except_map && vcm->ex_bitmap &&
3596 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3599 clib_bitmap_set_no_check (except_map, session_index, 1);
3604 clib_bitmap_set_no_check (read_map, session_index, 1);
3612 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3614 clib_spinlock_lock (&vcm->sessions_lockp);
3615 rv = vppcom_session_at_index (session_index, &session);
3618 clib_spinlock_unlock (&vcm->sessions_lockp);
3619 if (VPPCOM_DEBUG > 0)
3620 clib_warning ("VCL<%d>: session %d specified in "
3621 "write_map is closed.", getpid (),
3623 bits_set = VPPCOM_EBADFD;
3627 rv = vppcom_session_write_ready (session, session_index);
3628 clib_spinlock_unlock (&vcm->sessions_lockp);
3629 if (write_map && (rv > 0))
3631 clib_bitmap_set_no_check (write_map, session_index, 1);
3639 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3641 clib_spinlock_lock (&vcm->sessions_lockp);
3642 rv = vppcom_session_at_index (session_index, &session);
3645 clib_spinlock_unlock (&vcm->sessions_lockp);
3646 if (VPPCOM_DEBUG > 1)
3647 clib_warning ("VCL<%d>: session %d specified in "
3648 "except_map is closed.", getpid (),
3650 bits_set = VPPCOM_EBADFD;
3654 rv = vppcom_session_read_ready (session, session_index);
3655 clib_spinlock_unlock (&vcm->sessions_lockp);
3658 clib_bitmap_set_no_check (except_map, session_index, 1);
3666 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3673 vep_verify_epoll_chain (u32 vep_idx)
3676 vppcom_epoll_t *vep;
3680 if (VPPCOM_DEBUG <= 1)
3683 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3684 rv = vppcom_session_at_index (vep_idx, &session);
3685 if (PREDICT_FALSE (rv))
3687 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3688 getpid (), vep_idx);
3691 if (PREDICT_FALSE (!session->is_vep))
3693 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3694 getpid (), vep_idx);
3697 vep = &session->vep;
3698 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3701 " is_vep_session = %u\n"
3702 " next_sid = 0x%x (%u)\n"
3703 " wait_cont_idx = 0x%x (%u)\n"
3704 "}\n", getpid (), vep_idx,
3705 session->is_vep, session->is_vep_session,
3706 vep->next_sid, vep->next_sid,
3707 session->wait_cont_idx, session->wait_cont_idx);
3709 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3711 rv = vppcom_session_at_index (sid, &session);
3712 if (PREDICT_FALSE (rv))
3714 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3717 if (PREDICT_FALSE (session->is_vep))
3718 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3719 getpid (), vep_idx);
3720 else if (PREDICT_FALSE (!session->is_vep_session))
3722 clib_warning ("VCL<%d>: ERROR: session (%u) "
3723 "is not a vep session!", getpid (), sid);
3726 vep = &session->vep;
3727 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3728 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3729 "vep_idx (%u)!", getpid (),
3730 sid, session->vep.vep_idx, vep_idx);
3731 if (session->is_vep_session)
3733 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3735 " next_sid = 0x%x (%u)\n"
3736 " prev_sid = 0x%x (%u)\n"
3737 " vep_idx = 0x%x (%u)\n"
3738 " ev.events = 0x%x\n"
3739 " ev.data.u64 = 0x%llx\n"
3743 vep->next_sid, vep->next_sid,
3744 vep->prev_sid, vep->prev_sid,
3745 vep->vep_idx, vep->vep_idx,
3746 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3751 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3752 getpid (), vep_idx);
3756 vppcom_epoll_create (void)
3758 session_t *vep_session;
3761 clib_spinlock_lock (&vcm->sessions_lockp);
3762 pool_get (vcm->sessions, vep_session);
3763 memset (vep_session, 0, sizeof (*vep_session));
3764 vep_idx = vep_session - vcm->sessions;
3766 vep_session->is_vep = 1;
3767 vep_session->vep.vep_idx = ~0;
3768 vep_session->vep.next_sid = ~0;
3769 vep_session->vep.prev_sid = ~0;
3770 vep_session->wait_cont_idx = ~0;
3771 vep_session->vpp_handle = ~0;
3772 clib_spinlock_unlock (&vcm->sessions_lockp);
3774 if (VPPCOM_DEBUG > 0)
3775 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3776 getpid (), vep_idx, vep_idx);
3778 if (VPPCOM_DEBUG > 0)
3780 vep_session->elog_track.name =
3781 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3783 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3786 ELOG_TYPE_DECLARE (e) =
3788 .format = "created epoll session:%d",
3789 .format_args = "i4",
3797 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
3806 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3807 struct epoll_event *event)
3809 session_t *vep_session;
3813 if (vep_idx == session_index)
3815 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3816 getpid (), vep_idx);
3817 return VPPCOM_EINVAL;
3820 clib_spinlock_lock (&vcm->sessions_lockp);
3821 rv = vppcom_session_at_index (vep_idx, &vep_session);
3822 if (PREDICT_FALSE (rv))
3824 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3827 if (PREDICT_FALSE (!vep_session->is_vep))
3829 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3830 getpid (), vep_idx);
3835 ASSERT (vep_session->vep.vep_idx == ~0);
3836 ASSERT (vep_session->vep.prev_sid == ~0);
3838 rv = vppcom_session_at_index (session_index, &session);
3839 if (PREDICT_FALSE (rv))
3841 if (VPPCOM_DEBUG > 0)
3842 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3843 getpid (), session_index);
3846 if (PREDICT_FALSE (session->is_vep))
3848 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3856 if (PREDICT_FALSE (!event))
3858 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3859 "epoll_event structure!", getpid ());
3863 if (vep_session->vep.next_sid != ~0)
3865 session_t *next_session;
3866 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3868 if (PREDICT_FALSE (rv))
3870 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3871 "vep.next_sid (%u) on vep_idx (%u)!",
3872 getpid (), vep_session->vep.next_sid, vep_idx);
3875 ASSERT (next_session->vep.prev_sid == vep_idx);
3876 next_session->vep.prev_sid = session_index;
3878 session->vep.next_sid = vep_session->vep.next_sid;
3879 session->vep.prev_sid = vep_idx;
3880 session->vep.vep_idx = vep_idx;
3881 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3882 session->vep.ev = *event;
3883 session->is_vep = 0;
3884 session->is_vep_session = 1;
3885 vep_session->vep.next_sid = session_index;
3886 if (VPPCOM_DEBUG > 1)
3887 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3888 "sid %u, events 0x%x, data 0x%llx!",
3889 getpid (), vep_idx, session_index,
3890 event->events, event->data.u64);
3891 if (VPPCOM_DEBUG > 0)
3894 ELOG_TYPE_DECLARE (e) =
3896 .format = "epoll_ctladd: events:%x data:%x",
3897 .format_args = "i4i4i8",
3905 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3907 ed->events = event->events;
3908 ed->event_data = event->data.u64;
3914 if (PREDICT_FALSE (!event))
3916 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3917 "epoll_event structure!", getpid ());
3921 else if (PREDICT_FALSE (!session->is_vep_session))
3923 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3924 "not a vep session!", getpid (), session_index);
3928 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3930 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3931 "vep_idx (%u) != vep_idx (%u)!",
3932 getpid (), session_index,
3933 session->vep.vep_idx, vep_idx);
3937 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3938 session->vep.ev = *event;
3939 if (VPPCOM_DEBUG > 1)
3941 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3942 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3947 if (PREDICT_FALSE (!session->is_vep_session))
3949 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3950 "not a vep session!", getpid (), session_index);
3954 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3956 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3957 "vep_idx (%u) != vep_idx (%u)!",
3958 getpid (), session_index,
3959 session->vep.vep_idx, vep_idx);
3964 vep_session->wait_cont_idx =
3965 (vep_session->wait_cont_idx == session_index) ?
3966 session->vep.next_sid : vep_session->wait_cont_idx;
3968 if (session->vep.prev_sid == vep_idx)
3969 vep_session->vep.next_sid = session->vep.next_sid;
3972 session_t *prev_session;
3973 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3974 if (PREDICT_FALSE (rv))
3976 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3977 "vep.prev_sid (%u) on sid (%u)!",
3978 getpid (), session->vep.prev_sid, session_index);
3981 ASSERT (prev_session->vep.next_sid == session_index);
3982 prev_session->vep.next_sid = session->vep.next_sid;
3984 if (session->vep.next_sid != ~0)
3986 session_t *next_session;
3987 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3988 if (PREDICT_FALSE (rv))
3990 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3991 "vep.next_sid (%u) on sid (%u)!",
3992 getpid (), session->vep.next_sid, session_index);
3995 ASSERT (next_session->vep.prev_sid == session_index);
3996 next_session->vep.prev_sid = session->vep.prev_sid;
3999 memset (&session->vep, 0, sizeof (session->vep));
4000 session->vep.next_sid = ~0;
4001 session->vep.prev_sid = ~0;
4002 session->vep.vep_idx = ~0;
4003 session->is_vep_session = 0;
4004 if (VPPCOM_DEBUG > 1)
4005 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
4006 getpid (), vep_idx, session_index);
4007 if (VPPCOM_DEBUG > 0)
4010 ELOG_TYPE_DECLARE (e) =
4012 .format = "epoll_ctldel: vep:%d",
4013 .format_args = "i4",
4020 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4028 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
4032 vep_verify_epoll_chain (vep_idx);
4035 clib_spinlock_unlock (&vcm->sessions_lockp);
4040 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4041 int maxevents, double wait_for_time)
4043 session_t *vep_session;
4045 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4046 u32 keep_trying = 1;
4048 u32 vep_next_sid, wait_cont_idx;
4051 if (PREDICT_FALSE (maxevents <= 0))
4053 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
4054 getpid (), maxevents);
4055 return VPPCOM_EINVAL;
4057 memset (events, 0, sizeof (*events) * maxevents);
4059 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4060 vep_next_sid = vep_session->vep.next_sid;
4061 is_vep = vep_session->is_vep;
4062 wait_cont_idx = vep_session->wait_cont_idx;
4063 clib_spinlock_unlock (&vcm->sessions_lockp);
4065 if (PREDICT_FALSE (!is_vep))
4067 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
4068 getpid (), vep_idx);
4072 if (PREDICT_FALSE (vep_next_sid == ~0))
4074 if (VPPCOM_DEBUG > 0)
4075 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
4076 getpid (), vep_idx);
4077 if (VPPCOM_DEBUG > 0)
4080 ELOG_TYPE_DECLARE (e) =
4082 .format = "WRN: vep_idx:%d empty",
4083 .format_args = "i4",
4090 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
4104 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4105 sid != ~0; sid = next_sid)
4107 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4108 u8 add_event, is_vep_session;
4110 u64 session_ev_data;
4112 VCL_LOCK_AND_GET_SESSION (sid, &session);
4113 next_sid = session->vep.next_sid;
4114 session_events = session->vep.ev.events;
4115 et_mask = session->vep.et_mask;
4116 is_vep = session->is_vep;
4117 is_vep_session = session->is_vep_session;
4118 session_vep_idx = session->vep.vep_idx;
4119 session_ev_data = session->vep.ev.data.u64;
4120 clib_spinlock_unlock (&vcm->sessions_lockp);
4122 if (PREDICT_FALSE (is_vep))
4124 if (VPPCOM_DEBUG > 0)
4125 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4126 getpid (), vep_idx);
4127 if (VPPCOM_DEBUG > 0)
4130 ELOG_TYPE_DECLARE (e) =
4132 .format = "ERR:vep_idx:%d is vep",
4133 .format_args = "i4",
4140 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4149 if (PREDICT_FALSE (!is_vep_session))
4151 if (VPPCOM_DEBUG > 0)
4152 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4153 "a vep session!", getpid (), sid);
4154 if (VPPCOM_DEBUG > 0)
4157 ELOG_TYPE_DECLARE (e) =
4159 .format = "ERR:SID:%d not vep",
4160 .format_args = "i4",
4167 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4176 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4178 clib_warning ("VCL<%d>: ERROR: session (%u) "
4179 "vep_idx (%u) != vep_idx (%u)!",
4180 getpid (), sid, session->vep.vep_idx, vep_idx);
4185 add_event = clear_et_mask = 0;
4187 if (EPOLLIN & session_events)
4189 VCL_LOCK_AND_GET_SESSION (sid, &session);
4190 ready = vppcom_session_read_ready (session, sid);
4191 clib_spinlock_unlock (&vcm->sessions_lockp);
4192 if ((ready > 0) && (EPOLLIN & et_mask))
4195 events[num_ev].events |= EPOLLIN;
4196 if (((EPOLLET | EPOLLIN) & session_events) ==
4197 (EPOLLET | EPOLLIN))
4198 clear_et_mask |= EPOLLIN;
4205 case VPPCOM_ECONNRESET:
4206 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4210 events[num_ev].events |= EPOLLERR;
4216 if (EPOLLOUT & session_events)
4218 VCL_LOCK_AND_GET_SESSION (sid, &session);
4219 ready = vppcom_session_write_ready (session, sid);
4220 clib_spinlock_unlock (&vcm->sessions_lockp);
4221 if ((ready > 0) && (EPOLLOUT & et_mask))
4224 events[num_ev].events |= EPOLLOUT;
4225 if (((EPOLLET | EPOLLOUT) & session_events) ==
4226 (EPOLLET | EPOLLOUT))
4227 clear_et_mask |= EPOLLOUT;
4234 case VPPCOM_ECONNRESET:
4235 events[num_ev].events |= EPOLLHUP;
4239 events[num_ev].events |= EPOLLERR;
4247 events[num_ev].data.u64 = session_ev_data;
4248 if (EPOLLONESHOT & session_events)
4250 VCL_LOCK_AND_GET_SESSION (sid, &session);
4251 session->vep.ev.events = 0;
4252 clib_spinlock_unlock (&vcm->sessions_lockp);
4255 if (num_ev == maxevents)
4257 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4258 vep_session->wait_cont_idx = next_sid;
4259 clib_spinlock_unlock (&vcm->sessions_lockp);
4263 if (wait_cont_idx != ~0)
4266 next_sid = vep_next_sid;
4267 else if (next_sid == wait_cont_idx)
4271 if (wait_for_time != -1)
4272 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4274 while ((num_ev == 0) && keep_trying);
4276 if (wait_cont_idx != ~0)
4278 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4279 vep_session->wait_cont_idx = ~0;
4280 clib_spinlock_unlock (&vcm->sessions_lockp);
4283 return (rv != VPPCOM_OK) ? rv : num_ev;
4287 vppcom_session_attr (uint32_t session_index, uint32_t op,
4288 void *buffer, uint32_t * buflen)
4292 u32 *flags = buffer;
4293 vppcom_endpt_t *ep = buffer;
4295 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4298 case VPPCOM_ATTR_GET_NREAD:
4299 rv = vppcom_session_read_ready (session, session_index);
4300 if (VPPCOM_DEBUG > 2)
4301 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4303 if (VPPCOM_DEBUG > 0)
4306 ELOG_TYPE_DECLARE (e) =
4308 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4309 .format_args = "i4",
4316 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4324 case VPPCOM_ATTR_GET_NWRITE:
4325 rv = vppcom_session_write_ready (session, session_index);
4326 if (VPPCOM_DEBUG > 2)
4327 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4328 getpid (), session_index, rv);
4329 if (VPPCOM_DEBUG > 0)
4332 ELOG_TYPE_DECLARE (e) =
4334 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4335 .format_args = "i4",
4342 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4349 case VPPCOM_ATTR_GET_FLAGS:
4350 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4352 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
4353 *buflen = sizeof (*flags);
4354 if (VPPCOM_DEBUG > 2)
4355 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4356 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4357 session_index, *flags, session->is_nonblocking);
4358 if (VPPCOM_DEBUG > 0)
4361 ELOG_TYPE_DECLARE (e) =
4363 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4364 .format_args = "i4i4",
4372 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4375 ed->is_nonblk = session->is_nonblocking;
4384 case VPPCOM_ATTR_SET_FLAGS:
4385 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4387 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
4388 if (VPPCOM_DEBUG > 2)
4389 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4390 "flags = 0x%08x, is_nonblocking = %u",
4391 getpid (), session_index, *flags,
4392 session->is_nonblocking);
4393 if (VPPCOM_DEBUG > 0)
4396 ELOG_TYPE_DECLARE (e) =
4398 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4399 .format_args = "i4i4",
4407 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4410 ed->is_nonblk = session->is_nonblocking;
4418 case VPPCOM_ATTR_GET_PEER_ADDR:
4419 if (PREDICT_TRUE (buffer && buflen &&
4420 (*buflen >= sizeof (*ep)) && ep->ip))
4422 ep->vrf = session->vrf;
4423 ep->is_ip4 = session->peer_addr.is_ip4;
4424 ep->port = session->peer_port;
4425 if (session->peer_addr.is_ip4)
4426 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4427 sizeof (ip4_address_t));
4429 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4430 sizeof (ip6_address_t));
4431 *buflen = sizeof (*ep);
4432 if (VPPCOM_DEBUG > 1)
4433 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4434 "is_ip4 = %u, addr = %U, port %u", getpid (),
4435 session_index, ep->is_ip4, format_ip46_address,
4436 &session->peer_addr.ip46, ep->is_ip4,
4437 clib_net_to_host_u16 (ep->port));
4438 if (VPPCOM_DEBUG > 0)
4443 ELOG_TYPE_DECLARE (e) =
4445 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4446 .format_args = "i1i1i1i1i2",
4448 CLIB_PACKED (struct {
4453 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4455 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4456 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4457 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4458 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4459 ed->port = clib_net_to_host_u16 (session->peer_port);
4465 ELOG_TYPE_DECLARE (e) =
4467 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4468 .format_args = "i2",
4470 CLIB_PACKED (struct {
4474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4476 ed->port = clib_net_to_host_u16 (session->peer_port);
4485 case VPPCOM_ATTR_GET_LCL_ADDR:
4486 if (PREDICT_TRUE (buffer && buflen &&
4487 (*buflen >= sizeof (*ep)) && ep->ip))
4489 ep->vrf = session->vrf;
4490 ep->is_ip4 = session->lcl_addr.is_ip4;
4491 ep->port = session->lcl_port;
4492 if (session->lcl_addr.is_ip4)
4493 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4494 sizeof (ip4_address_t));
4496 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4497 sizeof (ip6_address_t));
4498 *buflen = sizeof (*ep);
4499 if (VPPCOM_DEBUG > 1)
4500 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4501 "is_ip4 = %u, addr = %U port %d", getpid (),
4502 session_index, ep->is_ip4, format_ip46_address,
4503 &session->lcl_addr.ip46, ep->is_ip4,
4504 clib_net_to_host_u16 (ep->port));
4505 if (VPPCOM_DEBUG > 0)
4510 ELOG_TYPE_DECLARE (e) =
4512 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4513 .format_args = "i1i1i1i1i2",
4515 CLIB_PACKED (struct {
4520 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4522 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4523 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4524 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4525 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4526 ed->port = clib_net_to_host_u16 (session->peer_port);
4532 ELOG_TYPE_DECLARE (e) =
4534 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4535 .format_args = "i2",
4537 CLIB_PACKED (struct {
4541 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4543 ed->port = clib_net_to_host_u16 (session->peer_port);
4552 case VPPCOM_ATTR_GET_LIBC_EPFD:
4553 rv = session->libc_epfd;
4554 if (VPPCOM_DEBUG > 2)
4555 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4557 if (VPPCOM_DEBUG > 0)
4560 ELOG_TYPE_DECLARE (e) =
4562 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4563 .format_args = "t1i4i4",
4564 .n_enum_strings = 2,
4565 .enum_strings = {"", "-",},
4567 CLIB_PACKED (struct {
4572 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4574 ed->sign = (session->libc_epfd < 0);
4575 ed->data[0] = abs(session->libc_epfd);
4576 ed->data[1] = *buflen;
4582 case VPPCOM_ATTR_SET_LIBC_EPFD:
4583 if (PREDICT_TRUE (buffer && buflen &&
4584 (*buflen == sizeof (session->libc_epfd))))
4586 session->libc_epfd = *(int *) buffer;
4587 *buflen = sizeof (session->libc_epfd);
4589 if (VPPCOM_DEBUG > 2)
4590 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4591 "buflen %d", getpid (), session->libc_epfd,
4593 if (VPPCOM_DEBUG > 0)
4596 ELOG_TYPE_DECLARE (e) =
4598 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4599 .format_args = "t1i4i4",
4600 .n_enum_strings = 2,
4601 .enum_strings = {"", "-",},
4603 CLIB_PACKED (struct {
4608 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4610 ed->sign = (session->libc_epfd < 0);
4611 ed->data[0] = abs(session->libc_epfd);
4612 ed->data[1] = *buflen;
4620 case VPPCOM_ATTR_GET_PROTOCOL:
4621 if (buffer && buflen && (*buflen >= sizeof (int)))
4623 *(int *) buffer = session->proto;
4624 *buflen = sizeof (int);
4626 if (VPPCOM_DEBUG > 2)
4627 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4628 "buflen %d", getpid (), *(int *) buffer,
4629 *(int *) buffer ? "UDP" : "TCP", *buflen);
4630 if (VPPCOM_DEBUG > 0)
4633 ELOG_TYPE_DECLARE (e) =
4635 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4636 .format_args = "t1i4",
4637 .n_enum_strings = 2,
4638 .enum_strings = {"TCP", "UDP",},
4641 CLIB_PACKED (struct {
4646 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4647 ed->proto = session->proto;
4648 ed->buflen = *(int *) buffer;
4656 case VPPCOM_ATTR_GET_LISTEN:
4657 if (buffer && buflen && (*buflen >= sizeof (int)))
4659 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4660 VCL_SESS_ATTR_LISTEN);
4661 *buflen = sizeof (int);
4663 if (VPPCOM_DEBUG > 2)
4664 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4665 "buflen %d", getpid (), *(int *) buffer, *buflen);
4666 if (VPPCOM_DEBUG > 0)
4669 ELOG_TYPE_DECLARE (e) =
4671 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4672 .format_args = "i4i4",
4679 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4680 ed->data[0] = *(int *) buffer;
4681 ed->data[1] = *buflen;
4689 case VPPCOM_ATTR_GET_ERROR:
4690 if (buffer && buflen && (*buflen >= sizeof (int)))
4692 *(int *) buffer = 0;
4693 *buflen = sizeof (int);
4695 if (VPPCOM_DEBUG > 2)
4696 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4697 "buflen %d, #VPP-TBD#", getpid (),
4698 *(int *) buffer, *buflen);
4699 if (VPPCOM_DEBUG > 0)
4702 ELOG_TYPE_DECLARE (e) =
4704 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4705 .format_args = "i4i4",
4712 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4713 ed->data[0] = *(int *) buffer;
4714 ed->data[1] = *buflen;
4722 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4723 if (buffer && buflen && (*buflen >= sizeof (u32)))
4725 svm_fifo_t *tx_fifo;
4727 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
4728 session->server_tx_fifo : session->server_rx_fifo);
4731 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4732 tx_fifo ? tx_fifo->nitems :
4733 vcm->cfg.tx_fifo_size);
4734 *buflen = sizeof (u32);
4736 if (VPPCOM_DEBUG > 2)
4737 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4738 "buflen %d, #VPP-TBD#", getpid (),
4739 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4740 if (VPPCOM_DEBUG > 0)
4743 ELOG_TYPE_DECLARE (e) =
4745 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4746 .format_args = "i4i4",
4753 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4754 ed->data[0] = *(size_t *) buffer;
4755 ed->data[1] = *buflen;
4763 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4764 if (buffer && buflen && (*buflen == sizeof (u32)))
4767 session->sndbuf_size = *(u32 *) buffer;
4768 if (VPPCOM_DEBUG > 2)
4769 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4770 "buflen %d, #VPP-TBD#", getpid (),
4771 session->sndbuf_size, session->sndbuf_size,
4773 if (VPPCOM_DEBUG > 0)
4776 ELOG_TYPE_DECLARE (e) =
4778 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4779 .format_args = "i4i4",
4786 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4787 ed->data[0] = session->sndbuf_size;
4788 ed->data[1] = *buflen;
4796 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4797 if (buffer && buflen && (*buflen >= sizeof (u32)))
4799 svm_fifo_t *rx_fifo;
4801 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
4802 session->server_rx_fifo : session->server_tx_fifo);
4805 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4806 rx_fifo ? rx_fifo->nitems :
4807 vcm->cfg.rx_fifo_size);
4808 *buflen = sizeof (u32);
4810 if (VPPCOM_DEBUG > 2)
4811 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4812 "buflen %d, #VPP-TBD#", getpid (),
4813 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4814 if (VPPCOM_DEBUG > 0)
4817 ELOG_TYPE_DECLARE (e) =
4819 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4820 .format_args = "i4i4",
4827 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4828 ed->data[0] = *(size_t *) buffer;
4829 ed->data[1] = *buflen;
4837 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4838 if (buffer && buflen && (*buflen == sizeof (u32)))
4841 session->rcvbuf_size = *(u32 *) buffer;
4842 if (VPPCOM_DEBUG > 2)
4843 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4844 "buflen %d, #VPP-TBD#", getpid (),
4845 session->sndbuf_size, session->sndbuf_size,
4847 if (VPPCOM_DEBUG > 0)
4850 ELOG_TYPE_DECLARE (e) =
4852 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4853 .format_args = "i4i4",
4860 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4861 ed->data[0] = session->sndbuf_size;
4862 ed->data[1] = *buflen;
4870 case VPPCOM_ATTR_GET_REUSEADDR:
4871 if (buffer && buflen && (*buflen >= sizeof (int)))
4874 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4875 VCL_SESS_ATTR_REUSEADDR);
4876 *buflen = sizeof (int);
4878 if (VPPCOM_DEBUG > 2)
4879 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4880 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4882 if (VPPCOM_DEBUG > 0)
4885 ELOG_TYPE_DECLARE (e) =
4887 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4888 .format_args = "i4i4",
4895 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4896 ed->data[0] = *(int *) buffer;
4897 ed->data[1] = *buflen;
4905 case VPPCOM_ATTR_SET_REUSEADDR:
4906 if (buffer && buflen && (*buflen == sizeof (int)) &&
4907 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4910 if (*(int *) buffer)
4911 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4913 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4915 if (VPPCOM_DEBUG > 2)
4916 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4917 "buflen %d, #VPP-TBD#", getpid (),
4918 VCL_SESS_ATTR_TEST (session->attr,
4919 VCL_SESS_ATTR_REUSEADDR),
4921 if (VPPCOM_DEBUG > 0)
4924 ELOG_TYPE_DECLARE (e) =
4926 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4927 .format_args = "i4i4",
4934 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4935 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4936 VCL_SESS_ATTR_REUSEADDR);
4937 ed->data[1] = *buflen;
4945 case VPPCOM_ATTR_GET_REUSEPORT:
4946 if (buffer && buflen && (*buflen >= sizeof (int)))
4949 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4950 VCL_SESS_ATTR_REUSEPORT);
4951 *buflen = sizeof (int);
4953 if (VPPCOM_DEBUG > 2)
4954 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4955 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4957 if (VPPCOM_DEBUG > 0)
4960 ELOG_TYPE_DECLARE (e) =
4962 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4963 .format_args = "i4i4",
4970 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4971 ed->data[0] = *(int *) buffer;
4972 ed->data[1] = *buflen;
4980 case VPPCOM_ATTR_SET_REUSEPORT:
4981 if (buffer && buflen && (*buflen == sizeof (int)) &&
4982 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4985 if (*(int *) buffer)
4986 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4988 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4990 if (VPPCOM_DEBUG > 2)
4991 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4992 "buflen %d, #VPP-TBD#", getpid (),
4993 VCL_SESS_ATTR_TEST (session->attr,
4994 VCL_SESS_ATTR_REUSEPORT),
4996 if (VPPCOM_DEBUG > 0)
4999 ELOG_TYPE_DECLARE (e) =
5001 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5002 .format_args = "i4i4",
5009 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5010 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5011 VCL_SESS_ATTR_REUSEPORT);
5012 ed->data[1] = *buflen;
5020 case VPPCOM_ATTR_GET_BROADCAST:
5021 if (buffer && buflen && (*buflen >= sizeof (int)))
5024 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5025 VCL_SESS_ATTR_BROADCAST);
5026 *buflen = sizeof (int);
5028 if (VPPCOM_DEBUG > 2)
5029 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5030 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5032 if (VPPCOM_DEBUG > 0)
5035 ELOG_TYPE_DECLARE (e) =
5037 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5038 .format_args = "i4i4",
5045 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5046 ed->data[0] = *(int *) buffer;
5047 ed->data[1] = *buflen;
5055 case VPPCOM_ATTR_SET_BROADCAST:
5056 if (buffer && buflen && (*buflen == sizeof (int)))
5059 if (*(int *) buffer)
5060 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5062 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5064 if (VPPCOM_DEBUG > 2)
5065 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5066 "buflen %d, #VPP-TBD#", getpid (),
5067 VCL_SESS_ATTR_TEST (session->attr,
5068 VCL_SESS_ATTR_BROADCAST),
5075 case VPPCOM_ATTR_GET_V6ONLY:
5076 if (buffer && buflen && (*buflen >= sizeof (int)))
5079 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5080 VCL_SESS_ATTR_V6ONLY);
5081 *buflen = sizeof (int);
5083 if (VPPCOM_DEBUG > 2)
5084 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5085 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5092 case VPPCOM_ATTR_SET_V6ONLY:
5093 if (buffer && buflen && (*buflen == sizeof (int)))
5096 if (*(int *) buffer)
5097 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5099 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5101 if (VPPCOM_DEBUG > 2)
5102 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5103 "buflen %d, #VPP-TBD#", getpid (),
5104 VCL_SESS_ATTR_TEST (session->attr,
5105 VCL_SESS_ATTR_V6ONLY), *buflen);
5111 case VPPCOM_ATTR_GET_KEEPALIVE:
5112 if (buffer && buflen && (*buflen >= sizeof (int)))
5115 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5116 VCL_SESS_ATTR_KEEPALIVE);
5117 *buflen = sizeof (int);
5119 if (VPPCOM_DEBUG > 2)
5120 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5121 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5128 case VPPCOM_ATTR_SET_KEEPALIVE:
5129 if (buffer && buflen && (*buflen == sizeof (int)))
5132 if (*(int *) buffer)
5133 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5135 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5137 if (VPPCOM_DEBUG > 2)
5138 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5139 "buflen %d, #VPP-TBD#", getpid (),
5140 VCL_SESS_ATTR_TEST (session->attr,
5141 VCL_SESS_ATTR_KEEPALIVE),
5148 case VPPCOM_ATTR_GET_TCP_NODELAY:
5149 if (buffer && buflen && (*buflen >= sizeof (int)))
5152 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5153 VCL_SESS_ATTR_TCP_NODELAY);
5154 *buflen = sizeof (int);
5156 if (VPPCOM_DEBUG > 2)
5157 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5158 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5165 case VPPCOM_ATTR_SET_TCP_NODELAY:
5166 if (buffer && buflen && (*buflen == sizeof (int)))
5169 if (*(int *) buffer)
5170 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5172 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5174 if (VPPCOM_DEBUG > 2)
5175 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5176 "buflen %d, #VPP-TBD#", getpid (),
5177 VCL_SESS_ATTR_TEST (session->attr,
5178 VCL_SESS_ATTR_TCP_NODELAY),
5185 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5186 if (buffer && buflen && (*buflen >= sizeof (int)))
5189 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5190 VCL_SESS_ATTR_TCP_KEEPIDLE);
5191 *buflen = sizeof (int);
5193 if (VPPCOM_DEBUG > 2)
5194 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5195 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5202 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5203 if (buffer && buflen && (*buflen == sizeof (int)))
5206 if (*(int *) buffer)
5207 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5209 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5211 if (VPPCOM_DEBUG > 2)
5212 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5213 "buflen %d, #VPP-TBD#", getpid (),
5214 VCL_SESS_ATTR_TEST (session->attr,
5215 VCL_SESS_ATTR_TCP_KEEPIDLE),
5222 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5223 if (buffer && buflen && (*buflen >= sizeof (int)))
5226 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5227 VCL_SESS_ATTR_TCP_KEEPINTVL);
5228 *buflen = sizeof (int);
5230 if (VPPCOM_DEBUG > 2)
5231 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5232 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5239 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5240 if (buffer && buflen && (*buflen == sizeof (int)))
5243 if (*(int *) buffer)
5244 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5246 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5248 if (VPPCOM_DEBUG > 2)
5249 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5250 "buflen %d, #VPP-TBD#", getpid (),
5251 VCL_SESS_ATTR_TEST (session->attr,
5252 VCL_SESS_ATTR_TCP_KEEPINTVL),
5259 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5260 if (buffer && buflen && (*buflen >= sizeof (u32)))
5263 *(u32 *) buffer = session->user_mss;
5264 *buflen = sizeof (int);
5266 if (VPPCOM_DEBUG > 2)
5267 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5268 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5275 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5276 if (buffer && buflen && (*buflen == sizeof (u32)))
5279 session->user_mss = *(u32 *) buffer;
5281 if (VPPCOM_DEBUG > 2)
5282 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %u, "
5283 "buflen %d, #VPP-TBD#", getpid (),
5284 session->user_mss, *buflen);
5296 clib_spinlock_unlock (&vcm->sessions_lockp);
5301 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5302 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5305 session_t *session = 0;
5309 clib_spinlock_lock (&vcm->sessions_lockp);
5310 rv = vppcom_session_at_index (session_index, &session);
5311 if (PREDICT_FALSE (rv))
5313 clib_spinlock_unlock (&vcm->sessions_lockp);
5314 if (VPPCOM_DEBUG > 0)
5315 clib_warning ("VCL<%d>: invalid session, "
5316 "sid (%u) has been closed!",
5317 getpid (), session_index);
5319 clib_spinlock_unlock (&vcm->sessions_lockp);
5322 ep->vrf = session->vrf;
5323 ep->is_ip4 = session->peer_addr.is_ip4;
5324 ep->port = session->peer_port;
5325 if (session->peer_addr.is_ip4)
5326 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5327 sizeof (ip4_address_t));
5329 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5330 sizeof (ip6_address_t));
5331 clib_spinlock_unlock (&vcm->sessions_lockp);
5335 rv = vppcom_session_read (session_index, buffer, buflen);
5336 else if (flags & MSG_PEEK)
5337 rv = vppcom_session_peek (session_index, buffer, buflen);
5340 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5342 rv = VPPCOM_EAFNOSUPPORT;
5350 vppcom_session_sendto (uint32_t session_index, void *buffer,
5351 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5354 return VPPCOM_EINVAL;
5359 return VPPCOM_EINVAL;
5364 // TBD check the flags and do the right thing
5365 if (VPPCOM_DEBUG > 2)
5366 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5367 "not implemented yet.", getpid (), flags, flags);
5370 return (vppcom_session_write (session_index, buffer, buflen));
5374 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5376 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5377 u32 i, keep_trying = 1;
5380 if (VPPCOM_DEBUG > 3)
5381 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5382 getpid (), vp, n_sids, wait_for_time);
5385 return VPPCOM_EFAULT;
5391 for (i = 0; i < n_sids; i++)
5393 ASSERT (vp[i].revents);
5395 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5396 clib_spinlock_unlock (&vcm->sessions_lockp);
5401 if (POLLIN & vp[i].events)
5403 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5404 rv = vppcom_session_read_ready (session, vp[i].sid);
5405 clib_spinlock_unlock (&vcm->sessions_lockp);
5408 *vp[i].revents |= POLLIN;
5415 case VPPCOM_ECONNRESET:
5416 *vp[i].revents = POLLHUP;
5420 *vp[i].revents = POLLERR;
5427 if (POLLOUT & vp[i].events)
5429 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5430 rv = vppcom_session_write_ready (session, vp[i].sid);
5431 clib_spinlock_unlock (&vcm->sessions_lockp);
5434 *vp[i].revents |= POLLOUT;
5441 case VPPCOM_ECONNRESET:
5442 *vp[i].revents = POLLHUP;
5446 *vp[i].revents = POLLERR;
5456 *vp[i].revents = POLLNVAL;
5460 if (wait_for_time != -1)
5461 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5463 while ((num_ev == 0) && keep_trying);
5465 if (VPPCOM_DEBUG > 3)
5467 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5468 for (i = 0; i < n_sids; i++)
5470 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5471 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5472 vp[i].events, *vp[i].revents);
5479 * fd.io coding-style-patch-verification: ON
5482 * eval: (c-set-style "gnu")