trafgen: Get packet from proto_hdr if possible
[netsniff-ng-new.git] / trafgen_proto.h
blob36b8f2b6cc8d3daefb6c10682f6dd9563970e2dc
1 #ifndef TRAFGEN_PROTO_H
2 #define TRAFGEN_PROTO_H
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
8 #include "trafgen_dev.h"
10 struct packet;
12 enum proto_id {
13 PROTO_NONE = 0,
14 PROTO_ETH,
15 PROTO_PAUSE,
16 PROTO_PFC,
17 PROTO_VLAN,
18 PROTO_ARP,
19 PROTO_MPLS,
20 PROTO_IP4,
21 PROTO_ICMP4,
22 PROTO_IP6,
23 PROTO_ICMP6,
24 PROTO_UDP,
25 PROTO_TCP,
26 PROTO_DNS,
27 __PROTO_MAX,
30 enum proto_layer {
31 PROTO_L0, /* invalid layer */
32 PROTO_L2,
33 PROTO_L3,
34 PROTO_L4,
35 PROTO_L7,
38 struct proto_field;
39 struct proto_hdr;
41 struct proto_ops {
42 enum proto_id id;
43 enum proto_layer layer;
45 void (*header_init)(struct proto_hdr *hdr);
46 void (*header_finish)(struct proto_hdr *hdr);
47 void (*push_sub_header)(struct proto_hdr *hdr, struct proto_hdr *sub_hdr);
48 void (*field_changed)(struct proto_field *field);
49 void (*packet_finish)(struct proto_hdr *hdr);
50 void (*packet_update)(struct proto_hdr *hdr);
51 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
54 struct proto_hdr {
55 const struct proto_ops *ops;
56 struct proto_hdr *parent;
57 struct proto_hdr **sub_headers;
58 uint32_t sub_headers_count;
59 uint16_t pkt_offset;
60 uint32_t pkt_id;
61 uint32_t index;
62 struct proto_field *fields;
63 size_t fields_count;
64 bool is_csum_valid;
65 uint32_t id;
66 size_t len;
69 enum proto_field_func_t {
70 PROTO_FIELD_FUNC_INC = 1 << 0,
71 PROTO_FIELD_FUNC_MIN = 1 << 1,
72 PROTO_FIELD_FUNC_RND = 1 << 2,
75 struct proto_field_func {
76 enum proto_field_func_t type;
77 uint32_t min;
78 uint32_t max;
79 int32_t inc;
80 uint16_t offset;
81 uint32_t val;
82 size_t len;
84 void (*update_field)(struct proto_field *field);
87 struct proto_field {
88 uint32_t id;
89 size_t len;
90 uint32_t shift;
91 uint32_t mask;
92 /* might be negative (e.g. VLAN TPID field) */
93 int16_t offset;
95 struct proto_field_func func;
96 bool is_set;
97 uint16_t pkt_offset;
98 struct proto_hdr *hdr;
101 extern void protos_init(struct dev_io *dev);
102 extern void proto_ops_register(const struct proto_ops *ops);
104 extern struct proto_hdr *proto_header_push(enum proto_id pid);
105 extern void proto_header_finish(struct proto_hdr *hdr);
106 extern void proto_packet_finish(void);
107 extern void proto_packet_update(uint32_t idx);
109 extern struct packet *proto_hdr_packet(struct proto_hdr *hdr);
110 extern struct proto_hdr *proto_hdr_push_sub_header(struct proto_hdr *hdr, int id);
111 extern void proto_hdr_move_sub_header(struct proto_hdr *hdr, struct proto_hdr *from,
112 struct proto_hdr *to);
114 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
115 enum proto_id pid);
117 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
118 extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
119 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
121 extern void proto_header_fields_add(struct proto_hdr *hdr,
122 const struct proto_field *fields,
123 size_t count);
125 extern bool proto_hdr_field_is_set(struct proto_hdr *hdr, uint32_t fid);
126 extern void proto_hdr_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
127 const uint8_t *bytes, size_t len);
128 extern void proto_hdr_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
129 extern uint8_t proto_hdr_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
130 extern void proto_hdr_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
131 extern uint16_t proto_hdr_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
132 extern void proto_hdr_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
133 extern uint32_t proto_hdr_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
135 extern void proto_hdr_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
136 const uint8_t *bytes, size_t len);
137 extern void proto_hdr_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
138 uint8_t val);
139 extern void proto_hdr_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
140 uint16_t val);
141 extern void proto_hdr_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
142 uint32_t val);
144 extern void proto_hdr_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
145 extern void proto_hdr_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
147 extern void proto_hdr_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
148 uint16_t val);
149 extern void proto_hdr_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
150 uint32_t val);
152 extern void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
153 extern void proto_hdr_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
155 extern void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
156 extern void proto_hdr_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
158 extern void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
159 extern void proto_hdr_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
161 extern void proto_hdr_field_set_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
162 extern void proto_hdr_field_set_default_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
164 extern void proto_field_dyn_apply(struct proto_field *field);
166 extern struct proto_field *proto_hdr_field_by_id(struct proto_hdr *hdr, uint32_t fid);
169 extern void proto_field_set_u8(struct proto_field *field, uint8_t val);
170 extern uint8_t proto_field_get_u8(struct proto_field *field);
171 extern void proto_field_set_u16(struct proto_field *field, uint16_t val);
172 extern uint16_t proto_field_get_u16(struct proto_field *field);
173 extern void proto_field_set_u32(struct proto_field *field, uint32_t val);
174 extern uint32_t proto_field_get_u32(struct proto_field *field);
175 extern void proto_field_set_be16(struct proto_field *field, uint16_t val);
176 extern void proto_field_set_be32(struct proto_field *field, uint32_t val);
177 extern void proto_field_set_bytes(struct proto_field *field, const uint8_t *bytes, size_t len);
178 extern void proto_field_set_string(struct proto_field *field, const char *str);
179 extern void proto_field_set_default_string(struct proto_field *field, const char *str);
181 extern void proto_field_func_add(struct proto_field *field,
182 struct proto_field_func *func);
184 extern struct dev_io *proto_dev_get(void);
186 #endif /* TRAFGEN_PROTO_H */