-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcarp_sysfs.c
More file actions
345 lines (290 loc) · 9.64 KB
/
carp_sysfs.c
File metadata and controls
345 lines (290 loc) · 9.64 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
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
/*
* carp_sysfs.c -- sysfs interface to carp module
*
* Copyright (c) 2012 Damien Churchill <damoxc@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/in.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/inet.h>
#include <linux/rtnetlink.h>
#include <linux/etherdevice.h>
#include <linux/device.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <linux/nsproxy.h>
#include "carp.h"
#define to_dev(obj) container_of(obj, struct device, kobj)
#define to_carp(cd) ((struct carp *)(netdev_priv(to_net_dev(cd))))
static ssize_t carp_show_carps(struct class *cls,
struct class_attribute *attr,
char *buf)
{
//struct carp_net *cn =
// container_of(attr, struct carp_net, class_attr_carp);
ssize_t res = 0;
//struct carp *carp;
rtnl_lock();
rtnl_unlock();
return res;
}
static ssize_t carp_store_carps(struct class *cls,
struct class_attribute *attr,
const char *buffer, ssize_t count)
{
ssize_t res = 0;
return res;
}
static const void *carp_namespace(struct class *cls,
const struct class_attribute *attr)
{
const struct carp_net *cn =
container_of(attr, struct carp_net, class_attr_carp);
return cn->net;
}
static const struct class_attribute class_attr_carp = {
.attr = {
.name = "carp",
.mode = S_IWUSR | S_IRUGO,
},
.show = carp_show_carps,
.store = carp_store_carps,
.namespace = carp_namespace,
};
static ssize_t carp_show_adv_base(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct carp *carp = to_carp(dev);
return sprintf(buf, "%d\n", carp->advbase);
}
static ssize_t carp_store_adv_base(struct device *dev,
struct device_attribute *attr,
const char *buf, ssize_t count)
{
int new_value, ret = count;
struct carp *carp = to_carp(dev);
if (sscanf(buf, "%d", &new_value) != 1) {
pr_err("%s: no adv_base value specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (new_value < 0 || new_value > 255) {
pr_err("%s: invalid adv_base value, %d not in range 1-%d; rejected.\n",
carp->name, new_value, 255);
ret = -EINVAL;
goto out;
}
pr_info("%s: setting advertisement base to %d.\n", carp->name, new_value);
carp->advbase = new_value;
carp->md_timeout = carp_calculate_timeout(3, new_value, carp->advskew);
carp->adv_timeout = carp_calculate_timeout(1, new_value, carp->advskew);
out:
return ret;
}
static DEVICE_ATTR(advbase, S_IRUGO | S_IWUSR,
carp_show_adv_base, carp_store_adv_base);
static ssize_t carp_show_adv_skew(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct carp *carp = to_carp(dev);
return sprintf(buf, "%d\n", carp->advskew);
}
static ssize_t carp_store_adv_skew(struct device *dev,
struct device_attribute *attr,
const char *buf, ssize_t count)
{
int new_value, ret = count;
struct carp *carp = to_carp(dev);
if (sscanf(buf, "%d", &new_value) != 1) {
pr_err("%s: no adv_skew value specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (new_value < 0 || new_value > 255) {
pr_err("%s: invalid adv_skew value, %d not in range 1-%d; rejected.\n",
carp->name, new_value, 254);
ret = -EINVAL;
goto out;
}
pr_info("%s: setting advertisement skew to %d.\n", carp->name, new_value);
carp->advskew = new_value;
carp->md_timeout = carp_calculate_timeout(3, carp->advbase, new_value);
carp->adv_timeout = carp_calculate_timeout(1, carp->advbase, new_value);
out:
return ret;
}
static DEVICE_ATTR(advskew, S_IRUGO | S_IWUSR,
carp_show_adv_skew, carp_store_adv_skew);
static ssize_t carp_show_carpdev(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct carp *carp = to_carp(dev);
if (carp->odev != NULL)
return sprintf(buf, "%s\n", carp->odev->name);
else
return sprintf(buf, "(none)\n");
}
static ssize_t carp_store_carpdev(struct device *dev,
struct device_attribute *attr,
const char *buf, ssize_t count)
{
int ret = count;
char new_ifname[IFNAMSIZ];
struct carp *carp = to_carp(dev);
if (sscanf(buf, "%s", new_ifname) != 1) {
pr_err("%s: no carpdev value specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (carp_set_interface(carp, new_ifname) != 0) {
pr_err("%s: unable to set carpdev to %s.\n", carp->name, new_ifname);
ret = -EINVAL;
goto out;
}
out:
return ret;
}
static DEVICE_ATTR(carpdev, S_IRUGO | S_IWUSR,
carp_show_carpdev, carp_store_carpdev);
static ssize_t carp_show_state(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct carp *carp = to_carp(dev);
static const char *carp_states[] = { CARP_STATES };
return sprintf(buf, "%s\n", carp_states[carp->state]);
}
static ssize_t carp_store_state(struct device *dev,
struct device_attribute *attr,
const char *buf, ssize_t count)
{
int ret = count;
enum carp_state new_state;
char new_state_name[7];
struct carp *carp = to_carp(dev);
if (sscanf(buf, "%s", new_state_name) != 1) {
pr_err("%s: no state specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (strnicmp(new_state_name, "MASTER", 7) == 0) {
new_state = MASTER;
} else if (strnicmp(new_state_name, "BACKUP", 7) == 0) {
new_state = BACKUP;
} else if (strnicmp(new_state_name, "INIT", 4) == 0) {
new_state = INIT;
} else {
pr_err("%s: invalid state specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (new_state != INIT && new_state != carp->state) {
switch (new_state) {
case BACKUP:
carp_set_state(carp, new_state);
carp_set_run(carp, 0);
break;
case MASTER:
carp_master_down((unsigned long)carp);
break;
default:
break;
}
}
out:
return ret;
}
static DEVICE_ATTR(state, S_IRUGO | S_IWUSR,
carp_show_state, carp_store_state);
static ssize_t carp_show_vhid(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct carp *carp = to_carp(dev);
return sprintf(buf, "%d\n", carp->vhid);
}
static ssize_t carp_store_vhid(struct device *dev,
struct device_attribute *attr,
const char *buf, ssize_t count)
{
int new_value, ret = count;
struct carp *carp = to_carp(dev);
if (sscanf(buf, "%d", &new_value) != 1) {
pr_err("%s: no vhid value specified.\n", carp->name);
ret = -EINVAL;
goto out;
}
if (new_value < 0 || new_value > 255) {
pr_err("%s: invalid vhid value, %d not in range 1-%d; rejected.\n",
carp->name, new_value, 254);
ret = -EINVAL;
goto out;
}
pr_info("%s: setting vhid to %d.\n", carp->name, new_value);
carp->vhid = new_value;
out:
return ret;
}
static DEVICE_ATTR(vhid, S_IRUGO | S_IWUSR,
carp_show_vhid, carp_store_vhid);
static struct attribute *per_carp_attrs[] = {
&dev_attr_advbase.attr,
&dev_attr_advskew.attr,
&dev_attr_carpdev.attr,
&dev_attr_state.attr,
&dev_attr_vhid.attr,
NULL,
};
static struct attribute_group carp_group = {
.name = "carp",
.attrs = per_carp_attrs,
};
/*
* Initialise sysfs. This sets up the carpctl file in /sys/class/net.
*/
int carp_create_sysfs(struct carp_net *cn)
{
int ret;
cn->class_attr_carp = class_attr_carp;
sysfs_attr_init(&cn->class_attr_carp);
ret = netdev_class_create_file(&cn->class_attr_carp);
return ret;
}
void carp_destroy_sysfs(struct carp_net *cn)
{
netdev_class_remove_file(&cn->class_attr_carp);
}
/*
* Initialise sysfs for each carp. This sets up and registers
* the 'carpctl' directory for each individual carp under /sys/class/net.
*/
void carp_prepare_sysfs_group(struct carp *carp)
{
carp->dev->sysfs_groups[0] = &carp_group;
}