2 * Copyright (c) 2018 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.
16 #ifndef SRC_VCL_VCL_PRIVATE_H_
17 #define SRC_VCL_VCL_PRIVATE_H_
19 #include <vnet/session/application_interface.h>
20 #include <vcl/vppcom.h>
21 #include <vcl/vcl_event.h>
22 #include <vcl/vcl_debug.h>
25 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
26 * 3 for read/write debug output
28 * export VCL_DEBUG=<#> to set dynamically.
30 #define VPPCOM_DEBUG_INIT 1
32 #define VPPCOM_DEBUG_INIT 0
35 #define VPPCOM_DEBUG vcm->debug
38 * VPPCOM Private definitions and functions.
54 STATE_CLOSE_ON_EMPTY = 0x10,
55 STATE_DISCONNECT = 0x20,
59 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
60 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
61 #define STATE_OPEN (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)
63 typedef struct epoll_event vppcom_epoll_event_t;
70 vppcom_epoll_event_t ev;
71 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
72 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
82 typedef struct vcl_session_msg
87 session_accepted_msg_t accepted_msg;
94 VCL_SESS_ATTR_CUT_THRU,
96 VCL_SESS_ATTR_VEP_SESSION,
97 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
98 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
99 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
100 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
101 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
102 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
103 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
104 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
105 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
106 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
108 } vppcom_session_attr_t;
110 #define VCL_SESS_ATTR_SET(ATTR, VAL) \
112 (ATTR) |= 1 << (VAL); \
115 #define VCL_SESS_ATTR_CLR(ATTR, VAL) \
117 (ATTR) &= ~(1 << (VAL)); \
120 #define VCL_SESS_ATTR_TEST(ATTR, VAL) \
121 ((ATTR) & (1 << (VAL)) ? 1 : 0)
125 #define _(type, name) type name;
126 foreach_app_session_field
128 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
129 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
130 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
136 /* Socket configuration state */
143 svm_msg_q_t *our_evt_q;
146 vce_event_handler_reg_t *poll_reg;
147 vcl_session_msg_t *accept_evts_fifo;
149 elog_track_t elog_track;
153 typedef struct vppcom_cfg_t_
156 u32 vpp_api_q_length;
159 u32 add_segment_size;
160 u32 preallocated_fifo_pairs;
163 u32 event_queue_size;
164 u32 listen_queue_size;
165 u8 app_proxy_transport_tcp;
166 u8 app_proxy_transport_udp;
170 u64 namespace_secret;
175 char *event_log_path;
176 u8 *vpp_api_filename;
179 void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
181 typedef struct vcl_cut_through_registration_
185 } vcl_cut_through_registration_t;
187 typedef struct vppcom_main_t_
193 /* FIFO for accepted connections - used in epoll/select */
194 clib_spinlock_t session_fifo_lockp;
195 u32 *client_session_index_fifo;
197 /* vpp input queue */
198 svm_queue_t *vl_input_queue;
200 /* API client handle */
203 clib_spinlock_t sessions_lockp;
204 vcl_session_t *sessions;
206 /* Hash table for disconnect processing */
207 uword *session_index_by_vpp_handles;
210 clib_bitmap_t *rd_bitmap;
211 clib_bitmap_t *wr_bitmap;
212 clib_bitmap_t *ex_bitmap;
214 /* Our event queue */
215 svm_msg_q_t *app_event_queue;
217 /* unique segment name counter */
218 u32 unique_segment_index;
220 /* For deadman timers */
221 clib_time_t clib_time;
223 /* State of the connection, shared between msg RX thread and main thread */
224 volatile app_state_t app_state;
229 vce_event_thread_t event_thread;
232 vppcom_session_io_thread_t session_io_thread;
234 /* pool of ctrl msgs */
235 vcl_session_msg_t *ctrl_evt_pool;
237 /** Pool of cut through registrations */
238 vcl_cut_through_registration_t *cut_through_registrations;
241 /* VPP Event-logger */
242 elog_main_t elog_main;
243 elog_track_t elog_track;
246 /* VNET_API_ERROR_FOO -> "Foo" hash table */
247 uword *error_string_by_error_number;
250 extern vppcom_main_t *vcm;
252 #define VCL_SESSION_LOCK_AND_GET(I, S) \
254 clib_spinlock_lock (&vcm->sessions_lockp); \
255 rv = vppcom_session_at_index (I, S); \
256 if (PREDICT_FALSE (rv)) \
258 clib_spinlock_unlock (&vcm->sessions_lockp); \
259 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
265 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
266 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
268 #define VCL_IO_SESSIONS_LOCK() \
269 clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
270 #define VCL_IO_SESSIONS_UNLOCK() \
271 clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
273 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
274 #define VCL_ACCEPT_FIFO_UNLOCK() \
275 clib_spinlock_unlock (&(vcm->session_fifo_lockp))
277 #define VCL_EVENTS_LOCK() \
278 clib_spinlock_lock (&(vcm->event_thread.events_lockp))
279 #define VCL_EVENTS_UNLOCK() \
280 clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
282 #define VCL_INVALID_SESSION_INDEX ((u32)~0)
284 static inline vcl_session_t *
285 vcl_session_get (u32 session_index)
287 if (pool_is_free_index (vcm->sessions, session_index))
289 return pool_elt_at_index (vcm->sessions, session_index);
293 vcl_session_index (vcl_session_t * s)
295 return (s - vcm->sessions);
298 static inline vcl_session_t *
299 vcl_session_get_w_handle (u64 handle)
302 if ((p = hash_get (vcm->session_index_by_vpp_handles, handle)))
303 return vcl_session_get ((u32) p[0]);
308 vcl_session_get_index_from_handle (u64 handle)
311 if ((p = hash_get (vcm->session_index_by_vpp_handles, handle)))
313 return VCL_INVALID_SESSION_INDEX;
317 vppcom_session_at_index (u32 session_index, vcl_session_t * volatile *sess)
319 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
320 if (PREDICT_FALSE ((session_index == ~0) ||
321 pool_is_free_index (vcm->sessions, session_index)))
323 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
324 getpid (), session_index);
325 return VPPCOM_EBADFD;
327 *sess = pool_elt_at_index (vcm->sessions, session_index);
332 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
334 /* Session and listener handles have different formats. The latter has
335 * the thread index in the upper 32 bits while the former has the session
336 * type. Knowing that, for listeners we just flip the MSB to 1 */
337 listener_handle |= 1ULL << 63;
338 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
341 static inline vcl_session_t *
342 vppcom_session_table_lookup_listener (u64 listener_handle)
345 u64 handle = listener_handle | (1ULL << 63);
346 vcl_session_t *session;
348 p = hash_get (vcm->session_index_by_vpp_handles, handle);
351 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
352 "listener handle %llx", getpid (), listener_handle);
355 if (pool_is_free_index (vcm->sessions, p[0]))
357 VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
361 session = pool_elt_at_index (vcm->sessions, p[0]);
362 ASSERT (session->session_state & STATE_LISTEN);
366 const char *vppcom_session_state_str (session_state_t state);
371 int vppcom_connect_to_vpp (char *app_name);
372 void vppcom_init_error_string_table (void);
373 void vppcom_send_session_enable_disable (u8 is_enable);
374 void vppcom_app_send_attach (void);
375 void vppcom_app_send_detach (void);
376 void vppcom_send_connect_sock (vcl_session_t * session, u32 session_index);
377 void vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
379 void vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index);
380 void vppcom_send_bind_sock (vcl_session_t * session, u32 session_index);
381 void vppcom_send_unbind_sock (u64 vpp_handle);
382 void vppcom_api_hookup (void);
383 void vppcom_send_accept_session_reply (u64 handle, u32 context, int retval);
385 u32 vcl_max_nsid_len (void);
387 u8 *format_api_error (u8 * s, va_list * args);
389 #endif /* SRC_VCL_VCL_PRIVATE_H_ */
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")