176404edcSAsim Jamshed #ifndef __MOS_API_H_ 276404edcSAsim Jamshed #define __MOS_API_H_ 376404edcSAsim Jamshed 476404edcSAsim Jamshed #ifdef DARWIN 576404edcSAsim Jamshed #include <netinet/tcp.h> 676404edcSAsim Jamshed #include <netinet/if_ether.h> 776404edcSAsim Jamshed #else 876404edcSAsim Jamshed #include <linux/tcp.h> 976404edcSAsim Jamshed #include <linux/if_ether.h> 1076404edcSAsim Jamshed #endif 1176404edcSAsim Jamshed #include <netinet/in.h> 1276404edcSAsim Jamshed #include <arpa/inet.h> 1376404edcSAsim Jamshed #include <netinet/ip.h> 1476404edcSAsim Jamshed #include <stddef.h> /* for offsetof */ 1576404edcSAsim Jamshed #include "mtcp_epoll.h" 1676404edcSAsim Jamshed #include <stdbool.h> 1776404edcSAsim Jamshed 1876404edcSAsim Jamshed #ifndef __MTCP_MANAGER 1976404edcSAsim Jamshed #define __MTCP_MANAGER 2076404edcSAsim Jamshed typedef struct mtcp_manager * mtcp_manager_t; 2176404edcSAsim Jamshed #endif 2276404edcSAsim Jamshed #ifndef __SOCKET_MAP 2376404edcSAsim Jamshed #define __SOCKET_MAP 2476404edcSAsim Jamshed typedef struct socket_map * socket_map_t; 2576404edcSAsim Jamshed #endif 2676404edcSAsim Jamshed 2776404edcSAsim Jamshed /** Available hooking points */ 2876404edcSAsim Jamshed enum mtcp_hook_point 2976404edcSAsim Jamshed { 3076404edcSAsim Jamshed /* NOTE: The value of hooking points should not overlap with any of 3176404edcSAsim Jamshed * mos_event_types */ 3276404edcSAsim Jamshed 3376404edcSAsim Jamshed /** Very first hooking point of incoming packet even before flow 3476404edcSAsim Jamshed * identification*/ 3576404edcSAsim Jamshed MOS_NULL = (1 << 29), 3676404edcSAsim Jamshed /** Hooking point before TCP receiver */ 3776404edcSAsim Jamshed MOS_HK_RCV = (1 << 30), 3876404edcSAsim Jamshed /** Hooking point after TCP sender */ 3976404edcSAsim Jamshed MOS_HK_SND = (1 << 31), 4076404edcSAsim Jamshed }; 4176404edcSAsim Jamshed 4276404edcSAsim Jamshed /** Built-in events provided by mOS */ 4376404edcSAsim Jamshed enum mos_event_type 4476404edcSAsim Jamshed { 4576404edcSAsim Jamshed /** invalid event */ 4676404edcSAsim Jamshed MOS_NULL_EVENT = (0), 4776404edcSAsim Jamshed /* mos-defined tcp build-in events */ 4876404edcSAsim Jamshed /** A packet is coming in. */ 4976404edcSAsim Jamshed MOS_ON_PKT_IN = (0x1<<0), 5076404edcSAsim Jamshed /** A packet is going out. */ 5176404edcSAsim Jamshed /* THIS EVENT IS NOW DEPRECATED (USED ONLY FOR DEBUGGING) */ 5276404edcSAsim Jamshed MOS_ON_PKT_OUT = (0x1<<1), 5376404edcSAsim Jamshed /** SYN packet as seen by the monitor 5476404edcSAsim Jamshed * client side: activated when the client state is set to SYN_SENT 5576404edcSAsim Jamshed * server side: activated when the server state is set to SYN_RCVD 5676404edcSAsim Jamshed * 5776404edcSAsim Jamshed * Retransmitted SYN packets don't activate this event. 5876404edcSAsim Jamshed */ 5976404edcSAsim Jamshed MOS_ON_CONN_START = (0x1<<2), 6076404edcSAsim Jamshed /** 3-way handshake is finished. 6176404edcSAsim Jamshed * server side: ACK is coming in as a response of SYNACK. 6276404edcSAsim Jamshed * client side: SYNACK is coming in as a response of SYN. */ 6376404edcSAsim Jamshed /* THIS EVENT IS NOW DEPRECATED */ 6476404edcSAsim Jamshed MOS_ON_CONN_SETUP = (0x1<<3), 6576404edcSAsim Jamshed /** New data is now readable. 6676404edcSAsim Jamshed * This event is available in only MOS_NULL hook point. 6776404edcSAsim Jamshed * mOS raises this event only once while batched packet processing. */ 6876404edcSAsim Jamshed MOS_ON_CONN_NEW_DATA = (0x1<<4), 6976404edcSAsim Jamshed /** Abnormal behavior is detected. 7076404edcSAsim Jamshed * NOTE: This is not fully implemented yet. */ 7176404edcSAsim Jamshed MOS_ON_ERROR = (0x1<<5), 7276404edcSAsim Jamshed /** No packet is seen for a long time. 7376404edcSAsim Jamshed * This is implemented as mtcp_cb_settimer() 7476404edcSAsim Jamshed */ 7576404edcSAsim Jamshed MOS_ON_TIMEOUT = (0x1<<6), 7676404edcSAsim Jamshed /** TCP state is being changed. */ 7776404edcSAsim Jamshed MOS_ON_TCP_STATE_CHANGE = (0x1<<7), 7876404edcSAsim Jamshed /** A packet is not SYN and has no identified flow. */ 7976404edcSAsim Jamshed MOS_ON_ORPHAN = (0x1<<8), 8076404edcSAsim Jamshed /** Retransmission is detected */ 8176404edcSAsim Jamshed MOS_ON_REXMIT = (0x1<<9), 8276404edcSAsim Jamshed /** A flow is about to be destroyed. 8376404edcSAsim Jamshed * 4-way handshake, RST packet or timeout could be the reason. 8476404edcSAsim Jamshed * NOTE: In current implementation, mOS raises this event while destroying 8576404edcSAsim Jamshed * `struct tcp_stream`. There is possibility of false-positive especially 8676404edcSAsim Jamshed * when mOS is running out of memory. */ 8776404edcSAsim Jamshed MOS_ON_CONN_END = (0x1<<10), 8876404edcSAsim Jamshed 8976404edcSAsim Jamshed /** This event is for debugging. We can easily mute this later. */ 9076404edcSAsim Jamshed MOS_ON_DEBUG_MESSAGE = (0x1<<11), 9176404edcSAsim Jamshed }; 9276404edcSAsim Jamshed 9376404edcSAsim Jamshed #if 0 9476404edcSAsim Jamshed /* This may go away in future revisions */ 9576404edcSAsim Jamshed typedef union event_data { 9676404edcSAsim Jamshed uint32_t u32; 9776404edcSAsim Jamshed uint64_t u64; 9876404edcSAsim Jamshed void *ptr; 9976404edcSAsim Jamshed } event_data_t; 10076404edcSAsim Jamshed #endif 10176404edcSAsim Jamshed 10276404edcSAsim Jamshed /* Macros for updating packet context */ 10376404edcSAsim Jamshed #define MOS_ETH_HDR (1 << 0) 10476404edcSAsim Jamshed #define MOS_IP_HDR (1 << 1) 10576404edcSAsim Jamshed #define MOS_TCP_HDR (1 << 2) 10676404edcSAsim Jamshed #define MOS_TCP_PAYLOAD (1 << 3) 10776404edcSAsim Jamshed #define MOS_UPDATE_IP_CHKSUM (1 << 4) 10876404edcSAsim Jamshed #define MOS_UPDATE_TCP_CHKSUM (1 << 5) 10976404edcSAsim Jamshed #define MOS_DROP (1 << 6) 11076404edcSAsim Jamshed #define MOS_OVERWRITE (1 << 7) 11176404edcSAsim Jamshed #define MOS_CHOMP (1 << 8) 11276404edcSAsim Jamshed #define MOS_INSERT (1 << 9) 11376404edcSAsim Jamshed 11476404edcSAsim Jamshed /** 11576404edcSAsim Jamshed * struct pkt_info is the struct that is actually 11676404edcSAsim Jamshed * exposed to the monitor application. 11776404edcSAsim Jamshed * 11876404edcSAsim Jamshed * NOTE: When you retrieve the packet information using mtcp_getlastpkt() 11976404edcSAsim Jamshed * via MOS_SOCK_MONITOR_RAW socket, you can only use up to L3 information. 12076404edcSAsim Jamshed * (cur_ts, eth_len, ip_len, ethh, iph) 12176404edcSAsim Jamshed */ 12276404edcSAsim Jamshed struct pkt_info { 12376404edcSAsim Jamshed uint32_t cur_ts; /**< packet receiving time (read-only:ro) */ 124a834ea89SAsim Jamshed int8_t in_ifidx; /**< input interface (ro) */ 12576404edcSAsim Jamshed 12676404edcSAsim Jamshed /* ETH */ 12776404edcSAsim Jamshed uint16_t eth_len; 12876404edcSAsim Jamshed 12976404edcSAsim Jamshed /* IP */ 13076404edcSAsim Jamshed uint16_t ip_len; 13176404edcSAsim Jamshed 13276404edcSAsim Jamshed /* TCP */ 13376404edcSAsim Jamshed uint64_t offset; /**< TCP ring buffer offset */ 13476404edcSAsim Jamshed uint16_t payloadlen; 13576404edcSAsim Jamshed uint32_t seq; 13676404edcSAsim Jamshed uint32_t ack_seq; 13776404edcSAsim Jamshed uint16_t window; 13876404edcSAsim Jamshed 13976404edcSAsim Jamshed /* ~~ 28 byte boundary ~~ */ 14076404edcSAsim Jamshed 14176404edcSAsim Jamshed /* 14276404edcSAsim Jamshed * CAUTION!!! 14376404edcSAsim Jamshed * It is extremely critical that the last 5 fields (ethh .. frame) 14476404edcSAsim Jamshed * are always placed at the end of the definition. MOS relies on 14576404edcSAsim Jamshed * this specific arrangement when it is creating a new instantiation 14676404edcSAsim Jamshed * of pctx during mtcp_getlastpkt() invocation. 14776404edcSAsim Jamshed */ 14876404edcSAsim Jamshed struct ethhdr *ethh; 14976404edcSAsim Jamshed struct iphdr *iph; 15076404edcSAsim Jamshed struct tcphdr *tcph; 15176404edcSAsim Jamshed uint8_t *payload; 15276404edcSAsim Jamshed }; 15376404edcSAsim Jamshed 15476404edcSAsim Jamshed /** 15576404edcSAsim Jamshed * PACKET CONTEXT is the packet structure that goes through 15676404edcSAsim Jamshed * the mOS core... 15776404edcSAsim Jamshed */ 15876404edcSAsim Jamshed struct pkt_ctx { 15976404edcSAsim Jamshed struct pkt_info p; 16076404edcSAsim Jamshed 16176404edcSAsim Jamshed int8_t direction; /**< where does this packet originate from? (ro)*/ 16276404edcSAsim Jamshed uint8_t forward; /**< 0: drop, 1: forward to out_ifidx (rw) */ 16376404edcSAsim Jamshed int8_t out_ifidx; /**< output interface (rw) */ 16476404edcSAsim Jamshed int8_t batch_index; /**< index of packet in the rx batch */ 16576404edcSAsim Jamshed /* ~~ 64 byte boundary ~~ */ 16676404edcSAsim Jamshed }; 16776404edcSAsim Jamshed #define PKT_INFO_LEN offsetof(struct pkt_info, ethh) 16876404edcSAsim Jamshed 16976404edcSAsim Jamshed /* 17076404edcSAsim Jamshed * Sequence number change structure. 17176404edcSAsim Jamshed * Used for MOS_SEQ_REMAP. 17276404edcSAsim Jamshed */ 17376404edcSAsim Jamshed typedef struct { 17476404edcSAsim Jamshed int64_t seq_off; /* the amount of sequence number drift */ 17576404edcSAsim Jamshed int side; /* which side does this sequence number change apply to? */ 17676404edcSAsim Jamshed uint32_t base_seq; /* seq # of the flow where the actual sequence # translation starts */ 17776404edcSAsim Jamshed } seq_remap_info; 17876404edcSAsim Jamshed 17976404edcSAsim Jamshed typedef struct filter_arg { 18076404edcSAsim Jamshed void *arg; 18176404edcSAsim Jamshed size_t len; 18276404edcSAsim Jamshed } filter_arg_t; 18376404edcSAsim Jamshed 18476404edcSAsim Jamshed /** 18576404edcSAsim Jamshed * The available level number in the POSIX library for sockets is 18676404edcSAsim Jamshed * on SOL_SOCKET 18776404edcSAsim Jamshed */ 18876404edcSAsim Jamshed #ifndef SOL_SOCKET 18976404edcSAsim Jamshed /* Level number for (get/set)sockopt() to apply to socket itself. */ 19076404edcSAsim Jamshed #define SOL_SOCKET 0xffff /* options for socket level */ 19176404edcSAsim Jamshed #endif 19276404edcSAsim Jamshed #define SOL_MONSOCKET 0xfffe /* MOS monitor socket level */ 19376404edcSAsim Jamshed 19476404edcSAsim Jamshed /** 19576404edcSAsim Jamshed * MOS monitor socket option names (and values) 19676404edcSAsim Jamshed * This will contain options pertaining to monitor stream sockets 197*861ea7dfSAsim Jamshed * See mtcp_getsockopt() and mtcp_setsockopt() the mtcp_api.h file. 19876404edcSAsim Jamshed */ 19976404edcSAsim Jamshed enum mos_socket_opts { 20076404edcSAsim Jamshed MOS_FRAGINFO_CLIBUF = 0x01, 20176404edcSAsim Jamshed MOS_FRAGINFO_SVRBUF = 0x02, 20276404edcSAsim Jamshed MOS_INFO_CLIBUF = 0x03, 20376404edcSAsim Jamshed MOS_INFO_SVRBUF = 0x04, 20476404edcSAsim Jamshed MOS_TCP_STATE_CLI = 0x05, 20576404edcSAsim Jamshed MOS_TCP_STATE_SVR = 0x06, 20676404edcSAsim Jamshed MOS_CLIBUF = 0x09, 20776404edcSAsim Jamshed MOS_SVRBUF = 0x0a, 20876404edcSAsim Jamshed MOS_STOP_MON = 0x0c, 209a834ea89SAsim Jamshed MOS_CLIOVERLAP = 0x0f, 210a834ea89SAsim Jamshed MOS_SVROVERLAP = 0x10, 211*861ea7dfSAsim Jamshed 212*861ea7dfSAsim Jamshed MOS_TIMESTAMP = 0x07, /* supressed (not used) */ 213*861ea7dfSAsim Jamshed MOS_SEQ_REMAP = 0x0b, /* supressed (not used) */ 214*861ea7dfSAsim Jamshed MOS_FRAG_CLIBUF = 0x0d, /* supressed (not used) */ 215*861ea7dfSAsim Jamshed MOS_FRAG_SVRBUF = 0x0e, /* supressed (not used) */ 216*861ea7dfSAsim Jamshed 21776404edcSAsim Jamshed }; 21876404edcSAsim Jamshed 21976404edcSAsim Jamshed /** 22076404edcSAsim Jamshed * MOS tcp buf info structure. 22176404edcSAsim Jamshed * Used by the monitor application to retreive 22276404edcSAsim Jamshed * tcp_stream-related info. Usually called via 223*861ea7dfSAsim Jamshed * mtcp_getsockopt() function 22476404edcSAsim Jamshed */ 22576404edcSAsim Jamshed struct tcp_buf_info { 22676404edcSAsim Jamshed /** The initial TCP sequence number of TCP ring buffer. */ 22776404edcSAsim Jamshed uint32_t tcpbi_init_seq; 22876404edcSAsim Jamshed /** TCP sequence number of the 'last byte of payload that has 22976404edcSAsim Jamshed * already been read by the end application' (applies in the case 23076404edcSAsim Jamshed * of embedded monitor setup) 23176404edcSAsim Jamshed */ 23276404edcSAsim Jamshed uint32_t tcpbi_last_byte_read; 23376404edcSAsim Jamshed /** TCP sequence number of the 'last byte of the payload that 23476404edcSAsim Jamshed * is currently buffered and needs to be read by the end 23576404edcSAsim Jamshed * application' (applies in the case of embedded monitor setup). 23676404edcSAsim Jamshed * 23776404edcSAsim Jamshed * In case of standalone monitors, tcpbi_last_byte_read = 23876404edcSAsim Jamshed * tcpbi_next_byte_expected 23976404edcSAsim Jamshed */ 24076404edcSAsim Jamshed uint32_t tcpbi_next_byte_expected; 24176404edcSAsim Jamshed /** TCP sequence number of the 'last byte of the payload that 24276404edcSAsim Jamshed * is currently stored' in the TCP ring buffer. This value 24376404edcSAsim Jamshed * may be greater than tcpbi_next_byte_expected if packets 24476404edcSAsim Jamshed * arrive out of order. 24576404edcSAsim Jamshed */ 24676404edcSAsim Jamshed uint32_t tcpbi_last_byte_received; 24776404edcSAsim Jamshed }; 24876404edcSAsim Jamshed 24976404edcSAsim Jamshed /** Structure to expose TCP ring buffer's fragment information. */ 25076404edcSAsim Jamshed struct tcp_ring_fragment { 25176404edcSAsim Jamshed uint64_t offset; 25276404edcSAsim Jamshed uint32_t len; 25376404edcSAsim Jamshed }; 25476404edcSAsim Jamshed 25576404edcSAsim Jamshed /** 25676404edcSAsim Jamshed * mOS tcp stream states. 25776404edcSAsim Jamshed * used by the monitor application to retreive 25876404edcSAsim Jamshed * tcp_stream-state info. Usually called via 25976404edcSAsim Jamshed * getsockopt() function 26076404edcSAsim Jamshed */ 26176404edcSAsim Jamshed enum tcpstate 26276404edcSAsim Jamshed { 26376404edcSAsim Jamshed TCP_CLOSED = 0, 26476404edcSAsim Jamshed TCP_LISTEN = 1, 26576404edcSAsim Jamshed TCP_SYN_SENT = 2, 26676404edcSAsim Jamshed TCP_SYN_RCVD = 3, 26776404edcSAsim Jamshed TCP_ESTABLISHED = 4, 26876404edcSAsim Jamshed TCP_FIN_WAIT_1 = 5, 26976404edcSAsim Jamshed TCP_FIN_WAIT_2 = 6, 27076404edcSAsim Jamshed TCP_CLOSE_WAIT = 7, 27176404edcSAsim Jamshed TCP_CLOSING = 8, 27276404edcSAsim Jamshed TCP_LAST_ACK = 9, 27376404edcSAsim Jamshed TCP_TIME_WAIT = 10 27476404edcSAsim Jamshed }; 27576404edcSAsim Jamshed 276a834ea89SAsim Jamshed /** mOS segment overlapping policies */ 277a834ea89SAsim Jamshed enum { 278a834ea89SAsim Jamshed MOS_OVERLAP_POLICY_FIRST=0, 279a834ea89SAsim Jamshed MOS_OVERLAP_POLICY_LAST, 280a834ea89SAsim Jamshed MOS_OVERLAP_CNT 281a834ea89SAsim Jamshed }; 282a834ea89SAsim Jamshed 28376404edcSAsim Jamshed /** Definition of event type */ 28476404edcSAsim Jamshed typedef uint64_t event_t; 28576404edcSAsim Jamshed 28676404edcSAsim Jamshed /** Definition of monitor side */ 28776404edcSAsim Jamshed enum {MOS_SIDE_CLI=0, MOS_SIDE_SVR, MOS_SIDE_BOTH}; 28876404edcSAsim Jamshed 28976404edcSAsim Jamshed /* mos callback/filter function type definition */ 29076404edcSAsim Jamshed /** Prototype of callback function */ 29176404edcSAsim Jamshed typedef void (*callback_t)(mctx_t mctx, int sock, int side, 29276404edcSAsim Jamshed event_t event, filter_arg_t *arg); 29376404edcSAsim Jamshed /** Prototype of UDE's filter function */ 29476404edcSAsim Jamshed typedef bool (*filter_t)(mctx_t mctx, int sock, int side, 29576404edcSAsim Jamshed event_t event, filter_arg_t *arg); 29676404edcSAsim Jamshed 29776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 29876404edcSAsim Jamshed /* Definition of monitor_filter type */ 29976404edcSAsim Jamshed union monitor_filter { 30076404edcSAsim Jamshed /** For MOS_SOCK_MONITOR_RAW type socket **/ 30176404edcSAsim Jamshed char *raw_pkt_filter; 30276404edcSAsim Jamshed /** For MOS_SOCK_MONITOR_STREAM type socket **/ 30376404edcSAsim Jamshed struct { 30476404edcSAsim Jamshed char *stream_syn_filter; 30576404edcSAsim Jamshed char *stream_orphan_filter; 30676404edcSAsim Jamshed }; 30776404edcSAsim Jamshed }; 30876404edcSAsim Jamshed typedef union monitor_filter *monitor_filter_t; 30976404edcSAsim Jamshed 31076404edcSAsim Jamshed /* Assign an address range (specified by ft) to monitor via sock 31176404edcSAsim Jamshed * 31276404edcSAsim Jamshed * (1) If sock is MOS_SOCK_MONITOR_RAW type, ft.raw_pkt_filter is applied to 31376404edcSAsim Jamshed * every packet coming in. 31476404edcSAsim Jamshed * (2) If sock is MOS_SOCK_MONITOR_STREAM type, 31576404edcSAsim Jamshed * ft.stream_syn_filter is applied to the first SYN pkt of the flow. 31676404edcSAsim Jamshed * (The succeeding packets of that flow will bypass the filter operation.) 31776404edcSAsim Jamshed * ft.stream_orphan_filter is applied to the pkts that don't belong to any 31876404edcSAsim Jamshed * of the existing TCP streams which are being monitored. 31976404edcSAsim Jamshed * (e.g., non-SYN pkt with no identified flow) 32076404edcSAsim Jamshed * [*] ft.stream_syn_filter and ft.stream_orphan_filter should be consisted 32176404edcSAsim Jamshed * only of the following keywords: 32276404edcSAsim Jamshed * - 'tcp, 'host', 'src', 'dst', 'net', 'mask', 'port', 'portrange' 32376404edcSAsim Jamshed * - 'and', 'or', '&', '|' 32476404edcSAsim Jamshed * 32576404edcSAsim Jamshed * @param [in] mctx: mtcp context 32676404edcSAsim Jamshed * @param [in] sock: socket id (should be MOS_SOCK_MONITOR_RAW 32776404edcSAsim Jamshed * or MOS_SOCK_MONITOR_STREAM type) 32876404edcSAsim Jamshed * @param [in] cf: Describe a set of connections to accept 32976404edcSAsim Jamshed * in a BPF (Berkerley Packet Filter) format 33076404edcSAsim Jamshed * NULL if you want to monitor any packet 33176404edcSAsim Jamshed * @return zero on success, -1 on error 33276404edcSAsim Jamshed */ 33376404edcSAsim Jamshed int 33476404edcSAsim Jamshed mtcp_bind_monitor_filter(mctx_t mctx, int sock, monitor_filter_t ft); 33576404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 33676404edcSAsim Jamshed 33776404edcSAsim Jamshed /** Register a callback function in hook_point 33876404edcSAsim Jamshed * @param [in] mctx: mtcp context 33976404edcSAsim Jamshed * @param [in] sock: socket id 34076404edcSAsim Jamshed * @param [in] event: event id 34176404edcSAsim Jamshed * @param [in] hook_point: MOS_HK_RCV, MOS_HK_SND, MOS_DONTCARE 34276404edcSAsim Jamshed * @param [in] cb: callback fucntion 34376404edcSAsim Jamshed * @return zero on success, -1 on error 34476404edcSAsim Jamshed * 34576404edcSAsim Jamshed * (both for packet-level and flow-level) for events in hook_point 34676404edcSAsim Jamshed */ 34776404edcSAsim Jamshed int 34876404edcSAsim Jamshed mtcp_register_callback(mctx_t mctx, int sock, event_t event, 34976404edcSAsim Jamshed int hook_point, callback_t cb); 35076404edcSAsim Jamshed 35176404edcSAsim Jamshed /** Remove registered callback functions 35276404edcSAsim Jamshed * @param [in] mctx: mtcp context 35376404edcSAsim Jamshed * @param [in] sock: socket id 35476404edcSAsim Jamshed * @param [in] event: event id 35576404edcSAsim Jamshed * @param [in] hook_point: MOS_HK_RCV, MOS_HK_SND, MOS_NULL 35676404edcSAsim Jamshed * @return zero on success, -1 on error 35776404edcSAsim Jamshed * 35876404edcSAsim Jamshed * (both for packet-level and flow-level) for events in hook_point 35976404edcSAsim Jamshed */ 36076404edcSAsim Jamshed //int 36176404edcSAsim Jamshed //mtcp_unregister_callback(mctx_t mctx, int sock, event_t event, 36276404edcSAsim Jamshed // int hook_point); 36376404edcSAsim Jamshed 36476404edcSAsim Jamshed /** Allocate a child event 36576404edcSAsim Jamshed * @param [in] event: event id 36676404edcSAsim Jamshed * @return new event id on success, 0 on error 36776404edcSAsim Jamshed */ 36876404edcSAsim Jamshed event_t 36976404edcSAsim Jamshed mtcp_alloc_event(event_t event); 37076404edcSAsim Jamshed 37176404edcSAsim Jamshed /** Define a user-defined event function 37276404edcSAsim Jamshed * @param [in] event: event id 37376404edcSAsim Jamshed * @param [in] filter: filter fucntion for new event 37476404edcSAsim Jamshed * @param [in] arg: a filter argument to be delivered to the filter 37576404edcSAsim Jamshed * @return new event id on success, 0 on error 37676404edcSAsim Jamshed * 37776404edcSAsim Jamshed * (both for packet-level and flow-level) 37876404edcSAsim Jamshed */ 37976404edcSAsim Jamshed event_t 38076404edcSAsim Jamshed mtcp_define_event(event_t event, filter_t filter, struct filter_arg *arg); 38176404edcSAsim Jamshed 38276404edcSAsim Jamshed /** Raise a event 38376404edcSAsim Jamshed * @param [in] mctx: mtcp context 38476404edcSAsim Jamshed * @param [in] event: event id 38576404edcSAsim Jamshed * @return 0 on success, -1 on error 38676404edcSAsim Jamshed */ 38776404edcSAsim Jamshed int 38876404edcSAsim Jamshed mtcp_raise_event(mctx_t mctx, event_t event); 38976404edcSAsim Jamshed 39076404edcSAsim Jamshed /* 39176404edcSAsim Jamshed * Callback only functions 39276404edcSAsim Jamshed */ 39376404edcSAsim Jamshed 39476404edcSAsim Jamshed /** Set user-level context 39576404edcSAsim Jamshed * (e.g., to store any per-flow user-defined meatadata) 39676404edcSAsim Jamshed * @param [in] mctx: mtcp context 39776404edcSAsim Jamshed * @param [in] sock: the monitor socket id 39876404edcSAsim Jamshed * @param [in] uctx: user-level context 39976404edcSAsim Jamshed */ 40076404edcSAsim Jamshed void 40176404edcSAsim Jamshed mtcp_set_uctx(mctx_t mctx, int sock, void *uctx); 40276404edcSAsim Jamshed 40376404edcSAsim Jamshed /** Get user-level context 40476404edcSAsim Jamshed * (e.g., to retrieve user-defined metadata stored in mtcp_set_uctx()) 40576404edcSAsim Jamshed * @param [in] mctx: mtcp context 40676404edcSAsim Jamshed * @param [in] sock: the monitor socket id 40776404edcSAsim Jamshed * @return user-level context for input flow_ocntext 40876404edcSAsim Jamshed */ 40976404edcSAsim Jamshed void * 41076404edcSAsim Jamshed mtcp_get_uctx(mctx_t mctx, int sock); 41176404edcSAsim Jamshed 41276404edcSAsim Jamshed /** Peeking bytestream from flow_context 41376404edcSAsim Jamshed * @param [in] mctx: mtcp context 41476404edcSAsim Jamshed * @param [in] sock: monitoring stream socket id 41576404edcSAsim Jamshed * @param [in] side: side of monitoring (client side, server side or both) 41676404edcSAsim Jamshed * @param [in] buf: buffer for read byte stream 41776404edcSAsim Jamshed * @param [in] len: requested length 41876404edcSAsim Jamshed * 41976404edcSAsim Jamshed * It will return the number of bytes actually read. 42076404edcSAsim Jamshed * It will return -1 if there is an error 42176404edcSAsim Jamshed */ 42276404edcSAsim Jamshed ssize_t 42376404edcSAsim Jamshed mtcp_peek(mctx_t mctx, int sock, int side, 42476404edcSAsim Jamshed char *buf, size_t len); 42576404edcSAsim Jamshed 42676404edcSAsim Jamshed /** 42776404edcSAsim Jamshed * The mtcp_ppeek() function reads up to count bytes from the TCP ring 42876404edcSAsim Jamshed * buffer of the monitor socket sock in mctx into buf, starting from 42976404edcSAsim Jamshed * the TCP sequence number seq_num. 43076404edcSAsim Jamshed * Note that seq_num can point the data in the fragmented buffer list 43176404edcSAsim Jamshed * of the TCP ring buffer. If there is no received byte with TCP sequence 43276404edcSAsim Jamshed * number seq_num in the TCP ring buffer, it returns error. If there are 43376404edcSAsim Jamshed * received bytes starting from seq_num, count is set to be the number 43476404edcSAsim Jamshed * of bytes read from the buffer. After mtcp_ppeek(), the data in the 43576404edcSAsim Jamshed * TCP ring buffer will not be flushed, and the monitor offset used by 43676404edcSAsim Jamshed * mtcp_peek() is not changed. 43776404edcSAsim Jamshed * 43876404edcSAsim Jamshed * @param [in] mctx: mtcp context 43976404edcSAsim Jamshed * @param [in] sock: monitoring stream socket id 44076404edcSAsim Jamshed * @param [in] side: side of monitoring (client side, server side or both) 44176404edcSAsim Jamshed * @param [in] buf: buffer for read byte stream 44276404edcSAsim Jamshed * @param [in] count: No. of bytes to be read 44376404edcSAsim Jamshed * @param [in] seq_num: byte offset of the TCP bytestream (absolute offset: offset 0 = init_seq_num) 44476404edcSAsim Jamshed * @return # of bytes actually read on success, -1 for error 44576404edcSAsim Jamshed */ 44676404edcSAsim Jamshed ssize_t mtcp_ppeek(mctx_t mctx, int sock, int side, 44776404edcSAsim Jamshed char *buf, size_t count, uint64_t off); 44876404edcSAsim Jamshed 44976404edcSAsim Jamshed /* Use this macro to copy packets when mtcp_getlastpkt is called */ 45076404edcSAsim Jamshed #define MTCP_CB_GETCURPKT_CREATE_COPY 45176404edcSAsim Jamshed 45276404edcSAsim Jamshed /** Get current packet of mtcp context 45376404edcSAsim Jamshed * @param [in] mctx: mTCP/mOS context 45476404edcSAsim Jamshed * @param [in] sock: monitoring stream socket id 45576404edcSAsim Jamshed * @param [in] side: side of monitoring 45676404edcSAsim Jamshed * (MOS_NULL for MOS_SOCK_MONITOR_RAW socket) 45776404edcSAsim Jamshed * @param [in] p: ptr to packet info ptr 45876404edcSAsim Jamshed * (only L2-L3 information is available for MOS_SOCK_MONITOR_RAW socket) 45976404edcSAsim Jamshed * @return 0 on success, -1 on failure 46076404edcSAsim Jamshed * This is useful for running callback-only applications 46176404edcSAsim Jamshed */ 46276404edcSAsim Jamshed int 46376404edcSAsim Jamshed mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_info *p); 46476404edcSAsim Jamshed 46576404edcSAsim Jamshed /** Register user's custom timer 46676404edcSAsim Jamshed * @param [in] mctx: mtcp context 46776404edcSAsim Jamshed * @param [in] id: timer id 46876404edcSAsim Jamshed * @param [in] timeout: timeout length 46976404edcSAsim Jamshed * @param [in] cb: callback function 47076404edcSAsim Jamshed */ 47176404edcSAsim Jamshed int 47276404edcSAsim Jamshed mtcp_settimer(mctx_t mctx, int id, struct timeval *timeout, callback_t cb); 47376404edcSAsim Jamshed 47476404edcSAsim Jamshed /** A sibling function to mtcp_settimer that returns 47576404edcSAsim Jamshed * the current timestamp of the machine in microseconds. 47676404edcSAsim Jamshed * This avoids the monitor application to call current 47776404edcSAsim Jamshed * time getter functions (e.g. gettimeofday) that may 47876404edcSAsim Jamshed * incur overhead. 47976404edcSAsim Jamshed * 48076404edcSAsim Jamshed * @param [in] mctx: mtcp context 48176404edcSAsim Jamshed * Returns timestamp on success, 0 on failure. 48276404edcSAsim Jamshed */ 48376404edcSAsim Jamshed uint32_t 48476404edcSAsim Jamshed mtcp_cb_get_ts(mctx_t mctx); 48576404edcSAsim Jamshed 48676404edcSAsim Jamshed /** Pause mtcp application context since it is not running anything 48776404edcSAsim Jamshed * @param [in] mctx: mtcp context 48876404edcSAsim Jamshed * 48976404edcSAsim Jamshed * This is useful for running callback-only applications 49076404edcSAsim Jamshed */ 49176404edcSAsim Jamshed void 49276404edcSAsim Jamshed mtcp_app_join(mctx_t mctx); 49376404edcSAsim Jamshed 49476404edcSAsim Jamshed /** Get IP addrs/ports for both sides. 49576404edcSAsim Jamshed * (Server IP/port in 0th element) (Client IP/port in 1st element) 49676404edcSAsim Jamshed * Should only be called with MOS_SOCK_MONITOR_STREAM_ACTIVE socket 497a5e1a556SAsim Jamshed * _NOTE_: Code is currently not set for MOS_SOCK_STREAM!!! 49876404edcSAsim Jamshed * Returns 0 on success, -1 on failure 49976404edcSAsim Jamshed */ 50076404edcSAsim Jamshed int 50176404edcSAsim Jamshed mtcp_getpeername(mctx_t mctx, int sock, struct sockaddr *saddr, socklen_t *addrlen, int side); 50276404edcSAsim Jamshed 50376404edcSAsim Jamshed /** 50476404edcSAsim Jamshed * Updates the Ethernet frame at a given offset across 50576404edcSAsim Jamshed * datalen bytes. 50676404edcSAsim Jamshed * 50776404edcSAsim Jamshed * @param [in] mctx: mtcp context 50876404edcSAsim Jamshed * @param [in] sock: monitoring socket 50976404edcSAsim Jamshed * @param [in] side: monitoring side 51076404edcSAsim Jamshed * (MOS_NULL for MOS_SOCK_MONITOR_RAW socket) 51176404edcSAsim Jamshed * @param [in] offset: the offset from where the data needs to be written 51276404edcSAsim Jamshed * @param [in] data: the data buffer that needs to be written 51376404edcSAsim Jamshed * @param [in] datalen: the length of data that needs to be written 51476404edcSAsim Jamshed * @param [in] option: disjunction of MOS_ETH_HDR, MOS_IP_HDR, MOS_TCP_HDR, 51576404edcSAsim Jamshed * MOS_TCP_PAYLOAD, MOS_DROP_PKT, MOS_UPDATE_TCP_CHKSUM, 51676404edcSAsim Jamshed * MOS_UPDATE_IP_CHKSUM 51776404edcSAsim Jamshed * @return Returns 0 on success, -1 on failure 51876404edcSAsim Jamshed * 51976404edcSAsim Jamshed * If you want to chomp/insert something in the payload: 52076404edcSAsim Jamshed * (i) first update the ip header to adjust iph->tot_len field; (MOS_OVERWRITE) 52176404edcSAsim Jamshed * (ii) then update the tcp payload accordingly (MOS_CHOMP or MOS_INSERT) 52276404edcSAsim Jamshed * 52376404edcSAsim Jamshed * MOS_DROP, MOS_OVERWRITE, MOS_CHOMP and MOS_INSERT are mutually 5243b6b9ba6SAsim Jamshed * exclusive operations 52576404edcSAsim Jamshed */ 52676404edcSAsim Jamshed int 52776404edcSAsim Jamshed mtcp_setlastpkt(mctx_t mctx, int sock, int side, off_t offset, 52876404edcSAsim Jamshed byte *data, uint16_t datalen, int option); 52976404edcSAsim Jamshed 530a834ea89SAsim Jamshed /** Send a TCP packet of struct pkt_info 531a834ea89SAsim Jamshed * @param [in] mctx: mTCP/mOS context 532a834ea89SAsim Jamshed * @param [in] sock: monitoring stream socket id 533a834ea89SAsim Jamshed * @param [in] pkt: ptr to packet info (e.g., captured by mtcp_getlastpkt) 534a834ea89SAsim Jamshed * @return 0 on success, -1 on failure 535a834ea89SAsim Jamshed * (NOTE: this function supports only TCP packet for now. 536a834ea89SAsim Jamshed * we will add the support for any ethernet packets when required) 537a834ea89SAsim Jamshed */ 538a834ea89SAsim Jamshed int 539a834ea89SAsim Jamshed mtcp_sendpkt(mctx_t mctx, int sock, const struct pkt_info *pkt); 540a834ea89SAsim Jamshed 54176404edcSAsim Jamshed #endif /* __MOS_API_H_ */ 542