forked from sargon/ddhcpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.c
564 lines (449 loc) · 16.1 KB
/
block.c
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
#include "block.h"
#include <errno.h>
#include <math.h>
#include "dhcp.h"
#include "logger.h"
#include "statistics.h"
#include "tools.h"
// TODO define sane value
#define UPDATE_CLAIM_MAX_BLOCKS 32
int block_alloc(ddhcp_block* block) {
DEBUG("block_alloc(block)\n");
if (!block) {
WARNING("block_alloc(...): No block given to initialize\n");
return 1;
}
// Do not allocate memory and initialise, when the block is already allocated
if (block->addresses) {
return 0;
}
block->addresses = (struct dhcp_lease*) calloc(sizeof(struct dhcp_lease), block->subnet_len);
if (!block->addresses) {
WARNING("block_alloc(...): Failed to allocate memory for lease management on block %i\n", block->index);
return 1;
}
for (unsigned int index = 0; index < block->subnet_len; index++) {
block->addresses[index].state = FREE;
block->addresses[index].lease_end = 0;
}
return 0;
}
ATTR_NONNULL(2) int block_own(ddhcp_block* block, ddhcp_config* config) {
if (!block) {
WARNING("block_own(...): No block given to own\n");
return 1;
}
if (block_alloc(block)) {
WARNING("block_own(...): Failed to initialize block %i for owning\n", block->index);
return 1;
}
block->state = DDHCP_OURS;
block->first_claimed = time(NULL);
NODE_ID_CP(&block->node_id, &config->node_id);
return 0;
}
ATTR_NONNULL_ALL void block_free(ddhcp_block* block) {
DEBUG("block_free(%i)\n", block->index);
if (block->state != DDHCP_BLOCKED) {
NODE_ID_CLEAR(&block->node_id);
block->state = DDHCP_FREE;
}
if (block->addresses) {
DEBUG("block_free(%i): Freeing DHCP leases\n", block->index);
free(block->addresses);
block->addresses = NULL;
// Reset needless timeout
block->needless_since = 0;
}
}
ATTR_NONNULL_ALL ddhcp_block* block_find_free(ddhcp_config* config) {
DEBUG("block_find_free(config)\n");
ddhcp_block* block = config->blocks;
ddhcp_block_list free_blocks;
INIT_LIST_HEAD(&free_blocks);
uint32_t num_free_blocks = 0;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_FREE) {
list_add_tail((&block->tmp_list), &free_blocks);
num_free_blocks++;
}
block++;
}
DEBUG("block_find_free(...): found %i free blocks\n", num_free_blocks);
ddhcp_block* random_free = NULL;
uint32_t r = ~0u;
if (num_free_blocks > 0) {
r = (uint32_t)rand() % num_free_blocks;
} else {
DEBUG("block_find_free(...): no free block found\n");
return NULL;
}
struct list_head* pos, *q;
list_for_each_safe(pos, q, &free_blocks) {
block = list_entry(pos, ddhcp_block, tmp_list);
list_del(pos);
if (r == 0) {
random_free = block;
break;
}
r--;
}
#if LOG_LEVEL_LIMIT >= LOG_WARNING
if (random_free) {
DEBUG("block_find_free(...): found block %i\n", random_free->index);
} else {
WARNING("block_find_free(...): no free block found\n");
}
#endif
return random_free;
}
ATTR_NONNULL_ALL int block_claim(int32_t num_blocks, ddhcp_config* config) {
DEBUG("block_claim(count:%i, config)\n", num_blocks);
// Handle blocks already in claiming prozess
struct list_head* pos, *q;
time_t now = time(NULL);
list_for_each_safe(pos, q, &config->claiming_blocks) {
ddhcp_block* block = list_entry(pos, ddhcp_block, claim_list);
if (block->claiming_counts == 3) {
if ( block_own(block, config) > 0) {
ERROR("block_claim(...): Claiming block (%i) failed, reseting claim counter.", block->index);
block->claiming_counts = 0;
} else {
//Reduce number of blocks we need to claim
num_blocks--;
INFO("block_claim(...): block %i claimed after 3 claims.\n", block->index);
}
list_del(pos);
config->claiming_blocks_amount--;
} else if (block->state != DDHCP_CLAIMING) {
DEBUG("block_claim(...): block %i is no longer marked for claiming\n", block->index);
list_del(pos);
config->claiming_blocks_amount--;
}
}
// Do we still need more, then lets find some.
if (num_blocks > config->claiming_blocks_amount) {
// find num_blocks - config->claiming_blocks_amount free blocks
uint32_t needed_blocks = (uint32_t) num_blocks - config->claiming_blocks_amount;
for (uint32_t i = 0; i < needed_blocks; i++) {
ddhcp_block* block = block_find_free(config);
if (block) {
block->state = DDHCP_CLAIMING;
block->claiming_counts = 0;
block->timeout = now + config->tentative_timeout;
list_add_tail(&(block->claim_list), &config->claiming_blocks);
config->claiming_blocks_amount++;
} else {
// We are short on free blocks in the network.
WARNING("block_claim(...): Network has no free blocks left!\n");
// TODO In a future version we could start to forward DHCP requests
// to other servers.
}
}
}
// TODO Sort blocks in claiming process by number of claims already processed.
// TODO If we have more blocks in claiming process than we need, drop the tail
// of blocks for which we had less claim announcements.
if (config->claiming_blocks_amount < 1) {
DEBUG("block_claim(...): No blocks need claiming.\n");
return 0;
}
// Send claim message for all blocks in claiming process.
struct ddhcp_mcast_packet* packet = new_ddhcp_packet(DDHCP_MSG_INQUIRE, config);
if (!packet) {
WARNING("block_claim(...): Failed to allocate ddhcpd mcast packet.\n");
return -ENOMEM;
}
packet->count = config->claiming_blocks_amount;
packet->payload = (struct ddhcp_payload*) calloc(sizeof(struct ddhcp_payload), config->claiming_blocks_amount);
if (!packet->payload) {
free(packet);
WARNING("block_claim(...): Failed to allocate ddhcpd mcast packet payload.\n");
return -ENOMEM;
}
int index = 0;
ddhcp_block* block;
list_for_each_entry(block, &config->claiming_blocks, claim_list) {
packet->payload[index].block_index = block->index;
packet->payload[index].timeout = 0;
packet->payload[index].reserved = 0;
index++;
}
statistics_record(config, STAT_MCAST_SEND_PKG, 1);
statistics_record(config, STAT_MCAST_SEND_INQUIRE, 1);
ssize_t bytes_send = send_packet_mcast(packet, DDHCP_SKT_MCAST(config));
statistics_record(config, STAT_MCAST_SEND_BYTE, (long int) bytes_send);
if (bytes_send > 0) {
list_for_each_entry(block, &config->claiming_blocks, claim_list) {
block->claiming_counts++;
}
} else {
DEBUG("block_claim(...): Send failed, no updates made.\n");
}
free(packet->payload);
free(packet);
return 0;
}
ATTR_NONNULL_ALL uint32_t block_num_free_leases(ddhcp_config* config) {
DEBUG("block_num_free_leases(config)\n");
ddhcp_block* block = config->blocks;
uint32_t free_leases = 0;
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
uint32_t num_blocks = 0;
#endif
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
free_leases += dhcp_num_free(block);
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
num_blocks++;
#endif
}
block++;
}
DEBUG("block_num_free_leases(...): Found %lu free DHCP leases in OUR (%lu) blocks\n", free_leases, num_blocks);
return free_leases;
}
ATTR_NONNULL_ALL uint32_t block_num_owned(ddhcp_config* config) {
uint32_t owned_blocks = 0;
ddhcp_block* block = config->blocks;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
owned_blocks++;
}
}
DEBUG("block_num_owned(...): We own %lu blocks\n", owned_blocks);
return owned_blocks;
}
ATTR_NONNULL_ALL ddhcp_block* block_find_free_leases(ddhcp_config* config) {
DEBUG("block_find_free_leases(config)\n");
ddhcp_block* block = config->blocks;
ddhcp_block* selected = NULL;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
if (dhcp_has_free(block)) {
if (selected) {
// If observed block is claimed earlier, select that block.
if (selected->first_claimed > block->first_claimed) {
selected = block;
}
} else {
selected = block;
}
}
}
block++;
}
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
if (selected) {
DEBUG("block_find_free_leases(...): Block %i selected\n", selected->index);
} else {
DEBUG("block_find_free_leases(...): No block found!\n");
}
#endif
return selected;
}
ATTR_NONNULL_ALL void block_drop_unused(ddhcp_config* config) {
DEBUG("block_drop_unsued(config)\n");
ddhcp_block* block = config->blocks;
ddhcp_block* freeable_block = NULL;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
if (dhcp_num_free(block) == config->block_size) {
DEBUG("block_drop unused(...): block %i is unused.\n", block->index);
if (freeable_block) {
// If observed block is younger than current selected, select block.
if (freeable_block->first_claimed < block->first_claimed) {
freeable_block = block;
}
} else {
freeable_block = block;
}
}
}
block++;
}
if (freeable_block) {
time_t now = time(NULL);
if ( freeable_block->needless_since == 0 ) {
DEBUG("block_drop_unused(...): mark block %i to be needless\n",freeable_block->index);
freeable_block->needless_since = now;
// Set needless marks flag
config->needless_marks = 1;
} else {
if ( freeable_block->needless_since <= now - config->block_needless_timeout) {
DEBUG("block_drop_unused(...): free block %i.\n", freeable_block->index);
block_free(freeable_block);
config->needless_marks = 0;
} else {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
time_t time_left = now - config->block_needless_timeout - freeable_block->needless_since;
DEBUG("block_drop_unused(...): free block %i in about %li seconds\n",freeable_block->index,time_left);
#endif
}
}
}
}
ATTR_NONNULL_ALL static void _block_update_claim_send(struct ddhcp_mcast_packet* packet, time_t new_block_timeout, ddhcp_config* config) {
DEBUG("block_update_claims_send(packet:%i,%li,config)\n",packet->count,new_block_timeout);
statistics_record(config, STAT_MCAST_SEND_PKG, 1);
statistics_record(config, STAT_MCAST_SEND_UPDATECLAIM, 1);
ssize_t bytes_send = send_packet_mcast(packet, DDHCP_SKT_MCAST(config));
statistics_record(config, STAT_MCAST_SEND_BYTE, (long int) bytes_send);
// TODO? Stat the number of blocks reclaimed.
if (bytes_send > 0) {
// Update the timeout value of all contained blocks
// iff the packet has been transmitted
for (uint8_t i = 0; i < packet->count; i++) {
uint32_t index = packet->payload[i].block_index;
DEBUG("block_update_claims_send(...): updated claim for block %i\n", index);
config->blocks[index].timeout = new_block_timeout;
}
} else {
DEBUG("block_update_claims_send(...): Send failed, no updates made.\n");
}
}
ATTR_NONNULL_ALL void block_update_claims(ddhcp_config* config) {
DEBUG("block_update_claims(config)\n");
uint32_t our_blocks = 0;
ddhcp_block* block = config->blocks;
time_t now = time(NULL);
time_t timeout_factor = now + config->block_timeout - (time_t)(config->block_timeout / config->block_refresh_factor);
// Determine if we need to run a full update claim run
// we run through the list until we see one block which needs update.
// Running a full update claims (see below) is much more expensive
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS && block->timeout < timeout_factor) {
our_blocks++;
break;
}
block++;
}
if (our_blocks == 0) {
DEBUG("block_update_claims(...): No blocks need claim updates.\n");
return;
}
struct ddhcp_mcast_packet* packet = new_ddhcp_packet(DDHCP_MSG_UPDATECLAIM, config);
if (!packet) {
WARNING("block_update_claims(...): Failed to allocate ddhcpd mcast packet.\n");
return;
}
// Aggressively group blocks into packets, send packet iff
// at least one block in a packet is below the baseline.
packet->payload = (struct ddhcp_payload*) calloc(sizeof(struct ddhcp_payload), UPDATE_CLAIM_MAX_BLOCKS);
if (!packet->payload) {
WARNING("block_update_claims(...): Failed to allocate ddhcpd packet payload.\n");
free(packet);
return;
}
block = config->blocks;
uint8_t send_packet = 0;
uint8_t index = 0;
time_t new_block_timeout = now + config->block_timeout;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->state == DDHCP_OURS) {
if (block->timeout < timeout_factor) {
DEBUG("block_update_claims(...): update claim for block %i needed\n", block->index);
send_packet = 1;
}
packet->payload[index].block_index = block->index;
packet->payload[index].timeout = config->block_timeout;
packet->payload[index].reserved = 0;
index++;
if (index == UPDATE_CLAIM_MAX_BLOCKS) {
if (send_packet) {
packet->count = index;
send_packet = 0;
_block_update_claim_send(packet, new_block_timeout, config);
}
index = 0;
}
}
block++;
}
if (send_packet) {
packet->count = index;
_block_update_claim_send(packet, new_block_timeout, config);
}
free(packet->payload);
free(packet);
}
ATTR_NONNULL_ALL void block_check_timeouts(ddhcp_config* config) {
DEBUG("block_check_timeouts(config)\n");
ddhcp_block* block = config->blocks;
time_t now = time(NULL);
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
if (block->timeout < now && block->state != DDHCP_BLOCKED && block->state != DDHCP_FREE) {
INFO("block_check_timeouts(...): Block %i FREE through timeout.\n", block->index);
block_free(block);
}
if (block->state == DDHCP_OURS) {
dhcp_check_timeouts(block);
} else if (block->addresses) {
int free_leases = dhcp_check_timeouts(block);
if (free_leases == block->subnet_len) {
block_free(block);
}
}
block++;
}
}
ATTR_NONNULL_ALL void block_show_status(int fd, ddhcp_config* config) {
ddhcp_block* block = config->blocks;
dprintf(fd, "block size/number\t%u/%u \n", config->block_size, config->number_of_blocks);
dprintf(fd, " tentative timeout\t%u\n", config->tentative_timeout);
dprintf(fd, " timeout\t%u\n", config->block_timeout);
dprintf(fd, " refresh factor\t%u\n", config->block_refresh_factor);
dprintf(fd, " spare leases needed\t%u\n", config->spare_leases_needed);
dprintf(fd, " network: %s/%i \n", inet_ntoa(config->prefix), config->prefix_len);
char node_id[17];
for (uint32_t j = 0; j < 8; j++) {
sprintf(node_id + 2 * j, "%02X", config->node_id[j]);
}
node_id[16] = '\0';
dprintf(fd, "node id\t%s\n", node_id);
dprintf(fd, "ddhcp blocks\n");
dprintf(fd, "index\tstate\towner\t\t\tclaim\tleases\ttimeout\n");
time_t now = time(NULL);
uint32_t num_reserved_blocks = 0;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
uint32_t free_leases = 0;
uint32_t offered_leases = 0;
if (block->addresses) {
free_leases = dhcp_num_free(block);
offered_leases = dhcp_num_offered(block);
}
for (uint32_t j = 0; j < 8; j++) {
sprintf(node_id + 2 * j, "%02X", block->node_id[j]);
}
node_id[16] = '\0';
char leases[16];
if (block->addresses) {
snprintf(leases, sizeof(leases), "%u/%u", offered_leases, config->block_size - free_leases - offered_leases);
} else {
leases[0] = '-';
leases[1] = '\0';
}
time_t timeout = block->timeout - now;
if (timeout > 0) {
num_reserved_blocks++;
dprintf(fd, "%i\t%i\t%s\t%u\t%s\t%lu\n", block->index, block->state, node_id, block->claiming_counts, leases, timeout);
}
block++;
}
dprintf(fd, "\nblocks in use: %i\n", num_reserved_blocks);
}
void block_unmark_needless(ddhcp_config* config){
DEBUG("block_unmark_needless(config)\n");
ddhcp_block* block = config->blocks;
for (uint32_t i = 0; i < config->number_of_blocks; i++) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
if (block->needless_since > 0) {
DEBUG("block_unmark_needless(...): Unmark block %i\n",block->index);
}
#endif
block->needless_since = 0;
block++;
}
// Remove needless marks flag
config->needless_marks = 0;
}