1 // L2TPNS: token bucket filters
3 char const *cvs_id_tbf
= "$Id: tbf.c,v 1.2 2004/06/28 02:43:13 fred_nerk Exp $";
14 // Need a time interval.
16 #define TBF_MAX_QUEUE 2 // Maximum of 2 queued packet per
17 #define TBF_MAX_SIZE 3000 // Maxiumum queued packet size is 2048.
19 #define TBF_MAX_CREDIT 6000 // Maximum 6000 bytes of credit.
20 #define TBF_RATE 360 // 360 bytes per 1/10th of a second.
26 int oldest
; // Position of packet in the ring buffer.
27 sessionidt sid
; // associated session ID.
28 int max_credit
; // Maximum amount of credit available (burst size).
29 int rate
; // How many bytes of credit per second we get? (sustained rate)
30 void (*send
)(sessionidt s
, u8
*, int); // Routine to actually send out the data.
31 int prev
; // Timer chain position.
32 int next
; // Timer chain position.
34 u32 b_queued
; // Total bytes sent through this TBF
35 u32 b_sent
; // Total bytes sucessfully made it to the network.
36 u32 p_queued
; // ditto packets.
37 u32 p_sent
; // ditto packets.
38 u32 b_dropped
; // Total bytes dropped.
39 u32 p_dropped
; // Total packets dropped.
40 u32 p_delayed
; // Total packets not sent immediately.
42 int sizes
[TBF_MAX_QUEUE
];
43 char packets
[TBF_MAX_QUEUE
][TBF_MAX_SIZE
];
47 tbft
* filter_list
= NULL
;
48 int filter_list_size
= 0;
50 static int timer_chain
= -1; // Head of timer chain.
52 static void tbf_run_queue(int tbf_id
);
56 filter_list
= mmap(NULL
, sizeof(*filter_list
) * MAXTBFS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
60 filter_list_size
= MAXTBFS
;
61 filter_list
[0].sid
= -1; // Reserved.
64 // Put a TBF on the timer list.
65 // This is a doubly linked list..
66 // We put ourselves on the tail of the list.
68 static void add_to_timer(int id
)
73 if (timer_chain
== -1) {
74 filter_list
[id
].next
= filter_list
[id
].prev
= id
;
79 filter_list
[id
].next
= timer_chain
;
80 filter_list
[id
].prev
= filter_list
[timer_chain
].prev
;
81 filter_list
[filter_list
[timer_chain
].prev
].next
= id
;
82 filter_list
[timer_chain
].prev
= id
;
86 // Remove a TBF from the timer list.
87 // This is a doubly linked list.
88 static void del_from_timer(int id
)
93 if (filter_list
[id
].next
== id
) { // Last element in chain?
94 if (timer_chain
!= id
) { // WTF?
95 log(0,0,0,0, "Removed a singleton element from TBF, but tc didn't point to it!\n");
98 filter_list
[id
].next
= filter_list
[id
].prev
= 0;
102 filter_list
[filter_list
[id
].next
].prev
= filter_list
[id
].prev
;
103 filter_list
[filter_list
[id
].prev
].next
= filter_list
[id
].next
;
104 if (timer_chain
== id
)
105 timer_chain
= filter_list
[id
].next
;
107 filter_list
[id
].next
= filter_list
[id
].prev
= 0; // Mark as off the timer chain.
111 // Free a token bucket filter structure for re-use.
114 int free_tbf(int tid
)
116 if (tid
< 1) // Make sure we don't free id # 0
119 if (!filter_list
) // WTF?
122 if (filter_list
[tid
].next
)
124 filter_list
[tid
].sid
= 0;
130 // Allocate a new token bucket filter.
132 int new_tbf(int sid
, int max_credit
, int rate
, void (*f
)(sessionidt
, u8
*, int))
137 log(3,0,0,0, "Allocating new TBF (sess %d, rate %d, helper %p)\n", sid
, rate
, f
);
140 return 0; // Couldn't alloc memory!
144 for (i
= 0 ; i
< filter_list_size
; ++i
, p
= (p
+1)%filter_list_size
) {
145 if (filter_list
[p
].sid
)
148 memset((void*) &filter_list
[p
], 0, sizeof(filter_list
[p
]) ); // Clear counters and data.
149 filter_list
[p
].sid
= sid
;
150 filter_list
[p
].credit
= max_credit
;
151 filter_list
[p
].queued
= 0;
152 filter_list
[p
].max_credit
= max_credit
;
153 filter_list
[p
].rate
= rate
;
154 filter_list
[p
].oldest
= 0;
155 filter_list
[p
].send
= f
;
159 log(0,0,0,0, "Ran out of token bucket filters! Sess %d will be un-throttled\n", sid
);
163 // Not using. Disasterous if called via the CLI! :)
164 // All allocated filters are used! Increase the size of the allocated
167 i
= filter_list_size
;
168 filter_list_size
= filter_list_size
* 2 + 1;
170 filter_list
= realloc(filter_list
, filter_list_size
* sizeof(*filter_list
) );
172 for (; i
< filter_list_size
; ++i
)
173 filter_list
[i
].sid
= 0;
180 // Sanity check all the TBF records. This is
181 // typically done when we become a master..
190 for (i
= 1; i
< filter_list_size
; ++i
) {
191 if (!filter_list
[i
].sid
) // Is it used??
194 sid
= filter_list
[i
].sid
;
195 if (i
!= session
[sid
].tbf_in
&&
196 i
!= session
[sid
].tbf_out
) { // Ooops.
198 free_tbf(i
); // Mark it as free...
202 for (i
= 0; i
< config
->cluster_highest_sessionid
; ++i
) {
203 if (session
[i
].tbf_in
&& filter_list
[session
[i
].tbf_in
].sid
!= i
) {
204 filter_list
[session
[i
].tbf_in
].sid
= i
; // Ouch!? FIXME. What to do here?
206 if (session
[i
].tbf_out
&& filter_list
[session
[i
].tbf_out
].sid
!= i
) {
207 filter_list
[session
[i
].tbf_out
].sid
= i
; // Ouch!? FIXME. What to do here?
214 // Run a packet through a token bucket filter.
215 // If we can send it right away, we do. Else we
216 // try and queue it to send later. Else we drop it.
218 int tbf_queue_packet(int tbf_id
, char * data
, int size
)
226 if (tbf_id
> filter_list_size
|| tbf_id
< 1) { // Out of range ID??
227 // Very bad. Just drop it.
231 f
= &filter_list
[tbf_id
];
233 if (!f
->sid
) // Is this a real structure??
236 tbf_run_queue(tbf_id
); // Caculate credit and send any queued packets if possible..
241 if (!f
->queued
&& f
->credit
> size
) { // If the queue is empty, and we have
242 // enough credit, just send it now.
245 f
->send(f
->sid
, data
, size
);
249 f
->b_dropped
+= size
;
255 // Not enough credit. Can we have room in the queue?
256 if (f
->queued
>= TBF_MAX_QUEUE
) {
258 f
->b_dropped
+= size
;
259 return -1; // No, just drop it.
262 // Is it too big to fit into a queue slot?
263 if (size
>= TBF_MAX_SIZE
) {
265 f
->b_dropped
+= size
;
266 return -1; // Yes, just drop it.
269 // Ok. We have a slot, and it's big enough to
270 // contain the packet, so queue the packet!
271 i
= ( f
->oldest
+ f
->queued
) % TBF_MAX_QUEUE
;
272 memcpy(f
->packets
[i
], data
, size
);
278 if (!f
->next
) // Are we off the timer chain?
279 add_to_timer(tbf_id
); // Put ourselves on the timer chain.
281 return 0; // All done.
285 // Send queued packets from the filter if possible.
286 // (We're normally only called if this is possible.. )
287 static void tbf_run_queue(int tbf_id
)
294 f
= &filter_list
[tbf_id
];
296 // Calculate available credit...
297 f
->credit
+= (TIME
- f
->lasttime
) * f
->rate
/ 10; // current time is 1/10th of a second.
298 if (f
->credit
> f
->max_credit
)
299 f
->credit
= f
->max_credit
;
302 while (f
->queued
> 0 && f
->credit
>= f
->sizes
[f
->oldest
]) { // While we have enough credit..
305 f
->send(f
->sid
, f
->packets
[f
->oldest
], f
->sizes
[f
->oldest
]);
306 f
->b_sent
+= f
->sizes
[f
->oldest
];
309 f
->b_dropped
+= f
->sizes
[f
->oldest
];
313 f
->credit
-= f
->sizes
[f
->oldest
];
315 f
->oldest
= (f
->oldest
+ 1 ) % TBF_MAX_QUEUE
;
316 f
->queued
--; // One less queued packet..
319 if (f
->queued
) // Still more to do. Hang around on the timer list.
322 if (f
->next
) // Are we on the timer list??
323 del_from_timer(tbf_id
); // Nothing more to do. Get off the timer list.
327 // Periodically walk the timer list..
329 int tbf_run_timer(void)
332 int count
= filter_list_size
+ 1; // Safety check.
334 int tbf_id
; // structure being processed.
337 return 0; // Nothing to do...
339 if (!filter_list
) // No structures built yet.
342 last
= filter_list
[i
].prev
; // last element to process.
346 i
= filter_list
[i
].next
; // Get the next in the queue.
348 tbf_run_queue(tbf_id
); // Run the timer queue..
349 } while ( timer_chain
> 0 && i
&& tbf_id
!= last
&& --count
> 0);
353 for (i
= 0; i
< filter_list_size
; ++i
) {
354 if (!filter_list
[i
].next
)
356 if (filter_list
[i
].lasttime
== TIME
) // Did we just run it?
359 log(1,0,0,0, "Missed tbf %d! Not on the timer chain?(n %d, p %d, tc %d)\n", i
,
360 filter_list
[i
].next
, filter_list
[i
].prev
, timer_chain
);
368 int cmd_show_tbf(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
373 if (CLI_HELP_REQUESTED
)
374 return CLI_HELP_NO_ARGS
;
376 if (!config
->cluster_iam_master
) {
377 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
384 cli_print(cli
,"%6s %5s %5s %6s %6s | %7s %7s %8s %8s %8s %8s", "TBF#", "Sid", "Rate", "Credit", "Queued",
385 "ByteIn","PackIn","ByteSent","PackSent", "PackDrop", "PackDelay");
387 for (i
= 1; i
< filter_list_size
; ++i
) {
388 if (!filter_list
[i
].sid
) // Is it used?
391 cli_print(cli
, "%5d%1s %5d %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
392 i
, (filter_list
[i
].next
? "*" : " "),
394 filter_list
[i
].rate
* 8,
395 filter_list
[i
].credit
,
396 filter_list
[i
].queued
,
398 filter_list
[i
].b_queued
,
399 filter_list
[i
].p_queued
,
400 filter_list
[i
].b_sent
,
401 filter_list
[i
].p_sent
,
402 filter_list
[i
].p_dropped
,
403 filter_list
[i
].p_delayed
);
406 cli_print(cli
, "%d tbf entries used, %d total", count
, filter_list_size
);