forked from gao-feng/colo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxt_COLO.h
324 lines (267 loc) · 6.75 KB
/
xt_COLO.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (c) 2014, 2015 Fujitsu Limited.
* Copyright (c) 2014, 2015 HUAWEI TECHNOLOGIES CO.,LTD.
* Copyright (c) 2014, 2015 Intel Corporation.
*
* Authors:
* Zhang Hailiang <[email protected]>
* Gao feng <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#ifndef _XT_COLO_H
#define _XT_COLO_H
#include <net/netfilter/nf_conntrack_seqadj.h>
#include <linux/kref.h>
#include <net/tcp.h>
#include <net/netfilter/nf_conntrack_l3proto.h>
#define DEBUG
#ifdef DEBUG
#define pr_dbg(fmt, ...) printk(pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_dbg(fmt, ...) do {} while(0)
#endif
#define MIN(X, Y) ((X) <= (Y) ? (X) : (Y))
#define MAX(X, Y) ((X) >= (Y) ? (X) : (Y))
#define COLO_SLAVER_PACKET_MARK 0x1117
#define NF_CT_COLO_ZONE 0x1987
enum colo_conn_flags {
COLO_CONN_BYPASS = 0x1,
COLO_CONN_PRIMARY = 0x2,
COLO_CONN_SECONDARY = 0x4,
COLO_CONN_SYN_RECVD = 0x8,
COLO_CONN_POSITIVE = 0x10,
};
struct colo_primary {
struct task_struct *task;
wait_queue_head_t wait;
bool checkpoint;
};
struct colo_secondary {
bool failover;
};
/* Each VM has a colo_node struct */
struct colo_node {
struct list_head list; /* point to next colo node */
struct rcu_head rcu;
pid_t vm_pid; /* Help to find the this node */
int mode; /* in master side or slave side */
struct net *net;
struct list_head conn_list;
struct list_head wait_list;
spinlock_t lock;
struct work_struct destroy_work;
int (*do_checkpoint_cb)(struct colo_node *node);
void (*destroy_notify_cb)(struct colo_node *node);
union {
struct colo_primary p;
struct colo_secondary s;
} u;
struct kref refcnt; /* Protect colo_node struct */
};
/* MUST small than 48 - sizeof (struct inet_skb_parm) */
struct colo_tcp_cb {
u32 seq;
u32 seq_end;
u32 ack;
u16 dataoff;
u16 thoff;
u16 win;
u16 syn:1,
fin:1,
rst:1;
};
struct colo_udp_cb {
u32 size;
u16 dataoff;
};
struct colo_icmp_cb {
u32 gw;
u16 mtu;
u16 id;
u16 seq;
u8 type;
u8 code;
};
#define COLO_SKB_CB(__skb) ((void *)((__skb)->cb + sizeof(struct inet_skb_parm)))
#define COLO_COMPARE_FREE_MASTER 0x01
#define COLO_COMPARE_NEXT_MASTER 0x02
#define COLO_COMPARE_FREE_SLAVER 0x04
static inline void colo_get_data(struct sk_buff *skb,
char **buff,
u32 doff,
u32 *size)
{
u32 start = skb_headlen(skb);
struct sk_buff *frag_skb;
int i;
*buff = NULL;
/*
pr_dbg("dataoff %u, cb->dataoff %u, doff %u, size %u, skblen %u\n",
dataoff, cb->dataoff, doff, *size, skb->len);
*/
if (doff < start) {
*buff = skb->data + doff;
if (doff + *size <= start)
return;
*size = start - doff;
return;
}
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
u32 frag_size = skb_frag_size(frag);
u32 end = start + frag_size;
if (doff >= end) {
start = end;
continue;
}
*buff = (void *) (page_address(frag->page.p) +
frag->page_offset + doff - start);
if (*size > (frag_size - (doff - start)))
*size = frag_size - doff + start;
return;
}
skb_walk_frags(skb, frag_skb) {
u32 end = start + frag_skb->len;
if (doff >= end) {
start = end;
continue;
}
return colo_get_data(frag_skb, buff, doff - start, size);
}
}
static unsigned int optlen(const u_int8_t *opt, unsigned int offset)
{
if (opt[offset] <= TCPOPT_NOP || opt[offset + 1] == 0)
return 1;
else
return opt[offset + 1];
}
static inline void
colo_operate_tcpopt(struct sk_buff *skb, struct tcphdr *th, u16 *scale)
{
int i, j;
u32 optl = 0;
u8 *opt = (u8 *)th;
for (i = sizeof(*th); i < th->doff * 4; i += optl) {
u_int16_t n, o;
optl = optlen(opt, i);
if (i + optl > th->doff * 4)
break;
if (unlikely(th->syn && scale && opt[i] == TCPOPT_WINDOW &&
optl == TCPOLEN_WINDOW)) {
*scale = *(opt + i + 2);
pr_dbg("master get window sacle %u\n", *scale);
continue;
}
if (opt[i] != TCPOPT_SACK)
continue;
for (j = 0; j < optl; j++) {
o = opt[i + j];
n = TCPOPT_NOP;
if ((i + j) % 2 == 0) {
o <<= 8;
n <<= 8;
}
inet_proto_csum_replace2(&th->check, skb, htons(o), htons(n), 0);
}
memset(opt + i, TCPOPT_NOP, optl);
}
}
static inline
struct tcphdr *colo_get_tcphdr(u_int8_t pf, struct sk_buff *skb,
struct colo_tcp_cb *cb, u16 *scale)
{
struct tcphdr _tcph, *th;
u_int8_t protonum;
unsigned int nhoff = skb_network_offset(skb);
struct nf_conntrack_l3proto *l3proto;
u32 dataoff;
l3proto = __nf_ct_l3proto_find(pf);
if(!l3proto->get_l4proto(skb, nhoff, &dataoff, &protonum)) {
pr_dbg("get iphdr failed\n");
return NULL;
}
if (protonum != IPPROTO_TCP) {
pr_dbg("is not tcp packet\n");
return NULL;
}
th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
if (th == NULL) {
pr_dbg("get tcphdr failed\n");
return NULL;
}
if (!cb)
goto out;
/* Primary skbuff */
if ((th->doff * 4) == sizeof(*th))
goto set_cb;
/* strip sack options & get scale value */
if (!skb_make_writable(skb, dataoff + th->doff * 4))
return NULL;
/* BUG_ON(!scale) */
th = (struct tcphdr *)(skb->data + dataoff);
colo_operate_tcpopt(skb, th, scale);
set_cb:
cb->thoff = dataoff;
cb->dataoff = dataoff + (th->doff << 2);
cb->seq = ntohl(th->seq);
cb->seq_end = cb->seq + skb->len - (dataoff - nhoff) - (th->doff << 2)
+ th->fin + th->syn;
cb->ack = ntohl(th->ack_seq);
cb->win = ntohs(th->window);
cb->syn = th->syn;
cb->fin = th->fin;
cb->rst = th->rst;
out:
return th;
}
static inline struct udphdr *colo_get_udphdr(u_int8_t pf, struct sk_buff *skb,
struct colo_udp_cb *cb)
{
struct udphdr _udph, *uh;
u_int8_t protonum;
unsigned int nhoff = skb_network_offset(skb);
struct nf_conntrack_l3proto *l3proto;
u32 dataoff;
u32 udplen;
l3proto = __nf_ct_l3proto_find(pf);
if(!l3proto->get_l4proto(skb, nhoff, &dataoff, &protonum)) {
pr_dbg("get iphdr failed\n");
return NULL;
}
if (protonum != IPPROTO_UDP) {
pr_dbg("is not udp packet\n");
return NULL;
}
uh = skb_header_pointer(skb, dataoff, sizeof(_udph), &_udph);
if (uh == NULL) {
pr_dbg("get udphdr failed\n");
return NULL;
}
udplen = ntohs(uh->len);
if (udplen < sizeof(_udph)) {
pr_dbg("udplen %u, udphdr len %lu\n", udplen, sizeof(_udph));
return NULL;
}
if (skb->len != (dataoff + udplen)) {
pr_dbg("skblen %u, network len %u, udp len %u\n", skb->len, dataoff, udplen);
return NULL;
}
if (!cb)
goto out;
cb->dataoff = dataoff + sizeof(_udph);
cb->size = udplen - sizeof(_udph);
out:
return uh;
}
static inline
void colo_seqadj_init(struct nf_conn *ct,
enum ip_conntrack_info ctinfo,
s32 off)
{
nf_ct_seqadj_init(ct, ctinfo, off);
}
#endif