summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/ribmgr.c
blob: ec465c6b3aaa956b9ca21a6c79e4b59a45561624 (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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * RIB manager of the IPC Process
 *
 *    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., 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 <ouroboros/time_utils.h>
#include <ouroboros/ipcp-dev.h>
#include <ouroboros/errno.h>
#include <ouroboros/dev.h>
#include <ouroboros/fqueue.h>
#include <ouroboros/rib.h>

#include "ae.h"
#include "gam.h"
#include "ribconfig.h"
#include "ribmgr.h"

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

struct {
        flow_set_t *       fs;
        fqueue_t *         fq;

        struct gam *       gam;
        struct nbs *       nbs;
        struct ae *        ae;

        struct nb_notifier nb_notifier;
} ribmgr;

static int ribmgr_neighbor_event(enum nb_event event,
                                 struct conn   conn)
{
        /* We are only interested in neighbors being added and removed. */
        switch (event) {
        case NEIGHBOR_ADDED:
                flow_set_add(ribmgr.fs, conn.flow_info.fd);
                break;
        case NEIGHBOR_REMOVED:
                flow_set_del(ribmgr.fs, conn.flow_info.fd);
                break;
        default:
                break;
        }

        return 0;
}

int ribmgr_init(void)
{
        enum pol_gam     pg;
        struct conn_info info;

        strcpy(info.ae_name, MGMT_AE);
        strcpy(info.protocol, CDAP_PROTO);
        info.pref_version = 1;
        info.pref_syntax = PROTO_GPB;

        ribmgr.nbs = nbs_create();
        if (ribmgr.nbs == NULL) {
                log_err("Failed to create neighbors.");
                return -1;
        }

        ribmgr.ae = connmgr_ae_create(info);
        if (ribmgr.ae == NULL) {
                log_err("Failed to create AE struct.");
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        if (rib_read(BOOT_PATH "/rm/gam/type", &pg, sizeof(pg))
            != sizeof(pg)) {
                log_err("Failed to read policy for ribmgr gam.");
                connmgr_ae_destroy(ribmgr.ae);
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        ribmgr.gam = gam_create(pg, ribmgr.nbs, ribmgr.ae);
        if (ribmgr.gam == NULL) {
                log_err("Failed to create gam.");
                connmgr_ae_destroy(ribmgr.ae);
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        ribmgr.fs = flow_set_create();
        if (ribmgr.fs == NULL) {
                log_err("Failed to create flow set.");
                gam_destroy(ribmgr.gam);
                connmgr_ae_destroy(ribmgr.ae);
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        ribmgr.fq = fqueue_create();
        if (ribmgr.fq == NULL) {
                log_err("Failed to create fq.");
                flow_set_destroy(ribmgr.fs);
                gam_destroy(ribmgr.gam);
                connmgr_ae_destroy(ribmgr.ae);
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        ribmgr.nb_notifier.notify_call = ribmgr_neighbor_event;
        if (nbs_reg_notifier(ribmgr.nbs, &ribmgr.nb_notifier)) {
                log_err("Failed to register notifier.");
                fqueue_destroy(ribmgr.fq);
                flow_set_destroy(ribmgr.fs);
                gam_destroy(ribmgr.gam);
                connmgr_ae_destroy(ribmgr.ae);
                nbs_destroy(ribmgr.nbs);
                return -1;
        }

        return 0;
}

void ribmgr_fini(void)
{
        nbs_unreg_notifier(ribmgr.nbs, &ribmgr.nb_notifier);
        flow_set_destroy(ribmgr.fs);
        fqueue_destroy(ribmgr.fq);
        gam_destroy(ribmgr.gam);
        connmgr_ae_destroy(ribmgr.ae);
        nbs_destroy(ribmgr.nbs);
}

int ribmgr_disseminate(char *           path,
                       enum diss_target target,
                       enum diss_freq   freq,
                       size_t           delay)
{
        (void) path;
        (void) target;
        (void) freq;
        (void) delay;

        return 0;
}