| 1 |
ratelimit Module |
| 2 |
|
| 3 |
Ovidiu Sas |
| 4 |
|
| 5 |
Bogdan Vasile Harjoc |
| 6 |
|
| 7 |
Hendrik Scholz |
| 8 |
|
| 9 |
Edited by |
| 10 |
|
| 11 |
Ovidiu Sas |
| 12 |
|
| 13 |
Bogdan Vasile Harjoc |
| 14 |
|
| 15 |
Hendrik Scholz |
| 16 |
|
| 17 |
Copyright © 2006 Freenet Cityline GmbH |
| 18 |
|
| 19 |
Copyright © 2008-2010 VoIP Embedded Inc. |
| 20 |
__________________________________________________________________ |
| 21 |
|
| 22 |
Table of Contents |
| 23 |
|
| 24 |
1. Admin Guide |
| 25 |
|
| 26 |
1. Overview |
| 27 |
2. Use Cases |
| 28 |
3. Static Rate Limiting Algorithms |
| 29 |
|
| 30 |
3.1. Tail Drop Algorithm (TAILDROP) |
| 31 |
3.2. Random Early Detection Algorithm (RED) |
| 32 |
3.3. Network Algorithm (NETWORK) |
| 33 |
3.4. Dynamic Rate Limiting Algorithms |
| 34 |
3.5. Feedback Algorithm (FEEDBACK) |
| 35 |
|
| 36 |
4. Dependencies |
| 37 |
|
| 38 |
4.1. Kamailio Modules |
| 39 |
4.2. External Libraries or Applications |
| 40 |
|
| 41 |
5. Parameters |
| 42 |
|
| 43 |
5.1. timer_interval (integer) |
| 44 |
5.2. queue (integer:string) |
| 45 |
5.3. pipe (integer:string:integer) |
| 46 |
|
| 47 |
6. Functions |
| 48 |
|
| 49 |
6.1. rl_check([pvar]) |
| 50 |
6.2. rl_check_pipe(pipe_no) |
| 51 |
|
| 52 |
7. RPC Commands |
| 53 |
|
| 54 |
7.1. rl.stats |
| 55 |
7.2. rl.set_pipe |
| 56 |
7.3. rl.get_pipes |
| 57 |
7.4. rl.set_queue |
| 58 |
7.5. rl.get_queues |
| 59 |
7.6. rl.set_pid |
| 60 |
7.7. rl.get_pid |
| 61 |
7.8. rl.push_load |
| 62 |
7.9. rl.set_dbg |
| 63 |
|
| 64 |
8. Known limitations |
| 65 |
|
| 66 |
List of Examples |
| 67 |
|
| 68 |
1.1. Set timer_interval parameter |
| 69 |
1.2. Set queue parameter |
| 70 |
1.3. Set pipe parameter |
| 71 |
1.4. rl_check usage |
| 72 |
1.5. rl_check_pipe usage |
| 73 |
|
| 74 |
Chapter 1. Admin Guide |
| 75 |
|
| 76 |
Table of Contents |
| 77 |
|
| 78 |
1. Overview |
| 79 |
2. Use Cases |
| 80 |
3. Static Rate Limiting Algorithms |
| 81 |
|
| 82 |
3.1. Tail Drop Algorithm (TAILDROP) |
| 83 |
3.2. Random Early Detection Algorithm (RED) |
| 84 |
3.3. Network Algorithm (NETWORK) |
| 85 |
3.4. Dynamic Rate Limiting Algorithms |
| 86 |
3.5. Feedback Algorithm (FEEDBACK) |
| 87 |
|
| 88 |
4. Dependencies |
| 89 |
|
| 90 |
4.1. Kamailio Modules |
| 91 |
4.2. External Libraries or Applications |
| 92 |
|
| 93 |
5. Parameters |
| 94 |
|
| 95 |
5.1. timer_interval (integer) |
| 96 |
5.2. queue (integer:string) |
| 97 |
5.3. pipe (integer:string:integer) |
| 98 |
|
| 99 |
6. Functions |
| 100 |
|
| 101 |
6.1. rl_check([pvar]) |
| 102 |
6.2. rl_check_pipe(pipe_no) |
| 103 |
|
| 104 |
7. RPC Commands |
| 105 |
|
| 106 |
7.1. rl.stats |
| 107 |
7.2. rl.set_pipe |
| 108 |
7.3. rl.get_pipes |
| 109 |
7.4. rl.set_queue |
| 110 |
7.5. rl.get_queues |
| 111 |
7.6. rl.set_pid |
| 112 |
7.7. rl.get_pid |
| 113 |
7.8. rl.push_load |
| 114 |
7.9. rl.set_dbg |
| 115 |
|
| 116 |
8. Known limitations |
| 117 |
|
| 118 |
1. Overview |
| 119 |
|
| 120 |
This module implements rate limiting for SIP requests. In contrast to |
| 121 |
the PIKE module this limits the flow based on a per SIP request type |
| 122 |
basis and not per source IP. The RPC interface can be used to change |
| 123 |
tunables while running Kamailio. |
| 124 |
|
| 125 |
The module implements the pipe/queue policy from BSD's ipfw manual, |
| 126 |
with some simplifications. In principle, each specified method is |
| 127 |
associated with its own queue and a number of queues are connected to a |
| 128 |
certain pipe (see the queue and pipe params). |
| 129 |
|
| 130 |
Please also take a look at the “pipelimit” module, that implements the |
| 131 |
pipe policy with database support. Note that it doesn't implement the |
| 132 |
queues that exist in this module. |
| 133 |
|
| 134 |
2. Use Cases |
| 135 |
|
| 136 |
Limiting the rate messages are processed on a system directly |
| 137 |
influences the load. The ratelimit module can be used to protect a |
| 138 |
single host or to protect a Kamailio cluster when run on the |
| 139 |
dispatching box in front. |
| 140 |
|
| 141 |
A sample configuration snippet might look like this: |
| 142 |
... |
| 143 |
if (is_method("INVITE|REGISTER|SUBSCRIBE") { |
| 144 |
if (!rl_check()) { |
| 145 |
append_to_reply("Retry-After: 5\r\n"); |
| 146 |
sl_send_reply("503","Limiting"); |
| 147 |
exit; |
| 148 |
}; |
| 149 |
}; |
| 150 |
... |
| 151 |
|
| 152 |
Upon every incoming request listed above rl_check is invoked. It |
| 153 |
returns an OK code if the current per request load is below the |
| 154 |
configured threshold. If the load is exceeded the function returns an |
| 155 |
error and an administrator can discard requests with a stateless |
| 156 |
response. |
| 157 |
|
| 158 |
3. Static Rate Limiting Algorithms |
| 159 |
|
| 160 |
3.1. Tail Drop Algorithm (TAILDROP) |
| 161 |
3.2. Random Early Detection Algorithm (RED) |
| 162 |
3.3. Network Algorithm (NETWORK) |
| 163 |
3.4. Dynamic Rate Limiting Algorithms |
| 164 |
3.5. Feedback Algorithm (FEEDBACK) |
| 165 |
|
| 166 |
The ratelimit module supports two different statc algorithms to be used |
| 167 |
by rl_check to determine whether a message should be blocked or not. |
| 168 |
|
| 169 |
3.1. Tail Drop Algorithm (TAILDROP) |
| 170 |
|
| 171 |
This is a trivial algorithm that imposes some risks when used in |
| 172 |
conjunction with long timer intervals. At the start of each interval an |
| 173 |
internal counter is reset and incremented for each incoming message. |
| 174 |
Once the counter hits the configured limit rl_check returns an error. |
| 175 |
|
| 176 |
The downside of this algorithm is that it can lead to SIP client |
| 177 |
synchronization. During a relatively long interval only the first |
| 178 |
requests (i.e. REGISTERs) would make it through. Following messages |
| 179 |
(i.e. RE-REGISTERs) will all hit the SIP proxy at the same time when a |
| 180 |
common Expire timer expired. Other requests will be retransmitted after |
| 181 |
a given time, the same on all devices with the same firmware/by the |
| 182 |
same vendor. |
| 183 |
|
| 184 |
3.2. Random Early Detection Algorithm (RED) |
| 185 |
|
| 186 |
The Random Early Detection Algorithm tries to circumvent the |
| 187 |
synchronization problem imposed by the tail drop algorithm by measuring |
| 188 |
the average load and adapting the drop rate dynamically. When running |
| 189 |
with the RED algorithm (enabled by default) Kamailio will return errors |
| 190 |
to the Kamailio routing engine every n'th packet trying to evenly |
| 191 |
spread the measured load of the last timer interval onto the current |
| 192 |
interval. As a negative side effect Kamailio might drop messages |
| 193 |
although the limit might not be reached within the interval. Decrease |
| 194 |
the timer interval if you encounter this. |
| 195 |
|
| 196 |
3.3. Network Algorithm (NETWORK) |
| 197 |
|
| 198 |
This algorithm relies on information provided by network interfaces. |
| 199 |
The total amount of bytes waiting to be consumed on all the network |
| 200 |
interfaces is retrieved once every timer_interval seconds. If the |
| 201 |
returned amount exceeds the limit specified in the modparam, rl_check |
| 202 |
returns an error. |
| 203 |
|
| 204 |
3.4. Dynamic Rate Limiting Algorithms |
| 205 |
|
| 206 |
When running Kamailio on different machines, one has to adjust the drop |
| 207 |
rates for the static algorithms to maintain a sub 100% load average or |
| 208 |
packets will start getting dropped in the network stack. While this is |
| 209 |
not in itself difficult, it isn't neither accurate nor trivial: another |
| 210 |
server taking a notable fraction of the CPU time will require re-tuning |
| 211 |
the parameters. |
| 212 |
|
| 213 |
While tuning the drop rates from the outside based on a certain factor |
| 214 |
is possible, having the algorithm run inside ratelimit permits tuning |
| 215 |
the rates based on internal server parameters and is somewhat more |
| 216 |
flexible (or it will be when support for external load factors - as |
| 217 |
opposed to cpu load - is added). |
| 218 |
|
| 219 |
3.5. Feedback Algorithm (FEEDBACK) |
| 220 |
|
| 221 |
Using the PID Controller model (see Wikipedia page), the drop rate is |
| 222 |
adjusted dynamically based on the load factor so that the load factor |
| 223 |
always drifts towards the specified limit (or setpoint, in PID terms). |
| 224 |
|
| 225 |
As reading the CPU load average is relatively expensive (opening |
| 226 |
/proc/stat, parsing it, etc), this only happens once every |
| 227 |
timer_interval seconds and consequently the FEEDBACK value is only at |
| 228 |
these intervals recomputed. This in turn makes it difficult for the |
| 229 |
drop rate to adjust quickly. Worst case scenarios are request rates |
| 230 |
going up/down instantly by thousands - it takes up to 20 seconds for |
| 231 |
the controller to adapt to the new request rate. |
| 232 |
|
| 233 |
Generally though, as real life request rates drift by less, adapting |
| 234 |
should happen much faster. |
| 235 |
|
| 236 |
4. Dependencies |
| 237 |
|
| 238 |
4.1. Kamailio Modules |
| 239 |
4.2. External Libraries or Applications |
| 240 |
|
| 241 |
4.1. Kamailio Modules |
| 242 |
|
| 243 |
The following modules must be loaded before this module: |
| 244 |
* SL: Stateless request handling. |
| 245 |
|
| 246 |
4.2. External Libraries or Applications |
| 247 |
|
| 248 |
The following libraries or applications must be installed before |
| 249 |
running Kamailio with this module loaded: |
| 250 |
* None. |
| 251 |
|
| 252 |
5. Parameters |
| 253 |
|
| 254 |
5.1. timer_interval (integer) |
| 255 |
5.2. queue (integer:string) |
| 256 |
5.3. pipe (integer:string:integer) |
| 257 |
|
| 258 |
5.1. timer_interval (integer) |
| 259 |
|
| 260 |
The initial length of a timer interval in seconds. All amounts of |
| 261 |
messages have to be divided by this timer to get a messages per second |
| 262 |
value. |
| 263 |
|
| 264 |
IMPORTANT: A too small value may lead to performance penalties due to |
| 265 |
timer process overloading. |
| 266 |
|
| 267 |
Default value is 10. |
| 268 |
|
| 269 |
Example 1.1. Set timer_interval parameter |
| 270 |
... |
| 271 |
modparam("ratelimit", "timer_interval", 5) |
| 272 |
... |
| 273 |
|
| 274 |
5.2. queue (integer:string) |
| 275 |
|
| 276 |
The format of the queue parameter is "pipe_no:method". For each defined |
| 277 |
method, the algorithm defined by pipe number "pipe_no" will be used. |
| 278 |
|
| 279 |
To specify a queue that accepts all methods, use “*” instead of METHOD. |
| 280 |
As queues are matched against request methods, you will usually want to |
| 281 |
have this as the last queue added or other queues with specific methods |
| 282 |
will never match. At this time, glob or regexp patterns are not |
| 283 |
supported. |
| 284 |
|
| 285 |
Example 1.2. Set queue parameter |
| 286 |
... |
| 287 |
# assign pipe no 0 to method REGISTER |
| 288 |
# assign pipe no 3 to method INVITE |
| 289 |
# assign pipe no 2 to all other methods |
| 290 |
modparam("ratelimit", "queue", "0:REGISTER") |
| 291 |
modparam("ratelimit", "queue", "3:INVITE") |
| 292 |
modparam("ratelimit", "queue", "2:*") |
| 293 |
... |
| 294 |
|
| 295 |
5.3. pipe (integer:string:integer) |
| 296 |
|
| 297 |
The format of the pipe param is "pipe_no:algorithm:limit". For each |
| 298 |
defined pipe, the given algorithm with the given limit will be used. |
| 299 |
|
| 300 |
A pipe is characterised by its algorithm and limit (bandwidth, in ipfw |
| 301 |
terms). When specifying a limit, the unit depends on the algorithm used |
| 302 |
and doesn't need to be specified also (eg, for TAILDROP or RED, limit |
| 303 |
means packets/sec, whereas with the FEEDBACK algorithm, it means [CPU] |
| 304 |
load factor). |
| 305 |
|
| 306 |
Example 1.3. Set pipe parameter |
| 307 |
... |
| 308 |
# define pipe 0 with a limit of 200 pkts/sec using TAILDROP algorithm |
| 309 |
# define pipe 1 with a limit of 100 pkts/sec using RED algorithm |
| 310 |
# define pipe 2 with a limit of 50 pkts/sec using TAILDROP algorithm |
| 311 |
# define pipe 3 with a limit of load factor 80 using FEEDBACK algorithm |
| 312 |
# define pipe 4 with a limit of 10000 pending bytes in the rx_queue |
| 313 |
# using NETWORK algorithm |
| 314 |
modparam("ratelimit", "pipe", "0:TAILDROP:200") |
| 315 |
modparam("ratelimit", "pipe", "1:RED:100") |
| 316 |
modparam("ratelimit", "pipe", "2:TAILDROP:50") |
| 317 |
modparam("ratelimit", "pipe", "3:FEEDBACK:80") |
| 318 |
modparam("ratelimit", "pipe", "4:NETWORK:10000") |
| 319 |
... |
| 320 |
|
| 321 |
6. Functions |
| 322 |
|
| 323 |
6.1. rl_check([pvar]) |
| 324 |
6.2. rl_check_pipe(pipe_no) |
| 325 |
|
| 326 |
6.1. rl_check([pvar]) |
| 327 |
|
| 328 |
Check the current request against the matched ratelimit algorithm. If |
| 329 |
no parameter is provided, the queue will be matched based on method |
| 330 |
type, and then the pipe will be identified based on the matched queue. |
| 331 |
If a pipe number is provided as a parameter, then the given pipe number |
| 332 |
will be used for identifying the ratelimit algorithm. The pipe number |
| 333 |
must be provided as number or via a pseudovariable. |
| 334 |
|
| 335 |
The method will return an error code if the limit for the matched |
| 336 |
algorithm is reached. |
| 337 |
|
| 338 |
Meaning of the parameters is as follows: |
| 339 |
* pvar - the pseudovariable holding the pipe id to be used by |
| 340 |
ratelimit. |
| 341 |
|
| 342 |
This function can be used from ANY_ROUTE. |
| 343 |
|
| 344 |
Example 1.4. rl_check usage |
| 345 |
... |
| 346 |
# perform queue/pipe match for current method |
| 347 |
if (!rl_check()) { |
| 348 |
append_to_reply("Retry-After: 5\r\n"); |
| 349 |
sl_send_reply("503","Limiting"); |
| 350 |
exit; |
| 351 |
} |
| 352 |
... |
| 353 |
# use pipe no 1 for the current method |
| 354 |
# set int pvar to 1 |
| 355 |
$var(p) = 1; |
| 356 |
if (!rl_check("$var(p)")) { |
| 357 |
append_to_reply("Retry-After: 5\r\n"); |
| 358 |
sl_send_reply("503","Limiting"); |
| 359 |
exit; |
| 360 |
} |
| 361 |
... |
| 362 |
|
| 363 |
6.2. rl_check_pipe(pipe_no) |
| 364 |
|
| 365 |
Check the current request against the matched ratelimit algorithm of |
| 366 |
the pipe provided as parameter. The parameter can be provided as number |
| 367 |
or variable. |
| 368 |
|
| 369 |
The method will return an error code if the limit for the matched |
| 370 |
algorithm is reached. |
| 371 |
|
| 372 |
Meaning of the parameters is as follows: |
| 373 |
* pipe_no - the pipe id to be used by ratelimit. |
| 374 |
|
| 375 |
This function can be used from REQUEST_ROUTE. |
| 376 |
|
| 377 |
Example 1.5. rl_check_pipe usage |
| 378 |
... |
| 379 |
# use pipe no 1 for the current method |
| 380 |
if (!rl_check_pipe("1") { |
| 381 |
append_to_reply("Retry-After: 5\r\n"); |
| 382 |
sl_send_reply("503","Limiting"); |
| 383 |
exit; |
| 384 |
} |
| 385 |
... |
| 386 |
|
| 387 |
7. RPC Commands |
| 388 |
|
| 389 |
7.1. rl.stats |
| 390 |
7.2. rl.set_pipe |
| 391 |
7.3. rl.get_pipes |
| 392 |
7.4. rl.set_queue |
| 393 |
7.5. rl.get_queues |
| 394 |
7.6. rl.set_pid |
| 395 |
7.7. rl.get_pid |
| 396 |
7.8. rl.push_load |
| 397 |
7.9. rl.set_dbg |
| 398 |
|
| 399 |
7.1. rl.stats |
| 400 |
|
| 401 |
Lists the parameters and variables in the ratelimit module. |
| 402 |
|
| 403 |
Name: rl.stats |
| 404 |
|
| 405 |
Parameters: none |
| 406 |
|
| 407 |
RPC Command Format: |
| 408 |
kamcmd rl.stats |
| 409 |
|
| 410 |
7.2. rl.set_pipe |
| 411 |
|
| 412 |
Sets the pipe parameters for the given pipe id. |
| 413 |
|
| 414 |
Name: rl.set_pipe |
| 415 |
|
| 416 |
Parameters: |
| 417 |
* pipe_id - pipe id. |
| 418 |
* pipe_algorithm - the algorithm assigned to the given pipe id. |
| 419 |
* pipe_limit - the limit assigned to the given pipe id. |
| 420 |
|
| 421 |
RPC Command Format: |
| 422 |
kamcmd rl.set_pipe 2 RED 10 |
| 423 |
|
| 424 |
7.3. rl.get_pipes |
| 425 |
|
| 426 |
Gets the list of in use pipes. |
| 427 |
|
| 428 |
Name: rl.get_pipes |
| 429 |
|
| 430 |
Parameters:none |
| 431 |
|
| 432 |
RPC Command Format: |
| 433 |
kamcmd rl.get_pipes |
| 434 |
|
| 435 |
7.4. rl.set_queue |
| 436 |
|
| 437 |
Sets the queue parameters for the given queue id. |
| 438 |
|
| 439 |
Name: rl.set_queue |
| 440 |
|
| 441 |
Parameters: |
| 442 |
* queue_id - queue id. |
| 443 |
* queue_method - the method assigned to the given queue id. |
| 444 |
* pipe_id - the pipe id assigned to the given queue id. |
| 445 |
|
| 446 |
RPC Command Format: |
| 447 |
kamcmd rl.set_queue 3 INVITE 2 |
| 448 |
|
| 449 |
7.5. rl.get_queues |
| 450 |
|
| 451 |
Gets the list of in use queues. |
| 452 |
|
| 453 |
Name: rl.get_queues |
| 454 |
|
| 455 |
Parameters: none |
| 456 |
|
| 457 |
RPC Command Format: |
| 458 |
kamcmd rl.get_queues |
| 459 |
|
| 460 |
7.6. rl.set_pid |
| 461 |
|
| 462 |
Sets the PID Controller parameters for the Feedback Algorithm. |
| 463 |
|
| 464 |
Name: rl.set_pid |
| 465 |
|
| 466 |
Parameters: |
| 467 |
* ki - the integral parameter. |
| 468 |
* kp - the proportional parameter. |
| 469 |
* kd - the derivative parameter. |
| 470 |
|
| 471 |
RPC Command Format: |
| 472 |
kamcmd rl.set_pid 0.5 0.5 0.5 |
| 473 |
|
| 474 |
7.7. rl.get_pid |
| 475 |
|
| 476 |
Gets the list of in use PID Controller parameters. |
| 477 |
|
| 478 |
Name: rl.get_pid |
| 479 |
|
| 480 |
Parameters: none |
| 481 |
|
| 482 |
RPC Command Format: |
| 483 |
kamcmd rl.get_pid |
| 484 |
|
| 485 |
7.8. rl.push_load |
| 486 |
|
| 487 |
Force the value of the load parameter. This method is useful for |
| 488 |
testing the Feedback algorithm. |
| 489 |
|
| 490 |
Name: rl.push_load |
| 491 |
|
| 492 |
Parameters: |
| 493 |
* load - the forced value of load (it must be greater than 0.0 and |
| 494 |
less than 1.0). |
| 495 |
|
| 496 |
RPC Command Format: |
| 497 |
kamcmd rl.push_load 0.85 |
| 498 |
|
| 499 |
7.9. rl.set_dbg |
| 500 |
|
| 501 |
This function will enable/disable a WARNING debug log exposing the |
| 502 |
internal counters for each pipe (useful in monitoring the ratelimit |
| 503 |
internals). |
| 504 |
|
| 505 |
Name: rl.set_dbg |
| 506 |
|
| 507 |
Parameters: |
| 508 |
* dbg - the debug value (0 means disable and 1 means enable). |
| 509 |
|
| 510 |
RPC Command Format: |
| 511 |
kamcmd rl.set_dbg 1 |
| 512 |
|
| 513 |
8. Known limitations |
| 514 |
|
| 515 |
The pipes and queues are stored as static vectors, so no more than |
| 516 |
MAX_PIPES/MAX_QUEUES can be added without recompilation. |
| 517 |
* MAX_PIPES - 32 |
| 518 |
* MAX_QUEUES - 32 |