summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/ribmgr.c
blob: 8bb320c01d2bcc894c468abc11f5bfb608d169e6 (plain)
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
/*
 * Ouroboros - Copyright (C) 2016
 *
 * RIB manager of the IPC Process
 *
 *    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 as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 "rib-manager"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/cdap.h>
#include <ouroboros/list.h>

#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#include "ribmgr.h"
#include "dt_const.h"
#include "frct.h"
#include "ipcp.h"

#define ENROLLMENT "enrollment"

extern struct ipcp * _ipcp;

enum cdap_opcode {
        READ = 0,
        WRITE,
        START,
        STOP,
        CREATE,
        DELETE
};

struct cdap_request {
        enum cdap_opcode code;
        char *           name;
        int              invoke_id;
        struct cdap *    instance;

        struct list_head next;
};

struct mgmt_flow {
        struct cdap *    instance;
        int              fd;
        struct list_head next;
};

struct rib {
        struct dt_const  dtc;

        struct list_head flows;
        pthread_rwlock_t flows_lock;

        struct list_head cdap_reqs;
        pthread_mutex_t  cdap_reqs_lock;
} * rib = NULL;

/* Call while holding cdap_reqs_lock */
int cdap_request_add(struct cdap * instance,
                     enum cdap_opcode code,
                     char * name,
                     int invoke_id)
{
        struct cdap_request * req;

        req = malloc(sizeof(*req));
        if (req == NULL)
                return -1;

        req->code = code;
        req->invoke_id = invoke_id;
        req->instance = instance;

        req->name = strdup(name);
        if (req->name == NULL) {
                free(req);
                return -1;
        }

        INIT_LIST_HEAD(&req->next);

        list_add(&req->next, &rib->cdap_reqs);

        return 0;
}

int ribmgr_init()
{
        rib = malloc(sizeof(*rib));
        if (rib == NULL)
                return -1;

        INIT_LIST_HEAD(&rib->flows);
        INIT_LIST_HEAD(&rib->cdap_reqs);

        if (pthread_rwlock_init(&rib->flows_lock, NULL)) {
                LOG_ERR("Failed to initialize rwlock.");
                free(rib);
                return -1;
        }

        if (pthread_mutex_init(&rib->cdap_reqs_lock, NULL)) {
                LOG_ERR("Failed to initialize mutex.");
                free(rib);
                return -1;
        }

        return 0;
}

int ribmgr_fini()
{
        struct list_head * pos = NULL;
        struct list_head * n = NULL;

        pthread_mutex_lock(&rib->cdap_reqs_lock);
        list_for_each_safe(pos, n, &rib->cdap_reqs) {
                struct cdap_request * req =
                        list_entry(pos, struct cdap_request, next);

                free(req->name);
                list_del(&req->next);
                free(req);
        }
        pthread_mutex_unlock(&rib->cdap_reqs_lock);

        pthread_rwlock_wrlock(&rib->flows_lock);
        list_for_each_safe(pos, n, &rib->flows) {
                struct mgmt_flow * flow =
                        list_entry(pos, struct mgmt_flow, next);
                if (cdap_destroy(flow->instance))
                        LOG_ERR("Failed to destroy CDAP instance.");
                list_del(&flow->next);
                free(flow);
        }
        pthread_rwlock_unlock(&rib->flows_lock);

        free(rib);

        return 0;
}

int ribmgr_cdap_reply(struct cdap * instance,
                      int           invoke_id,
                      int           result,
                      buffer_t *    val,
                      size_t        len)
{
        LOG_MISSING;

        /* FIXME: Check all cdap_reqs here to see if we expect a reply */

        return -1;
}

int ribmgr_cdap_read(struct cdap * instance,
                     char *        name)
{
        LOG_MISSING;

        return -1;
}

int ribmgr_cdap_write(struct cdap * instance,
                      char *        name,
                      buffer_t *    val,
                      size_t        len,
                      uint32_t      flags)
{
        LOG_MISSING;

        return -1;
}

int ribmgr_cdap_create(struct cdap * instance,
                       char *        name,
                       buffer_t      val)
{
        LOG_MISSING;

        return -1;
}

int ribmgr_cdap_delete(struct cdap * instance,
                       char *        name,
                       buffer_t      val)
{
        LOG_MISSING;

        return -1;
}

int ribmgr_cdap_start(struct cdap * instance,
                      char *        name)
{
        LOG_MISSING;

        /* FIXME: Handle enrollment request here */

        return -1;
}

int ribmgr_cdap_stop(struct cdap * instance,
                     char *        name)
{
        LOG_MISSING;

        return -1;
}

static struct cdap_ops ribmgr_ops = {
        .cdap_reply  = ribmgr_cdap_reply,
        .cdap_read   = ribmgr_cdap_read,
        .cdap_write  = ribmgr_cdap_write,
        .cdap_create = ribmgr_cdap_create,
        .cdap_delete = ribmgr_cdap_delete,
        .cdap_start  = ribmgr_cdap_start,
        .cdap_stop   = ribmgr_cdap_stop
};

int ribmgr_add_flow(int fd)
{
        struct cdap * instance = NULL;
        struct mgmt_flow * flow;
        int iid = 0;

        flow = malloc(sizeof(*flow));
        if (flow == NULL)
                return -1;

        instance = cdap_create(&ribmgr_ops, fd);
        if (instance == NULL) {
                LOG_ERR("Failed to create CDAP instance");
                free(flow);
                return -1;
        }

        INIT_LIST_HEAD(&flow->next);
        flow->instance = instance;
        flow->fd = fd;

        pthread_rwlock_rdlock(&_ipcp->state_lock);
        pthread_rwlock_wrlock(&rib->flows_lock);
        if (list_empty(&rib->flows) &&
            (_ipcp->state == IPCP_INIT ||
             _ipcp->state == IPCP_DISCONNECTED)) {
                _ipcp->state = IPCP_PENDING_ENROLL;
                pthread_rwlock_unlock(&_ipcp->state_lock);

                pthread_mutex_lock(&rib->cdap_reqs_lock);
                iid = cdap_send_start(instance,
                                      ENROLLMENT);
                if (iid < 0) {
                        pthread_mutex_unlock(&rib->cdap_reqs_lock);
                        pthread_rwlock_unlock(&rib->flows_lock);
                        LOG_ERR("Failed to start enrollment.");
                        cdap_destroy(instance);
                        free(flow);
                        return -1;
                }

                if (cdap_request_add(instance, START, ENROLLMENT, iid)) {
                        pthread_mutex_unlock(&rib->cdap_reqs_lock);
                        pthread_rwlock_unlock(&rib->flows_lock);
                        LOG_ERR("Failed to add CDAP request to list.");
                        cdap_destroy(instance);
                        free(flow);
                        return -1;
                }
                pthread_mutex_unlock(&rib->cdap_reqs_lock);
        }
        pthread_rwlock_unlock(&_ipcp->state_lock);

        list_add(&flow->next, &rib->flows);
        pthread_rwlock_unlock(&rib->flows_lock);

        return 0;
}

int ribmgr_remove_flow(int fd)
{
        struct list_head * pos, * n = NULL;

        pthread_rwlock_wrlock(&rib->flows_lock);
        list_for_each_safe(pos, n, &rib->flows) {
                struct mgmt_flow * flow =
                        list_entry(pos, struct mgmt_flow, next);
                if (flow->fd == fd) {
                        if (cdap_destroy(flow->instance))
                                LOG_ERR("Failed to destroy CDAP instance.");
                        list_del(&flow->next);
                        free(flow);
                        return 0;
                }
        }
        pthread_rwlock_unlock(&rib->flows_lock);

        return -1;
}

int ribmgr_bootstrap(struct dif_config * conf)
{
        if (conf == NULL ||
            conf->type != IPCP_NORMAL) {
                LOG_ERR("Bad DIF configuration.");
                return -1;
        }

        rib->dtc.addr_size = conf->addr_size;
        rib->dtc.cep_id_size  = conf->cep_id_size;
        rib->dtc.pdu_length_size = conf->pdu_length_size;
        rib->dtc.seqno_size = conf->seqno_size;
        rib->dtc.ttl_size = conf->ttl_size;
        rib->dtc.chk_size = conf->chk_size;
        rib->dtc.min_pdu_size = conf->min_pdu_size;
        rib->dtc.max_pdu_size = conf->max_pdu_size;

        if (frct_init(&rib->dtc)) {
                LOG_ERR("Failed to initialize FRCT.");
                return -1;
        }

        LOG_DBG("Bootstrapped RIB Manager.");

        return 0;
}