1 #ifndef __MTCP_API_H_ 2 #define __MTCP_API_H_ 3 4 #include <stdint.h> 5 #include <netinet/in.h> 6 #include <sys/uio.h> 7 8 #ifndef UNUSED 9 #define UNUSED(x) (void)x 10 #endif 11 12 #ifndef INPORT_ANY 13 #define INPORT_ANY (uint16_t)0 14 #endif 15 16 typedef unsigned char byte; 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 /** mTCP context */ 23 struct mtcp_context 24 { 25 int cpu; 26 }; 27 28 typedef struct mtcp_context *mctx_t; 29 30 /** mTCP signal handler type */ 31 typedef void (*mtcp_sighandler_t)(int); 32 33 /** Socket types */ 34 enum socket_type 35 { 36 /** unused */ 37 MOS_SOCK_UNUSED, 38 39 /* listening socket type */ 40 /** regular mTCP connection listen socket */ 41 MOS_SOCK_STREAM_LISTEN, 42 /** MOS proxy socket listening socket */ 43 /* (pending implementation) */ 44 MOS_SOCK_PROXY_LISTEN, 45 /** MOS monitor socket listening socket */ 46 MOS_SOCK_MONITOR_STREAM, 47 48 /* stream socket type */ 49 50 /** regular mTCP connection socket */ 51 MOS_SOCK_STREAM, 52 /** MOS proxy socket */ 53 /* (pending implementation) */ 54 MOS_SOCK_PROXY, 55 /** MOS monitor socket */ 56 MOS_SOCK_MONITOR_STREAM_ACTIVE, 57 /** MOS monitor socket (raw, stateless) */ 58 MOS_SOCK_MONITOR_RAW, 59 60 /* for epoll */ 61 MOS_SOCK_EPOLL, 62 /* for pipe */ 63 MOS_SOCK_PIPE, 64 }; 65 66 /* Configurable mTCP attributes 67 * This is automatically generated by mOS core after the 68 * mtcp_init() function call. You can change this option 69 * in your program using mtcp_get/set_conf(). 70 */ 71 struct mtcp_conf 72 { 73 /** Maximum length of application name */ 74 #define APP_NAME_LEN 40 75 /** Maximum concurrently runable application */ 76 #define MOS_APP 20 77 78 int num_cores; /**< number of cores to use */ 79 int max_concurrency; /**< number of concurrent flows per core*/ 80 uint64_t cpu_mask; 81 82 int max_num_buffers; /**< number of socket buffers */ 83 int rcvbuf_size; /**< size of receive buffer */ 84 int sndbuf_size; /**< size of send buffer */ 85 86 int tcp_timewait; /**< time wait time in sec */ 87 int tcp_timeout; /**< timeout in sec, -1 for not to check timeout */ 88 89 #define MOS_APP_ARGC 20 90 uint64_t app_cpu_mask[MOS_APP]; 91 char *app_argv[MOS_APP][MOS_APP_ARGC]; 92 int app_argc[MOS_APP]; 93 int num_app; /**< number of registered apps */ 94 }; 95 96 /* Target to modify */ 97 struct app_context 98 { 99 mctx_t mctx; /**< mTCP context */ 100 int socket_id; /**< listing socket ID */ 101 struct conn_filter *cf; 102 int ep_id; 103 }; 104 105 /* Application need to specify those parameters. */ 106 /* NOTE: This structure is not used in mos_release 0.2a 107 * This structure will be used by embedded mOS application using 108 * mOS server. 109 */ 110 struct app_ops 111 { 112 /** Initialization function which is called for only once per each 113 * application */ 114 void (*app_init)(int argc, char **argv); 115 /** Initialization function which is called for each CPU core */ 116 void (*thread_init)(mctx_t mctx, void **app_ctx); 117 /** Function contains code which will be executed after initialization 118 * phase */ 119 void (*run)(mctx_t mctx, void **app_ctx); 120 }; 121 122 123 /** Initialize mOS context with parameters mentioned in the config file 124 * @param [in] config_file: location of config file 125 * @return 0 on success, -1 on error 126 * 127 * Setting dpdk devices, interface, load configuration from config file, 128 * routing table, arp table, etc. 129 */ 130 int 131 mtcp_init(const char *config_file); 132 133 /** Destroy the global mOS context 134 * @return 0 on success, -1 on error 135 */ 136 int 137 mtcp_destroy(); 138 139 /** Load current mtcp configuration in *conf 140 * @param [out] conf: configurations 141 * @return 0 on success, -1 on error 142 */ 143 int 144 mtcp_getconf(struct mtcp_conf *conf); 145 146 /** Update mOS base with parameters mentioned in *conf 147 * @param [in] conf: configurations to set 148 * @return 0 on success, -1 on error 149 */ 150 int 151 mtcp_setconf(const struct mtcp_conf *conf); 152 153 /** Bind a thread to a specific CPU core 154 * @param [in] cpu: CPU ID 155 * @return 0 on success, -1 on error 156 */ 157 int 158 mtcp_core_affinitize(int cpu); 159 160 /** Create mOS/mtcp context thread based on the parameters passed 161 * by mtcp_init() & mtcp_setconf() functions 162 * @param [in] cpu: Core id to affinitize new mtcp thread 163 * @return mtcp context, NULL on error 164 */ 165 mctx_t 166 mtcp_create_context(int cpu); 167 168 /** Destory mtcp context that was created by mOS/mTCP thread 169 * @param [in] mctx: mtcp context 170 * @return 0 on success, -1 on error 171 */ 172 int 173 mtcp_destroy_context(mctx_t mctx); 174 175 /** Register signal handler (mtcp_sighandler_t handler ) 176 * for int signum 177 * @param [in] signum: Signal number to handle 178 * @param [in] handler: Handler function 179 * @return Same handler as input 180 */ 181 mtcp_sighandler_t 182 mtcp_register_signal(int signum, mtcp_sighandler_t handler); 183 184 /** Create pipe 185 * @param [in] mctx: mtcp context 186 * @param [out] pipeid: created pipe 187 * @return 0 on success, -1 on error 188 */ 189 int 190 mtcp_pipe(mctx_t mctx, int pipeid[2]); 191 192 /** Get socket options 193 * 194 * <ol> 195 * 196 * <li> MOS_TCP_STATE_CLI and MOS_TCP_STATE_SVR \n 197 * Returns a TCP state variable. This socket option is read-only. </li> 198 * 199 * <li> MOS_INFO_CLIBUF and MOS_INFO_SVRBUF \n 200 * Returns a structured information for upstream and downstream 201 * TCP buffer. The argument is a `tcp_buf_info` structure. This socket 202 * option is read-only. </li> 203 * 204 * <li> MOS_FRAGINFO_CLIBUF and MOS_FRAGINFO_SVRBUF \n 205 * Returns a TCP fragment buffer list information of TCP ring buffer. 206 * The argument is an array of `tcp_ring_fragment` structure, including 207 * the first contiguous data in the ring buffer. If there is no data in the 208 * TCP ring buffer, optval will be NULL. This socket option is read-only. 209 * For this option, note that optval represents the number of object entries 210 * that are allocated to read (input) and have been read (output). </li> 211 * 212 * </ol> 213 * 214 * @param [in] mctx: mtcp context 215 * @param [in] sock: socket id 216 * @param [in] level: SOL_MONSOCKET 217 * @param [in] optname: __variable__ 218 * @param [out] optval: value of getting option 219 * @param [out] optlen: length of getting option 220 * @return Zero on success, -1 on error 221 */ 222 int 223 mtcp_getsockopt(mctx_t mctx, int sock, int level, 224 int optname, void *optval, socklen_t *optlen); 225 226 /** Set socket options 227 * 228 * <ol> 229 * 230 * <li> MOS_CLIBUF and MOS_SVRBUF \n 231 * Resize tcp ring buffer. Putting 0 disables tcp ring buffer. 232 * Takes integer as argument. </li> 233 * 234 * <li> MOS_FRAG_CLIBUF and MOS_FRAG_SVRBUF \n 235 * This version is `suppressed' at the moment (works but not advertised 236 * for the sake of simplicity). Sets the metadata tracking range to the 237 * specified value. </li> 238 * 239 * <li> MOS_STOP_MON \n 240 * Stop the monitoring for the specific side (passed via optval) </li> 241 * </ol> 242 * 243 * @param [in] mctx: mtcp context 244 * @param [in] sock: socket id 245 * @param [in] level: SOL_MONSOCKET 246 * @param [in] optname: __variable__ 247 * @param [in] optval: value of setting option 248 * @param [in] optlen: length of setting option 249 * @return Zero on success, -1 on error 250 */ 251 int 252 mtcp_setsockopt(mctx_t mctx, int sock, int level, 253 int optname, const void *optval, socklen_t optlen); 254 255 /** Set socket as nonblock 256 * @param [in] mctx: mtcp context 257 * @param [in] sock: socket id 258 * @return Zero on success, -1 on error 259 * 260 * DEPRECATED 261 */ 262 int 263 mtcp_setsock_nonblock(mctx_t mctx, int sock); 264 265 /** Control socket 266 * @param [in] mctx: mtcp context 267 * @param [in] sock: socket id 268 * @param [in] request: requested operation 269 * @param [in,out] argp: pointer to argument 270 * @return Zero on success, -1 on error 271 * 272 * Operations | Description 273 * ---------- | ----------- 274 * FIONREAD | number of data bytes in receive buffer 275 * FIONBIO | for non-blocking I/O 276 */ 277 int 278 mtcp_ioctl(mctx_t mctx, int sock, int request, void *argp); 279 280 /** Create a socket 281 * @param [in] mctx: mtcp context 282 * @param [in] domain: AF_INET, Not used 283 * @param [in] type: Any of the following keyword 284 * - MOS_SOCK_STREAM (End TCP) 285 * - MOS_SOCK_MONITOR_RAW (Packet-level monitor) 286 * - MOS_SOCK_MONITOR_STREAM (Flow-level monitor) 287 * (MOS_SOCK_PROXY: To be supported) 288 * @param [in] protocol: NULL, Not used 289 * @return Zero on success, -1 on error 290 */ 291 int 292 mtcp_socket(mctx_t mctx, int domain, int type, int protocol); 293 294 /** Bind a socket, same as `bind()` 295 * @return Zero on success, -1 on error 296 */ 297 int 298 mtcp_bind(mctx_t mctx, int sock, 299 const struct sockaddr *addr, socklen_t addrlen); 300 301 /** Listen a listening socket, same as `listen()` 302 * @return Zero on success, -1 on error 303 * 304 * Only MOS_SOCK_LISTENER, MOS_SOCK_PROXY are allowed 305 */ 306 int 307 mtcp_listen(mctx_t mctx, int sock, int backlog); 308 309 /** Accept new flow, same as `accept()` 310 * @param [in] mctx: mtcp context 311 * @param [in] sock: socket id for MOS_SOCK_STREAM 312 * @param [in] addr: address of the peer host(s) 313 * @param [in] addrlen: length of `addr` 314 * @return Zero on success, -1 on error 315 */ 316 int 317 mtcp_accept(mctx_t mctx, int sock, struct sockaddr *addr, socklen_t *addrlen); 318 319 /** Initialize RSS pool for decide appropriate port numbers 320 * @param [in] mctx: mtcp context 321 * @param [in] saddr_base: source IP address base 322 * @param [in] num_addr: number of source IP address to use 323 * @param [in] daddr: destination IP address 324 * @param [in] dport: destination port number 325 * @return Zero on success, -1 on error 326 * 327 * w.r.t source IP addr, destination IP addr, destination port 328 */ 329 int 330 mtcp_init_rss(mctx_t mctx, in_addr_t saddr_base, int num_addr, 331 in_addr_t daddr, in_addr_t dport); 332 333 /** Connect new flow, same as `connect()` 334 * @return Zero on success, -1 on error 335 */ 336 int 337 mtcp_connect(mctx_t mctx, int sock, 338 const struct sockaddr *addr, socklen_t addrlen); 339 340 /** Close flow, same as `close()` 341 * @return Zero on success, -1 on error 342 */ 343 int 344 mtcp_close(mctx_t mctx, int sock); 345 346 /** Abort flow by sending RST 347 * @return Zero on success, -1 on error 348 * 349 * Different from `mtcp_close()` which processes 4way handshake by FIN 350 */ 351 int 352 mtcp_abort(mctx_t mctx, int sock); 353 354 /** Read byte stream from flow, same as `read()` 355 * @return number of bytes read on success, -1 on error 356 */ 357 ssize_t 358 mtcp_read(mctx_t mctx, int sock, char *buf, size_t len); 359 360 /* readv should work in atomic */ 361 /** Read byte stream from flow in vector, same as `readv()` 362 * @return number of bytes read on success, -1 on error 363 */ 364 ssize_t 365 mtcp_readv(mctx_t mctx, int sock, struct iovec *iov, int numIOV); 366 367 /** Write byte stream to flow, same as `write()` 368 * @return number of bytes write on success, -1 on error 369 */ 370 ssize_t 371 mtcp_write(mctx_t mctx, int sock, char *buf, size_t len); 372 373 /* writev should work in atomic */ 374 /** Write byte stream to flow in vector, same as `writev()` 375 * @return number of bytes write on success, -1 on error 376 */ 377 ssize_t 378 mtcp_writev(mctx_t mctx, int sock, struct iovec *iov, int numIOV); 379 380 /** Get concurrent flow count of the underlying mtcp manager context (per-thread) 381 * @param [in] mctx: mtcp context 382 * @return concurrent flow count 383 */ 384 uint32_t 385 mtcp_get_connection_cnt(mctx_t mctx); 386 387 #ifdef __cplusplus 388 }; 389 #endif 390 391 #endif /* __MTCP_API_H_ */ 392