Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #ifndef SRC_VCL_VCL_PRIVATE_H_ |
| 17 | #define SRC_VCL_VCL_PRIVATE_H_ |
| 18 | |
| 19 | #include <vnet/session/application_interface.h> |
| 20 | #include <vcl/vppcom.h> |
| 21 | #include <vcl/vcl_event.h> |
| 22 | #include <vcl/vcl_debug.h> |
| 23 | |
| 24 | #if (CLIB_DEBUG > 0) |
| 25 | /* Set VPPCOM_DEBUG_INIT 2 for connection debug, |
| 26 | * 3 for read/write debug output |
| 27 | * or |
| 28 | * export VCL_DEBUG=<#> to set dynamically. |
| 29 | */ |
| 30 | #define VPPCOM_DEBUG_INIT 1 |
| 31 | #else |
| 32 | #define VPPCOM_DEBUG_INIT 0 |
| 33 | #endif |
| 34 | |
| 35 | #define VPPCOM_DEBUG vcm->debug |
| 36 | |
| 37 | /* |
| 38 | * VPPCOM Private definitions and functions. |
| 39 | */ |
| 40 | typedef enum |
| 41 | { |
| 42 | STATE_APP_START, |
| 43 | STATE_APP_CONN_VPP, |
| 44 | STATE_APP_ENABLED, |
| 45 | STATE_APP_ATTACHED, |
| 46 | } app_state_t; |
| 47 | |
| 48 | typedef enum |
| 49 | { |
| 50 | STATE_START = 0x01, |
| 51 | STATE_CONNECT = 0x02, |
| 52 | STATE_LISTEN = 0x04, |
| 53 | STATE_ACCEPT = 0x08, |
| 54 | STATE_CLOSE_ON_EMPTY = 0x10, |
| 55 | STATE_DISCONNECT = 0x20, |
| 56 | STATE_FAILED = 0x40 |
| 57 | } session_state_t; |
| 58 | |
| 59 | #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY) |
| 60 | #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY) |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 61 | #define STATE_OPEN (SERVER_STATE_OPEN | CLIENT_STATE_OPEN) |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 62 | |
| 63 | typedef struct epoll_event vppcom_epoll_event_t; |
| 64 | |
| 65 | typedef struct |
| 66 | { |
| 67 | u32 next_sid; |
| 68 | u32 prev_sid; |
| 69 | u32 vep_idx; |
| 70 | vppcom_epoll_event_t ev; |
| 71 | #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT) |
| 72 | #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE) |
| 73 | u32 et_mask; |
| 74 | } vppcom_epoll_t; |
| 75 | |
| 76 | typedef struct |
| 77 | { |
| 78 | u8 is_ip4; |
| 79 | ip46_address_t ip46; |
| 80 | } vppcom_ip46_t; |
| 81 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 82 | typedef struct vcl_session_msg |
| 83 | { |
| 84 | u32 next; |
| 85 | union |
| 86 | { |
| 87 | session_accepted_msg_t accepted_msg; |
| 88 | }; |
| 89 | } vcl_session_msg_t; |
| 90 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 91 | enum |
| 92 | { |
| 93 | VCL_SESS_ATTR_SERVER, |
| 94 | VCL_SESS_ATTR_CUT_THRU, |
| 95 | VCL_SESS_ATTR_VEP, |
| 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 |
| 107 | VCL_SESS_ATTR_MAX |
| 108 | } vppcom_session_attr_t; |
| 109 | |
| 110 | #define VCL_SESS_ATTR_SET(ATTR, VAL) \ |
| 111 | do { \ |
| 112 | (ATTR) |= 1 << (VAL); \ |
| 113 | } while (0) |
| 114 | |
| 115 | #define VCL_SESS_ATTR_CLR(ATTR, VAL) \ |
| 116 | do { \ |
| 117 | (ATTR) &= ~(1 << (VAL)); \ |
| 118 | } while (0) |
| 119 | |
| 120 | #define VCL_SESS_ATTR_TEST(ATTR, VAL) \ |
| 121 | ((ATTR) & (1 << (VAL)) ? 1 : 0) |
| 122 | |
| 123 | typedef struct |
| 124 | { |
| 125 | #define _(type, name) type name; |
| 126 | foreach_app_session_field |
| 127 | #undef _ |
| 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) |
| 131 | u8 *segment_name; |
| 132 | u32 sm_seg_index; |
| 133 | u32 client_context; |
| 134 | u64 vpp_handle; |
| 135 | |
| 136 | /* Socket configuration state */ |
| 137 | u8 is_vep; |
| 138 | u8 is_vep_session; |
| 139 | u32 attr; |
| 140 | u32 wait_cont_idx; |
| 141 | vppcom_epoll_t vep; |
| 142 | int libc_epfd; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 143 | svm_msg_q_t *our_evt_q; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 144 | u64 options[16]; |
| 145 | vce_event_handler_reg_t *poll_reg; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 146 | vcl_session_msg_t *accept_evts_fifo; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 147 | #if VCL_ELOG |
| 148 | elog_track_t elog_track; |
| 149 | #endif |
| 150 | } vcl_session_t; |
| 151 | |
| 152 | typedef struct vppcom_cfg_t_ |
| 153 | { |
| 154 | u64 heapsize; |
| 155 | u32 vpp_api_q_length; |
| 156 | u64 segment_baseva; |
| 157 | u32 segment_size; |
| 158 | u32 add_segment_size; |
| 159 | u32 preallocated_fifo_pairs; |
| 160 | u32 rx_fifo_size; |
| 161 | u32 tx_fifo_size; |
| 162 | u32 event_queue_size; |
| 163 | u32 listen_queue_size; |
| 164 | u8 app_proxy_transport_tcp; |
| 165 | u8 app_proxy_transport_udp; |
| 166 | u8 app_scope_local; |
| 167 | u8 app_scope_global; |
| 168 | u8 *namespace_id; |
| 169 | u64 namespace_secret; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 170 | u8 use_mq_eventfd; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 171 | f64 app_timeout; |
| 172 | f64 session_timeout; |
| 173 | f64 accept_timeout; |
| 174 | u32 event_ring_size; |
| 175 | char *event_log_path; |
| 176 | u8 *vpp_api_filename; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 177 | u8 *vpp_api_socket_name; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 178 | } vppcom_cfg_t; |
| 179 | |
| 180 | void vppcom_cfg (vppcom_cfg_t * vcl_cfg); |
| 181 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 182 | typedef struct vcl_cut_through_registration_ |
| 183 | { |
| 184 | svm_msg_q_t *mq; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 185 | svm_msg_q_t *peer_mq; |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 186 | u32 sid; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 187 | u32 epoll_evt_conn_index; /*< mq evt connection index part of |
| 188 | the mqs evtfd epoll (if used) */ |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 189 | } vcl_cut_through_registration_t; |
| 190 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 191 | typedef struct vcl_mq_evt_conn_ |
| 192 | { |
| 193 | svm_msg_q_t *mq; |
| 194 | int mq_fd; |
| 195 | } vcl_mq_evt_conn_t; |
| 196 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 197 | typedef struct vppcom_main_t_ |
| 198 | { |
| 199 | u8 init; |
| 200 | u32 debug; |
| 201 | int main_cpu; |
| 202 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 203 | /* vpp input queue */ |
| 204 | svm_queue_t *vl_input_queue; |
| 205 | |
| 206 | /* API client handle */ |
| 207 | u32 my_client_index; |
| 208 | /* Session pool */ |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 209 | vcl_session_t *sessions; |
| 210 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 211 | /** Message queues epoll fd. Initialized only if using mqs with eventfds */ |
| 212 | int mqs_epfd; |
| 213 | |
| 214 | /** Pool of event message queue event connections */ |
| 215 | vcl_mq_evt_conn_t *mq_evt_conns; |
| 216 | |
| 217 | /** Per worker buffer for receiving mq epoll events */ |
| 218 | struct epoll_event *mq_events; |
| 219 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 220 | /* Hash table for disconnect processing */ |
| 221 | uword *session_index_by_vpp_handles; |
| 222 | |
| 223 | /* Select bitmaps */ |
| 224 | clib_bitmap_t *rd_bitmap; |
| 225 | clib_bitmap_t *wr_bitmap; |
| 226 | clib_bitmap_t *ex_bitmap; |
| 227 | |
| 228 | /* Our event queue */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 229 | svm_msg_q_t *app_event_queue; |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 230 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 231 | svm_msg_q_t **vpp_event_queues; |
| 232 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 233 | /* unique segment name counter */ |
| 234 | u32 unique_segment_index; |
| 235 | |
| 236 | /* For deadman timers */ |
| 237 | clib_time_t clib_time; |
| 238 | |
| 239 | /* State of the connection, shared between msg RX thread and main thread */ |
| 240 | volatile app_state_t app_state; |
| 241 | |
| 242 | vppcom_cfg_t cfg; |
| 243 | |
| 244 | /* Event thread */ |
| 245 | vce_event_thread_t event_thread; |
| 246 | |
| 247 | /* IO thread */ |
| 248 | vppcom_session_io_thread_t session_io_thread; |
| 249 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 250 | /* pool of ctrl msgs */ |
| 251 | vcl_session_msg_t *ctrl_evt_pool; |
| 252 | |
| 253 | /** Pool of cut through registrations */ |
| 254 | vcl_cut_through_registration_t *cut_through_registrations; |
| 255 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 256 | /** Lock for accessing ct registration pool */ |
| 257 | clib_spinlock_t ct_registration_lock; |
| 258 | |
| 259 | /** Cut-through registration by mq address hash table */ |
| 260 | uword *ct_registration_by_mq; |
| 261 | |
| 262 | svm_msg_q_msg_t *mq_msg_vector; |
| 263 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 264 | /** Flag indicating that a new segment is being mounted */ |
| 265 | volatile u32 mounting_segment; |
| 266 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 267 | #ifdef VCL_ELOG |
| 268 | /* VPP Event-logger */ |
| 269 | elog_main_t elog_main; |
| 270 | elog_track_t elog_track; |
| 271 | #endif |
| 272 | |
| 273 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 274 | uword *error_string_by_error_number; |
| 275 | } vppcom_main_t; |
| 276 | |
| 277 | extern vppcom_main_t *vcm; |
| 278 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 279 | #define VCL_INVALID_SESSION_INDEX ((u32)~0) |
| 280 | |
| 281 | static inline vcl_session_t * |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 282 | vcl_session_alloc (void) |
| 283 | { |
| 284 | vcl_session_t *s; |
| 285 | pool_get (vcm->sessions, s); |
| 286 | memset (s, 0, sizeof (*s)); |
| 287 | return s; |
| 288 | } |
| 289 | |
| 290 | static inline void |
| 291 | vcl_session_free (vcl_session_t * s) |
| 292 | { |
| 293 | pool_put (vcm->sessions, s); |
| 294 | } |
| 295 | |
| 296 | static inline vcl_session_t * |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 297 | vcl_session_get (u32 session_index) |
| 298 | { |
| 299 | if (pool_is_free_index (vcm->sessions, session_index)) |
| 300 | return 0; |
| 301 | return pool_elt_at_index (vcm->sessions, session_index); |
| 302 | } |
| 303 | |
| 304 | static inline u32 |
| 305 | vcl_session_index (vcl_session_t * s) |
| 306 | { |
| 307 | return (s - vcm->sessions); |
| 308 | } |
| 309 | |
| 310 | static inline vcl_session_t * |
| 311 | vcl_session_get_w_handle (u64 handle) |
| 312 | { |
| 313 | uword *p; |
| 314 | if ((p = hash_get (vcm->session_index_by_vpp_handles, handle))) |
| 315 | return vcl_session_get ((u32) p[0]); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static inline u32 |
| 320 | vcl_session_get_index_from_handle (u64 handle) |
| 321 | { |
| 322 | uword *p; |
| 323 | if ((p = hash_get (vcm->session_index_by_vpp_handles, handle))) |
| 324 | return p[0]; |
| 325 | return VCL_INVALID_SESSION_INDEX; |
| 326 | } |
| 327 | |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 328 | static inline u8 |
| 329 | vcl_session_is_ct (vcl_session_t * s) |
| 330 | { |
| 331 | return (s->our_evt_q != 0); |
| 332 | } |
| 333 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 334 | static inline void |
| 335 | vppcom_session_table_add_listener (u64 listener_handle, u32 value) |
| 336 | { |
| 337 | /* Session and listener handles have different formats. The latter has |
| 338 | * the thread index in the upper 32 bits while the former has the session |
| 339 | * type. Knowing that, for listeners we just flip the MSB to 1 */ |
| 340 | listener_handle |= 1ULL << 63; |
| 341 | hash_set (vcm->session_index_by_vpp_handles, listener_handle, value); |
| 342 | } |
| 343 | |
| 344 | static inline vcl_session_t * |
| 345 | vppcom_session_table_lookup_listener (u64 listener_handle) |
| 346 | { |
| 347 | uword *p; |
| 348 | u64 handle = listener_handle | (1ULL << 63); |
| 349 | vcl_session_t *session; |
| 350 | |
| 351 | p = hash_get (vcm->session_index_by_vpp_handles, handle); |
| 352 | if (!p) |
| 353 | { |
| 354 | clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp " |
| 355 | "listener handle %llx", getpid (), listener_handle); |
| 356 | return 0; |
| 357 | } |
| 358 | if (pool_is_free_index (vcm->sessions, p[0])) |
| 359 | { |
| 360 | VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | session = pool_elt_at_index (vcm->sessions, p[0]); |
| 365 | ASSERT (session->session_state & STATE_LISTEN); |
| 366 | return session; |
| 367 | } |
| 368 | |
| 369 | const char *vppcom_session_state_str (session_state_t state); |
| 370 | |
| 371 | /* |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 372 | * Helpers |
| 373 | */ |
| 374 | vcl_cut_through_registration_t *vcl_ct_registration_lock_and_alloc (void); |
| 375 | void vcl_ct_registration_del (vcl_cut_through_registration_t * ctr); |
| 376 | u32 vcl_ct_registration_index (vcl_cut_through_registration_t * ctr); |
| 377 | void vcl_ct_registration_unlock (void); |
| 378 | vcl_cut_through_registration_t *vcl_ct_registration_get (u32 ctr_index); |
| 379 | vcl_cut_through_registration_t *vcl_ct_registration_lock_and_lookup (uword); |
| 380 | void vcl_ct_registration_lookup_add (uword mq_addr, u32 ctr_index); |
| 381 | void vcl_ct_registration_lookup_del (uword mq_addr); |
| 382 | vcl_mq_evt_conn_t *vcl_mq_evt_conn_alloc (void); |
| 383 | u32 vcl_mq_evt_conn_index (vcl_mq_evt_conn_t * mqc); |
| 384 | vcl_mq_evt_conn_t *vcl_mq_evt_conn_get (u32 mq_conn_idx); |
| 385 | int vcl_mq_epoll_add_evfd (svm_msg_q_t * mq); |
| 386 | int vcl_mq_epoll_del_evfd (u32 mqc_index); |
| 387 | |
| 388 | /* |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 389 | * VCL Binary API |
| 390 | */ |
| 391 | int vppcom_connect_to_vpp (char *app_name); |
| 392 | void vppcom_init_error_string_table (void); |
| 393 | void vppcom_send_session_enable_disable (u8 is_enable); |
| 394 | void vppcom_app_send_attach (void); |
| 395 | void vppcom_app_send_detach (void); |
| 396 | void vppcom_send_connect_sock (vcl_session_t * session, u32 session_index); |
| 397 | void vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index, |
| 398 | int rv); |
| 399 | void vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index); |
| 400 | void vppcom_send_bind_sock (vcl_session_t * session, u32 session_index); |
| 401 | void vppcom_send_unbind_sock (u64 vpp_handle); |
| 402 | void vppcom_api_hookup (void); |
| 403 | void vppcom_send_accept_session_reply (u64 handle, u32 context, int retval); |
| 404 | |
| 405 | u32 vcl_max_nsid_len (void); |
| 406 | |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 407 | u8 *format_api_error (u8 * s, va_list * args); |
| 408 | |
Florin Coras | 697faea | 2018-06-27 17:10:49 -0700 | [diff] [blame] | 409 | #endif /* SRC_VCL_VCL_PRIVATE_H_ */ |
| 410 | |
| 411 | /* |
| 412 | * fd.io coding-style-patch-verification: ON |
| 413 | * |
| 414 | * Local Variables: |
| 415 | * eval: (c-set-style "gnu") |
| 416 | * End: |
| 417 | */ |