forked from gao-feng/colo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnfnetlink_colo.c
373 lines (316 loc) · 9.17 KB
/
nfnetlink_colo.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* This is a module which is used for COLO FT related communication between userspace and
* modules in kernel via nfetlink.
*
* (C) 2015 by Zhang Hailiang <[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/init.h>
#include <linux/notifier.h>
#include <net/netlink.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/kernel.h>
#include <net/sock.h>
#include "nfnetlink_colo.h"
#include "nf_conntrack_colo.h"
static LIST_HEAD(master_nodes_head); /* Be Array with slave_nodes_head ? */
static LIST_HEAD(slave_nodes_head);
static struct colo_node *colo_find_node(pid_t pid)
{
struct colo_node *node;
list_for_each_entry_rcu (node, &master_nodes_head, list) {
if (node->vm_pid == pid) {
return rcu_dereference(node);
}
}
list_for_each_entry_rcu (node, &slave_nodes_head, list) {
if (node->vm_pid == pid) {
return rcu_dereference(node);
}
}
return NULL;
}
/* Use toghter with colo_node_put*/
struct colo_node *colo_node_get(u32 vm_pid)
{
struct colo_node *node = NULL;
rcu_read_lock();
node = colo_find_node(vm_pid);
if (node)
kref_get (&node->refcnt);
rcu_read_unlock();
return node;
}
EXPORT_SYMBOL_GPL(colo_node_get);
/* We are in workqueue, so it is safe to do work no matter leading sleep or not */
static void colo_node_destroy_fn(struct work_struct *work)
{
struct colo_node *node = container_of(work, struct colo_node, destroy_work);
pr_dbg("%s: destroy node:%d and kcolo:%d thread\n", __func__, node->vm_pid, node->vm_pid);
if (node->destroy_notify_cb)
node->destroy_notify_cb(node);
kfree(node);
}
/* call_rcu callback function, it should never do work that leading to sleep. */
static void
colo_node_destroy_rcu(struct rcu_head *head)
{
struct colo_node *node = container_of(head, struct colo_node, rcu);
INIT_WORK(&node->destroy_work, colo_node_destroy_fn);
schedule_work(&node->destroy_work);
}
/* In atomic_notifier callback, never do work that leading to sleep or schedule. */
static void colo_node_release(struct kref *kref)
{
struct colo_node *node = container_of(kref, struct colo_node, refcnt);
pr_dbg("%s, destroy node:%d\n", __func__, node->vm_pid);
list_del_rcu (&node->list);
call_rcu(&node->rcu, colo_node_destroy_rcu);
}
void colo_node_put(struct colo_node *node)
{
if (node)
kref_put(&node->refcnt, colo_node_release);
}
EXPORT_SYMBOL_GPL(colo_node_put);
static const struct nla_policy nfnl_colo_policy[NFNL_COLO_MAX + 1] = {
[NFNL_COLO_MODE] = { .type = NLA_U8 },
};
static int colo_init_proxy(struct sock *nl, struct sk_buff *skb,
const struct nlmsghdr *nlh,
const struct nlattr * const cda[])
{
struct colo_node *node;
enum colo_mode mode;
if (!cda[NFNL_COLO_MODE]) {
return -EINVAL;
}
mode = nla_get_u8(cda[NFNL_COLO_MODE]);
if (mode >= COLO_MODE_MAX) {
printk(KERN_ERR "colo mode only can be 1 or 2\n");
return -EINVAL;
}
rcu_read_lock();
node = colo_find_node(nlh->nlmsg_pid);
if (node) {
rcu_read_unlock();
pr_dbg("node %d exist\n", nlh->nlmsg_pid);
return -EEXIST;
}
rcu_read_unlock();
node = kzalloc(sizeof(*node), GFP_KERNEL);
if (node == NULL)
return -ENOMEM;
node->vm_pid = nlh->nlmsg_pid;
node->mode = mode;
node->net = sock_net(nl);
kref_init(&node->refcnt);
INIT_LIST_HEAD(&node->conn_list);
INIT_LIST_HEAD(&node->wait_list);
spin_lock_init(&node->lock);
node->destroy_notify_cb = NULL;
node->do_checkpoint_cb = NULL;
pr_dbg("%s: node %d init\n", __func__, nlh->nlmsg_pid);
if (mode == COLO_PRIMARY_MODE)
list_add_rcu(&node->list, &master_nodes_head);
else
list_add_rcu(&node->list, &slave_nodes_head);
return 0;
}
/*
* guest has stopped now. no network output now.
*/
static int colo_do_checkpoint(struct sock *nl, struct sk_buff *skb,
const struct nlmsghdr *nlh,
const struct nlattr * const cda[])
{
struct colo_node *node;
enum colo_mode mode;
if(!cda[NFNL_COLO_MODE]) {
return -EINVAL;
}
mode = nla_get_u8(cda[NFNL_COLO_MODE]);
if (mode >= COLO_MODE_MAX) {
printk(KERN_ERR "mode only can be 1 or 2\n");
return -EINVAL;
}
node = colo_node_get(nlh->nlmsg_pid);
if (!node) {
printk(KERN_ERR "Do checkpoint: node %d not exist\n", nlh->nlmsg_pid);
return -EEXIST;
}
BUG_ON(mode != node->mode);
if (node->do_checkpoint_cb)
node->do_checkpoint_cb(node);
colo_node_put (node);
return 0;
}
static int colo_do_failover(struct sock *nl, struct sk_buff *skb,
const struct nlmsghdr *nlh,
const struct nlattr * const cda[])
{
struct colo_node *node;
enum colo_mode mode;
if (!cda[NFNL_COLO_MODE]) {
return -EINVAL;
}
mode = nla_get_u8 (cda[NFNL_COLO_MODE]);
if (mode >= COLO_MODE_MAX) {
return -EINVAL;
}
node = colo_node_get(nlh->nlmsg_pid);
if (!node) {
return -EEXIST;
}
if (mode == COLO_SECONDARY_MODE) {
node->u.s.failover = true;
} else {
colo_node_put(node);
/* For PVM, if failover happens, should do some clean work? */
return -EINVAL;
}
colo_node_put(node);
return 0;
}
static int colo_reset_proxy(struct sock *nl, struct sk_buff *skb,
const struct nlmsghdr *nlh,
const struct nlattr * const cda[])
{
struct colo_node *node;
enum colo_mode mode;
if (!cda[NFNL_COLO_MODE]) {
return -EINVAL;
}
mode = nla_get_u8 (cda[NFNL_COLO_MODE]);
if (mode >= COLO_MODE_MAX) {
return -EINVAL;
}
node = colo_node_get (nlh->nlmsg_pid);
if (!node) {
printk(KERN_ERR "Node %d not exist\n", nlh->nlmsg_pid);
return -EEXIST;
}
INIT_LIST_HEAD(&node->conn_list);
node->destroy_notify_cb = NULL;
colo_node_put(node);
return 0;
}
static const struct nfnl_callback nfnl_colo_cb[NFCOLO_MSG_MAX] = {
[NFCOLO_KERNEL_NOTIFY] = { .call = NULL,
.policy = NULL,
.attr_count = 0, },
[NFCOLO_DO_CHECKPOINT] = { .call = colo_do_checkpoint,
.policy = nfnl_colo_policy,
.attr_count = NFNL_COLO_MAX, },
[NFCOLO_DO_FAILOVER] = { .call = colo_do_failover,
.policy = nfnl_colo_policy,
.attr_count = NFNL_COLO_MAX, },
[NFCOLO_PROXY_INIT] = { .call = colo_init_proxy,
.policy = nfnl_colo_policy,
.attr_count = NFNL_COLO_MAX, },
[NFCOLO_PROXY_RESET] = { .call = colo_reset_proxy,
.policy = nfnl_colo_policy,
.attr_count = NFNL_COLO_MAX,},
};
int colo_send_checkpoint_req(struct colo_primary *colo)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
struct nfcolo_packet_compare nfcm;
int portid, ret;
struct colo_node *node = container_of(colo, struct colo_node, u.p);
struct nfgenmsg *nfmsg;
colo->checkpoint = true;
portid = node->vm_pid;
skb = nfnetlink_alloc_skb(&init_net,
nlmsg_total_size(sizeof(struct nfgenmsg)) +
nla_total_size(sizeof(int32_t)), portid, GFP_ATOMIC);
if (skb == NULL)
return -ENOMEM;
nlh = nlmsg_put(skb, portid, 0, NFNL_SUBSYS_COLO << 8 | NFCOLO_KERNEL_NOTIFY,
sizeof(struct nfgenmsg), 0);
if (!nlh)
goto nla_put_failure;
nfmsg = nlmsg_data(nlh);
nfmsg->nfgen_family = AF_UNSPEC;
nfmsg->version = NFNETLINK_V0;
nfmsg->res_id = 0;
nfcm.different = 1;
if (nla_put_s32(skb, NFNL_COLO_COMPARE_RESULT,
htonl(nfcm.different))) {
goto nla_put_failure;
}
nlh->nlmsg_len = skb->len; /* Don't forget to update this value */
ret = nfnetlink_unicast(skb, node->net, portid, MSG_DONTWAIT);
return ret;
nla_put_failure:
printk("Error: colo_send_checkpoint_req failed\n");
skb_tx_error(skb);
kfree_skb(skb);
return -1;
}
EXPORT_SYMBOL_GPL(colo_send_checkpoint_req);
static const struct nfnetlink_subsystem nfulnl_subsys = {
.name = "colo",
.subsys_id = NFNL_SUBSYS_COLO,
.cb_count = NFCOLO_MSG_MAX,
.cb = nfnl_colo_cb,
};
/*
* It is not allowed to call any functions that could lead to sleep in
* this notifier callback.
*/
static int colonl_close_event(struct notifier_block *nb,
unsigned long event, void *ptr)
{
struct netlink_notify *n = ptr;
struct colo_node *node;
if (event != NETLINK_URELEASE || !n->portid ||
n->protocol != NETLINK_NETFILTER)
return NOTIFY_DONE;
rcu_read_lock();
node = colo_find_node(n->portid); /* Should this be changed to colo_node_get ? */
rcu_read_unlock();
if (node == NULL) {
return NOTIFY_DONE;
}
colo_node_destroy(node);
return NOTIFY_DONE;
}
static struct notifier_block colonl_notifier = {
.notifier_call = colonl_close_event,
};
static int __init nfnetlink_colo_init(void)
{
int status = -ENOMEM;
netlink_register_notifier(&colonl_notifier);
status = nfnetlink_subsys_register(&nfulnl_subsys);
if (status < 0) {
pr_err("log: failed to create netlink socket\n");
goto cleanup_netlink_notifier;
}
return status;
cleanup_netlink_notifier:
netlink_unregister_notifier(&colonl_notifier);
return status;
}
static void __exit nfnetlink_colo_fini(void)
{
nfnetlink_subsys_unregister(&nfulnl_subsys);
netlink_unregister_notifier(&colonl_notifier);
rcu_barrier(); /* Wait for completion of call_rcu()'s */
}
MODULE_DESCRIPTION("netfilter userspace colo communication");
MODULE_AUTHOR("Zhanghailiang <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_COLO);
module_init(nfnetlink_colo_init);
module_exit(nfnetlink_colo_fini);
/*
* TODO: Add some statistics info to userspace by proc,
* like what nfnetlink_log and nfnetlink_queue do.
*/