summaryrefslogtreecommitdiffstats
path: root/pathd/path_pcep.c
blob: ce631eb7b040933484c13e0c0bfe4f0dfe3a88f3 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2020  NetDEF, Inc.
 *
 * 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 "pceplib/pcep_utils_counters.h"

#include "memory.h"
#include "log.h"
#include "command.h"
#include "libfrr.h"
#include "printfrr.h"
#include "lib/version.h"
#include "northbound.h"
#include "frr_pthread.h"
#include "jhash.h"
#include "termtable.h"

#include "pathd/pathd.h"
#include "pathd/path_errors.h"
#include "pathd/path_pcep.h"
#include "pathd/path_pcep_cli.h"
#include "pathd/path_pcep_controller.h"
#include "pathd/path_pcep_lib.h"
#include "pathd/path_pcep_config.h"
#include "pathd/path_pcep_debug.h"

DEFINE_MTYPE(PATHD, PCEP, "PCEP module");

/*
 * Globals.
 */
static struct pcep_glob pcep_glob_space = {.dbg = {0, "pathd module: pcep"}};
struct pcep_glob *pcep_g = &pcep_glob_space;

/* Main Thread Even Handler */
static int pcep_main_event_handler(enum pcep_main_event_type type, int pcc_id,
				   void *payload);
static int pcep_main_event_start_sync(int pcc_id);
static int pcep_main_event_start_sync_cb(struct path *path, void *arg);
static int pcep_main_event_initiate_candidate(struct path *path);
static int pcep_main_event_update_candidate(struct path *path);
static int pcep_main_event_remove_candidate_segments(const char *originator,
						     bool force);

/* Hook Handlers called from the Main Thread */
static int pathd_candidate_created_handler(struct srte_candidate *candidate);
static int pathd_candidate_updated_handler(struct srte_candidate *candidate);
static int pathd_candidate_removed_handler(struct srte_candidate *candidate);

/* Path manipulation functions */
static struct path_metric *pcep_copy_metrics(struct path_metric *metric);
static struct path_hop *pcep_copy_hops(struct path_hop *hop);

/* Other static functions */
static void notify_status(struct path *path, bool not_changed);

/* Module Functions */
static int pcep_module_finish(void);
static int pcep_module_late_init(struct thread_master *tm);
static int pcep_module_init(void);

/* ------------ Path Helper Functions ------------ */

struct path *pcep_new_path(void)
{
	struct path *path;
	path = XCALLOC(MTYPE_PCEP, sizeof(*path));
	path->binding_sid = MPLS_LABEL_NONE;
	path->enforce_bandwidth = true;
	return path;
}

struct path_hop *pcep_new_hop(void)
{
	struct path_hop *hop;
	hop = XCALLOC(MTYPE_PCEP, sizeof(*hop));
	return hop;
}

struct path_metric *pcep_new_metric(void)
{
	struct path_metric *metric;
	metric = XCALLOC(MTYPE_PCEP, sizeof(*metric));
	return metric;
}

struct path_metric *pcep_copy_metrics(struct path_metric *metric)
{
	if (metric == NULL)
		return NULL;
	struct path_metric *new_metric = pcep_new_metric();
	*new_metric = *metric;
	new_metric->next = pcep_copy_metrics(metric->next);
	return new_metric;
}

struct path_hop *pcep_copy_hops(struct path_hop *hop)
{
	if (hop == NULL)
		return NULL;
	struct path_hop *new_hop = pcep_new_hop();
	*new_hop = *hop;
	new_hop->next = pcep_copy_hops(hop->next);
	return new_hop;
}

struct path *pcep_copy_path(struct path *path)
{
	struct path *new_path = pcep_new_path();

	*new_path = *path;
	new_path->first_metric = pcep_copy_metrics(path->first_metric);
	new_path->first_hop = pcep_copy_hops(path->first_hop);
	if (path->name != NULL)
		new_path->name = XSTRDUP(MTYPE_PCEP, path->name);
	if (path->originator != NULL)
		new_path->originator = XSTRDUP(MTYPE_PCEP, path->originator);
	return new_path;
}

void pcep_free_path(struct path *path)
{
	struct path_hop *hop;
	struct path_metric *metric;
	char *tmp;

	metric = path->first_metric;
	while (metric != NULL) {
		struct path_metric *next = metric->next;
		XFREE(MTYPE_PCEP, metric);
		metric = next;
	}
	hop = path->first_hop;
	while (hop != NULL) {
		struct path_hop *next = hop->next;
		XFREE(MTYPE_PCEP, hop);
		hop = next;
	}
	if (path->originator != NULL) {
		/* The path own the memory, it is const so it is clear it
		shouldn't be modified. XFREE macro do not support type casting
		so we need a temporary variable */
		tmp = (char *)path->originator;
		XFREE(MTYPE_PCEP, tmp);
		path->originator = NULL;
	}
	if (path->name != NULL) {
		/* The path own the memory, it is const so it is clear it
		shouldn't be modified. XFREE macro do not support type casting
		so we need a temporary variable */
		tmp = (char *)path->name;
		XFREE(MTYPE_PCEP, tmp);
		path->name = NULL;
	}
	XFREE(MTYPE_PCEP, path);
}

/* ------------ Other Static Functions ------------ */

void notify_status(struct path *path, bool not_changed)
{
	struct path *resp = NULL;

	if ((resp = path_pcep_config_get_path(&path->nbkey))) {
		resp->srp_id = path->srp_id;
		flog_warn(EC_PATH_PCEP_RECOVERABLE_INTERNAL_ERROR,
			  "(%s) Send report for candidate path %s", __func__,
			  path->name);
		pcep_ctrl_send_report(pcep_g->fpt, path->pcc_id, resp,
				      not_changed);
	}
}

/* ------------ Main Thread Even Handler ------------ */

int pcep_main_event_handler(enum pcep_main_event_type type, int pcc_id,
			    void *payload)
{
	int ret = 0;

	switch (type) {
	case PCEP_MAIN_EVENT_START_SYNC:
		ret = pcep_main_event_start_sync(pcc_id);
		break;
	case PCEP_MAIN_EVENT_INITIATE_CANDIDATE:
		assert(payload != NULL);
		ret = pcep_main_event_initiate_candidate(
			(struct path *)payload);
		break;
	case PCEP_MAIN_EVENT_UPDATE_CANDIDATE:
		assert(payload != NULL);
		ret = pcep_main_event_update_candidate((struct path *)payload);
		break;
	case PCEP_MAIN_EVENT_REMOVE_CANDIDATE_LSP:
		ret = pcep_main_event_remove_candidate_segments(
			(const char *)payload, true);
		break;
	default:
		flog_warn(EC_PATH_PCEP_RECOVERABLE_INTERNAL_ERROR,
			  "Unexpected event received in the main thread: %u",
			  type);
		break;
	}

	return ret;
}

int pcep_main_event_start_sync(int pcc_id)
{
	path_pcep_config_list_path(pcep_main_event_start_sync_cb, &pcc_id);
	pcep_ctrl_sync_done(pcep_g->fpt, pcc_id);
	return 0;
}

int pcep_main_event_start_sync_cb(struct path *path, void *arg)
{
	int *pcc_id = (int *)arg;
	pcep_ctrl_sync_path(pcep_g->fpt, *pcc_id, path);
	return 1;
}

int pcep_main_event_initiate_candidate(struct path *path)
{
	int ret = 0;

	ret = path_pcep_config_initiate_path(path);
	if (path->do_remove) {
		struct pcep_error *error;
		error = XCALLOC(MTYPE_PCEP, sizeof(*error));
		error->path = path;
		error->error_type = PCEP_ERRT_INVALID_OPERATION;
		switch (ret) {
		case ERROR_19_1:
			error->error_value =
				PCEP_ERRV_LSP_UPDATE_FOR_NON_DELEGATED_LSP;
			break;
		case ERROR_19_3:
			error->error_value =
				PCEP_ERRV_LSP_UPDATE_UNKNOWN_PLSP_ID;
			break;
		case ERROR_19_9:
			error->error_value = PCEP_ERRV_LSP_NOT_PCE_INITIATED;
			break;
		default:
			zlog_warn("(%s)PCE tried to REMOVE unknown error!",
				  __func__);
			XFREE(MTYPE_PCEP, error);
			pcep_free_path(path);
			return ret;
			break;
		}
		pcep_ctrl_send_error(pcep_g->fpt, path->pcc_id, error);
	} else if (ret != PATH_NB_ERR && path->srp_id != 0)
		notify_status(path, ret == PATH_NB_NO_CHANGE);
	return ret;
}

int pcep_main_event_update_candidate(struct path *path)
{
	int ret = 0;

	ret = path_pcep_config_update_path(path);
	if (ret != PATH_NB_ERR && path->srp_id != 0)
		notify_status(path, ret == PATH_NB_NO_CHANGE);
	return ret;
}

int pcep_main_event_remove_candidate_segments(const char *originator,
					      bool force)
{
	srte_candidate_unset_segment_list(originator, force);
	/* Avoid compiler warnings about const char* */
	void *free_ptr = (void *)originator;
	XFREE(MTYPE_PCEP, free_ptr);

	srte_apply_changes();

	return 0;
}

/* ------------ Hook Handlers Functions Called From Main Thread ------------ */

int pathd_candidate_created_handler(struct srte_candidate *candidate)
{
	struct path *path = candidate_to_path(candidate);
	int ret = pcep_ctrl_pathd_event(pcep_g->fpt, PCEP_PATH_CREATED, path);
	return ret;
}

int pathd_candidate_updated_handler(struct srte_candidate *candidate)
{
	struct path *path = candidate_to_path(candidate);
	int ret = pcep_ctrl_pathd_event(pcep_g->fpt, PCEP_PATH_UPDATED, path);
	return ret;
}

int pathd_candidate_removed_handler(struct srte_candidate *candidate)
{
	struct path *path = candidate_to_path(candidate);
	int ret = pcep_ctrl_pathd_event(pcep_g->fpt, PCEP_PATH_REMOVED, path);
	return ret;
}


/* ------------ Module Functions ------------ */

/* this creates threads, therefore must run after fork().  but it must also
 * run before config load, so the CLI commands don't try to touch things that
 * aren't set up yet...
 */
static int pcep_module_config_pre(struct thread_master *tm)
{
	assert(pcep_g->fpt == NULL);
	assert(pcep_g->master == NULL);

	struct frr_pthread *fpt;

	if (pcep_ctrl_initialize(tm, &fpt, pcep_main_event_handler))
		return 1;

	if (pcep_lib_initialize(fpt))
		return 1;

	pcep_g->master = tm;
	pcep_g->fpt = fpt;

	return 0;
}

static int pcep_module_late_init(struct thread_master *tm)
{
	hook_register(pathd_candidate_created, pathd_candidate_created_handler);
	hook_register(pathd_candidate_updated, pathd_candidate_updated_handler);
	hook_register(pathd_candidate_removed, pathd_candidate_removed_handler);

	hook_register(frr_config_pre, pcep_module_config_pre);
	hook_register(frr_fini, pcep_module_finish);

	pcep_cli_init();

	return 0;
}

int pcep_module_finish(void)
{
	pcep_ctrl_finalize(&pcep_g->fpt);
	pcep_lib_finalize();

	for (int i = 0; i < MAX_PCC; i++)
		if (pcep_g->pce_opts_cli[i] != NULL)
			XFREE(MTYPE_PCEP, pcep_g->pce_opts_cli[i]);

	return 0;
}

int pcep_module_init(void)
{
	pcep_g->num_pce_opts_cli = 0;
	for (int i = 0; i < MAX_PCE; i++)
		pcep_g->pce_opts_cli[i] = NULL;
	pcep_g->num_config_group_opts = 0;
	for (int i = 0; i < MAX_PCE; i++)
		pcep_g->config_group_opts[i] = NULL;

	hook_register(frr_late_init, pcep_module_late_init);
	return 0;
}

FRR_MODULE_SETUP(.name = "frr_pathd_pcep", .version = FRR_VERSION,
		 .description = "FRR pathd PCEP module",
		 .init = pcep_module_init,
);