summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/fmgr.c
blob: 437dac134653eb69eaddc52882d3a40aa2e06aec (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
/*
 * Ouroboros - Copyright (C) 2016
 *
 * Flow 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 "flow-manager"

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

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

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

extern struct ipcp * _ipcp;

struct n_1_flow {
        int fd;
        char * ae_name;
        struct list_head next;
};

struct fmgr {
        pthread_t listen_thread;

        struct list_head n_1_flows;
        pthread_mutex_t n_1_flows_lock;

} * fmgr = NULL;

static int add_n_1_fd(int fd,
                      char * ae_name)
{
        struct n_1_flow * tmp;

        if (ae_name == NULL)
                return -1;

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

        tmp->fd = fd;
        tmp->ae_name = ae_name;

        pthread_mutex_lock(&fmgr->n_1_flows_lock);
        list_add(&tmp->next, &fmgr->n_1_flows);
        pthread_mutex_unlock(&fmgr->n_1_flows_lock);

        return 0;
}

static void * fmgr_listen(void * o)
{
        int fd;
        char * ae_name;
        bool bound = false;

        while (true) {
                pthread_mutex_lock(&_ipcp->state_lock);
                while (!(_ipcp->state == IPCP_ENROLLED ||
                         _ipcp->state == IPCP_SHUTDOWN))
                        pthread_cond_wait(&_ipcp->state_cond,
                                          &_ipcp->state_lock);

                if (_ipcp->state == IPCP_SHUTDOWN) {
                        pthread_mutex_unlock(&_ipcp->state_lock);
                        return 0;
                }
                pthread_mutex_unlock(&_ipcp->state_lock);

                if (!bound && api_bind(_ipcp->data->dif_name) < 0) {
                        LOG_ERR("Failed to bind the server instance.");
                        pthread_mutex_unlock(&_ipcp->state_lock);
                        return (void *) -1;
                }

                bound = true;

                fd = flow_accept(&ae_name);
                if (fd < 0) {
                        LOG_ERR("Failed to accept flow.");
                        continue;
                }

                if (!(strcmp(ae_name, MGMT_AE) == 0 ||
                      strcmp(ae_name, DT_AE) == 0)) {
                        if (flow_alloc_resp(fd, -1))
                                LOG_ERR("Failed to reply to flow allocation.");
                        flow_dealloc(fd);
                        continue;
                }

                if (flow_alloc_resp(fd, 0)) {
                        LOG_ERR("Failed to reply to flow allocation.");
                        flow_dealloc(fd);
                        continue;
                }

                LOG_DBG("Accepted new flow allocation request for AE %s.",
                        ae_name);

                if (strcmp(ae_name, MGMT_AE) == 0) {
                        if (ribmgr_add_flow(fd)) {
                                LOG_ERR("Failed to hand fd to RIB.");
                                flow_dealloc(fd);
                                continue;
                        }
                }

                if (strcmp(ae_name, DT_AE) == 0) {
                        if (frct_dt_flow(fd)) {
                                LOG_ERR("Failed to hand fd to FRCT.");
                                flow_dealloc(fd);
                                continue;
                        }
                }

                if (add_n_1_fd(fd, ae_name)) {
                        LOG_ERR("Failed to add file descriptor to list.");
                        flow_dealloc(fd);
                        continue;
                }
        }

        return (void *) 0;
}

int fmgr_init()
{
        fmgr = malloc(sizeof(*fmgr));
        if (fmgr == NULL)
                return -1;

        INIT_LIST_HEAD(&fmgr->n_1_flows);

        pthread_mutex_init(&fmgr->n_1_flows_lock, NULL);

        pthread_create(&fmgr->listen_thread,
                       NULL,
                       fmgr_listen,
                       NULL);

        return 0;
}

int fmgr_fini()
{
        struct list_head * pos = NULL;

        pthread_cancel(fmgr->listen_thread);

        pthread_join(fmgr->listen_thread,
                     NULL);

        list_for_each(pos, &fmgr->n_1_flows) {
                struct n_1_flow * e =
                        list_entry(pos, struct n_1_flow, next);
                if (e->ae_name != NULL)
                        free(e->ae_name);
                if (ribmgr_remove_flow(e->fd))
                    LOG_ERR("Failed to remove management flow.");
        }

        free(fmgr);

        return 0;
}

int fmgr_mgmt_flow(char * dst_name)
{
        int fd;
        int result;

        /* FIXME: Request retransmission. */
        fd = flow_alloc(dst_name, MGMT_AE, NULL);
        if (fd < 0) {
                LOG_ERR("Failed to allocate flow to %s", dst_name);
                return -1;
        }

        result = flow_alloc_res(fd);
        if (result < 0) {
                LOG_ERR("Result of flow allocation to %s is %d",
                        dst_name, result);
                return -1;
        }

        if (ribmgr_add_flow(fd)) {
                LOG_ERR("Failed to hand file descriptor to RIB manager");
                flow_dealloc(fd);
                return -1;
        }

        if (add_n_1_fd(fd, strdup(MGMT_AE))) {
                LOG_ERR("Failed to add file descriptor to list.");
                flow_dealloc(fd);
                return -1;
        }

        return 0;
}

int fmgr_dt_flow(char * dst_name)
{
        LOG_MISSING;

        return -1;
}

int fmgr_flow_alloc(pid_t         n_api,
                    int           port_id,
                    char *        dst_ap_name,
                    char *        src_ae_name,
                    enum qos_cube qos)
{
        LOG_MISSING;

        return -1;
}

int fmgr_flow_alloc_resp(pid_t n_api,
                         int   port_id,
                         int   response)
{
        LOG_MISSING;

        return -1;
}

int fmgr_flow_dealloc(int port_id)
{
        LOG_MISSING;

        return -1;
}