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
|
#include "routing.h"
#include "log.h"
#include "metrics.h"
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <prom_metric_sample_histogram.h>
// The DHT routing table has a keyspace of 0 -- 2^160 split into buckets of 8.
// When a bucket becomes full, we split it in half. As we further expand the
// routing table we only continue to split the buckets on the side we fall on.
//
// Initially, this may sound like a binary tree (because we split it in two),
// but looking at it as a flat array leads to some interesting intuitions.
// Since we only expand one half of the "tree", the total size is bounded by
// the depth of the tree log2(2^160) == 160.
//
// As a flat array we notice the intrinsic properties of the routing table.
// With a bucket size of 8, the routing table contains 160 * 8 == 1280 nodes.
// As the node ids get less similar to our own our grouping of them becomes
// less detailed. While the bucket we are in contains node very close to us,
// the nodes furthest away from us are grouped in buckets with nodes they
// barely resemble.
//
// +----------------------------+
// | n1 | n2 | n3 | ... | n1280 |
// +----------------------------+
// More Less
// <--------Similarity-------->
// <----------Detail---------->
//
#include <arpa/inet.h>
struct table {
struct nodeid myID;
struct entry table[RT_SIZE];
};
struct table* pTable;
struct nodeid myID;
struct entry *table;
int table_size = RT_SIZE;
void routing_init(struct nodeid* myid) {
pTable = malloc(sizeof(struct table));
if(myid != NULL) pTable->myID = *myid;
table = pTable->table;
myID = pTable->myID;
routing_flush();
}
void routing_setid(struct nodeid *id) {
pTable->myID = *id;
myID = pTable->myID;
}
void routing_update_metrics() {
char buf[4]; // Largest value is 157 + \0
struct entry *entry = pTable->table;
for(uint8_t i = 0; i < RT_IDBITS-RT_BBITS; i++) {
sprintf(buf, "%d", i);
uint8_t filled = 0;
for(uint8_t j = 0; j < RT_BSIZE; j++) {
filled += entry->set;
entry++;
}
prom_gauge_set(routing_table_occupied, filled, (const char*[]){buf});
}
}
void routing_rebuild_table() {
struct entry table_swap[RT_SIZE];
memcpy(table_swap, pTable->table, sizeof(pTable->table));
routing_flush();
for(size_t i = 0; i < RT_SIZE; i++) {
if(!table_swap[i].set) continue;
struct entry *entry;
if(routing_offer(&table_swap[i].id, &entry)) {
*entry = table_swap[i];
} else {
dbg("Dropping a client since we already have enough in that bucket");
}
}
}
void routing_flush() {
memset(pTable->table, 0, sizeof(pTable->table));
}
// Calculate the common bit prefix between two node ids.
uint8_t prefix(struct nodeid* a, struct nodeid* b) {
uint8_t c = 0;
for(uint8_t i = 0; i < 5; i++) {
// Since the nodeids are stored in host byteorder in the words we have
// to make sure they're big endian before doing the prefix match,
// otherwise we end up with prefix matching that's different from the
// rest of the network
uint32_t word = htonl(a->inner[i]) ^ htonl(b->inner[i]);
// This word is different, find the location of the difference
if (word != 0)
return c + __builtin_clz(word);
// This word is completely the same
c += sizeof(word) * CHAR_BIT;
}
return c;
}
static int8_t scan(uint16_t baseIndex, struct nodeid* id) {
assert(baseIndex < RT_SIZE - RT_BSIZE);
int8_t index = -2;
for(size_t i = baseIndex; i < baseIndex + RT_BSIZE; i++) {
if(!pTable->table[i].set) {
index = index == -2 ? i - baseIndex : index;
continue;
}
if(memcmp(&pTable->table[i].id, id, sizeof(struct nodeid)) == 0) {
return -1;
}
}
return index;
}
static uint16_t base_bucket(struct nodeid* id) {
uint16_t bucketIndex = prefix(&pTable->myID, id);
assert(bucketIndex != RT_IDBITS);
// If they are sufficiently similar they end up in the final bucket. Clamp the index to ensure.
bucketIndex = bucketIndex > (RT_IDBITS - RT_BBITS) ? (RT_IDBITS - RT_BBITS) : bucketIndex;
assert(bucketIndex <= RT_IDBITS - RT_BBITS);
return bucketIndex * RT_BSIZE;
}
struct entry* routing_get(struct nodeid* id) {
uint16_t baseIndex = base_bucket(id);
for(size_t i = baseIndex; i < baseIndex + RT_BSIZE; i++) {
if(!pTable->table[i].set) continue;
if(memcmp(&pTable->table[i].id, id, sizeof(struct nodeid)) == 0) {
return &pTable->table[i];
}
}
return NULL;
}
#if UINT8_MAX > RAND_MAX
#error UINT8_MAX is larger than RAND_MAX
#endif
static uint8_t rand_byte() {
int limit = (RAND_MAX / UINT8_MAX)*UINT8_MAX;
int val;
while((val = rand()) >= limit);
return val % UINT8_MAX;
}
struct nodeid rand_nodeid_in_bucket(struct nodeid *self, struct nodeid *other) {
struct nodeid target;
{
char *target_byte = (char*)⌖
for(size_t i = 0; i < sizeof(target); i++) (*target_byte++) = rand_byte();
}
uint8_t bucket = prefix(self, other);
uint8_t byte = bucket / 8;
uint8_t residual = bucket % 8;
for(size_t i = 0; i < byte; i++) {
target.inner_b[i] = self->inner_b[i];
}
uint8_t mask = 0xFF >> residual;
target.inner_b[byte] = (target.inner_b[byte] & mask) | (self->inner_b[byte] & ~mask);
return target;
}
void routing_remove(struct nodeid* id) {
struct entry* entry = routing_get(id);
entry->set = false;
entry->expire = 0;
routing_update_metrics();
}
bool routing_interested(struct nodeid* id) {
uint16_t bucketIndex = prefix(&pTable->myID, id);
// The nodeid is the same as our own
if(bucketIndex == RT_IDBITS) {
return false;
}
prom_histogram_observe(offered, bucketIndex, NULL);
uint16_t baseIndex = base_bucket(id);
int8_t inBucketIndex = scan(baseIndex, id);
if(inBucketIndex < 0) {
// The bucket either already contains the node, or it has no more space
return false;
}
return true;
}
// Offer the routing table a new node
bool routing_offer(struct nodeid* id, struct entry **dest) {
uint16_t bucketIndex = prefix(&pTable->myID, id);
// The nodeid is the same as our own
if(bucketIndex == RT_IDBITS) {
return false;
}
uint16_t baseIndex = base_bucket(id);
int8_t inBucketIndex = scan(baseIndex, id);
if(inBucketIndex < 0) {
// The bucket either already contains the node, or it has no more space
return false;
}
struct entry* entry = &pTable->table[baseIndex + inBucketIndex];
entry->set = true;
entry->id = *id;
*dest = entry;
return true;
}
struct item {
struct nodeid distance;
bool set;
uint16_t index;
};
int compareItem(const void* a_v, const void* b_v) {
struct item* a = (struct item*)a_v;
struct item* b = (struct item*)b_v;
// If either of the two are not set, the one that is set comes before the
// one that isn't.
if(!a->set || !b->set) return b->set - a->set;
return memcmp(&a->distance, &b->distance, sizeof(struct nodeid));
}
size_t routing_closest(struct nodeid* needle, size_t n, struct entry** res) {
assert(n <= RT_SIZE);
static struct item items[RT_SIZE] = {0};
for(uint16_t i = 0; i < RT_SIZE; i++) {
items[i].index = i;
}
{
struct item* item;
struct entry* entry;
for(item = &items[0], entry = &pTable->table[0]; item < &items[RT_SIZE] && entry < &pTable->table[RT_SIZE]; item++, entry++){
item->set = entry->set;
for(uint8_t j = 0; j < 5; j++) {
item->distance.inner[j] = entry->id.inner[j] ^ needle->inner[j];
}
}
}
// @PERFORMANCE: There's an algorithm known as quickselect which can select
// the top k elements from a list while only doing a partial sort.
// I imagine that would be more efficient than this full sort.
qsort(items, RT_SIZE, sizeof(struct item), compareItem);
size_t read;
for(read = 0; read < n; read++) {
if(!items[read].set)
break;
res[read] = &pTable->table[items[read].index];
}
return read;
}
void routing_oldest(struct entry** dest) {
*dest = NULL;
for(struct entry* entry = pTable->table; entry < pTable->table+RT_SIZE; entry++){
if(!entry->set)
continue;
if(entry->expire == 0)
continue;
if(*dest == NULL) {
*dest = entry;
continue;
}
if(difftime((*dest)->expire, entry->expire) > 0.0) {
*dest = entry;
}
}
}
void routing_reset_expire(time_t expire) {
for(struct entry* entry = pTable->table; entry < pTable->table+RT_SIZE; entry++){
if(!entry->set)
continue;
if(entry->expire != 0)
continue;
entry->expire = expire;
}
}
void routing_status(int* filled, int* size, double* load_factor, size_t load_factor_len) {
*size = RT_SIZE;
*filled = 0;
for(size_t i = 0; i < RT_SIZE; i++) {
if(pTable->table[i].set)
(*filled)++;
}
int per_bucket = RT_SIZE / load_factor_len;
int overflow = RT_SIZE % load_factor_len;
struct entry* table_cursor = pTable->table;
for(int i = 0; i < load_factor_len; i++) {
int is_overflow = i < overflow;
for(int j = 0; j < per_bucket + is_overflow; j++) {
load_factor[i] += table_cursor->set;
table_cursor++;
}
load_factor[i] /= per_bucket + is_overflow;
}
}
|