Update version
[l2tpns.git] / tbf.c
diff --git a/tbf.c b/tbf.c
index c024c67..4835ecc 100644 (file)
--- a/tbf.c
+++ b/tbf.c
@@ -1,58 +1,25 @@
-#include <malloc.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/mman.h>
+// L2TPNS: token bucket filters
 
+#include <string.h>
+#include <netinet/ip6.h>
+#include "dhcp6.h"
 #include "l2tpns.h"
+#include "util.h"
 #include "tbf.h"
 
-// Need a time interval.
-
-#define TBF_MAX_QUEUE  2       // Maximum of 2 queued packet per
-#define TBF_MAX_SIZE   3000    // Maxiumum queued packet size is 2048.
-
-#define TBF_MAX_CREDIT 6000    // Maximum 6000 bytes of credit.
-#define TBF_RATE       360     // 360 bytes per 1/10th of a second.
-
-typedef struct {
-       int             credit;
-       int             lasttime;
-       int             queued;
-       int             oldest; // Position of packet in the ring buffer.
-       sessionidt      sid;    // associated session ID.
-       int             max_credit; // Maximum amount of credit available (burst size).
-       int             rate;   // How many bytes of credit per second we get? (sustained rate)
-       void            (*send)(sessionidt s, u8 *, int);       // Routine to actually send out the data.
-       int             prev;   // Timer chain position.
-       int             next;   // Timer chain position.
-
-       u32     b_queued;       // Total bytes sent through this TBF
-       u32     b_sent;         // Total bytes sucessfully made it to the network.
-       u32     p_queued;       // ditto packets.
-       u32     p_sent;         // ditto packets.
-       u32     b_dropped;      // Total bytes dropped.
-       u32     p_dropped;      // Total packets dropped.
-       u32     p_delayed;      // Total packets not sent immediately.
-
-       int             sizes[TBF_MAX_QUEUE];
-       char            packets[TBF_MAX_QUEUE][TBF_MAX_SIZE];
-} tbft;
-
-
-tbft * filter_list = NULL;
-int filter_list_size = 0;
+tbft *filter_list = NULL;
+static int filter_list_size = 0;
 
 static int timer_chain = -1;   // Head of timer chain.
 
 static void tbf_run_queue(int tbf_id);
 
-void init_tbf(void)
+void init_tbf(int num_tbfs)
 {
-       filter_list = mmap(NULL, sizeof(*filter_list) * MAXTBFS, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
-       if (!filter_list)
+       if (!(filter_list = shared_malloc(sizeof(*filter_list) * num_tbfs)))
                return;
 
-       filter_list_size = MAXTBFS;
+       filter_list_size = num_tbfs;
        filter_list[0].sid = -1;        // Reserved.
 }
 //
@@ -87,7 +54,7 @@ static void del_from_timer(int id)
 
        if (filter_list[id].next == id) {       // Last element in chain?
                if (timer_chain != id) { // WTF?
-                       log(0,0,0,0, "Removed a singleton element from TBF, but tc didn't point to it!\n");
+                       LOG(0, 0, 0, "Removed a singleton element from TBF, but tc didn't point to it!\n");
                } else
                        timer_chain = -1;
                filter_list[id].next = filter_list[id].prev = 0;
@@ -124,18 +91,16 @@ int free_tbf(int tid)
 //
 // Allocate a new token bucket filter.
 //
-int new_tbf(int sid, int max_credit, int rate, void (*f)(sessionidt, u8 *, int))
+int new_tbf(int sid, int max_credit, int rate, void (*f)(sessionidt, uint8_t *, int))
 {
        int i;
        static int p = 0;
 
-       log(3,0,0,0, "Allocating new TBF (sess %d, rate %d, helper %p)\n", sid, rate, f);
+       LOG(4, 0, 0, "Allocating new TBF (sess %d, rate %d, helper %p)\n", sid, rate, f);
 
        if (!filter_list)
                return 0;       // Couldn't alloc memory!
 
-//    again:
-
        for (i = 0 ; i < filter_list_size ; ++i, p = (p+1)%filter_list_size ) {
                if (filter_list[p].sid)
                        continue;
@@ -151,24 +116,8 @@ int new_tbf(int sid, int max_credit, int rate, void (*f)(sessionidt, u8 *, int))
                return p;
        }
 
-       log(0,0,0,0, "Ran out of token bucket filters! Sess %d will be un-throttled\n", sid);
+       LOG(0, 0, 0, "Ran out of token bucket filters!  Sess %d will be un-throttled\n", sid);
        return 0;
-
-#if 0
-       // Not using. Disasterous if called via the CLI! :)
-               // All allocated filters are used! Increase the size of the allocated
-               // filters.
-
-       i = filter_list_size;
-       filter_list_size = filter_list_size * 2 + 1;
-
-       filter_list = realloc(filter_list, filter_list_size * sizeof(*filter_list) );
-
-       for (; i < filter_list_size; ++i)
-               filter_list[i].sid = 0;
-
-       goto again;
-#endif
 }
 
 //
@@ -210,10 +159,10 @@ void fsck_tbfs(void)
 // If we can send it right away, we do. Else we
 // try and queue it to send later. Else we drop it.
 //
-int tbf_queue_packet(int tbf_id, char * data, int size)
+int tbf_queue_packet(int tbf_id, uint8_t *data, int size)
 {
        int i;
-       tbft * f;
+       tbft *f;
 
        if (!filter_list)
                return -1;
@@ -289,10 +238,10 @@ static void tbf_run_queue(int tbf_id)
        f = &filter_list[tbf_id];
 
                // Calculate available credit...
-       f->credit += (config->current_time - f->lasttime) * f->rate / 10; // current time is 1/10th of a second.
+       f->credit += (TIME - f->lasttime) * f->rate / 10; // current time is 1/10th of a second.
        if (f->credit > f->max_credit)
                f->credit = f->max_credit;
-       f->lasttime = config->current_time;
+       f->lasttime = TIME;
 
        while (f->queued > 0 && f->credit >= f->sizes[f->oldest]) { // While we have enough credit..
 
@@ -348,10 +297,10 @@ int tbf_run_timer(void)
        for (i = 0; i < filter_list_size; ++i) {
                if (!filter_list[i].next)
                        continue;
-               if (filter_list[i].lasttime == config->current_time)    // Did we just run it?
+               if (filter_list[i].lasttime == TIME)    // Did we just run it?
                        continue;
 
-               log(1,0,0,0, "Missed tbf %d! Not on the timer chain?(n %d, p %d, tc %d)\n", i,
+               LOG(1, 0, 0, "Missed tbf %d! Not on the timer chain?(n %d, p %d, tc %d)\n", i,
                        filter_list[i].next, filter_list[i].prev, timer_chain);
                tbf_run_queue(i);
        }
@@ -360,15 +309,21 @@ int tbf_run_timer(void)
        return 1;
 }
 
-int cmd_show_tbf(struct cli_def *cli, char *command, char **argv, int argc)
+int cmd_show_tbf(struct cli_def *cli, const char *command, char **argv, int argc)
 {
        int i;
        int count = 0;
 
+       if (CLI_HELP_REQUESTED)
+               return CLI_HELP_NO_ARGS;
+
        if (!config->cluster_iam_master) {
-               cli_print(cli, "Command can't be run on a slave.");
+               cli_error(cli, "Can't do this on a slave.  Do it on %s",
+                       fmtaddr(config->cluster_master_address, 0));
+
                return CLI_OK;
        }
+
        if (!filter_list)
                return CLI_OK;
 
@@ -397,4 +352,3 @@ int cmd_show_tbf(struct cli_def *cli, char *command, char **argv, int argc)
        cli_print(cli, "%d tbf entries used, %d total", count, filter_list_size);
        return CLI_OK;
 }
-