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
|
/*
* Ouroboros - Copyright (C) 2016 - 2017
*
* Undirected graph structure
*
* Dimitri Staessens <dimitri.staessens@ugent.be>
* Sander Vrijders <sander.vrijders@ugent.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#define _POSIX_C_SOURCE 200112L
#define OUROBOROS_PREFIX "graph"
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/list.h>
#include "graph.h"
#include "ipcp.h"
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <limits.h>
struct edge {
struct list_head next;
struct vertex * nb;
qosspec_t qs;
};
struct vertex {
struct list_head next;
uint64_t addr;
struct list_head edges;
};
struct graph {
size_t nr_vertices;
struct list_head vertices;
pthread_mutex_t lock;
};
static struct edge * find_edge_by_addr(struct vertex * vertex,
uint64_t dst_addr)
{
struct list_head * p = NULL;
list_for_each(p, &vertex->edges) {
struct edge * e = list_entry(p, struct edge, next);
if (e->nb->addr == dst_addr)
return e;
}
return NULL;
}
static struct vertex * find_vertex_by_addr(struct graph * graph,
uint64_t addr)
{
struct list_head * p = NULL;
list_for_each(p, &graph->vertices) {
struct vertex * e = list_entry(p, struct vertex, next);
if (e->addr == addr)
return e;
}
return NULL;
}
static struct edge * add_edge(struct vertex * vertex,
struct vertex * nb)
{
struct edge * edge;
edge = malloc(sizeof(*edge));
if (edge == NULL)
return NULL;
list_head_init(&edge->next);
edge->nb = nb;
list_add(&edge->next, &vertex->edges);
return edge;
}
static void del_edge(struct edge * edge)
{
list_del(&edge->next);
free(edge);
}
static struct vertex * add_vertex(struct graph * graph,
uint64_t addr)
{
struct vertex * vertex;
struct list_head * p;
vertex = malloc(sizeof(*vertex));
if (vertex == NULL)
return NULL;
list_head_init(&vertex->next);
list_head_init(&vertex->edges);
vertex->addr = addr;
/* Keep them ordered on address. */
list_for_each(p, &graph->vertices) {
struct vertex * v = list_entry(p, struct vertex, next);
if (v->addr > addr)
break;
}
list_add_tail(&vertex->next, p);
graph->nr_vertices++;
return vertex;
}
static void del_vertex(struct graph * graph,
struct vertex * vertex)
{
struct list_head * p = NULL;
struct list_head * n = NULL;
list_del(&vertex->next);
list_for_each_safe(p, n, &vertex->edges) {
struct edge * e = list_entry(p, struct edge, next);
del_edge(e);
}
free(vertex);
graph->nr_vertices--;
}
struct graph * graph_create(void)
{
struct graph * graph;
graph = malloc(sizeof(*graph));
if (graph == NULL)
return NULL;
if (pthread_mutex_init(&graph->lock, NULL)) {
free(graph);
return NULL;
}
graph->nr_vertices = 0;
list_head_init(&graph->vertices);
return graph;
}
void graph_destroy(struct graph * graph)
{
struct list_head * p = NULL;
struct list_head * n = NULL;
assert(graph);
pthread_mutex_lock(&graph->lock);
list_for_each_safe(p, n, &graph->vertices) {
struct vertex * e = list_entry(p, struct vertex, next);
del_vertex(graph, e);
}
pthread_mutex_unlock(&graph->lock);
pthread_mutex_destroy(&graph->lock);
free(graph);
}
int graph_update_edge(struct graph * graph,
uint64_t s_addr,
uint64_t d_addr,
qosspec_t qs)
{
struct vertex * v;
struct edge * e;
struct vertex * nb;
struct edge * nb_e;
assert(graph);
pthread_mutex_lock(&graph->lock);
v = find_vertex_by_addr(graph, s_addr);
if (v == NULL) {
v = add_vertex(graph, s_addr);
if (v == NULL) {
pthread_mutex_unlock(&graph->lock);
log_err("Failed to add vertex.");
return -ENOMEM;
}
}
nb = find_vertex_by_addr(graph, d_addr);
if (nb == NULL) {
nb = add_vertex(graph, d_addr);
if (nb == NULL) {
if (list_is_empty(&v->edges))
del_vertex(graph, v);
pthread_mutex_unlock(&graph->lock);
log_err("Failed to add vertex.");
return -ENOMEM;
}
}
e = find_edge_by_addr(v, d_addr);
if (e == NULL) {
e = add_edge(v, nb);
if (e == NULL) {
if (list_is_empty(&v->edges))
del_vertex(graph, v);
if (list_is_empty(&nb->edges))
del_vertex(graph, nb);
pthread_mutex_unlock(&graph->lock);
log_err("Failed to add edge.");
return -ENOMEM;
}
}
e->qs = qs;
nb_e = find_edge_by_addr(nb, s_addr);
if (nb_e == NULL) {
nb_e = add_edge(nb, v);
if (nb_e == NULL) {
del_edge(e);
if (list_is_empty(&v->edges))
del_vertex(graph, v);
if (list_is_empty(&nb->edges))
del_vertex(graph, nb);
pthread_mutex_unlock(&graph->lock);
log_err("Failed to add edge.");
return -ENOMEM;
}
}
nb_e->qs = qs;
pthread_mutex_unlock(&graph->lock);
return 0;
}
int graph_del_edge(struct graph * graph,
uint64_t s_addr,
uint64_t d_addr)
{
struct vertex * v;
struct edge * e;
struct vertex * nb;
struct edge * nb_e;
assert(graph);
pthread_mutex_lock(&graph->lock);
v = find_vertex_by_addr(graph, s_addr);
if (v == NULL) {
pthread_mutex_unlock(&graph->lock);
log_err("No such vertex.");
return -1;
}
nb = find_vertex_by_addr(graph, d_addr);
if (nb == NULL) {
pthread_mutex_unlock(&graph->lock);
log_err("No such vertex.");
return -1;
}
e = find_edge_by_addr(v, d_addr);
if (e == NULL) {
pthread_mutex_unlock(&graph->lock);
log_err("No such edge.");
return -1;
}
nb_e = find_edge_by_addr(nb, s_addr);
if (nb_e == NULL) {
pthread_mutex_unlock(&graph->lock);
log_err("No such edge.");
return -1;
}
del_edge(e);
del_edge(nb_e);
/* Removing vertex if it was the last edge */
if (list_is_empty(&v->edges))
del_vertex(graph, v);
if (list_is_empty(&nb->edges))
del_vertex(graph, nb);
pthread_mutex_unlock(&graph->lock);
return 0;
}
static int get_min_vertex(struct graph * graph,
int * dist,
bool * used,
struct vertex ** v)
{
int min = INT_MAX;
int index = -1;
int i = 0;
struct list_head * p = NULL;
*v = NULL;
list_for_each(p, &graph->vertices) {
if (used[i] == true) {
i++;
continue;
}
if (dist[i] < min) {
min = dist[i];
index = i;
*v = list_entry(p, struct vertex, next);
}
i++;
}
if (index != -1)
used[index] = true;
return index;
}
static int get_vertex_number(struct graph * graph,
struct vertex * v)
{
int i = 0;
struct list_head * p = NULL;
list_for_each(p, &graph->vertices) {
struct vertex * vertex = list_entry(p, struct vertex, next);
if (vertex == v)
return i;
i++;
}
return -1;
}
static struct vertex ** dijkstra(struct graph * graph,
uint64_t src)
{
int dist[graph->nr_vertices];
bool used[graph->nr_vertices];
struct list_head * p = NULL;
int i = 0;
int j = 0;
struct vertex * v = NULL;
struct edge * e = NULL;
int alt;
struct vertex ** prev;
prev = malloc(sizeof(*prev) * graph->nr_vertices);
if (prev == NULL)
return NULL;
/* Init the data structures */
list_for_each(p, &graph->vertices) {
v = list_entry(p, struct vertex, next);
if (v->addr == src)
dist[i] = 0;
else
dist[i] = INT_MAX;
prev[i] = NULL;
used[i] = false;
i++;
}
/* Perform actual Dijkstra */
i = get_min_vertex(graph, dist, used, &v);
while (v != NULL) {
list_for_each(p, &v->edges) {
e = list_entry(p, struct edge, next);
j = get_vertex_number(graph, e->nb);
if (j == -1)
continue;
/*
* NOTE: Current weight is just hop count.
* Method could be extended to use a different
* weight for a different QoS cube.
*/
alt = dist[i] + 1;
if (alt < dist[j]) {
dist[j] = alt;
prev[j] = v;
}
}
i = get_min_vertex(graph, dist, used, &v);
}
return prev;
}
static void free_routing_table(struct list_head * table)
{
struct list_head * h;
struct list_head * p;
struct list_head * q;
struct list_head * i;
list_for_each_safe(p, h, table) {
struct routing_table * t =
list_entry(p, struct routing_table, next);
list_for_each_safe(q, i, &t->nhops) {
struct nhop * n =
list_entry(q, struct nhop, next);
list_del(&n->next);
free(n);
}
list_del(&t->next);
free(t);
}
}
void graph_free_routing_table(struct graph * graph,
struct list_head * table)
{
assert(table);
pthread_mutex_lock(&graph->lock);
free_routing_table(table);
pthread_mutex_unlock(&graph->lock);
}
int graph_routing_table(struct graph * graph,
uint64_t s_addr,
struct list_head * table)
{
struct vertex ** prevs;
struct list_head * p;
int i = 0;
int index = 0;
struct vertex * prev;
struct vertex * nhop;
struct vertex * v;
struct routing_table * t;
struct nhop * n;
pthread_mutex_lock(&graph->lock);
/* We need at least 2 vertices for a table */
if (graph->nr_vertices < 2)
goto fail_vertices;
prevs = dijkstra(graph, s_addr);
if (prevs == NULL)
goto fail_vertices;
list_head_init(table);
/*
* Now loop through the list of predecessors
* to construct the routing table
*/
list_for_each(p, &graph->vertices) {
v = list_entry(p, struct vertex, next);
prev = prevs[i];
nhop = v;
/* This is the src */
if (prev == NULL) {
i++;
continue;
}
index = get_vertex_number(graph, prev);
while (prevs[index] != NULL) {
nhop = prev;
prev = prevs[index];
index = get_vertex_number(graph, prev);
}
t = malloc(sizeof(*t));
if (t == NULL)
goto fail_t;
list_head_init(&t->nhops);
n = malloc(sizeof(*n));
if (n == NULL)
goto fail_n;
t->dst = v->addr;
n->nhop = nhop->addr;
list_add(&n->next, &t->nhops);
list_add(&t->next, table);
i++;
}
pthread_mutex_unlock(&graph->lock);
free(prevs);
return 0;
fail_n:
free(t);
fail_t:
free_routing_table(table);
free(prevs);
fail_vertices:
pthread_mutex_unlock(&graph->lock);
return -1;
}
|