summaryrefslogtreecommitdiffstats
path: root/ldpd/rlfa.c
blob: 697ec08af86102b202b1470e4c342104cf5fe182 (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
/*
 * Copyright (C) 2020  NetDEF, Inc.
 *                     Renato Westphal
 *
 * 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; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include "ldpd.h"
#include "lde.h"
#include "ldpe.h"
#include "log.h"
#include "ldp_debug.h"
#include "rlfa.h"

#include <lib/log.h>

struct ldp_rlfa_node_head rlfa_node_tree;

static int ldp_rlfa_client_compare(const struct ldp_rlfa_client *a,
				   const struct ldp_rlfa_client *b)
{
	if (a->igp.vrf_id < b->igp.vrf_id)
		return -1;
	if (a->igp.vrf_id > b->igp.vrf_id)
		return 1;

	if (a->igp.protocol < b->igp.protocol)
		return -1;
	if (a->igp.protocol > b->igp.protocol)
		return 1;

	if (a->igp.isis.spf.tree_id < b->igp.isis.spf.tree_id)
		return -1;
	if (a->igp.isis.spf.tree_id > b->igp.isis.spf.tree_id)
		return 1;

	if (a->igp.isis.spf.level < b->igp.isis.spf.level)
		return -1;
	if (a->igp.isis.spf.level > b->igp.isis.spf.level)
		return 1;

	return 0;
}
RB_GENERATE(ldp_rlfa_client_head, ldp_rlfa_client, entry,
	    ldp_rlfa_client_compare)

static int ldp_rlfa_node_compare(const struct ldp_rlfa_node *a,
				 const struct ldp_rlfa_node *b)
{
	if (ntohl(a->pq_address.s_addr) < ntohl(b->pq_address.s_addr))
		return -1;
	if (ntohl(a->pq_address.s_addr) > ntohl(b->pq_address.s_addr))
		return 1;

	return prefix_cmp(&a->destination, &b->destination);
}
RB_GENERATE(ldp_rlfa_node_head, ldp_rlfa_node, entry, ldp_rlfa_node_compare)

struct ldp_rlfa_client *rlfa_client_new(struct ldp_rlfa_node *rnode,
					struct zapi_rlfa_igp *igp)
{
	struct ldp_rlfa_client *rclient;

	if ((rclient = calloc(1, sizeof(*rclient))) == NULL)
		fatal(__func__);

	rclient->igp = *igp;
	rclient->node = rnode;
	RB_INSERT(ldp_rlfa_client_head, &rnode->clients, rclient);

	return rclient;
}

void rlfa_client_del(struct ldp_rlfa_client *rclient)
{
	struct ldp_rlfa_node *rnode = rclient->node;

	RB_REMOVE(ldp_rlfa_client_head, &rnode->clients, rclient);
	free(rclient);

	/* Delete RLFA node if it's empty. */
	if (RB_EMPTY(ldp_rlfa_client_head, &rnode->clients))
		rlfa_node_del(rnode);
}

struct ldp_rlfa_client *rlfa_client_find(struct ldp_rlfa_node *rnode,
					 struct zapi_rlfa_igp *igp)
{
	struct ldp_rlfa_client rclient;

	rclient.igp = *igp;
	return RB_FIND(ldp_rlfa_client_head, &rnode->clients, &rclient);
}

struct ldp_rlfa_node *rlfa_node_new(const struct prefix *destination,
				    struct in_addr pq_address)
{
	struct ldp_rlfa_node *rnode;

	if ((rnode = calloc(1, sizeof(*rnode))) == NULL)
		fatal(__func__);

	rnode->destination = *destination;
	rnode->pq_address = pq_address;
	rnode->pq_label = MPLS_INVALID_LABEL;
	RB_INIT(ldp_rlfa_client_head, &rnode->clients);
	RB_INSERT(ldp_rlfa_node_head, &rlfa_node_tree, rnode);

	return rnode;
}

void rlfa_node_del(struct ldp_rlfa_node *rnode)
{
	/* Delete RLFA clients. */
	while (!RB_EMPTY(ldp_rlfa_client_head, &rnode->clients)) {
		struct ldp_rlfa_client *rclient;

		rclient = RB_ROOT(ldp_rlfa_client_head, &rnode->clients);
		rlfa_client_del(rclient);
	}

	RB_REMOVE(ldp_rlfa_node_head, &rlfa_node_tree, rnode);
	free(rnode);
}

struct ldp_rlfa_node *rlfa_node_find(const struct prefix *destination,
				     struct in_addr pq_address)
{
	struct ldp_rlfa_node rnode = {};

	rnode.destination = *destination;
	rnode.pq_address = pq_address;
	return RB_FIND(ldp_rlfa_node_head, &rlfa_node_tree, &rnode);
}

void lde_rlfa_client_send(struct ldp_rlfa_client *rclient)
{
	struct ldp_rlfa_node		*rnode = rclient->node;
	struct zapi_rlfa_response	 rlfa_labels = {};
	struct fec			 fec;
	struct fec_node			*fn;
	struct fec_nh			*fnh;
	int				 i = 0;

	/* Fill in inner label (allocated by PQ node). */
	rlfa_labels.igp = rclient->igp;
	rlfa_labels.destination = rnode->destination;
	rlfa_labels.pq_label = rnode->pq_label;

	/* Fill in outer label(s) (allocated by the nexthop routers). */
	fec.type = FEC_TYPE_IPV4;
	fec.u.ipv4.prefix = rnode->pq_address;
	fec.u.ipv4.prefixlen = IPV4_MAX_BITLEN;
	fn = (struct fec_node *)fec_find(&ft, &fec);
	if (!fn)
		return;
	LIST_FOREACH(fnh, &fn->nexthops, entry) {
		if (fnh->remote_label == NO_LABEL)
			continue;

		rlfa_labels.nexthops[i].family = fnh->af;
		switch (fnh->af) {
		case AF_INET:
			rlfa_labels.nexthops[i].gate.ipv4 = fnh->nexthop.v4;
			break;
		case AF_INET6:
			rlfa_labels.nexthops[i].gate.ipv6 = fnh->nexthop.v6;
			break;
		default:
			continue;
		}
		rlfa_labels.nexthops[i].label = fnh->remote_label;
		i++;
	}
	rlfa_labels.nexthop_num = i;

	lde_imsg_compose_parent(IMSG_RLFA_LABELS, 0, &rlfa_labels,
				sizeof(rlfa_labels));
}

void lde_rlfa_label_update(const struct fec *fec)
{
	struct ldp_rlfa_node *rnode;

	if (fec->type != FEC_TYPE_IPV4
	    || fec->u.ipv4.prefixlen != IPV4_MAX_BITLEN)
		return;

	/*
	 * TODO: use an rb-tree lookup to restrict the iteration to the RLFAs
	 * that were effectivelly affected by the label update.
	 */
	RB_FOREACH (rnode, ldp_rlfa_node_head, &rlfa_node_tree) {
		struct ldp_rlfa_client *rclient;

		if (!IPV4_ADDR_SAME(&rnode->pq_address, &fec->u.ipv4.prefix))
			continue;

		RB_FOREACH (rclient, ldp_rlfa_client_head, &rnode->clients)
			lde_rlfa_client_send(rclient);
	}
}

void lde_rlfa_check(struct ldp_rlfa_client *rclient)
{
	struct lde_nbr *ln;
	struct lde_map *me;
	struct fec fec;
	union ldpd_addr pq_address = {};

	pq_address.v4 = rclient->node->pq_address;
	ln = lde_nbr_find_by_addr(AF_INET, &pq_address);
	if (!ln)
		return;

	lde_prefix2fec(&rclient->node->destination, &fec);
	me = (struct lde_map *)fec_find(&ln->recv_map, &fec);
	if (!me)
		return;

	rclient->node->pq_label = me->map.label;
	lde_rlfa_client_send(rclient);
}

/*
 * Check if there's any registered RLFA client for this prefix/neighbor (PQ
 * node) and notify about the updated label.
 */
void lde_rlfa_update_clients(struct fec *fec, struct lde_nbr *ln,
			     uint32_t label)
{
	struct prefix		 rlfa_dest;
	struct ldp_rlfa_node	*rnode;

	lde_fec2prefix(fec, &rlfa_dest);
	rnode = rlfa_node_find(&rlfa_dest, ln->id);
	if (rnode) {
		struct ldp_rlfa_client *rclient;

		rnode->pq_label = label;
		RB_FOREACH (rclient, ldp_rlfa_client_head, &rnode->clients)
			lde_rlfa_client_send(rclient);
	} else
		lde_rlfa_label_update(fec);
}

void ldpe_rlfa_init(struct ldp_rlfa_client *rclient)
{
	struct tnbr *tnbr;
	union ldpd_addr pq_address = {};

	pq_address.v4 = rclient->node->pq_address;
	tnbr = tnbr_find(leconf, AF_INET, &pq_address);
	if (tnbr == NULL) {
		tnbr = tnbr_new(AF_INET, &pq_address);
		tnbr_update(tnbr);
		RB_INSERT(tnbr_head, &leconf->tnbr_tree, tnbr);
	}

	tnbr->rlfa_count++;
}

void ldpe_rlfa_exit(struct ldp_rlfa_client *rclient)
{
	struct tnbr *tnbr;
	union ldpd_addr pq_address = {};

	pq_address.v4 = rclient->node->pq_address;
	tnbr = tnbr_find(leconf, AF_INET, &pq_address);
	if (tnbr) {
		tnbr->rlfa_count--;
		tnbr_check(leconf, tnbr);
	}
}