ae0d33c0ef19744b9262435d445a164787b37300
[l2tpns.git] / rl.c
1 // L2TPNS Rate Limiting Stuff
2 // $Id: rl.c,v 1.1 2003/12/16 07:07:39 fred_nerk Exp $
3
4 #include <stdio.h>
5 #include <sys/file.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <malloc.h>
12 #include "l2tpns.h"
13
14 extern char *radiussecret;
15 extern radiust *radius;
16 extern sessiont *session;
17 extern ipt radiusserver[MAXRADSERVER]; // radius servers
18 extern u32 sessionid;
19 extern u8 radiusfree;
20 extern int radfd;
21 extern u8 numradiusservers;
22 extern char debug;
23 extern char *tapdevice;
24 extern tbft *filter_buckets;
25
26 #define DEVICE "tun0"
27
28 unsigned long rl_rate = 0;
29 int next_tbf = 1;
30
31 void init_rl()
32 {
33 #ifdef TC_TBF
34 system("tc qdisc del dev " DEVICE " root");
35 system("tc qdisc add dev " DEVICE " root handle 1: cbq avpkt 10000 bandwidth 100mbit");
36 system("tc filter del dev " DEVICE " protocol ip pref 1 fw");
37 system("iptables -t mangle -N throttle 2>&1 > /dev/null");
38 system("iptables -t mangle -F throttle");
39 system("iptables -t mangle -A l2tpns -j throttle");
40 #endif
41 #ifdef TC_HTB
42 char *commands[] = {
43 "tc qdisc add dev " DEVICE " root handle 1: htb default 1",
44 "tc class add dev " DEVICE " parent 1: classid 1:1 htb rate 100mbit burst 300k",
45 "tc filter del dev " DEVICE " protocol ip pref 1 fw",
46 "iptables -t mangle -N throttle 2>&1 > /dev/null",
47 "iptables -t mangle -F throttle",
48 "iptables -t mangle -A l2tpns -j throttle",
49 NULL
50 };
51 int i;
52
53 if (!rl_rate) return;
54
55 log(2, 0, 0, 0, "Initializing HTB\n");
56 for (i = 0; commands[i] && *commands[i]; i++)
57 {
58 log(3, 0, 0, 0, "Running \"%s\"\n", commands[i]);
59 system(commands[i]);
60 }
61 log(2, 0, 0, 0, "Done initializing HTB\n");
62 #endif
63 }
64
65 u16 rl_create_tbf()
66 {
67 u16 t;
68 char cmd[2048];
69 if (!rl_rate) return 0;
70
71 if (next_tbf >= MAXSESSION) return 0;
72 t = next_tbf++;
73 snprintf(filter_buckets[t].handle, 9, "1:%d0", t);
74
75 #ifdef TC_TBF
76 log(2, 0, 0, 0, "Creating new tbf %s\n", filter_buckets[t].handle);
77 snprintf(cmd, 2048, "tc class add dev " DEVICE " parent 1: classid 1:%d cbq bandwidth 100Mbit rate 100Mbit "
78 "weight 1 prio 8 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded isolated",
79 t);
80 log(3, 0, 0, 0, "%s\n", cmd);
81 system(cmd);
82
83 snprintf(cmd, 2048, "tc qdisc add dev " DEVICE " parent 1:%d handle %s tbf rate %dkbit buffer 1600 limit 3000",
84 t, filter_buckets[t].handle, rl_rate);
85 log(3, 0, 0, 0, "%s\n", cmd);
86 system(cmd);
87
88 snprintf(cmd, 2048, "tc filter add dev " DEVICE " protocol ip parent 1:0 prio 1 handle %d fw flowid 1:%d",
89 t, t);
90 log(3, 0, 0, 0, "%s\n", cmd);
91 system(cmd);
92 #endif
93 #ifdef TC_HTB
94 log(2, 0, 0, 0, "Creating new htb %s\n", filter_buckets[t].handle);
95 snprintf(cmd, 2048, "tc class add dev " DEVICE " parent 1: classid %s htb rate %lukbit burst 15k",
96 filter_buckets[t].handle, rl_rate);
97 log(3, 0, 0, 0, "%s\n", cmd);
98 system(cmd);
99
100 snprintf(cmd, 2048, "tc filter add dev " DEVICE " protocol ip parent 1:0 prio 1 handle %d fw flowid %s",
101 t, filter_buckets[t].handle);
102 log(3, 0, 0, 0, "%s\n", cmd);
103 system(cmd);
104 #endif
105
106 next_tbf++;
107 return t;
108 }
109
110 u16 rl_get_tbf()
111 {
112 int i;
113 if (!rl_rate) return 0;
114
115 for (i = 1; i < MAXSESSION; i++)
116 {
117 if (!filter_buckets[i].in_use && *filter_buckets[i].handle)
118 {
119 filter_buckets[i].in_use = 1;
120 log(2, 0, 0, 0, "Returning tbf %s\n", filter_buckets[i].handle);
121 return i;
122 }
123 }
124 i = rl_create_tbf();
125 if (i) filter_buckets[i].in_use = 1;
126 return i;
127 }
128
129 void rl_done_tbf(u16 t)
130 {
131 if (!t) return;
132 if (!rl_rate) return;
133 log(2, 0, 0, 0, "Freeing up TBF %s\n", filter_buckets[t].handle);
134 filter_buckets[t].in_use = 0;
135 }
136
137 void rl_destroy_tbf(u16 t)
138 {
139 char cmd[2048];
140 if (!rl_rate) return;
141 if (filter_buckets[t].in_use)
142 {
143 log(0, 0, 0, 0, "Trying to destroy an in-use TBF %s\n", filter_buckets[t].handle);
144 return;
145 }
146 #ifdef TC_TBF
147 snprintf(cmd, 2048, "tc qdisc del dev " DEVICE " handle %s", filter_buckets[t].handle);
148 system(cmd);
149 #endif
150 #ifdef TC_HTB
151 snprintf(cmd, 2048, "tc qdisc del dev " DEVICE " handle %s", filter_buckets[t].handle);
152 system(cmd);
153 #endif
154 system("iptables -t mangle -D l2tpns -j throttle");
155 system("iptables -t mangle -X throttle");
156 memset(filter_buckets[t].handle, 0, sizeof(filter_buckets[t].handle));
157 }
158