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
|
/*
* Ouroboros - Copyright (C) 2016 - 2017
*
* Graph adjacency manager for IPC Process components
*
* Dimitri Staessens <dimitri.staessens@intec.ugent.be>
* Sander Vrijders <sander.vrijders@intec.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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define OUROBOROS_PREFIX "graph-adjacency-manager"
#include <ouroboros/config.h>
#include <ouroboros/cdap.h>
#include <ouroboros/dev.h>
#include <ouroboros/logs.h>
#include <ouroboros/list.h>
#include <ouroboros/errno.h>
#include <ouroboros/rib.h>
#include "ipcp.h"
#include "gam.h"
#include "pol-gam-ops.h"
#include "pol/complete.h"
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct ga {
struct list_head next;
qosspec_t qs;
int fd;
struct conn_info * info;
};
struct gam {
struct list_head gas;
pthread_mutex_t gas_lock;
pthread_cond_t gas_cond;
struct pol_gam_ops * ops;
void * ops_o;
};
struct gam * gam_create(enum pol_gam gam_type)
{
struct gam * tmp;
tmp = malloc(sizeof(*tmp));
if (tmp == NULL)
return NULL;
switch (gam_type) {
case COMPLETE:
tmp->ops = &complete_ops;
break;
default:
log_err("Unknown gam policy: %d.", gam_type);
free(tmp);
return NULL;
}
list_head_init(&tmp->gas);
if (pthread_mutex_init(&tmp->gas_lock, NULL)) {
free(tmp);
return NULL;
}
if (pthread_cond_init(&tmp->gas_cond, NULL)) {
pthread_mutex_destroy(&tmp->gas_lock);
free(tmp);
return NULL;
}
tmp->ops_o = tmp->ops->create(tmp);
if (tmp->ops_o == NULL) {
pthread_cond_destroy(&tmp->gas_cond);
pthread_mutex_destroy(&tmp->gas_lock);
free(tmp);
return NULL;
}
if (tmp->ops->start(tmp->ops_o)) {
pthread_cond_destroy(&tmp->gas_cond);
pthread_mutex_destroy(&tmp->gas_lock);
free(tmp);
return NULL;
}
return tmp;
}
void gam_destroy(struct gam * instance)
{
struct list_head * p = NULL;
struct list_head * n = NULL;
assert(instance);
instance->ops->stop(instance->ops_o);
pthread_mutex_lock(&instance->gas_lock);
list_for_each_safe(p, n, &instance->gas) {
struct ga * e = list_entry(p, struct ga, next);
list_del(&e->next);
free(e->info);
free(e);
}
pthread_mutex_unlock(&instance->gas_lock);
pthread_mutex_destroy(&instance->gas_lock);
pthread_cond_destroy(&instance->gas_cond);
instance->ops->destroy(instance->ops_o);
free(instance);
}
static int add_ga(struct gam * instance,
int fd,
qosspec_t qs,
struct conn_info * info)
{
struct ga * ga;
ga = malloc(sizeof(*ga));
if (ga == NULL)
return -ENOMEM;
ga->fd = fd;
ga->info = info;
ga->qs = qs;
list_head_init(&ga->next);
pthread_mutex_lock(&instance->gas_lock);
list_add(&ga->next, &instance->gas);
pthread_cond_signal(&instance->gas_cond);
pthread_mutex_unlock(&instance->gas_lock);
log_info("Added flow.");
return 0;
}
int gam_flow_arr(struct gam * instance,
int fd,
qosspec_t qs)
{
struct conn_info * rcv_info;
struct conn_info snd_info;
if (flow_alloc_resp(fd, instance->ops->accept_new_flow(instance->ops_o))
< 0) {
log_err("Could not respond to new flow.");
return -1;
}
rcv_info = malloc(sizeof(*rcv_info));
if (rcv_info == NULL)
return -ENOMEM;
memset(&snd_info, 0, sizeof(snd_info));
memset(rcv_info, 0, sizeof(*rcv_info));
/* FIXME: send correct AE */
strcpy(snd_info.ae_name, "FIXME:CORRECT_AE");
strcpy(snd_info.protocol, CDAP_PROTO);
snd_info.pref_version = 1;
snd_info.pref_syntax = PROTO_GPB;
snd_info.ae.addr = ipcpi.address;
if (cacep_rcv(fd, rcv_info)) {
log_err("Error establishing application connection.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (cacep_snd(fd, &snd_info)) {
log_err("Failed to respond to application connection request.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (strcmp(snd_info.ae_name, rcv_info->ae_name)) {
log_err("Received connection for wrong AE.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (strcmp(snd_info.protocol, rcv_info->protocol) ||
snd_info.pref_version != rcv_info->pref_version ||
snd_info.pref_syntax != rcv_info->pref_syntax) {
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (instance->ops->accept_flow(instance->ops_o, qs, rcv_info)) {
flow_dealloc(fd);
free(rcv_info);
return 0;
}
if (add_ga(instance, fd, qs, rcv_info)) {
log_err("Failed to add ga to graph adjacency manager list.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
return 0;
}
int gam_flow_alloc(struct gam * instance,
char * dst_name,
qosspec_t qs)
{
struct conn_info * rcv_info;
struct conn_info snd_info;
int fd;
log_dbg("Allocating flow to %s.", dst_name);
rcv_info = malloc(sizeof(*rcv_info));
if (rcv_info == NULL)
return -ENOMEM;
fd = flow_alloc(dst_name, NULL);
if (fd < 0) {
log_err("Failed to allocate flow to %s.", dst_name);
return -1;
}
if (flow_alloc_res(fd)) {
log_err("Flow allocation to %s failed.", dst_name);
flow_dealloc(fd);
return -1;
}
memset(&snd_info, 0, sizeof(snd_info));
memset(rcv_info, 0, sizeof(*rcv_info));
/* FIXME: send correct AE */
strcpy(snd_info.ae_name, "FIXME:CORRECT_AE");
strcpy(snd_info.protocol, CDAP_PROTO);
snd_info.pref_version = 1;
snd_info.pref_syntax = PROTO_GPB;
snd_info.ae.addr = ipcpi.address;
if (cacep_snd(fd, &snd_info)) {
log_err("Failed to create application connection.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (cacep_rcv(fd, rcv_info)) {
log_err("Failed to connect to application.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (strcmp(snd_info.protocol, rcv_info->protocol) ||
snd_info.pref_version != rcv_info->pref_version ||
snd_info.pref_syntax != rcv_info->pref_syntax) {
flow_dealloc(fd);
free(rcv_info);
return -1;
}
if (instance->ops->accept_flow(instance->ops_o, qs, rcv_info)) {
flow_dealloc(fd);
free(rcv_info);
return 0;
}
if (add_ga(instance, fd, qs, rcv_info)) {
log_err("Failed to add GA to graph adjacency manager list.");
flow_dealloc(fd);
free(rcv_info);
return -1;
}
return 0;
}
int gam_flow_wait(struct gam * instance,
int * fd,
struct conn_info ** info,
qosspec_t * qs)
{
struct ga * ga;
assert(fd);
assert(info);
assert(qs);
pthread_mutex_lock(&instance->gas_lock);
pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
(void *) &instance->gas_lock);
while (list_is_empty(&instance->gas))
pthread_cond_wait(&instance->gas_cond, &instance->gas_lock);
ga = list_first_entry((&instance->gas), struct ga, next);
if (ga == NULL) {
pthread_mutex_unlock(&instance->gas_lock);
return -1;
}
*fd = ga->fd;
*info = ga->info;
*qs = ga->qs;
list_del(&ga->next);
free(ga);
pthread_cleanup_pop(true);
return 0;
}
|