1 // L2TPNS: token bucket filters
3 char const *cvs_id_tbf
= "$Id: tbf.c,v 1.6 2004-09-02 04:18:07 fred_nerk Exp $";
15 tbft
*filter_list
= NULL
;
16 static int filter_list_size
= 0;
18 static int timer_chain
= -1; // Head of timer chain.
20 static void tbf_run_queue(int tbf_id
);
24 if ((filter_list
= shared_malloc(sizeof(*filter_list
) * MAXTBFS
)) == MAP_FAILED
)
27 filter_list_size
= MAXTBFS
;
28 filter_list
[0].sid
= -1; // Reserved.
31 // Put a TBF on the timer list.
32 // This is a doubly linked list..
33 // We put ourselves on the tail of the list.
35 static void add_to_timer(int id
)
40 if (timer_chain
== -1) {
41 filter_list
[id
].next
= filter_list
[id
].prev
= id
;
46 filter_list
[id
].next
= timer_chain
;
47 filter_list
[id
].prev
= filter_list
[timer_chain
].prev
;
48 filter_list
[filter_list
[timer_chain
].prev
].next
= id
;
49 filter_list
[timer_chain
].prev
= id
;
53 // Remove a TBF from the timer list.
54 // This is a doubly linked list.
55 static void del_from_timer(int id
)
60 if (filter_list
[id
].next
== id
) { // Last element in chain?
61 if (timer_chain
!= id
) { // WTF?
62 log(0,0,0,0, "Removed a singleton element from TBF, but tc didn't point to it!\n");
65 filter_list
[id
].next
= filter_list
[id
].prev
= 0;
69 filter_list
[filter_list
[id
].next
].prev
= filter_list
[id
].prev
;
70 filter_list
[filter_list
[id
].prev
].next
= filter_list
[id
].next
;
71 if (timer_chain
== id
)
72 timer_chain
= filter_list
[id
].next
;
74 filter_list
[id
].next
= filter_list
[id
].prev
= 0; // Mark as off the timer chain.
78 // Free a token bucket filter structure for re-use.
83 if (tid
< 1) // Make sure we don't free id # 0
86 if (!filter_list
) // WTF?
89 if (filter_list
[tid
].next
)
91 filter_list
[tid
].sid
= 0;
97 // Allocate a new token bucket filter.
99 int new_tbf(int sid
, int max_credit
, int rate
, void (*f
)(sessionidt
, u8
*, int))
104 log(3,0,0,0, "Allocating new TBF (sess %d, rate %d, helper %p)\n", sid
, rate
, f
);
107 return 0; // Couldn't alloc memory!
111 for (i
= 0 ; i
< filter_list_size
; ++i
, p
= (p
+1)%filter_list_size
) {
112 if (filter_list
[p
].sid
)
115 memset((void*) &filter_list
[p
], 0, sizeof(filter_list
[p
]) ); // Clear counters and data.
116 filter_list
[p
].sid
= sid
;
117 filter_list
[p
].credit
= max_credit
;
118 filter_list
[p
].queued
= 0;
119 filter_list
[p
].max_credit
= max_credit
;
120 filter_list
[p
].rate
= rate
;
121 filter_list
[p
].oldest
= 0;
122 filter_list
[p
].send
= f
;
127 // All allocated filters are used! Increase the size of the allocated
131 int new_size
= filter_list_size
* 2;
132 tbft
*new = mremap(filter_list
, filter_list_size
* sizeof(*new), new_size
* sizeof(*new), MREMAP_MAYMOVE
);
134 if (new == MAP_FAILED
)
136 log(0,0,0,0, "Ran out of token bucket filters and mremap failed! Sess %d will be un-throttled\n", sid
);
140 i
= filter_list_size
;
141 filter_list_size
= new_size
;
145 for (; i
< filter_list_size
; ++i
)
146 filter_list
[i
].sid
= 0;
150 log(0,0,0,0, "Ran out of token bucket filters! Sess %d will be un-throttled\n", sid
);
156 // Sanity check all the TBF records. This is
157 // typically done when we become a master..
166 for (i
= 1; i
< filter_list_size
; ++i
) {
167 if (!filter_list
[i
].sid
) // Is it used??
170 sid
= filter_list
[i
].sid
;
171 if (i
!= session
[sid
].tbf_in
&&
172 i
!= session
[sid
].tbf_out
) { // Ooops.
174 free_tbf(i
); // Mark it as free...
178 for (i
= 0; i
< config
->cluster_highest_sessionid
; ++i
) {
179 if (session
[i
].tbf_in
&& filter_list
[session
[i
].tbf_in
].sid
!= i
) {
180 filter_list
[session
[i
].tbf_in
].sid
= i
; // Ouch!? FIXME. What to do here?
182 if (session
[i
].tbf_out
&& filter_list
[session
[i
].tbf_out
].sid
!= i
) {
183 filter_list
[session
[i
].tbf_out
].sid
= i
; // Ouch!? FIXME. What to do here?
190 // Run a packet through a token bucket filter.
191 // If we can send it right away, we do. Else we
192 // try and queue it to send later. Else we drop it.
194 int tbf_queue_packet(int tbf_id
, char * data
, int size
)
202 if (tbf_id
> filter_list_size
|| tbf_id
< 1) { // Out of range ID??
203 // Very bad. Just drop it.
207 f
= &filter_list
[tbf_id
];
209 if (!f
->sid
) // Is this a real structure??
212 tbf_run_queue(tbf_id
); // Caculate credit and send any queued packets if possible..
217 if (!f
->queued
&& f
->credit
> size
) { // If the queue is empty, and we have
218 // enough credit, just send it now.
221 f
->send(f
->sid
, data
, size
);
225 f
->b_dropped
+= size
;
231 // Not enough credit. Can we have room in the queue?
232 if (f
->queued
>= TBF_MAX_QUEUE
) {
234 f
->b_dropped
+= size
;
235 return -1; // No, just drop it.
238 // Is it too big to fit into a queue slot?
239 if (size
>= TBF_MAX_SIZE
) {
241 f
->b_dropped
+= size
;
242 return -1; // Yes, just drop it.
245 // Ok. We have a slot, and it's big enough to
246 // contain the packet, so queue the packet!
247 i
= ( f
->oldest
+ f
->queued
) % TBF_MAX_QUEUE
;
248 memcpy(f
->packets
[i
], data
, size
);
254 if (!f
->next
) // Are we off the timer chain?
255 add_to_timer(tbf_id
); // Put ourselves on the timer chain.
257 return 0; // All done.
261 // Send queued packets from the filter if possible.
262 // (We're normally only called if this is possible.. )
263 static void tbf_run_queue(int tbf_id
)
270 f
= &filter_list
[tbf_id
];
272 // Calculate available credit...
273 f
->credit
+= (TIME
- f
->lasttime
) * f
->rate
/ 10; // current time is 1/10th of a second.
274 if (f
->credit
> f
->max_credit
)
275 f
->credit
= f
->max_credit
;
278 while (f
->queued
> 0 && f
->credit
>= f
->sizes
[f
->oldest
]) { // While we have enough credit..
281 f
->send(f
->sid
, f
->packets
[f
->oldest
], f
->sizes
[f
->oldest
]);
282 f
->b_sent
+= f
->sizes
[f
->oldest
];
285 f
->b_dropped
+= f
->sizes
[f
->oldest
];
289 f
->credit
-= f
->sizes
[f
->oldest
];
291 f
->oldest
= (f
->oldest
+ 1 ) % TBF_MAX_QUEUE
;
292 f
->queued
--; // One less queued packet..
295 if (f
->queued
) // Still more to do. Hang around on the timer list.
298 if (f
->next
) // Are we on the timer list??
299 del_from_timer(tbf_id
); // Nothing more to do. Get off the timer list.
303 // Periodically walk the timer list..
305 int tbf_run_timer(void)
308 int count
= filter_list_size
+ 1; // Safety check.
310 int tbf_id
; // structure being processed.
313 return 0; // Nothing to do...
315 if (!filter_list
) // No structures built yet.
318 last
= filter_list
[i
].prev
; // last element to process.
322 i
= filter_list
[i
].next
; // Get the next in the queue.
324 tbf_run_queue(tbf_id
); // Run the timer queue..
325 } while ( timer_chain
> 0 && i
&& tbf_id
!= last
&& --count
> 0);
329 for (i
= 0; i
< filter_list_size
; ++i
) {
330 if (!filter_list
[i
].next
)
332 if (filter_list
[i
].lasttime
== TIME
) // Did we just run it?
335 log(1,0,0,0, "Missed tbf %d! Not on the timer chain?(n %d, p %d, tc %d)\n", i
,
336 filter_list
[i
].next
, filter_list
[i
].prev
, timer_chain
);
344 int cmd_show_tbf(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
349 if (CLI_HELP_REQUESTED
)
350 return CLI_HELP_NO_ARGS
;
352 if (!config
->cluster_iam_master
) {
353 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
360 cli_print(cli
,"%6s %5s %5s %6s %6s | %7s %7s %8s %8s %8s %8s", "TBF#", "Sid", "Rate", "Credit", "Queued",
361 "ByteIn","PackIn","ByteSent","PackSent", "PackDrop", "PackDelay");
363 for (i
= 1; i
< filter_list_size
; ++i
) {
364 if (!filter_list
[i
].sid
) // Is it used?
367 cli_print(cli
, "%5d%1s %5d %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
368 i
, (filter_list
[i
].next
? "*" : " "),
370 filter_list
[i
].rate
* 8,
371 filter_list
[i
].credit
,
372 filter_list
[i
].queued
,
374 filter_list
[i
].b_queued
,
375 filter_list
[i
].p_queued
,
376 filter_list
[i
].b_sent
,
377 filter_list
[i
].p_sent
,
378 filter_list
[i
].p_dropped
,
379 filter_list
[i
].p_delayed
);
382 cli_print(cli
, "%d tbf entries used, %d total", count
, filter_list_size
);