trafgen: proto: Improve to find lower header by index
[netsniff-ng.git] / trafgen_proto.h
blob35a55d5fd414094afe5e9a843889b1f7ad935a94
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_VLAN,
12 PROTO_ARP,
13 PROTO_MPLS,
14 PROTO_IP4,
15 PROTO_ICMP4,
16 PROTO_IP6,
17 PROTO_ICMP6,
18 PROTO_UDP,
19 PROTO_TCP,
20 __PROTO_MAX,
23 enum proto_layer {
24 PROTO_L0, /* invalid layer */
25 PROTO_L2,
26 PROTO_L3,
27 PROTO_L4,
30 struct proto_field;
31 struct proto_hdr;
33 struct proto_ops {
34 enum proto_id id;
35 enum proto_layer layer;
37 void (*header_init)(struct proto_hdr *hdr);
38 void (*header_finish)(struct proto_hdr *hdr);
39 void (*field_changed)(struct proto_field *field);
40 void (*packet_finish)(struct proto_hdr *hdr);
41 void (*packet_update)(struct proto_hdr *hdr);
42 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
45 struct proto_hdr {
46 const struct proto_ops *ops;
47 uint16_t pkt_offset;
48 uint32_t pkt_id;
49 uint32_t index;
50 struct proto_field *fields;
51 size_t fields_count;
52 bool is_csum_valid;
53 size_t len;
56 enum proto_field_func_t {
57 PROTO_FIELD_FUNC_INC = 1 << 0,
58 PROTO_FIELD_FUNC_MIN = 1 << 1,
59 PROTO_FIELD_FUNC_RND = 1 << 2,
62 struct proto_field_func {
63 enum proto_field_func_t type;
64 uint32_t min;
65 uint32_t max;
66 int32_t inc;
67 uint32_t val;
69 void (*update_field)(struct proto_field *field);
72 struct proto_field {
73 uint32_t id;
74 size_t len;
75 uint32_t shift;
76 uint32_t mask;
77 /* might be negative (e.g. VLAN TPID field) */
78 int16_t offset;
80 struct proto_field_func func;
81 bool is_set;
82 uint16_t pkt_offset;
83 struct proto_hdr *hdr;
86 extern void protos_init(const char *dev);
87 extern void proto_ops_register(const struct proto_ops *ops);
89 extern struct proto_hdr *proto_header_push(enum proto_id pid);
90 extern void proto_header_finish(struct proto_hdr *hdr);
91 extern void proto_packet_finish(void);
92 extern void proto_packet_update(uint32_t idx);
94 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
95 enum proto_id pid);
97 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
98 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
100 extern void proto_header_fields_add(struct proto_hdr *hdr,
101 const struct proto_field *fields,
102 size_t count);
104 extern bool proto_field_is_set(struct proto_hdr *hdr, uint32_t fid);
105 extern void proto_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
106 const uint8_t *bytes);
107 extern void proto_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
108 extern uint8_t proto_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
109 extern void proto_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
110 extern uint16_t proto_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
111 extern void proto_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
112 extern uint32_t proto_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
114 extern void proto_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
115 const uint8_t *bytes);
116 extern void proto_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
117 uint8_t val);
118 extern void proto_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
119 uint16_t val);
120 extern void proto_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
121 uint32_t val);
123 extern void proto_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
124 extern void proto_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
126 extern void proto_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
127 uint16_t val);
128 extern void proto_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
129 uint32_t val);
131 extern void proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
132 extern void proto_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
134 extern void proto_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
135 extern void proto_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
137 extern void proto_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
138 extern void proto_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
140 extern void proto_field_dyn_apply(struct proto_field *field);
142 extern void proto_field_func_add(struct proto_hdr *hdr, uint32_t fid,
143 struct proto_field_func *func);
145 #endif /* TRAFGEN_PROTO_H */