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)
62 typedef struct epoll_event vppcom_epoll_event_t;
69 vppcom_epoll_event_t ev;
70 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
71 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
84 VCL_SESS_ATTR_CUT_THRU,
86 VCL_SESS_ATTR_VEP_SESSION,
87 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
88 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
89 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
90 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
91 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
92 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
93 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
94 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
95 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
96 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
98 } vppcom_session_attr_t;
100 #define VCL_SESS_ATTR_SET(ATTR, VAL) \
102 (ATTR) |= 1 << (VAL); \
105 #define VCL_SESS_ATTR_CLR(ATTR, VAL) \
107 (ATTR) &= ~(1 << (VAL)); \
110 #define VCL_SESS_ATTR_TEST(ATTR, VAL) \
111 ((ATTR) & (1 << (VAL)) ? 1 : 0)
115 #define _(type, name) type name;
116 foreach_app_session_field
118 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
119 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
120 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
126 /* Socket configuration state */
133 u64 client_queue_address;
135 vce_event_handler_reg_t *poll_reg;
137 elog_track_t elog_track;
141 typedef struct vppcom_cfg_t_
144 u32 vpp_api_q_length;
147 u32 add_segment_size;
148 u32 preallocated_fifo_pairs;
151 u32 event_queue_size;
152 u32 listen_queue_size;
153 u8 app_proxy_transport_tcp;
154 u8 app_proxy_transport_udp;
158 u64 namespace_secret;
163 char *event_log_path;
164 u8 *vpp_api_filename;
167 void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
169 typedef struct vppcom_main_t_
175 /* FIFO for accepted connections - used in epoll/select */
176 clib_spinlock_t session_fifo_lockp;
177 u32 *client_session_index_fifo;
179 /* vpp input queue */
180 svm_queue_t *vl_input_queue;
182 /* API client handle */
185 clib_spinlock_t sessions_lockp;
186 vcl_session_t *sessions;
188 /* Hash table for disconnect processing */
189 uword *session_index_by_vpp_handles;
192 clib_bitmap_t *rd_bitmap;
193 clib_bitmap_t *wr_bitmap;
194 clib_bitmap_t *ex_bitmap;
196 /* Our event queue */
197 svm_queue_t *app_event_queue;
199 /* unique segment name counter */
200 u32 unique_segment_index;
202 /* For deadman timers */
203 clib_time_t clib_time;
205 /* State of the connection, shared between msg RX thread and main thread */
206 volatile app_state_t app_state;
211 vce_event_thread_t event_thread;
214 vppcom_session_io_thread_t session_io_thread;
217 /* VPP Event-logger */
218 elog_main_t elog_main;
219 elog_track_t elog_track;
222 /* VNET_API_ERROR_FOO -> "Foo" hash table */
223 uword *error_string_by_error_number;
226 extern vppcom_main_t *vcm;
228 #define VCL_SESSION_LOCK_AND_GET(I, S) \
230 clib_spinlock_lock (&vcm->sessions_lockp); \
231 rv = vppcom_session_at_index (I, S); \
232 if (PREDICT_FALSE (rv)) \
234 clib_spinlock_unlock (&vcm->sessions_lockp); \
235 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
241 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
242 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
244 #define VCL_IO_SESSIONS_LOCK() \
245 clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
246 #define VCL_IO_SESSIONS_UNLOCK() \
247 clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
249 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
250 #define VCL_ACCEPT_FIFO_UNLOCK() \
251 clib_spinlock_unlock (&(vcm->session_fifo_lockp))
253 #define VCL_EVENTS_LOCK() \
254 clib_spinlock_lock (&(vcm->event_thread.events_lockp))
255 #define VCL_EVENTS_UNLOCK() \
256 clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
259 vppcom_session_at_index (u32 session_index, vcl_session_t * volatile *sess)
261 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
262 if (PREDICT_FALSE ((session_index == ~0) ||
263 pool_is_free_index (vcm->sessions, session_index)))
265 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
266 getpid (), session_index);
267 return VPPCOM_EBADFD;
269 *sess = pool_elt_at_index (vcm->sessions, session_index);
274 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
276 /* Session and listener handles have different formats. The latter has
277 * the thread index in the upper 32 bits while the former has the session
278 * type. Knowing that, for listeners we just flip the MSB to 1 */
279 listener_handle |= 1ULL << 63;
280 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
283 static inline vcl_session_t *
284 vppcom_session_table_lookup_listener (u64 listener_handle)
287 u64 handle = listener_handle | (1ULL << 63);
288 vcl_session_t *session;
290 p = hash_get (vcm->session_index_by_vpp_handles, handle);
293 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
294 "listener handle %llx", getpid (), listener_handle);
297 if (pool_is_free_index (vcm->sessions, p[0]))
299 VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
303 session = pool_elt_at_index (vcm->sessions, p[0]);
304 ASSERT (session->session_state & STATE_LISTEN);
308 const char *vppcom_session_state_str (session_state_t state);
313 int vppcom_connect_to_vpp (char *app_name);
314 void vppcom_init_error_string_table (void);
315 void vppcom_send_session_enable_disable (u8 is_enable);
316 void vppcom_app_send_attach (void);
317 void vppcom_app_send_detach (void);
318 void vppcom_send_connect_sock (vcl_session_t * session, u32 session_index);
319 void vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
321 void vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index);
322 void vppcom_send_bind_sock (vcl_session_t * session, u32 session_index);
323 void vppcom_send_unbind_sock (u64 vpp_handle);
324 void vppcom_api_hookup (void);
325 void vppcom_send_accept_session_reply (u64 handle, u32 context, int retval);
327 u32 vcl_max_nsid_len (void);
329 #endif /* SRC_VCL_VCL_PRIVATE_H_ */
332 * fd.io coding-style-patch-verification: ON
335 * eval: (c-set-style "gnu")