blob: f01ba61b282eedc77e53110f3044fb9ac764af49 (
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
|
/* 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 <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 "vrt.h"
#include "vcc_if.h"
#include "vsha256.h"
#include "pthread.h"
#if VARNISHVERSION == 3
# include "bin/varnishd/cache.h"
# define VCL_VOID void
# define VCL_INT int
# define VCL_REAL double
# define VCL_BOOL unsigned
# define VCL_STRING const char *
# define MOD_CTX struct sess *
# define WSPTR(s) ((s)->wrk->ws)
#else
# include "bin/varnishd/cache/cache.h"
# define MOD_CTX const struct vrt_ctx *
# define WSPTR(s) ((s)->ws)
#endif
#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 };
|