aboutsummaryrefslogtreecommitdiff
path: root/src/tbf.c
blob: 1ad754ede904dd4a296aedaa10d81cb066d336ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* This file is part of vmod-tbf
   Copyright (C) 2013-2014 Sergey Poznyakoff
  
   Vmod-tbf is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   Vmod-tbf is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with vmod-tbf.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "tbf.h"
#include "vsha256.h"

#ifndef USEC_PER_SEC
# define USEC_PER_SEC  1000000L
#endif

#define DEBUG 1
static unsigned gc_interval = 3600;
static int debug_level = 0;

static void
debugprt(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vsyslog(LOG_DAEMON|LOG_DEBUG, fmt, ap);
	va_end(ap);
}
#define debug(n,c) do { if (debug_level>=(n)) debugprt c; } while (0)

enum { CHILD_LEFT, CHILD_RIGHT };

struct node {
	uint8_t key[SHA256_LEN];
#ifdef DEBUG
	char *keystr;
#endif
	struct node *parent;
	struct node *child[2];
	struct node *prev, *next;
	pthread_cond_t notbusy;
	int busy;
	uint64_t timestamp;  /* microseconds since epoch */
	size_t tokens;       /* tokens available */
};

struct tree
{
	/* Root node of the tree */
	struct node *root;
	/* All nodes are linked in a LRU fashion, head pointing to
	   the most recently used, and tail to the last recently used
	   ones. */
	struct node *head, *tail;	
	pthread_mutex_t mutex;
};

/* Linked list management */

/* Link NODE after REF in TREE.  If REF is NULL, link at head */
static void
lru_link_node(struct tree *tree, struct node *node, struct node *ref)
{
	if (!ref) {
		node->prev = NULL;
		node->next = tree->head;
		if (tree->head)
			tree->head->prev = node;
		else
			tree->tail = node;
		tree->head = node;
	} else {
		struct node *x;

		node->prev = ref;
		if ((x = ref->next))
			x->prev = node;
		else
			tree->tail = node;
		ref->next = node;
	}
}

static void
lru_unlink_node(struct tree *tree, struct node *node)
{
	struct node *x;

	debug(1,("UNLINK %p %p\n", node, node->prev, node->next));

	if ((x = node->prev))
		x->next = node->next;
	else
		tree->head = node->next;
	if ((x = node->next))
		x->prev = node->prev;
	else
		tree->tail = node->prev;
	node->prev = node->next = NULL;
}

static int
keycmp(uint8_t *a, uint8_t *b)
{
	return memcmp(a, b, SHA256_LEN);
}

static void
keycpy(uint8_t *a, uint8_t *b)
{
	memcpy(a, b, SHA256_LEN);
}

static void
key_create(char const *input, uint8_t key[])
{
	struct SHA256Context ctx;
	SHA256_Init(&ctx);
	SHA256_Update(&ctx, input, strlen (input));
	SHA256_Final(key, &ctx);
}

static void
node_lock(struct tree *tree, struct node *node)
{
	if (node->busy) {
		pthread_cond_wait(&node->notbusy, &tree->mutex);
		node->busy = 1;
	}
}

static void
node_unlock(struct node *node)
{
	node->busy = 0;
	pthread_cond_broadcast(&node->notbusy);
}

enum node_lookup_result {
	NODE_FOUND,
	NODE_NEW
};

static int
tree_lookup_node(struct tree *tree, uint8_t key[], struct node **ret)
{
	int res;
	struct node *node, *parent = NULL;
	struct node **nodeptr;
	
 	pthread_mutex_lock(&tree->mutex);

	nodeptr = &tree->root;
	while ((node = *nodeptr) != NULL) {
		res = keycmp(key, node->key);
		if (res == 0)
			break;
		parent = node;
		nodeptr = &node->child[res > 0];
	}

	if (node) {
		node_lock(tree, node);
		lru_unlink_node(tree, node);
		res = NODE_FOUND;
	} else {
		node = calloc(1, sizeof(*node));
		AN(node);
		node->parent = parent;
		keycpy(node->key, key);
		pthread_cond_init(&node->notbusy, NULL);
		node->busy = 1;
		*nodeptr = node;
		debug(2, ("%x: allocated new node %p", pthread_self(), node));
		res = NODE_NEW;
	}
	lru_link_node(tree, node, NULL);
	*ret = node;
 	pthread_mutex_unlock(&tree->mutex);
//	debug(0, ("head: %p, root: %p", tree->head, tree->root));
	return res;
}

static void
node_free(struct node *node)
{
#ifdef DEBUG
	free(node->keystr);
#endif
	pthread_cond_destroy(&node->notbusy);
	free(node);
}

static void
tree_delete_node_unlocked(struct tree *tree, struct node *node)
{
	struct node *parent = node->parent;
	struct node **slot;

	if (!parent)
		slot = &tree->root;
	else if (node == parent->child[CHILD_LEFT])
		slot = &parent->child[CHILD_LEFT];
	else
		slot = &parent->child[CHILD_RIGHT];
	
	if (!node->child[CHILD_LEFT]) {
		/* No left subtree: link the right subtree to the parent slot */
		*slot = node->child[CHILD_RIGHT];
		if (node->child[CHILD_RIGHT])
			node->child[CHILD_RIGHT]->parent = parent;
	} else if (!node->child[CHILD_RIGHT]) {
		/* No right subtree: link the left subtree to the parent slot */
		*slot = node->child[CHILD_LEFT];
		if (node->child[CHILD_LEFT])
			node->child[CHILD_LEFT]->parent = parent;
	} else {
		/* Node has both subtrees. Find the largest value in the
		   right subtree */
		struct node *p;
		for (p = node->child[CHILD_LEFT]; p->child[CHILD_RIGHT];
		     p = p->child[CHILD_RIGHT])
			;

		p->child[CHILD_RIGHT] = node->child[CHILD_RIGHT];
		p->child[CHILD_RIGHT]->parent = p;

		*slot = node->child[CHILD_LEFT];
		node->child[CHILD_LEFT]->parent = parent;
	}
	lru_unlink_node(tree, node);
}

/* Dispose of tree nodes that were last accessed TIMEOUT seconds ago or
   earlier */
void
tree_gc(struct tree *tree, time_t timeout)
{
	struct node *p;
	uint64_t t;

	pthread_mutex_lock(&tree->mutex);
	t = (uint64_t) (time(NULL) - timeout) * USEC_PER_SEC;
	debug(1,("gc till %"PRIu64, t));
	while ((p = tree->tail) && p->timestamp < t) {
#ifdef DEBUG
		debug(1,("deleting %s", tree->tail->keystr));
		debug(1,("%p %p %p\n", tree->head, tree->tail, tree->tail->prev));
#endif
		node_lock(tree, p);
		tree_delete_node_unlocked(tree, p);
		node_unlock(p);
		node_free(p);
		debug(1,("%p %p\n", tree->head, tree->tail));
	}
	pthread_mutex_unlock(&tree->mutex);
}

/* Algorithm:
   
   * A token is added to the bucket at a constant rate of 1 token per INTERVAL
     microseconds.

   * A bucket can hold at most BURST_SIZE tokens.  If a token arrives when the
     bucket is full, that token is discarded.

   * When COST items of data arrive, COST tokens are removed
     from the bucket and the data are accepted.

   * If fewer than COST tokens are available, no tokens are removed from
     the bucket and the data are not accepted.

   This keeps the data traffic at a constant rate INTERVAL with bursts of
   up to BURST_SIZE data items.  Such bursts occur when no data was being
   arrived for BURST_SIZE*INTERVAL or more microseconds.
*/

int
tbf_proc(struct tree *tree, const char *keystr, int cost,
	 unsigned long interval, int burst_size)
{
	uint8_t key[SHA256_LEN];
	struct node *node = NULL;
	struct timeval tv;
	uint64_t now;
	uint64_t elapsed;
	uint64_t tokens;
	int res;

	key_create(keystr, key);

	gettimeofday(&tv, NULL);
	now = (uint64_t) tv.tv_sec * USEC_PER_SEC + (uint64_t)tv.tv_usec;

	switch (tree_lookup_node(tree, key, &node)) {
	case NODE_FOUND:
		/* calculate elapsed time and number of new tokens since
		   last add */;
		elapsed = now - node->timestamp;
		tokens = elapsed / interval; /* partial tokens ignored */
		/* timestamp set to time of most recent token */
		node->timestamp += tokens * interval; 
		
		/* add existing tokens to 64bit counter to prevent overflow
		   in range check */
		tokens += node->tokens;
		if (tokens >= burst_size)
			node->tokens = burst_size;
		else
			node->tokens = (size_t)tokens;
		
		debug(2, ("%x: found, elapsed time: %"PRIu64" us, "
			  "new tokens: %"PRIu64", total: %lu ",
			  pthread_self(),
			  elapsed, tokens, (unsigned long) node->tokens));
		break;

	case NODE_NEW:
		/* Initialize the structure */
#ifdef DEBUG
		node->keystr = strdup(keystr);
#endif
		node->timestamp = now;
		node->tokens = burst_size;
	}

	if (cost <= node->tokens) {
		res = 1;
		node->tokens -= cost;
		debug(2, ("%x: tbf_rate matched %s, tokens left %lu",
			  pthread_self(), keystr,
			  (unsigned long) node->tokens));
	} else {
		res = 0;
		debug(1, ("%x: tbf_rate overlimit on %s",
			  pthread_self(), keystr));
	}
	node_unlock(node);
	debug(1, ("tbf_proc: return"));
	return res;
}

struct tree *
tree_create(void)
{
	struct tree *tree = calloc(1, sizeof(*tree));
	AN(tree);
	pthread_mutex_init(&tree->mutex, NULL);
	return tree;
}

void
tree_free(void *data)
{
	struct tree *tree = data;
	struct node *p;

 	pthread_mutex_lock(&tree->mutex);
	while ((p = tree->tail)) {
		node_lock(tree, p);
		lru_unlink_node(tree, p);
		node_unlock(p);
		node_free(p);
	}
	pthread_mutex_unlock(&tree->mutex);
	pthread_mutex_destroy(&tree->mutex);
	free(tree);
}

int
tbf_event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
{
	switch (e) {
	case VCL_EVENT_LOAD:
		priv->priv = tree_create();
		priv->free = tree_free;
		break;

	case VCL_EVENT_DISCARD:
		break;

	default:
		/* ignore */
		break;
	}
	return 0;
}

struct tree *
get_tree(struct vmod_priv *priv)
{
	return priv->priv;
}

VCL_VOID
vmod_debug(VRT_CTX, VCL_INT newval)
{
	debug_level = newval;
}

VCL_VOID
vmod_set_gc_interval(VRT_CTX, VCL_REAL interval)
{
	gc_interval = interval;
}

VCL_VOID
vmod_gc(VRT_CTX, struct vmod_priv *priv, VCL_REAL interval)
{
	tree_gc(get_tree(priv), interval);
}

VCL_BOOL
vmod_rate(VRT_CTX, struct vmod_priv *priv,
	  VCL_STRING key, VCL_INT cost, VCL_REAL t,
	  VCL_INT burst_size)
{
	struct tree *tree = get_tree(priv);
	unsigned long interval = t * USEC_PER_SEC;
	
	debug(2, ("%x: entering rate(%s,%d,%g,%d)",
		  pthread_self(), key, cost, t, burst_size));
		
	if (interval == 0 || burst_size == 0)
		return false;

	tree_gc(tree, gc_interval);

	if (!cost) {
		/* cost free, so don't waste time on tree lookup */
		return true;
	}
	if (cost > burst_size) {
		/* impossibly expensive, so don't waste time on
		   tree lookup */
		return false;
	}

	return tbf_proc(tree, key, cost, interval, burst_size);
}

#define ISWS(c) ((c)==' '||(c)=='\t')

VCL_BOOL
vmod_check(VRT_CTX, struct vmod_priv *priv,
	   VCL_STRING key, VCL_STRING spec)
{
	double v, n;
	char *p;
#define SKIPWS(init) for (init; *spec && ISWS(*spec); spec++)
	
	errno = 0;
	v = strtod(spec, &p);
	if (errno || v < 0) {
		syslog(LOG_DAEMON|LOG_ERR, "bad rate: %s", spec);
		return false;
	}
	SKIPWS(spec = p);
	if (strncmp(spec, "req", 3)) {
		syslog(LOG_DAEMON|LOG_ERR,
		       "bad rate: expected \"req\", but found \"%s\"", spec);
		return false;
	}
	SKIPWS(spec += 3);
	if (*spec != '/') {
		syslog(LOG_DAEMON|LOG_ERR,
		       "bad rate: expected \"/\", but found \"%c\"", *spec);
		return false;
	}
	SKIPWS(++spec);
	if (*spec >= '0' && *spec <= '9') {
		errno = 0;
		n = strtod(spec, &p);
		if (errno || n < 0) {
			syslog(LOG_DAEMON|LOG_ERR, "bad interval: %s", spec);
			return false;
		}
		spec = p;
	} else
		n = 1;
	SKIPWS();

	switch (*spec) {
	case 0:
	case 's':
		break;
	case 'd':
		n *= 24;
	case 'h':
		n *= 60;
	case 'm':
		n *= 60;
		break;
	default:
		syslog(LOG_DAEMON|LOG_ERR, "invalid interval specifier: %s",
		       spec);
		return false;
	}

	SKIPWS(++spec);

	if (*spec)
		syslog(LOG_DAEMON|LOG_WARNING, "garbage after rate spec: %s",
		       spec);

	return vmod_rate(ctx, priv, key, 1, n/v, v/n+1);
}

static char xdig[] = "0123456789abcdef";
	
static void
key_to_str(uint8_t key[], char *buf)
{
	size_t i;

	for (i = 0; i < SHA256_LEN; i++) {
		*buf++ = xdig[key[i] >> 4];
		*buf++ = xdig[key[i] & 0xf];
	}
	*buf = 0;
}

static void
node_to_keystr(struct node *node, char *buf)
{
	if (node)
		key_to_str(node->key, buf);
	else
		*buf = 0;
}

VCL_VOID
vmod_dump(VRT_CTX, struct vmod_priv *priv, VCL_STRING file)
{
	struct tree *tree = get_tree(priv);
	struct node *node;
	char keybuf[3][2*SHA256_LEN+1];
	FILE *fp;
	int err = 0;
	
	fp = fopen(file, "w");
	if (!fp) {
		syslog(LOG_DAEMON|LOG_ERR,
		       "tbf.dump: can't open file %s for output: %m", file);
		return;
	}
 	pthread_mutex_lock(&tree->mutex);
	if (tree->root) {
		node_to_keystr(tree->root, keybuf[0]);
		fprintf(fp, "%s\n", keybuf[0]);
	}
	for (node = tree->head; node; node = node->next) {
		node_to_keystr(node, keybuf[0]);
		node_to_keystr(node->child[CHILD_LEFT], keybuf[1]);
		node_to_keystr(node->child[CHILD_RIGHT], keybuf[2]);
#ifdef DEBUG
		fprintf(fp, "# %s\n", node->keystr);
#endif
		fprintf(fp, "%s:%s:%s:%"PRIu64":%lu\n",
			keybuf[0], keybuf[1], keybuf[2],
			node->timestamp, (unsigned long)node->tokens);
		if (ferror(fp)) {
			syslog(LOG_DAEMON|LOG_ERR,
			       "tbf.dump: error writing to %s: %m", file);
			err = 1;
			break;
		}
	}
 	pthread_mutex_unlock(&tree->mutex);
	fclose(fp);
	if (err)
		unlink(file);
}

Return to:

Send suggestions and report system problems to the System administrator.