1 // L2TPNS Rate Limiting Stuff
2 // $Id: rl.c,v 1.4 2004-05-24 04:28:41 fred_nerk Exp $
12 #include <sys/types.h>
18 extern radiust
*radius
;
19 extern sessiont
*session
;
21 extern tbft
*filter_buckets
;
22 extern struct configt
*config
;
29 "tc qdisc add dev " DEVICE
" root handle 1: htb",
30 "tc filter del dev " DEVICE
" protocol ip pref 1 fw",
31 "iptables -t mangle -N throttle 2>&1 >/dev/null",
32 "iptables -t mangle -F throttle 2>&1 >/dev/null",
33 "iptables -t mangle -A l2tpns -j throttle 2>&1 >/dev/null",
38 log(2, 0, 0, 0, "Initializing HTB\n");
39 for (i
= 0; commands
[i
] && *commands
[i
]; i
++)
41 log(3, 0, 0, 0, "Running \"%s\"\n", commands
[i
]);
44 log(2, 0, 0, 0, "Done initializing HTB\n");
51 if (!config
->rl_rate
) return 0;
53 t
= ++config
->next_tbf
;
54 if (config
->next_tbf
>= MAXSESSION
) return 0;
55 snprintf(filter_buckets
[t
].handle
, 9, "1:%d0", t
);
57 log(2, 0, 0, 0, "Creating new htb %s\n", filter_buckets
[t
].handle
);
58 snprintf(cmd
, 2048, "tc class add dev " DEVICE
" parent 1: classid %s htb rate %lukbit burst 15k",
59 filter_buckets
[t
].handle
, config
->rl_rate
);
60 log(3, 0, 0, 0, "%s\n", cmd
);
61 if (WEXITSTATUS(system(cmd
)) != 0)
63 memset(filter_buckets
[t
].handle
, 0, sizeof(filter_buckets
[t
].handle
));
64 log(0, 0, 0, 0, "tc returned an error creating a token bucket\n");
68 snprintf(cmd
, 2048, "tc filter add dev " DEVICE
" protocol ip parent 1:0 prio 1 handle %d fw flowid %s",
69 t
, filter_buckets
[t
].handle
);
70 log(3, 0, 0, 0, "%s\n", cmd
);
71 if (WEXITSTATUS(system(cmd
)) != 0)
73 memset(filter_buckets
[t
].handle
, 0, sizeof(filter_buckets
[t
].handle
));
74 log(0, 0, 0, 0, "tc returned an error creating a filter\n");
84 if (!config
->rl_rate
) return 0;
86 for (i
= 1; i
< MAXSESSION
; i
++)
88 if (!*filter_buckets
[i
].handle
) continue;
89 if (filter_buckets
[i
].in_use
) continue;
91 filter_buckets
[i
].in_use
= 1;
92 log(2, 0, 0, 0, "Returning tbf %s\n", filter_buckets
[i
].handle
);
96 if (i
) filter_buckets
[i
].in_use
= 1;
100 void rl_done_tbf(u16 t
)
103 log(2, 0, 0, 0, "Freeing up HTB %s\n", filter_buckets
[t
].handle
);
104 filter_buckets
[t
].in_use
= 0;
107 void rl_destroy_tbf(u16 t
)
110 if (!config
->rl_rate
) return;
111 if (filter_buckets
[t
].in_use
)
113 log(0, 0, 0, 0, "Trying to destroy an in-use HTB %s\n", filter_buckets
[t
].handle
);
116 snprintf(cmd
, 2048, "tc qdisc del dev " DEVICE
" handle %s", filter_buckets
[t
].handle
);
117 if (WEXITSTATUS(system(cmd
)) != 0)
118 log(0, 0, 0, 0, "tc returned an error deleting a token bucket\n");
119 memset(filter_buckets
[t
].handle
, 0, sizeof(filter_buckets
[t
].handle
));