flowtop: Minimize delay via halfdelay(1) function
[netsniff-ng.git] / trafgen_proto.h
blob29d68db1016d384bd1df2618a2dfe7f6267bb924
1 #ifndef TRAFGEN_PROTO_H
2 #define TRAFGEN_PROTO_H
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
8 enum proto_id {
9 PROTO_NONE = 0,
10 PROTO_ETH,
11 PROTO_PAUSE,
12 PROTO_PFC,
13 PROTO_VLAN,
14 PROTO_ARP,
15 PROTO_MPLS,
16 PROTO_IP4,
17 PROTO_ICMP4,
18 PROTO_IP6,
19 PROTO_ICMP6,
20 PROTO_UDP,
21 PROTO_TCP,
22 __PROTO_MAX,
25 enum proto_layer {
26 PROTO_L0, /* invalid layer */
27 PROTO_L2,
28 PROTO_L3,
29 PROTO_L4,
32 struct proto_field;
33 struct proto_hdr;
35 struct proto_ops {
36 enum proto_id id;
37 enum proto_layer layer;
39 void (*header_init)(struct proto_hdr *hdr);
40 void (*header_finish)(struct proto_hdr *hdr);
41 void (*field_changed)(struct proto_field *field);
42 void (*packet_finish)(struct proto_hdr *hdr);
43 void (*packet_update)(struct proto_hdr *hdr);
44 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
47 struct proto_hdr {
48 const struct proto_ops *ops;
49 uint16_t pkt_offset;
50 uint32_t pkt_id;
51 uint32_t index;
52 struct proto_field *fields;
53 size_t fields_count;
54 bool is_csum_valid;
55 size_t len;
58 enum proto_field_func_t {
59 PROTO_FIELD_FUNC_INC = 1 << 0,
60 PROTO_FIELD_FUNC_MIN = 1 << 1,
61 PROTO_FIELD_FUNC_RND = 1 << 2,
64 struct proto_field_func {
65 enum proto_field_func_t type;
66 uint32_t min;
67 uint32_t max;
68 int32_t inc;
69 uint16_t offset;
70 uint32_t val;
71 size_t len;
73 void (*update_field)(struct proto_field *field);
76 struct proto_field {
77 uint32_t id;
78 size_t len;
79 uint32_t shift;
80 uint32_t mask;
81 /* might be negative (e.g. VLAN TPID field) */
82 int16_t offset;
84 struct proto_field_func func;
85 bool is_set;
86 uint16_t pkt_offset;
87 struct proto_hdr *hdr;
90 extern void protos_init(const char *dev);
91 extern void proto_ops_register(const struct proto_ops *ops);
93 extern struct proto_hdr *proto_header_push(enum proto_id pid);
94 extern void proto_header_finish(struct proto_hdr *hdr);
95 extern void proto_packet_finish(void);
96 extern void proto_packet_update(uint32_t idx);
98 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
99 enum proto_id pid);
101 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
102 extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
103 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
105 extern void proto_header_fields_add(struct proto_hdr *hdr,
106 const struct proto_field *fields,
107 size_t count);
109 extern bool proto_hdr_field_is_set(struct proto_hdr *hdr, uint32_t fid);
110 extern void proto_hdr_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
111 const uint8_t *bytes);
112 extern void proto_hdr_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
113 extern uint8_t proto_hdr_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
114 extern void proto_hdr_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
115 extern uint16_t proto_hdr_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
116 extern void proto_hdr_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
117 extern uint32_t proto_hdr_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
119 extern void proto_hdr_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
120 const uint8_t *bytes);
121 extern void proto_hdr_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
122 uint8_t val);
123 extern void proto_hdr_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
124 uint16_t val);
125 extern void proto_hdr_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
126 uint32_t val);
128 extern void proto_hdr_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
129 extern void proto_hdr_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
131 extern void proto_hdr_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
132 uint16_t val);
133 extern void proto_hdr_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
134 uint32_t val);
136 extern void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
137 extern void proto_hdr_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
139 extern void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
140 extern void proto_hdr_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
142 extern void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
143 extern void proto_hdr_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
145 extern void proto_field_dyn_apply(struct proto_field *field);
147 extern struct proto_field *proto_hdr_field_by_id(struct proto_hdr *hdr, uint32_t fid);
150 extern void proto_field_set_u8(struct proto_field *field, uint8_t val);
151 extern uint8_t proto_field_get_u8(struct proto_field *field);
152 extern void proto_field_set_u16(struct proto_field *field, uint16_t val);
153 extern uint16_t proto_field_get_u16(struct proto_field *field);
154 extern void proto_field_set_u32(struct proto_field *field, uint32_t val);
155 extern uint32_t proto_field_get_u32(struct proto_field *field);
156 extern void proto_field_set_be16(struct proto_field *field, uint16_t val);
157 extern void proto_field_set_be32(struct proto_field *field, uint32_t val);
159 extern void proto_field_func_add(struct proto_field *field,
160 struct proto_field_func *func);
162 #endif /* TRAFGEN_PROTO_H */