-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtcplog_idcache.c
More file actions
186 lines (163 loc) · 4.6 KB
/
Copy pathtcplog_idcache.c
File metadata and controls
186 lines (163 loc) · 4.6 KB
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
/*-
* Copyright (c) 2017 Netflix, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/cdefs.h>
#include <sys/queue.h>
#include <sys/stddef.h>
#include <sys/tree.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tcplog_dumper.h"
/* The number of seconds we cache entries. */
#define EXPIRY_TIME (5 * 60)
RB_HEAD(id_tree, id_record);
static struct id_tree id_tree_head[NUM_THREADS];
TAILQ_HEAD(expiryq, id_record);
static struct expiryq expiryq_head[NUM_THREADS];
static _Thread_local int tdidx;
struct id_record {
char *id;
RB_ENTRY(id_record) treenode;
TAILQ_ENTRY(id_record) tq;
time_t last_used;
int next_filenum;
};
RB_PROTOTYPE_STATIC(id_tree, id_record, treenode, id_cmp)
/*
* Ensure we can directly compare keys using (char **) and
* (struct id_record *).
*/
_Static_assert(offsetof(struct id_record, id) == 0,
"id must be the first field in (struct id_record)");
static inline int
id_cmp(struct id_record *a, struct id_record *b)
{
return (strcmp(a->id, b->id));
}
RB_GENERATE_STATIC(id_tree, id_record, treenode, id_cmp)
/*
* Initialize the idcache for a thread.
*
* This also sets the thread index, which we hold in thread-local storage
* and use as an index into our arrays.
*
* Arguments:
* - The thread index.
*/
void
idcache_init(int idx)
{
assert(idx < NUM_THREADS);
tdidx = idx;
RB_INIT(&id_tree_head[idx]);
TAILQ_INIT(&expiryq_head[idx]);
}
/*
* Check the expiry queue for a thread.
*
* The calling threads should run this periodically to clean up their
* expired ID entries.
*/
void
idcache_expire(void)
{
struct id_record *idr;
time_t expirytime;
expirytime = time(NULL) - EXPIRY_TIME;
while ((idr = TAILQ_FIRST(&expiryq_head[tdidx])) != NULL) {
if (idr->last_used > expirytime)
break;
TAILQ_REMOVE(&expiryq_head[tdidx], idr, tq);
RB_REMOVE(id_tree, &id_tree_head[tdidx], idr);
free(idr->id);
free(idr);
}
}
/*
* Add an entry to the cache.
*
* Arguments:
* - The ID
* - The next filenumber we should cache
*/
void
idcache_add(const char *id, int next_filenum)
{
struct id_record *existing, *idr;
if ((idr = malloc(sizeof(*idr))) == NULL)
return;
if ((idr->id = strdup(id)) == NULL) {
free(idr);
return;
}
idr->last_used = time(NULL);
idr->next_filenum = next_filenum;
/*
* Add to the red/black tree. In theory, there should not be
* an existing entry. Check for this.
*/
existing = RB_INSERT(id_tree, &id_tree_head[tdidx], idr);
assert(existing == NULL);
if (existing != NULL) {
existing->next_filenum = next_filenum;
free(idr->id);
free(idr);
return;
}
TAILQ_INSERT_TAIL(&expiryq_head[tdidx], idr, tq);
}
/*
* Get an entry from the cache.
*
* Arguments:
* - The ID
*
* Return Value:
* If a matching entry is found, a pointer to its next_filenum field.
* Otherwise, NULL.
*/
int *
idcache_get(const char *id)
{
struct id_record *idr;
/* Find a matching record. */
idr = RB_FIND(id_tree, &id_tree_head[tdidx],
__DECONST(struct id_record *, &id));
/* If we found a matching record, reset its expiry. */
if (idr != NULL) {
idr->last_used = time(NULL);
TAILQ_REMOVE(&expiryq_head[tdidx], idr, tq);
TAILQ_INSERT_TAIL(&expiryq_head[tdidx], idr, tq);
}
return (idr == NULL ? (int *)NULL : &idr->next_filenum);
}