-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclass.h
270 lines (214 loc) · 6.35 KB
/
class.h
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef __CLASS_H__
#define __CLASS_H__
#include <linux/skbuff.h>
/*
* Classification can be based on skb->dev, src hwaddr, ip, tcp, etc.
* It is your job to ensure that exactly ONE of the #defines are
* defined, preferably in your Makefile.
*/
// #define ISO_TX_CLASS_ETHER_SRC
// #define ISO_TX_CLASS_DEV
// #define ISO_TX_CLASS_MARK
struct iso_tx_context;
#if defined ISO_TX_CLASS_DEV
typedef struct net_device *iso_class_t;
#elif defined ISO_TX_CLASS_ETHER_SRC
typedef struct {
u8 addr[ETH_ALEN];
}iso_class_t;
#elif defined ISO_TX_CLASS_MARK
typedef u32 iso_class_t;
#elif defined ISO_TX_CLASS_IPADDR || defined ISO_TX_CLASS_L4PORT
typedef u32 iso_class_t;
#endif
/* Maybe we need an "ops" structure */
static inline iso_class_t iso_txc_classify(struct sk_buff *);
static inline void iso_class_free(iso_class_t);
static inline int iso_class_cmp(iso_class_t a, iso_class_t b);
static inline u32 iso_class_hash(iso_class_t);
static inline void iso_class_show(iso_class_t, char *);
static inline iso_class_t iso_class_parse(char*);
static inline struct iso_tx_class *iso_txc_find(iso_class_t, struct iso_tx_context *);
/* First attempt: out device classification. Its address is usually
aligned, so shift out the zeroes */
#if defined ISO_TX_CLASS_DEV
static inline iso_class_t iso_txc_classify(struct sk_buff *pkt) {
return pkt->dev;
}
static inline void iso_class_free(iso_class_t klass) {
dev_put((struct net_device *)klass);
}
static inline int iso_class_cmp(iso_class_t a, iso_class_t b) {
return (u64)a - (u64)b;
}
static inline u32 iso_class_hash(iso_class_t klass) {
return (u32) ((u64)klass >> 12);
}
static inline void iso_class_show(iso_class_t klass, char *buff) {
sprintf(buff, "%p", klass);
}
/* XXX: should this routine do "dev_put" as well? Maybe it should, as
* we will not be dereferencing iso_class_t anyway?
*/
static inline iso_class_t iso_class_parse(char *devname) {
struct net_device *dev;
rcu_read_lock();
dev = dev_get_by_name_rcu(&init_net, devname);
rcu_read_unlock();
dev_put(dev);
return dev;
}
static inline iso_class_t iso_rx_classify(struct sk_buff *skb) {
return skb->dev;
}
#elif defined ISO_TX_CLASS_ETHER_SRC
static inline iso_class_t iso_txc_classify(struct sk_buff *skb) {
iso_class_t ret;
memcpy((unsigned char *)&ret, eth_hdr(skb)->h_source, ETH_ALEN);
return ret;
}
static inline void iso_class_free(iso_class_t klass) {}
static inline int iso_class_cmp(iso_class_t a, iso_class_t b) {
return memcmp(&a, &b, sizeof(a));
}
static inline u32 iso_class_hash(iso_class_t klass) {
return jhash((void *)&klass, sizeof(iso_class_t), 0);
}
/* Just lazy, looks weird */
static inline void iso_class_show(iso_class_t klass, char *buff) {
#define O "%02x:"
#define OO "%02x"
u8 *o = &klass.addr[0];
sprintf(buff, O O O O O OO,
o[0], o[1], o[2], o[3], o[4], o[5]);
#undef O
#undef OO
}
static inline iso_class_t iso_class_parse(char *hwaddr) {
iso_class_t ret = {.addr = {0}};
mac_pton(hwaddr, (u8*)&ret);
return ret;
}
static inline iso_class_t iso_rx_classify(struct sk_buff *skb) {
iso_class_t klass;
memcpy((void *)&klass, eth_hdr(skb)->h_dest, ETH_ALEN);
return klass;
}
#elif defined ISO_TX_CLASS_MARK
static inline iso_class_t iso_txc_classify(struct sk_buff *skb) {
return skb->mark;
}
static inline void iso_class_free(iso_class_t klass) {}
static inline int iso_class_cmp(iso_class_t a, iso_class_t b) {
return a - b;
}
/* We don't do any bit mixing here; it's for ease of use */
static inline u32 iso_class_hash(iso_class_t klass) {
return klass;
}
/* Just lazy, looks weird */
static inline void iso_class_show(iso_class_t klass, char *buff) {
sprintf(buff, "%d", klass);
}
static inline iso_class_t iso_class_parse(char *hwaddr) {
int ret = 0;
sscanf(hwaddr, "%d", &ret);
return ret;
}
static inline iso_class_t iso_rx_classify(struct sk_buff *skb) {
return skb->mark;
}
#elif defined ISO_TX_CLASS_IPADDR
static inline iso_class_t iso_txc_classify(struct sk_buff *skb) {
struct ethhdr *eth;
u32 addr = 0;
eth = eth_hdr(skb);
if(likely(eth->h_proto == __constant_htons(ETH_P_IP))) {
addr = ip_hdr(skb)->saddr;
}
return addr;
}
static inline void iso_class_free(iso_class_t klass) {}
static inline int iso_class_cmp(iso_class_t a, iso_class_t b) {
return a - b;
}
/* We don't do any bit mixing here; it's for ease of use */
static inline u32 iso_class_hash(iso_class_t klass) {
return jhash_1word(klass, 0xdeadbeef);
}
static inline void iso_class_show(iso_class_t klass, char *buff) {
u32 addr = htonl(klass);
sprintf(buff, "%u.%u.%u.%u",
(addr & 0xFF000000) >> 24,
(addr & 0x00FF0000) >> 16,
(addr & 0x0000FF00) >> 8,
(addr & 0x000000FF));
}
static inline iso_class_t iso_class_parse(char *ipaddr) {
u32 addr, oct[4];
int n;
addr = 0;
n = sscanf(ipaddr, "%u.%u.%u.%u", oct, oct+1, oct+2, oct+3);
if(n == 4) {
addr = (oct[0] << 24) | (oct[1] << 16) | (oct[2] << 8) | oct[3];
}
return htonl(addr);
}
static inline iso_class_t iso_rx_classify(struct sk_buff *skb) {
iso_class_t klass = 0;
struct ethhdr *eth = NULL;
klass = 0;
eth = eth_hdr(skb);
if(likely(eth->h_proto == __constant_htons(ETH_P_IP))) {
klass = ip_hdr(skb)->daddr;
}
return klass;
}
#elif defined ISO_TX_CLASS_L4PORT
static inline iso_class_t iso_txc_classify(struct sk_buff *skb) {
struct ethhdr *eth;
struct iphdr *iph;
u32 port = 0;
eth = eth_hdr(skb);
if(likely(eth->h_proto == __constant_htons(ETH_P_IP))) {
iph = ip_hdr(skb);
switch (iph->protocol) {
case IPPROTO_TCP:
port = tcp_hdr(skb)->dest;
break;
case IPPROTO_UDP:
port = udp_hdr(skb)->dest;
break;
}
}
return port;
}
static inline void iso_class_free(iso_class_t klass) {}
static inline int iso_class_cmp(iso_class_t a, iso_class_t b) {
return a - b;
}
/* We don't do any bit mixing here; it's for ease of use */
static inline u32 iso_class_hash(iso_class_t klass) {
return jhash_1word(klass, 0xdeadbeef);
}
static inline void iso_class_show(iso_class_t klass, char *buff) {
u32 addr = htons(klass);
sprintf(buff, "%u", addr);
}
static inline iso_class_t iso_class_parse(char *portnum) {
u32 port;
int n;
port = 0;
n = sscanf(portnum, "%u", &port);
return htons(port);
}
/* Even on the receive side, we should only look at the L4 dest. port
* number... */
static inline iso_class_t iso_rx_classify(struct sk_buff *skb) {
return iso_txc_classify(skb);
}
#endif
#endif /* __CLASS_H__ */
/* Local Variables: */
/* indent-tabs-mode:t */
/* End: */