IPv6 load-balancing
[l2tpns.git] / throttlectl.c
1 #include <string.h>
2 #include <linux/rtnetlink.h>
3 #include <netinet/ip6.h>
4
5 #include "dhcp6.h"
6 #include "l2tpns.h"
7 #include "plugin.h"
8 #include "control.h"
9
10 /* throttle control */
11
12 int plugin_api_version = PLUGIN_API_VERSION;
13 static struct pluginfuncs *f = 0;
14
15 char *plugin_control_help[] = {
16 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
17 " unthrottle USER|SID Stop throttling user",
18 0
19 };
20
21 int plugin_control(struct param_control *data)
22 {
23 sessionidt session;
24 sessiont *s = 0;
25 int flag;
26 char *end;
27 int rate_in = 0;
28 int rate_out = 0;
29
30 if (data->argc < 1)
31 return PLUGIN_RET_OK;
32
33 if (strcmp(data->argv[0], "throttle") &&
34 strcmp(data->argv[0], "unthrottle"))
35 return PLUGIN_RET_OK; // not for us
36
37 if (!data->iam_master)
38 return PLUGIN_RET_NOTMASTER;
39
40 flag = data->argv[0][0] == 't';
41
42 if (flag)
43 {
44 if (data->argc < 2 || data->argc > 6)
45 {
46 data->response = NSCTL_RES_ERR;
47 data->additional = "requires username or session id and optional rate(s)";
48 return PLUGIN_RET_STOP;
49 }
50 }
51 else
52 {
53 if (data->argc != 2)
54 {
55 data->response = NSCTL_RES_ERR;
56 data->additional = "requires username or session id";
57 return PLUGIN_RET_STOP;
58 }
59 }
60
61 if (!(session = strtol(data->argv[1], &end, 10)) || *end)
62 session = f->get_session_by_username(data->argv[1]);
63
64 if (session)
65 s = f->get_session_by_id(session);
66
67 if (!s || !s->ip)
68 {
69 data->response = NSCTL_RES_ERR;
70 data->additional = "session not found";
71 return PLUGIN_RET_STOP;
72 }
73
74 if (flag)
75 {
76 rate_in = rate_out = -1;
77 if (data->argc == 2)
78 {
79 unsigned long *rate = f->getconfig("throttle_speed", UNSIGNED_LONG);
80 rate_in = rate_out = *rate;
81 }
82 else if (data->argc == 3)
83 {
84 rate_in = rate_out = atoi(data->argv[2]);
85 }
86 else
87 {
88 int i;
89 for (i = 2; i < data->argc - 1; i += 2)
90 {
91 int len = strlen(data->argv[i]);
92 if (!strncmp(data->argv[i], "in", len))
93 {
94 rate_in = atoi(data->argv[i+1]);
95 }
96 else if (!strncmp(data->argv[i], "out", len))
97 {
98 rate_out = atoi(data->argv[i+1]);
99 }
100 else
101 {
102 data->response = NSCTL_RES_ERR;
103 data->additional = "invalid rate";
104 return PLUGIN_RET_STOP;
105 }
106 }
107 }
108
109 if (!rate_in || !rate_out)
110 {
111 data->response = NSCTL_RES_ERR;
112 data->additional = "invalid rate";
113 return PLUGIN_RET_STOP;
114 }
115 }
116
117 if (rate_in != -1 && rate_in == s->throttle_in &&
118 rate_out != -1 && rate_out == s->throttle_out)
119 {
120 data->response = NSCTL_RES_ERR;
121 data->additional = flag ? "already throttled" : "not throttled";
122 return PLUGIN_RET_STOP;
123 }
124
125 f->throttle(session, rate_in, rate_out);
126 f->session_changed(session);
127
128 data->response = NSCTL_RES_OK;
129 data->additional = 0;
130
131 return PLUGIN_RET_STOP;
132 }
133
134 int plugin_init(struct pluginfuncs *funcs)
135 {
136 return ((f = funcs)) ? 1 : 0;
137 }