9 // Need a time interval.
11 #define TBF_MAX_QUEUE 2 // Maximum of 2 queued packet per
12 #define TBF_MAX_SIZE 3000 // Maxiumum queued packet size is 2048.
14 #define TBF_MAX_CREDIT 6000 // Maximum 6000 bytes of credit.
15 #define TBF_RATE 360 // 360 bytes per 1/10th of a second.
21 int oldest
; // Position of packet in the ring buffer.
22 sessionidt sid
; // associated session ID.
23 int max_credit
; // Maximum amount of credit available (burst size).
24 int rate
; // How many bytes of credit per second we get? (sustained rate)
25 void (*send
)(sessionidt s
, u8
*, int); // Routine to actually send out the data.
26 int prev
; // Timer chain position.
27 int next
; // Timer chain position.
29 u32 b_queued
; // Total bytes sent through this TBF
30 u32 b_sent
; // Total bytes sucessfully made it to the network.
31 u32 p_queued
; // ditto packets.
32 u32 p_sent
; // ditto packets.
33 u32 b_dropped
; // Total bytes dropped.
34 u32 p_dropped
; // Total packets dropped.
35 u32 p_delayed
; // Total packets not sent immediately.
37 int sizes
[TBF_MAX_QUEUE
];
38 char packets
[TBF_MAX_QUEUE
][TBF_MAX_SIZE
];
42 tbft
* filter_list
= NULL
;
43 int filter_list_size
= 0;
45 static int timer_chain
= -1; // Head of timer chain.
47 static void tbf_run_queue(int tbf_id
);
51 filter_list
= mmap(NULL
, sizeof(*filter_list
) * MAXTBFS
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
55 filter_list_size
= MAXTBFS
;
56 filter_list
[0].sid
= -1; // Reserved.
59 // Put a TBF on the timer list.
60 // This is a doubly linked list..
61 // We put ourselves on the tail of the list.
63 static void add_to_timer(int id
)
68 if (timer_chain
== -1) {
69 filter_list
[id
].next
= filter_list
[id
].prev
= id
;
74 filter_list
[id
].next
= timer_chain
;
75 filter_list
[id
].prev
= filter_list
[timer_chain
].prev
;
76 filter_list
[filter_list
[timer_chain
].prev
].next
= id
;
77 filter_list
[timer_chain
].prev
= id
;
81 // Remove a TBF from the timer list.
82 // This is a doubly linked list.
83 static void del_from_timer(int id
)
88 if (filter_list
[id
].next
== id
) { // Last element in chain?
89 if (timer_chain
!= id
) { // WTF?
90 log(0,0,0,0, "Removed a singleton element from TBF, but tc didn't point to it!\n");
93 filter_list
[id
].next
= filter_list
[id
].prev
= 0;
97 filter_list
[filter_list
[id
].next
].prev
= filter_list
[id
].prev
;
98 filter_list
[filter_list
[id
].prev
].next
= filter_list
[id
].next
;
99 if (timer_chain
== id
)
100 timer_chain
= filter_list
[id
].next
;
102 filter_list
[id
].next
= filter_list
[id
].prev
= 0; // Mark as off the timer chain.
106 // Free a token bucket filter structure for re-use.
109 int free_tbf(int tid
)
111 if (tid
< 1) // Make sure we don't free id # 0
114 if (!filter_list
) // WTF?
117 if (filter_list
[tid
].next
)
119 filter_list
[tid
].sid
= 0;
125 // Allocate a new token bucket filter.
127 int new_tbf(int sid
, int max_credit
, int rate
, void (*f
)(sessionidt
, u8
*, int))
132 log(3,0,0,0, "Allocating new TBF (sess %d, rate %d, helper %p)\n", sid
, rate
, f
);
135 return 0; // Couldn't alloc memory!
139 for (i
= 0 ; i
< filter_list_size
; ++i
, p
= (p
+1)%filter_list_size
) {
140 if (filter_list
[p
].sid
)
143 memset((void*) &filter_list
[p
], 0, sizeof(filter_list
[p
]) ); // Clear counters and data.
144 filter_list
[p
].sid
= sid
;
145 filter_list
[p
].credit
= max_credit
;
146 filter_list
[p
].queued
= 0;
147 filter_list
[p
].max_credit
= max_credit
;
148 filter_list
[p
].rate
= rate
;
149 filter_list
[p
].oldest
= 0;
150 filter_list
[p
].send
= f
;
154 log(0,0,0,0, "Ran out of token bucket filters! Sess %d will be un-throttled\n", sid
);
158 // Not using. Disasterous if called via the CLI! :)
159 // All allocated filters are used! Increase the size of the allocated
162 i
= filter_list_size
;
163 filter_list_size
= filter_list_size
* 2 + 1;
165 filter_list
= realloc(filter_list
, filter_list_size
* sizeof(*filter_list
) );
167 for (; i
< filter_list_size
; ++i
)
168 filter_list
[i
].sid
= 0;
175 // Sanity check all the TBF records. This is
176 // typically done when we become a master..
185 for (i
= 1; i
< filter_list_size
; ++i
) {
186 if (!filter_list
[i
].sid
) // Is it used??
189 sid
= filter_list
[i
].sid
;
190 if (i
!= session
[sid
].tbf_in
&&
191 i
!= session
[sid
].tbf_out
) { // Ooops.
193 free_tbf(i
); // Mark it as free...
197 for (i
= 0; i
< config
->cluster_highest_sessionid
; ++i
) {
198 if (session
[i
].tbf_in
&& filter_list
[session
[i
].tbf_in
].sid
!= i
) {
199 filter_list
[session
[i
].tbf_in
].sid
= i
; // Ouch!? FIXME. What to do here?
201 if (session
[i
].tbf_out
&& filter_list
[session
[i
].tbf_out
].sid
!= i
) {
202 filter_list
[session
[i
].tbf_out
].sid
= i
; // Ouch!? FIXME. What to do here?
209 // Run a packet through a token bucket filter.
210 // If we can send it right away, we do. Else we
211 // try and queue it to send later. Else we drop it.
213 int tbf_queue_packet(int tbf_id
, char * data
, int size
)
221 if (tbf_id
> filter_list_size
|| tbf_id
< 1) { // Out of range ID??
222 // Very bad. Just drop it.
226 f
= &filter_list
[tbf_id
];
228 if (!f
->sid
) // Is this a real structure??
231 tbf_run_queue(tbf_id
); // Caculate credit and send any queued packets if possible..
236 if (!f
->queued
&& f
->credit
> size
) { // If the queue is empty, and we have
237 // enough credit, just send it now.
240 f
->send(f
->sid
, data
, size
);
244 f
->b_dropped
+= size
;
250 // Not enough credit. Can we have room in the queue?
251 if (f
->queued
>= TBF_MAX_QUEUE
) {
253 f
->b_dropped
+= size
;
254 return -1; // No, just drop it.
257 // Is it too big to fit into a queue slot?
258 if (size
>= TBF_MAX_SIZE
) {
260 f
->b_dropped
+= size
;
261 return -1; // Yes, just drop it.
264 // Ok. We have a slot, and it's big enough to
265 // contain the packet, so queue the packet!
266 i
= ( f
->oldest
+ f
->queued
) % TBF_MAX_QUEUE
;
267 memcpy(f
->packets
[i
], data
, size
);
273 if (!f
->next
) // Are we off the timer chain?
274 add_to_timer(tbf_id
); // Put ourselves on the timer chain.
276 return 0; // All done.
280 // Send queued packets from the filter if possible.
281 // (We're normally only called if this is possible.. )
282 static void tbf_run_queue(int tbf_id
)
289 f
= &filter_list
[tbf_id
];
291 // Calculate available credit...
292 f
->credit
+= (config
->current_time
- f
->lasttime
) * f
->rate
/ 10; // current time is 1/10th of a second.
293 if (f
->credit
> f
->max_credit
)
294 f
->credit
= f
->max_credit
;
295 f
->lasttime
= config
->current_time
;
297 while (f
->queued
> 0 && f
->credit
>= f
->sizes
[f
->oldest
]) { // While we have enough credit..
300 f
->send(f
->sid
, f
->packets
[f
->oldest
], f
->sizes
[f
->oldest
]);
301 f
->b_sent
+= f
->sizes
[f
->oldest
];
304 f
->b_dropped
+= f
->sizes
[f
->oldest
];
308 f
->credit
-= f
->sizes
[f
->oldest
];
310 f
->oldest
= (f
->oldest
+ 1 ) % TBF_MAX_QUEUE
;
311 f
->queued
--; // One less queued packet..
314 if (f
->queued
) // Still more to do. Hang around on the timer list.
317 if (f
->next
) // Are we on the timer list??
318 del_from_timer(tbf_id
); // Nothing more to do. Get off the timer list.
322 // Periodically walk the timer list..
324 int tbf_run_timer(void)
327 int count
= filter_list_size
+ 1; // Safety check.
329 int tbf_id
; // structure being processed.
332 return 0; // Nothing to do...
334 if (!filter_list
) // No structures built yet.
337 last
= filter_list
[i
].prev
; // last element to process.
341 i
= filter_list
[i
].next
; // Get the next in the queue.
343 tbf_run_queue(tbf_id
); // Run the timer queue..
344 } while ( timer_chain
> 0 && i
&& tbf_id
!= last
&& --count
> 0);
348 for (i
= 0; i
< filter_list_size
; ++i
) {
349 if (!filter_list
[i
].next
)
351 if (filter_list
[i
].lasttime
== config
->current_time
) // Did we just run it?
354 log(1,0,0,0, "Missed tbf %d! Not on the timer chain?(n %d, p %d, tc %d)\n", i
,
355 filter_list
[i
].next
, filter_list
[i
].prev
, timer_chain
);
363 int cmd_show_tbf(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
368 if (!config
->cluster_iam_master
) {
369 cli_print(cli
, "Command can't be run on a slave.");
375 cli_print(cli
,"%6s %5s %5s %6s %6s | %7s %7s %8s %8s %8s %8s", "TBF#", "Sid", "Rate", "Credit", "Queued",
376 "ByteIn","PackIn","ByteSent","PackSent", "PackDrop", "PackDelay");
378 for (i
= 1; i
< filter_list_size
; ++i
) {
379 if (!filter_list
[i
].sid
) // Is it used?
382 cli_print(cli
, "%5d%1s %5d %5d %6d %6d | %7d %7d %8d %8d %8d %8d",
383 i
, (filter_list
[i
].next
? "*" : " "),
385 filter_list
[i
].rate
* 8,
386 filter_list
[i
].credit
,
387 filter_list
[i
].queued
,
389 filter_list
[i
].b_queued
,
390 filter_list
[i
].p_queued
,
391 filter_list
[i
].b_sent
,
392 filter_list
[i
].p_sent
,
393 filter_list
[i
].p_dropped
,
394 filter_list
[i
].p_delayed
);
397 cli_print(cli
, "%d tbf entries used, %d total", count
, filter_list_size
);