forked from gao-feng/colo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xt_SECCOLO.c
342 lines (281 loc) · 8.3 KB
/
xt_SECCOLO.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* SECONDARY side proxy module for COLO
*
* Copyright (c) 2014, 2015 Fujitsu Limited.
* Copyright (c) 2014, 2015 HUAWEI TECHNOLOGIES CO.,LTD.
* Copyright (c) 2014, 2015 Intel Corporation.
*
* Authors:
* 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.
*/
#include <linux/module.h>
#include <linux/netfilter/xt_COLO.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/tcp.h>
#include "xt_COLO.h"
#include "nf_conntrack_colo.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gao feng <[email protected]>");
MODULE_DESCRIPTION("Xtables: secondary proxy module for colo.");
static unsigned int
colo_secondary_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
enum ip_conntrack_info ctinfo;
struct nf_conn_colo *conn;
struct nf_conn *ct;
struct ip_ct_tcp *state;
struct tcphdr *th;
union nf_conn_colo_tcp *proto;
ct = nf_ct_get(skb, &ctinfo);
if ((ct == NULL) || ((conn = nfct_colo(ct)) == NULL))
return NF_ACCEPT;
if ((nf_ct_protonum(ct) != IPPROTO_TCP) ||
(conn->flags & COLO_CONN_BYPASS))
return NF_STOP;
proto = (union nf_conn_colo_tcp *) conn->proto;
th = colo_get_tcphdr(ops->pf, skb, NULL, NULL);
if (th == NULL)
return NF_DROP;
if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
state = &ct->proto.tcp;
if (unlikely(state->state == TCP_CONNTRACK_CLOSE &&
th->syn && !th->ack &&
CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)) {
clear_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
proto->s.sec_isn = 0;
proto->s.pri_isn = 0;
}
goto out;
}
if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
if (!th->syn || !th->ack)
goto out;
if (conn->flags & COLO_CONN_POSITIVE) {
proto->s.pri_isn = ntohl(th->ack_seq) - 1;
pr_dbg("[secondary] get master guest's first syn %u\n", proto->s.pri_isn);
/* check again, setup seqadj extension in lock,
* since checkpoint may occur before this packet */
spin_lock_bh(&conn->lock);
if (!(conn->flags & COLO_CONN_BYPASS) && proto->s.sec_isn &&
(proto->s.pri_isn != proto->s.sec_isn)) {
colo_seqadj_init(ct, IP_CT_NEW,
proto->s.pri_isn - proto->s.sec_isn);
}
spin_unlock_bh(&conn->lock);
} else {
/* syn/ack packet sent out by slaver guest */
proto->s.sec_isn = ntohl(th->seq);
pr_dbg("[secondary] get slaver guest's first syn %u\n", proto->s.sec_isn);
}
} else {
if (conn->flags & COLO_CONN_POSITIVE) {
if (!th->syn || th->ack)
goto out;
proto->s.sec_isn = ntohl(th->seq);
pr_dbg("[secondary] get slaver guest's first syn %u\n", proto->s.sec_isn);
} else {
if (proto->s.pri_isn || th->syn || th->fin || th->rst)
goto out;
proto->s.pri_isn = ntohl(th->ack_seq) - 1;
pr_dbg("[secondary] get master guest's first syn %u\n", proto->s.pri_isn);
/* check again, setup seqadj extension in lock */
spin_lock_bh(&conn->lock);
if (!(conn->flags & COLO_CONN_BYPASS) && proto->s.sec_isn &&
(proto->s.pri_isn != proto->s.sec_isn)) {
colo_seqadj_init(ct, IP_CT_IS_REPLY,
proto->s.pri_isn - proto->s.sec_isn);
}
spin_unlock_bh(&conn->lock);
}
}
out:
return NF_STOP;
}
static struct nf_hook_ops colo_secondary_ops[] __read_mostly = {
{
.hook = colo_secondary_hook,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV4,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_MANGLE + 1,
},
{
.hook = colo_secondary_hook,
.owner = THIS_MODULE,
.pf = NFPROTO_IPV6,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_MANGLE + 1,
},
};
static void colo_tcp_chk_finish(struct nf_conn_colo *conn)
{
struct nf_conn *ct = (struct nf_conn *)conn->nfct;
pr_dbg("call colo_tcp_chk_finish to cleanup conn %p\n", ct);
clear_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
}
static void colo_sec_do_failover(struct colo_node *node)
{
node->u.s.failover = true;
}
static void colo_sec_do_checkpoint(struct colo_node *node)
{
struct nf_conn_colo *conn;
pr_dbg("master starts checkpoint, slaver do checkpoint\n");
spin_lock_bh(&node->lock);
next:
if (!list_empty(&node->list))
conn = list_first_entry(&node->list, struct nf_conn_colo, conn_list);
else
goto out;
spin_lock_bh(&conn->lock);
conn->flags |= COLO_CONN_BYPASS;
list_move_tail(&conn->conn_list, &node->wait_list);
if (nf_ct_protonum((struct nf_conn *)conn->nfct) == IPPROTO_TCP) {
colo_tcp_chk_finish(conn);
}
spin_unlock_bh(&conn->lock);
goto next;
out:
spin_unlock_bh(&node->lock);
}
static int colo_secondary_receive(void *node,
struct sk_buff *skb,
struct nlmsghdr *nlh)
{
switch (nlh->nlmsg_type) {
/* Start failover */
case COLO_FAILOVER:
colo_sec_do_failover(node);
break;
/* guest stopped, do checkpoint */
case COLO_CHECKPOINT:
colo_sec_do_checkpoint(node);
return 0;
default:
break;
}
return 0;
}
static void colo_secondary_destroy(void *_node)
{
struct colo_node *node = (struct colo_node *) _node;
struct nf_conn_colo *colo_conn, *next;
node->func = NULL;
node->notify = NULL;
spin_lock_bh(&node->lock);
list_for_each_entry_safe(colo_conn, next, &node->list, conn_list) {
list_del_init(&colo_conn->conn_list);
colo_conn->flags |= COLO_CONN_BYPASS;
}
spin_unlock_bh(&node->lock);
colo_node_unregister(node);
module_put(THIS_MODULE);
}
static int colo_secondary_tg_check(const struct xt_tgchk_param *par)
{
struct xt_colo_secondary_info *info = par->targinfo;
struct colo_secondary *colo;
struct colo_node *node;
if (info->index >= COLO_NODES_NUM)
return -EINVAL;
node = colo_node_find_get(info->index);
if (node == NULL) {
pr_dbg("cannot find colo node whose index is %d\n", info->index);
return -EINVAL;
}
colo = &node->u.s;
if (node->func)
goto out;
__module_get(THIS_MODULE);
node->func = colo_secondary_receive;
node->notify = colo_secondary_destroy;
colo->failover = false;
out:
info->colo = colo;
return 0;
}
static void colo_secondary_tg_destroy(const struct xt_tgdtor_param *par)
{
struct xt_colo_secondary_info *info = par->targinfo;
struct colo_node *node;
node = container_of(info->colo, struct colo_node, u.s);
colo_node_unregister(node);
}
static unsigned int
colo_secondary_tg(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct xt_colo_secondary_info *info = par->targinfo;
struct colo_secondary *colo = info->colo;
struct nf_conn_colo *conn = NULL;
enum ip_conntrack_info ctinfo;
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
struct colo_node *node;
if (ct == NULL || colo->failover) {
/* after failover, before migration */
return XT_CONTINUE;
}
node = container_of(colo, struct colo_node, u.s);
conn = nf_ct_colo_get(skb, node, COLO_CONN_SECONDARY);
if (conn == NULL || (conn->flags & COLO_CONN_BYPASS))
return XT_CONTINUE;
/* Add ct into colo_secondary list */
spin_lock_bh(&node->lock);
if (list_empty(&conn->conn_list)) {
list_add_tail(&conn->conn_list, &node->list);
if ((nf_ct_protonum(ct) == IPPROTO_TCP) &&
(CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)) {
/* Guest start connection positively */
conn->flags |= COLO_CONN_POSITIVE;
}
}
spin_unlock_bh(&node->lock);
return XT_CONTINUE;
}
static struct xt_target colo_secondary_tg_regs[] __read_mostly = {
{
.name = "SECCOLO",
.family = NFPROTO_UNSPEC,
.target = colo_secondary_tg,
.checkentry = colo_secondary_tg_check,
.destroy = colo_secondary_tg_destroy,
.targetsize = sizeof(struct xt_colo_secondary_info),
.table = "mangle",
.hooks = (1 << NF_INET_PRE_ROUTING),
.me = THIS_MODULE,
},
};
static int colo_secondary_init(void)
{
int err;
pr_dbg("register_hooks\n");
err = nf_register_hooks(colo_secondary_ops,
ARRAY_SIZE(colo_secondary_ops));
if (err < 0)
goto err;
pr_dbg("register targets\n");
err = xt_register_targets(colo_secondary_tg_regs,
ARRAY_SIZE(colo_secondary_tg_regs));
if (err < 0)
goto err1;
return 0;
err1:
nf_unregister_hooks(colo_secondary_ops,
ARRAY_SIZE(colo_secondary_ops));
err:
return err;
}
static void colo_secondary_exit(void)
{
xt_unregister_targets(colo_secondary_tg_regs,
ARRAY_SIZE(colo_secondary_tg_regs));
nf_unregister_hooks(colo_secondary_ops,
ARRAY_SIZE(colo_secondary_ops));
}
module_init(colo_secondary_init);
module_exit(colo_secondary_exit);