-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
210 lines (154 loc) · 3.88 KB
/
common.h
File metadata and controls
210 lines (154 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef BPFW_COMMON_H
#define BPFW_COMMON_H
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <linux/types.h>
#include <linux/if_ether.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define STRINGIFY(x) #x
#define BPFW_NAME(x) STRINGIFY(x) // Used to get the prog/map name as a string (used for user space programs)
#define BPFW_XDP_PROG bpfw_xdp
#define BPFW_TC_PROG bpfw_tc
#define XDP_PROG_NAME BPFW_NAME(BPFW_XDP_PROG)
#define TC_PROG_NAME BPFW_NAME(BPFW_TC_PROG)
#define BPFW_IPV4_FLOW_MAP bpfw_ipv4_flows
#define BPFW_IPV6_FLOW_MAP bpfw_ipv6_flows
#define IPV4_FLOW_MAP_NAME BPFW_NAME(BPFW_IPV4_FLOW_MAP)
#define IPV6_FLOW_MAP_NAME BPFW_NAME(BPFW_IPV6_FLOW_MAP)
#define FLOW_MAP_DEFAULT_MAX_ENTRIES 1024
#define BPFW_RSS_IPV4_FLOW_MAP bpfw_rss_ipv4_flows
#define BPFW_RSS_IPV6_FLOW_MAP bpfw_rss_ipv6_flows
#define BPFW_RSS_IPV4_FLOW_MAP_NAME BPFW_NAME(BPFW_RSS_IPV4_FLOW_MAP)
#define BPFW_RSS_IPV6_FLOW_MAP_NAME BPFW_NAME(BPFW_RSS_IPV6_FLOW_MAP)
#define RSS_FLOW_MAP_DEFAULT_MAX_ENTRIES FLOW_MAP_DEFAULT_MAX_ENTRIES
#define BPFW_CPU_MAP bpfw_cpu_map
#define BPFW_CPU_MAP_NAME BPFW_NAME(BPFW_CPU_MAP)
#define BPFW_CPU_COUNT_SECTION ".rodata.cpu_count"
#define CPU_MAP_QUEUE_SIZE 16384
#define USERSPACE_TIME_SECTION ".bss.time"
#define DSA_TAG_SECTION ".rodata.dsa.tag"
#define DSA_SWITCH_SECTION ".rodata.dsa.switch"
#define DSA_PROTO_MAX_LEN 8
#define DSA_PORT_SET (1U << 7)
enum {
STATE_NEW_FLOW,
STATE_NONE,
ACTION_NONE = STATE_NONE,
STATE_PASS,
ACTION_PASS = STATE_PASS,
STATE_DROP,
ACTION_DROP = STATE_DROP,
STATE_FORWARD,
ACTION_FORWARD = STATE_FORWARD,
};
enum {
REWRITE_SRC_IP = 1U << 0,
REWRITE_DEST_IP = 1U << 1,
REWRITE_SRC_PORT = 1U << 2,
REWRITE_DEST_PORT = 1U << 3
};
#define FLOW_KEY_COMMON \
__u32 ifindex; \
__le16 vlan_id; \
__be16 pppoe_id; \
__be16 src_port; \
__be16 dest_port; \
__u8 dsa_port; \
__u8 family; \
__u8 proto; \
__u8 __pad;
struct flow4_ip {
__be32 src[1], dest[1];
};
struct flow6_ip {
__be32 src[4], dest[4];
};
union flow_ip {
struct flow4_ip v4;
struct flow6_ip v6;
};
struct flow_key {
FLOW_KEY_COMMON
union flow_ip ip;
};
struct next_hop {
__u32 ifindex;
__u8 src_mac [ETH_ALEN];
__u8 dest_mac[ETH_ALEN];
__le16 vlan_id;
__be16 pppoe_id;
__u16 mtu;
__u8 dsa_port;
__s8 l2_diff;
};
struct nat_entry {
__be32 src_ip[4];
__be32 dest_ip[4];
__be16 src_port;
__be16 dest_port;
__sum16 l4_cksum_diff;
__u8 rewrite_flag;
};
struct next_entry {
struct next_hop hop;
struct nat_entry nat;
__sum16 ipv4_cksum_diff;
};
struct flow_value {
__u64 time;
__u8 src_mac[ETH_ALEN];
union {
__u8 action, state;
};
struct next_entry next;
};
struct user_time {
__u64 timeout;
__u64 last_time;
bool warned_about_timeout;
};
struct dsa_switch {
__u32 ifindex;
__u8 proto;
};
struct dsa_tag {
char proto[DSA_PROTO_MAX_LEN];
__u8 rx_size, tx_size;
};
struct vlanhdr {
__be16 tci;
__be16 proto;
};
struct pppoehdr {
__u8 vertype;
__u8 code;
__be16 sid;
__be16 length;
__be16 proto;
};
__always_inline static void ip4cpy(__be32 *dest, const __be32 *src) {
dest[0] = src[0];
}
__always_inline static void ip6cpy(__be32 *dest, const __be32 *src) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
}
__always_inline static void ipcpy(__be32 *dest, const __be32 *src, __u8 family) {
switch (family) {
case AF_INET:
return ip4cpy(dest, src);
case AF_INET6:
return ip6cpy(dest, src);
}
}
__always_inline static __be32 *flow_ip_get_src(union flow_ip *flow_ip, __u8 family) {
return family == AF_INET ? flow_ip->v4.src : flow_ip->v6.src;
}
__always_inline static __be32 *flow_ip_get_dest(union flow_ip *flow_ip, __u8 family) {
return family == AF_INET ? flow_ip->v4.dest : flow_ip->v6.dest;
}
#endif