2 #include <linux/rtnetlink.h>
3 #include <netinet/ip6.h>
10 /* throttle control */
12 int plugin_api_version
= PLUGIN_API_VERSION
;
13 static struct pluginfuncs
*f
= 0;
15 char *plugin_control_help
[] = {
16 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
17 " unthrottle USER|SID Stop throttling user",
21 int plugin_control(struct param_control
*data
)
33 if (strcmp(data
->argv
[0], "throttle") &&
34 strcmp(data
->argv
[0], "unthrottle"))
35 return PLUGIN_RET_OK
; // not for us
37 if (!data
->iam_master
)
38 return PLUGIN_RET_NOTMASTER
;
40 flag
= data
->argv
[0][0] == 't';
44 if (data
->argc
< 2 || data
->argc
> 6)
46 data
->response
= NSCTL_RES_ERR
;
47 data
->additional
= "requires username or session id and optional rate(s)";
48 return PLUGIN_RET_STOP
;
55 data
->response
= NSCTL_RES_ERR
;
56 data
->additional
= "requires username or session id";
57 return PLUGIN_RET_STOP
;
61 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
62 session
= f
->get_session_by_username(data
->argv
[1]);
65 s
= f
->get_session_by_id(session
);
69 data
->response
= NSCTL_RES_ERR
;
70 data
->additional
= "session not found";
71 return PLUGIN_RET_STOP
;
76 rate_in
= rate_out
= -1;
79 unsigned long *rate
= f
->getconfig("throttle_speed", UNSIGNED_LONG
);
80 rate_in
= rate_out
= *rate
;
82 else if (data
->argc
== 3)
84 rate_in
= rate_out
= atoi(data
->argv
[2]);
89 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
91 int len
= strlen(data
->argv
[i
]);
92 if (!strncmp(data
->argv
[i
], "in", len
))
94 rate_in
= atoi(data
->argv
[i
+1]);
96 else if (!strncmp(data
->argv
[i
], "out", len
))
98 rate_out
= atoi(data
->argv
[i
+1]);
102 data
->response
= NSCTL_RES_ERR
;
103 data
->additional
= "invalid rate";
104 return PLUGIN_RET_STOP
;
109 if (!rate_in
|| !rate_out
)
111 data
->response
= NSCTL_RES_ERR
;
112 data
->additional
= "invalid rate";
113 return PLUGIN_RET_STOP
;
117 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
118 rate_out
!= -1 && rate_out
== s
->throttle_out
)
120 data
->response
= NSCTL_RES_ERR
;
121 data
->additional
= flag
? "already throttled" : "not throttled";
122 return PLUGIN_RET_STOP
;
125 f
->throttle(session
, rate_in
, rate_out
);
126 f
->session_changed(session
);
128 data
->response
= NSCTL_RES_OK
;
129 data
->additional
= 0;
131 return PLUGIN_RET_STOP
;
134 int plugin_init(struct pluginfuncs
*funcs
)
136 return ((f
= funcs
)) ? 1 : 0;