astraceroute: use switch instead of lookup table for short proto id
[netsniff-ng.git] / trafgen_proto.h
blobd756acaaceeffeff782352c3d4a256e7b4afc9de
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);
52 enum proto_id (*get_next_proto)(struct proto_hdr *hdr);
55 struct proto_hdr {
56 const struct proto_ops *ops;
57 struct proto_hdr *parent;
58 struct proto_hdr **sub_headers;
59 uint32_t sub_headers_count;
60 uint16_t pkt_offset;
61 uint32_t pkt_id;
62 uint32_t index;
63 struct proto_field *fields;
64 size_t fields_count;
65 bool is_csum_valid;
66 uint32_t id;
67 size_t len;
70 enum proto_field_func_t {
71 PROTO_FIELD_FUNC_INC = 1 << 0,
72 PROTO_FIELD_FUNC_MIN = 1 << 1,
73 PROTO_FIELD_FUNC_RND = 1 << 2,
76 struct proto_field_func {
77 enum proto_field_func_t type;
78 uint32_t min;
79 uint32_t max;
80 int32_t inc;
81 uint16_t offset;
82 uint32_t val;
83 size_t len;
85 void (*update_field)(struct proto_field *field);
88 struct proto_field {
89 uint32_t id;
90 size_t len;
91 uint32_t shift;
92 uint32_t mask;
93 /* might be negative (e.g. VLAN TPID field) */
94 int16_t offset;
96 struct proto_field_func func;
97 bool is_set;
98 uint16_t pkt_offset;
99 struct proto_hdr *hdr;
102 extern void protos_init(struct dev_io *dev);
103 extern void proto_ops_register(const struct proto_ops *ops);
105 struct proto_hdr *proto_packet_apply(enum proto_id pid, struct packet *pkt);
106 extern struct proto_hdr *proto_header_push(enum proto_id pid);
107 extern void proto_header_finish(struct proto_hdr *hdr);
108 extern void proto_packet_finish(void);
109 extern void proto_packet_update(uint32_t idx);
111 extern enum proto_id proto_hdr_get_next_proto(struct proto_hdr *hdr);
112 extern struct packet *proto_hdr_packet(struct proto_hdr *hdr);
113 extern struct proto_hdr *proto_hdr_push_sub_header(struct proto_hdr *hdr, int id);
114 extern void proto_hdr_move_sub_header(struct proto_hdr *hdr, struct proto_hdr *from,
115 struct proto_hdr *to);
117 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
118 enum proto_id pid);
120 extern struct proto_hdr *packet_last_header(struct packet *pkt);
121 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
122 extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
123 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
125 extern void proto_header_fields_add(struct proto_hdr *hdr,
126 const struct proto_field *fields,
127 size_t count);
129 extern bool proto_hdr_field_is_set(struct proto_hdr *hdr, uint32_t fid);
130 extern uint8_t *proto_hdr_field_get_bytes(struct proto_hdr *hdr, uint32_t fid);
131 extern void proto_hdr_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
132 const uint8_t *bytes, size_t len);
133 extern void proto_hdr_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
134 extern uint8_t proto_hdr_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
135 extern void proto_hdr_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
136 extern uint16_t proto_hdr_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
137 extern void proto_hdr_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
138 extern uint32_t proto_hdr_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
139 extern uint32_t proto_hdr_field_get_be32(struct proto_hdr *hdr, uint32_t fid);
141 extern void proto_hdr_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
142 const uint8_t *bytes, size_t len);
143 extern void proto_hdr_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
144 uint8_t val);
145 extern void proto_hdr_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
146 uint16_t val);
147 extern void proto_hdr_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
148 uint32_t val);
150 extern void proto_hdr_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
151 extern void proto_hdr_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
153 extern void proto_hdr_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
154 uint16_t val);
155 extern void proto_hdr_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
156 uint32_t val);
158 extern void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
159 extern void proto_hdr_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
161 extern void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
162 extern void proto_hdr_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
164 extern void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
165 extern void proto_hdr_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
167 extern void proto_hdr_field_set_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
168 extern void proto_hdr_field_set_default_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
170 extern void proto_field_dyn_apply(struct proto_field *field);
172 extern struct proto_field *proto_hdr_field_by_id(struct proto_hdr *hdr, uint32_t fid);
175 extern void proto_field_set_u8(struct proto_field *field, uint8_t val);
176 extern uint8_t proto_field_get_u8(struct proto_field *field);
177 extern void proto_field_set_u16(struct proto_field *field, uint16_t val);
178 extern uint16_t proto_field_get_u16(struct proto_field *field);
179 extern void proto_field_set_u32(struct proto_field *field, uint32_t val);
180 extern uint32_t proto_field_get_u32(struct proto_field *field);
181 extern uint32_t proto_field_get_be32(struct proto_field *field);
182 extern void proto_field_set_be16(struct proto_field *field, uint16_t val);
183 extern void proto_field_set_be32(struct proto_field *field, uint32_t val);
184 extern void proto_field_set_bytes(struct proto_field *field, const uint8_t *bytes, size_t len);
185 extern void proto_field_set_string(struct proto_field *field, const char *str);
186 extern void proto_field_set_default_string(struct proto_field *field, const char *str);
188 extern void proto_field_func_add(struct proto_field *field,
189 struct proto_field_func *func);
191 extern struct dev_io *proto_dev_get(void);
193 #endif /* TRAFGEN_PROTO_H */