mOS API description
mos_api.h
Go to the documentation of this file.
1 #ifndef __MOS_API_H_
2 #define __MOS_API_H_
3 
4 #ifdef DARWIN
5 #include <netinet/tcp.h>
6 #include <netinet/if_ether.h>
7 #else
8 #include <linux/tcp.h>
9 #include <linux/if_ether.h>
10 #endif
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <netinet/ip.h>
14 #include <stddef.h> /* for offsetof */
15 #include "mtcp_epoll.h"
16 #include <stdbool.h>
17 
18 #ifndef __MTCP_MANAGER
19 #define __MTCP_MANAGER
20 typedef struct mtcp_manager * mtcp_manager_t;
21 #endif
22 #ifndef __SOCKET_MAP
23 #define __SOCKET_MAP
24 typedef struct socket_map * socket_map_t;
25 #endif
26 
29 {
30  /* NOTE: The value of hooking points should not overlap with any of
31  * mos_event_types */
32 
35  MOS_NULL = (1 << 29),
37  MOS_HK_RCV = (1 << 30),
39  MOS_HK_SND = (1 << 31),
40 };
41 
44 {
47  /* mos-defined tcp build-in events */
49  MOS_ON_PKT_IN = (0x1<<0),
51  /* THIS EVENT IS NOW DEPRECATED (USED ONLY FOR DEBUGGING) */
52  MOS_ON_PKT_OUT = (0x1<<1),
59  MOS_ON_CONN_START = (0x1<<2),
63  /* THIS EVENT IS NOW DEPRECATED */
64  MOS_ON_CONN_SETUP = (0x1<<3),
71  MOS_ON_ERROR = (0x1<<5),
75  MOS_ON_TIMEOUT = (0x1<<6),
79  MOS_ON_ORPHAN = (0x1<<8),
81  MOS_ON_REXMIT = (0x1<<9),
87  MOS_ON_CONN_END = (0x1<<10),
88 
90  MOS_ON_DEBUG_MESSAGE = (0x1<<11),
91 };
92 
93 #if 0
94 /* This may go away in future revisions */
95 typedef union event_data {
96  uint32_t u32;
97  uint64_t u64;
98  void *ptr;
99 } event_data_t;
100 #endif
101 
102 /* Macros for updating packet context */
103 #define MOS_ETH_HDR (1 << 0)
104 #define MOS_IP_HDR (1 << 1)
105 #define MOS_TCP_HDR (1 << 2)
106 #define MOS_TCP_PAYLOAD (1 << 3)
107 #define MOS_UPDATE_IP_CHKSUM (1 << 4)
108 #define MOS_UPDATE_TCP_CHKSUM (1 << 5)
109 #define MOS_DROP (1 << 6)
110 #define MOS_OVERWRITE (1 << 7)
111 #define MOS_CHOMP (1 << 8)
112 #define MOS_INSERT (1 << 9)
113 
122 struct pkt_info {
123  uint32_t cur_ts;
125  /* ETH */
126  uint16_t eth_len;
127 
128  /* IP */
129  uint16_t ip_len;
130 
131  /* TCP */
132  uint64_t offset;
133  uint16_t payloadlen;
134  uint32_t seq;
135  uint32_t ack_seq;
136  uint16_t window;
137 
138  /* ~~ 28 byte boundary ~~ */
139 
140  /*
141  * CAUTION!!!
142  * It is extremely critical that the last 5 fields (ethh .. frame)
143  * are always placed at the end of the definition. MOS relies on
144  * this specific arrangement when it is creating a new instantiation
145  * of pctx during mtcp_getlastpkt() invocation.
146  */
147  struct ethhdr *ethh;
148  struct iphdr *iph;
149  struct tcphdr *tcph;
150  uint8_t *payload;
151 };
152 
157 struct pkt_ctx {
158  struct pkt_info p;
159 
160  int8_t direction;
161  uint8_t forward;
162  int8_t in_ifidx;
163  int8_t out_ifidx;
164  int8_t batch_index;
165  /* ~~ 64 byte boundary ~~ */
166 };
167 #define PKT_INFO_LEN offsetof(struct pkt_info, ethh)
168 
169 /*
170  * Sequence number change structure.
171  * Used for MOS_SEQ_REMAP.
172  */
173 typedef struct {
174  int64_t seq_off; /* the amount of sequence number drift */
175  int side; /* which side does this sequence number change apply to? */
176  uint32_t base_seq; /* seq # of the flow where the actual sequence # translation starts */
178 
179 typedef struct filter_arg {
180  void *arg;
181  size_t len;
182 } filter_arg_t;
183 
188 #ifndef SOL_SOCKET
189 /* Level number for (get/set)sockopt() to apply to socket itself. */
190 #define SOL_SOCKET 0xffff /* options for socket level */
191 #endif
192 #define SOL_MONSOCKET 0xfffe /* MOS monitor socket level */
193 
236  MOS_MONLEVEL = 0x08,
237  MOS_CLIBUF = 0x09,
238  MOS_SVRBUF = 0x0a,
240  MOS_STOP_MON = 0x0c,
245 #ifdef OLD_API
246  MOS_NO_CLIBUF = 0x0f,
247  MOS_NO_SVRBUF = 0x10,
248 #endif
249 };
250 
257 struct tcp_buf_info {
259  uint32_t tcpbi_init_seq;
279 };
280 
283  uint64_t offset;
284  uint32_t len;
285 };
286 
294 {
306 };
307 
309 enum {
313 };
314 
316 typedef uint64_t event_t;
317 
320 
321 /* mos callback/filter function type definition */
323 typedef void (*callback_t)(mctx_t mctx, int sock, int side,
324  event_t event, filter_arg_t *arg);
326 typedef bool (*filter_t)(mctx_t mctx, int sock, int side,
327  event_t event, filter_arg_t *arg);
328 
329 /*----------------------------------------------------------------------------*/
330 /* Definition of monitor_filter type */
335  struct {
338  };
339 };
341 
342 /* Assign an address range (specified by ft) to monitor via sock
343  *
344  * (1) If sock is MOS_SOCK_MONITOR_RAW type, ft.raw_pkt_filter is applied to
345  * every packet coming in.
346  * (2) If sock is MOS_SOCK_MONITOR_STREAM type,
347  * ft.stream_syn_filter is applied to the first SYN pkt of the flow.
348  * (The succeeding packets of that flow will bypass the filter operation.)
349  * ft.stream_orphan_filter is applied to the pkts that don't belong to any
350  * of the existing TCP streams which are being monitored.
351  * (e.g., non-SYN pkt with no identified flow)
352  * [*] ft.stream_syn_filter and ft.stream_orphan_filter should be consisted
353  * only of the following keywords:
354  * - 'tcp, 'host', 'src', 'dst', 'net', 'mask', 'port', 'portrange'
355  * - 'and', 'or', '&', '|'
356  *
357  * @param [in] mctx: mtcp context
358  * @param [in] sock: socket id (should be MOS_SOCK_MONITOR_RAW
359  * or MOS_SOCK_MONITOR_STREAM type)
360  * @param [in] cf: Describe a set of connections to accept
361  * in a BPF (Berkerley Packet Filter) format
362  * NULL if you want to monitor any packet
363  * @return zero on success, -1 on error
364  */
365 int
366 mtcp_bind_monitor_filter(mctx_t mctx, int sock, monitor_filter_t ft);
367 /*----------------------------------------------------------------------------*/
368 
379 int
380 mtcp_register_callback(mctx_t mctx, int sock, event_t event,
381  int hook_point, callback_t cb);
382 
387 event_t
388 mtcp_alloc_event(event_t event);
389 
398 event_t
399 mtcp_define_event(event_t event, filter_t filter, struct filter_arg *arg);
400 
406 int
407 mtcp_raise_event(mctx_t mctx, event_t event);
408 
409 /*
410  * Callback only functions
411  */
412 
419 void
420 mtcp_set_uctx(mctx_t mctx, int sock, void *uctx);
421 
428 void *
429 mtcp_get_uctx(mctx_t mctx, int sock);
430 
441 ssize_t
442 mtcp_peek(mctx_t mctx, int sock, int side,
443  char *buf, size_t len);
444 
465 ssize_t mtcp_ppeek(mctx_t mctx, int sock, int side,
466  char *buf, size_t count, uint64_t off);
467 
468 /* Use this macro to copy packets when mtcp_getlastpkt is called */
469 #define MTCP_CB_GETCURPKT_CREATE_COPY
470 
481 int
482 mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_info *p);
483 
490 int
491 mtcp_settimer(mctx_t mctx, int id, struct timeval *timeout, callback_t cb);
492 
502 uint32_t
503 mtcp_cb_get_ts(mctx_t mctx);
504 
510 void
511 mtcp_app_join(mctx_t mctx);
512 
518 int
519 mtcp_getpeername(mctx_t mctx, int sock, struct sockaddr *saddr, socklen_t *addrlen, int side);
520 
547 int
548 mtcp_setlastpkt(mctx_t mctx, int sock, int side, off_t offset,
549  byte *data, uint16_t datalen, int option);
550 
557 //int
558 //mtcp_cb_dropcurpkt(mctx_t mctx);
559 
560 /* Reset the connection (send RST to both sides)
561  * (This API will be updated after discussion.)
562  */
563 int
564 mtcp_reset_conn(mctx_t mctx, int sock);
565 
566 int
567 mtcp_set_debug_string(mtcp_manager_t mtcp, const char *fmt, ...);
568 
569 int
570 mtcp_get_debug_string(mctx_t mctx, char *buf, int len);
571 
572 /**************************************************************************/
573 /* experimental mOS APIs from here
574  (NOTE: do not include those when releasing the mOS release package) */
575 
584 int
585 mtcp_sendpkt(mctx_t mctx, int sock, const struct pkt_info *pkt);
586 
587 /**************************************************************************/
588 
589 #endif /* __MOS_API_H_ */
Definition: mos_api.h:296
TCP state is being changed.
Definition: mos_api.h:77
MOS tcp buf info structure.
Definition: mos_api.h:257
Definition: mos_api.h:233
Definition: mos_api.h:242
bool(* filter_t)(mctx_t mctx, int sock, int side, event_t event, filter_arg_t *arg)
Prototype of UDE's filter function.
Definition: mos_api.h:326
ssize_t mtcp_ppeek(mctx_t mctx, int sock, int side, char *buf, size_t count, uint64_t off)
The mtcp_ppeek() function reads up to count bytes from the TCP ring buffer of the monitor socket sock...
int mtcp_sendpkt(mctx_t mctx, int sock, const struct pkt_info *pkt)
Send a TCP packet of struct pkt_info.
uint32_t seq
Definition: mos_api.h:134
int mtcp_register_callback(mctx_t mctx, int sock, event_t event, int hook_point, callback_t cb)
Register a callback function in hook_point.
int8_t in_ifidx
input interface (ro)
Definition: mos_api.h:162
New data is now readable.
Definition: mos_api.h:68
Definition: mos_api.h:319
struct socket_map * socket_map_t
Definition: mos_api.h:24
struct pkt_info p
Definition: mos_api.h:158
struct mtcp_manager * mtcp_manager_t
Definition: mos_api.h:20
Definition: mos_api.h:299
uint32_t len
Definition: mos_api.h:284
int64_t seq_off
Definition: mos_api.h:174
Definition: mos_api.h:237
mos_event_type
Built-in events provided by mOS.
Definition: mos_api.h:43
Definition: mos_api.h:311
uint64_t offset
TCP ring buffer offset.
Definition: mos_api.h:132
int mtcp_raise_event(mctx_t mctx, event_t event)
Raise a event.
char * stream_syn_filter
Definition: mos_api.h:336
uint8_t * payload
Definition: mos_api.h:150
mos_socket_opts
MOS monitor socket option names (and values) This will contain options pertaining to monitor stream s...
Definition: mos_api.h:228
struct ethhdr * ethh
Definition: mos_api.h:147
uint32_t base_seq
Definition: mos_api.h:176
int mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_info *p)
Get current packet of mtcp context.
event_t mtcp_alloc_event(event_t event)
Allocate a child event.
event_t mtcp_define_event(event_t event, filter_t filter, struct filter_arg *arg)
Define a user-defined event function.
Definition: mos_api.h:305
Definition: mos_api.h:173
int8_t batch_index
index of packet in the rx batch
Definition: mos_api.h:164
int mtcp_settimer(mctx_t mctx, int id, struct timeval *timeout, callback_t cb)
Register user's custom timer.
PACKET CONTEXT is the packet structure that goes through the mOS core...
Definition: mos_api.h:157
Definition: mos_api.h:301
Definition: mos_api.h:298
void(* callback_t)(mctx_t mctx, int sock, int side, event_t event, filter_arg_t *arg)
Prototype of callback function.
Definition: mos_api.h:323
Definition: mos_api.h:229
mtcp_hook_point
Available hooking points.
Definition: mos_api.h:28
Definition: mos_api.h:319
Definition: mos_api.h:300
struct tcphdr * tcph
Definition: mos_api.h:149
uint32_t mtcp_cb_get_ts(mctx_t mctx)
A sibling function to mtcp_settimer that returns the current timestamp of the machine in microseconds...
Definition: mos_api.h:297
char * raw_pkt_filter
For MOS_SOCK_MONITOR_RAW type socket.
Definition: mos_api.h:333
struct filter_arg filter_arg_t
Definition: mos_api.h:179
uint32_t tcpbi_last_byte_read
TCP sequence number of the 'last byte of payload that has already been read by the end application' (...
Definition: mos_api.h:264
No packet is seen for a long time.
Definition: mos_api.h:75
This event is for debugging.
Definition: mos_api.h:90
uint32_t tcpbi_last_byte_received
TCP sequence number of the 'last byte of the payload that is currently stored' in the TCP ring buffer...
Definition: mos_api.h:278
uint16_t eth_len
Definition: mos_api.h:126
int mtcp_bind_monitor_filter(mctx_t mctx, int sock, monitor_filter_t ft)
Definition: mos_api.h:236
tcpstate
mOS tcp stream states.
Definition: mos_api.h:293
Definition: mos_api.h:304
int8_t direction
where does this packet originate from? (ro)
Definition: mos_api.h:160
Definition: mos_api.h:243
Definition: mos_api.h:312
uint32_t tcpbi_next_byte_expected
TCP sequence number of the 'last byte of the payload that is currently buffered and needs to be read ...
Definition: mos_api.h:272
Definition: mos_api.h:232
char * stream_orphan_filter
Definition: mos_api.h:337
Definition: mos_api.h:240
Definition: mos_api.h:331
3-way handshake is finished.
Definition: mos_api.h:64
uint8_t forward
0: drop, 1: forward to out_ifidx (rw)
Definition: mos_api.h:161
Hooking point after TCP sender.
Definition: mos_api.h:39
SYN packet as seen by the monitor client side: activated when the client state is set to SYN_SENT ser...
Definition: mos_api.h:59
Definition: mos_api.h:235
int mtcp_setlastpkt(mctx_t mctx, int sock, int side, off_t offset, byte *data, uint16_t datalen, int option)
Updates the Ethernet frame at a given offset across datalen bytes.
uint64_t event_t
Definition of event type.
Definition: mos_api.h:316
uint64_t offset
Definition: mos_api.h:283
void mtcp_set_uctx(mctx_t mctx, int sock, void *uctx)
Set user-level context (e.g., to store any per-flow user-defined meatadata)
uint16_t ip_len
Definition: mos_api.h:129
Definition: mos_api.h:244
uint16_t payloadlen
Definition: mos_api.h:133
Definition: mos_api.h:302
void * mtcp_get_uctx(mctx_t mctx, int sock)
Get user-level context (e.g., to retrieve user-defined metadata stored in mtcp_set_uctx()) ...
uint32_t ack_seq
Definition: mos_api.h:135
int mtcp_set_debug_string(mtcp_manager_t mtcp, const char *fmt,...)
Definition: mos_api.h:231
Definition: mos_api.h:303
uint32_t cur_ts
packet receiving time (read-only:ro)
Definition: mos_api.h:123
Definition: mos_api.h:241
int mtcp_reset_conn(mctx_t mctx, int sock)
Drop current packet (don't forward it to the peer node)
Definition: mos_api.h:239
A flow is about to be destroyed.
Definition: mos_api.h:87
A packet is not SYN and has no identified flow.
Definition: mos_api.h:79
Very first hooking point of incoming packet even before flow identification.
Definition: mos_api.h:35
void mtcp_app_join(mctx_t mctx)
Pause mtcp application context since it is not running anything.
invalid event
Definition: mos_api.h:46
Definition: mos_api.h:310
struct iphdr * iph
Definition: mos_api.h:148
uint32_t tcpbi_init_seq
The initial TCP sequence number of TCP ring buffer.
Definition: mos_api.h:259
int mtcp_get_debug_string(mctx_t mctx, char *buf, int len)
mTCP context
Definition: mtcp_api.h:23
A packet is coming in.
Definition: mos_api.h:49
Structure to expose TCP ring buffer's fragment information.
Definition: mos_api.h:282
size_t len
Definition: mos_api.h:181
Definition: mos_api.h:319
Definition: mos_api.h:238
Abnormal behavior is detected.
Definition: mos_api.h:71
uint16_t window
Definition: mos_api.h:136
Definition: mos_api.h:230
Definition: mos_api.h:295
struct pkt_info is the struct that is actually exposed to the monitor application.
Definition: mos_api.h:122
Definition: mos_api.h:234
int8_t out_ifidx
output interface (rw)
Definition: mos_api.h:163
ssize_t mtcp_peek(mctx_t mctx, int sock, int side, char *buf, size_t len)
Peeking bytestream from flow_context.
int mtcp_getpeername(mctx_t mctx, int sock, struct sockaddr *saddr, socklen_t *addrlen, int side)
Get IP addrs/ports for both sides.
Retransmission is detected.
Definition: mos_api.h:81
int side
Definition: mos_api.h:175
unsigned char byte
Definition: mtcp_api.h:16
union monitor_filter * monitor_filter_t
Definition: mos_api.h:340
A packet is going out.
Definition: mos_api.h:52
void * arg
Definition: mos_api.h:180
Hooking point before TCP receiver.
Definition: mos_api.h:37