summaryrefslogtreecommitdiffabout
path: root/src/tbf.h
blob: a138eeb408921717a02ed13dca65f8f667a73543 (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
/* This file is part of vmod-tbf
   Copyright (C) 2013-2018 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 <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <syslog.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "vsha256.h"
#include "pthread.h"
#if VARNISHAPI_MAJOR > 5
# include <cache/cache.h>
# include <vcl.h>
# include <vcc_if.h>
#else
# include <vcl.h>
# include <vrt.h>
# include <vcc_if.h>
# include <cache/cache.h>
#endif
#define WSPTR(s) ((s)->ws)

#ifndef USEC_PER_SEC
# define USEC_PER_SEC  1000000L
#endif

#define DEBUG 1

struct dump_header {
	uint32_t version;
	uint32_t debug;
	uint32_t size;
	uint32_t count;
	uint32_t root;
};
#define DUMP_VERSION 0

enum { CHILD_LEFT, CHILD_RIGHT };

#define FL_CHILD_LEFT  0x1
#define FL_CHILD_RIGHT 0x2

enum node_status { NST_INCOMPLETE, NST_INIT };

struct node {
	uint8_t key[SHA256_LEN];
#ifdef DEBUG
	char *keystr;
#endif
	struct node *parent;        /* Parent node */
	struct node *child[2];      /* Left and right child nodes */
	struct node *prev, *next;   /* More (prev) and less (next) recently
				       updated nodes. */
	pthread_cond_t notbusy;     /* Prevent simultaneous updates */
	int busy:1;                 /* Node is in use if 1 */
	enum node_status status;    /* Node status */
	uint32_t ord;        /* Used when dumping nodes and computing tree
				stats */
	/* Actual TBF payload: */
	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;
	size_t refcnt;
};

enum node_lookup_result { NODE_FOUND, NODE_NEW };

Return to:

Send suggestions and report system problems to the System administrator.