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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/*
* Label Manager for FRR
*
* Copyright (C) 2017 by Bingen Eguzkitza,
* Volta Networks Inc.
*
* This file is part of FreeRangeRouting (FRR)
*
* FRR 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, or (at your option) any
* later version.
*
* FRR 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 <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "lib/log.h"
#include "lib/memory.h"
#include "lib/mpls.h"
#include "lib/network.h"
#include "lib/stream.h"
#include "lib/zclient.h"
#include "lib/libfrr.h"
#include "zebra/zserv.h"
#include "zebra/label_manager.h"
#include "zebra/zebra_errors.h"
#define CONNECTION_DELAY 5
struct label_manager lbl_mgr;
extern struct zebra_privs_t zserv_privs;
DEFINE_MGROUP(LBL_MGR, "Label Manager");
DEFINE_MTYPE_STATIC(LBL_MGR, LM_CHUNK, "Label Manager Chunk");
/* In case this zebra daemon is not acting as label manager,
* it will be a proxy to relay messages to external label manager
* This zclient thus is to connect to it
*/
static struct stream *obuf;
static struct zclient *zclient;
bool lm_is_external;
static void delete_label_chunk(void *val)
{
XFREE(MTYPE_LM_CHUNK, val);
}
static int relay_response_back(void)
{
int ret = 0;
struct stream *src, *dst;
uint16_t size = 0;
uint8_t marker;
uint8_t version;
vrf_id_t vrf_id;
uint16_t resp_cmd;
uint8_t proto;
const char *proto_str;
unsigned short instance;
struct zserv *zserv;
/* input buffer with msg from label manager */
src = zclient->ibuf;
stream_reset(src);
/* parse header */
ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
&vrf_id, &resp_cmd);
if (ret < 0 && errno != EAGAIN) {
flog_err(EC_ZEBRA_LM_RESPONSE,
"Error reading Label Manager response: %s",
strerror(errno));
return -1;
}
/* do not relay a msg that has nothing to do with LM */
switch (resp_cmd) {
case ZEBRA_LABEL_MANAGER_CONNECT:
case ZEBRA_LABEL_MANAGER_CONNECT_ASYNC: /* should not be seen */
case ZEBRA_GET_LABEL_CHUNK:
case ZEBRA_RELEASE_LABEL_CHUNK:
break;
default:
zlog_debug("Not relaying '%s' response (size %d) from LM",
zserv_command_string(resp_cmd), size);
return -1;
}
zlog_debug("Received '%s' response (size %d) from LM",
zserv_command_string(resp_cmd), size);
if (size == 0)
return -1;
/* Get the 'proto' field of the message */
proto = stream_getc(src);
/* Get the 'instance' field of the message */
instance = stream_getw(src);
proto_str = zebra_route_string(proto);
/* lookup the client to relay the msg to */
zserv = zserv_find_client(proto, instance);
if (!zserv) {
flog_err(
EC_ZEBRA_LM_NO_SUCH_CLIENT,
"Error relaying LM response: can't find client %s, instance %u",
proto_str, instance);
return -1;
}
zlog_debug("Found client to relay LM response to client %s instance %u",
proto_str, instance);
/* copy msg into output buffer */
dst = obuf;
stream_copy(dst, src);
/* send response back */
ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
if (ret <= 0) {
flog_err(EC_ZEBRA_LM_RELAY_FAILED,
"Error relaying LM response to %s instance %u: %s",
proto_str, instance, strerror(errno));
return -1;
}
zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret,
proto_str, instance);
return 0;
}
static int lm_zclient_read(struct thread *t)
{
int ret;
zclient->t_read = NULL;
/* read response and send it back */
ret = relay_response_back();
/* on error, schedule another read */
if (ret == -1)
if (!zclient->t_read)
thread_add_read(zclient->master, lm_zclient_read, NULL,
zclient->sock, &zclient->t_read);
return ret;
}
static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
{
int ret;
struct stream *s;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, cmd, vrf_id);
/* proto */
stream_putc(s, zserv->proto);
/* instance */
stream_putw(s, zserv->instance);
/* result */
stream_putc(s, 1);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
ret = writen(zserv->sock, s->data, stream_get_endp(s));
stream_free(s);
return ret;
}
/**
* Receive a request to get or release a label chunk and forward it to external
* label manager.
*
* It's called from zserv in case it's not an actual label manager, but just a
* proxy.
*
* @param cmd Type of request (connect, get or release)
* @param zserv
* @return 0 on success, -1 otherwise
*/
int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
struct stream *msg, vrf_id_t vrf_id)
{
struct stream *dst;
int ret = 0;
uint8_t proto;
const char *proto_str;
unsigned short instance;
if (zclient->sock < 0) {
flog_err(EC_ZEBRA_LM_NO_SOCKET,
"Unable to relay LM request: no socket");
reply_error(cmd, zserv, vrf_id);
return -1;
}
/* peek msg to get proto and instance id. This zebra, which acts as
* a proxy needs to have such values for each client in order to
* relay responses back to it.
*/
/* Get the 'proto' field of incoming msg */
proto = stream_getc(msg);
/* Get the 'instance' field of incoming msg */
instance = stream_getw(msg);
/* stringify proto */
proto_str = zebra_route_string(proto);
/* check & set client proto if unset */
if (zserv->proto && zserv->proto != proto) {
flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH,
"Client proto(%u) != msg proto(%u)", zserv->proto,
proto);
return -1;
}
/* check & set client instance if unset */
if (zserv->instance && zserv->instance != instance) {
flog_err(EC_ZEBRA_LM_BAD_INSTANCE,
"Client instance(%u) != msg instance(%u)",
zserv->instance, instance);
return -1;
}
/* recall proto and instance */
zserv->instance = instance;
zserv->proto = proto;
/* in case there's any incoming message enqueued, read and forward it */
if (zserv->is_synchronous)
while (ret == 0)
ret = relay_response_back();
/* get the msg buffer used toward the 'master' Label Manager */
dst = zclient->obuf;
/* copy the message */
stream_copy(dst, msg);
/* Send request to external label manager */
ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
if (ret <= 0) {
flog_err(EC_ZEBRA_LM_RELAY_FAILED,
"Error relaying LM request from %s instance %u: %s",
proto_str, instance, strerror(errno));
reply_error(cmd, zserv, vrf_id);
return -1;
}
zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret,
proto_str, instance);
/* Release label chunk has no response */
if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
return 0;
/* make sure we listen to the response */
if (!zclient->t_read)
thread_add_read(zclient->master, lm_zclient_read, NULL,
zclient->sock, &zclient->t_read);
return 0;
}
static int lm_zclient_connect(struct thread *t)
{
zclient->t_connect = NULL;
if (zclient->sock >= 0)
return 0;
if (zclient_socket_connect(zclient) < 0) {
flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED,
"Error connecting synchronous zclient!");
thread_add_timer(zebrad.master, lm_zclient_connect, zclient,
CONNECTION_DELAY, &zclient->t_connect);
return -1;
}
/* make socket non-blocking */
(void)set_nonblocking(zclient->sock);
return 0;
}
/**
* Function to initialize zclient in case this is not an actual
* label manager, but just a proxy to an external one.
*
* @param lm_zserv_path Path to zserv socket of external label manager
*/
static void lm_zclient_init(char *lm_zserv_path)
{
if (lm_zserv_path)
frr_zclient_addr(&zclient_addr, &zclient_addr_len,
lm_zserv_path);
/* Set default values. */
zclient = zclient_new_notify(zebrad.master, &zclient_options_default);
zclient->privs = &zserv_privs;
zclient->sock = -1;
zclient->t_connect = NULL;
lm_zclient_connect(NULL);
}
/**
* Release label chunks from a client.
*
* Called on client disconnection or reconnection. It only releases chunks
* with empty keep value.
*
* @param proto Daemon protocol of client, to identify the owner
* @param instance Instance, to identify the owner
* @return Number of chunks released
*/
int release_daemon_label_chunks(struct zserv *client)
{
uint8_t proto = client->proto;
uint16_t instance = client->instance;
struct listnode *node;
struct label_manager_chunk *lmc;
int count = 0;
int ret;
for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
if (lmc->proto == proto && lmc->instance == instance
&& lmc->keep == 0) {
ret = release_label_chunk(lmc->proto, lmc->instance,
lmc->start, lmc->end);
if (ret == 0)
count++;
}
}
zlog_debug("%s: Released %d label chunks", __func__, count);
return count;
}
/**
* Init label manager (or proxy to an external one)
*/
void label_manager_init(char *lm_zserv_path)
{
/* this is an actual label manager */
if (!lm_zserv_path) {
zlog_debug("Initializing internal label manager");
lm_is_external = false;
lbl_mgr.lc_list = list_new();
lbl_mgr.lc_list->del = delete_label_chunk;
} else { /* it's acting just as a proxy */
zlog_debug("Initializing external label manager at %s",
lm_zserv_path);
lm_is_external = true;
lm_zclient_init(lm_zserv_path);
}
obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
hook_register(zserv_client_close, release_daemon_label_chunks);
}
/**
* Core function, assigns label cunks
*
* It first searches through the list to check if there's one available
* (previously released). Otherwise it creates and assigns a new one
*
* @param proto Daemon protocol of client, to identify the owner
* @param instance Instance, to identify the owner
* @param keep If set, avoid garbage collection
* @para size Size of the label chunk
* @return Pointer to the assigned label chunk
*/
struct label_manager_chunk *assign_label_chunk(uint8_t proto,
unsigned short instance,
uint8_t keep, uint32_t size)
{
struct label_manager_chunk *lmc;
struct listnode *node;
/* first check if there's one available */
for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
if (lmc->proto == NO_PROTO
&& lmc->end - lmc->start + 1 == size) {
lmc->proto = proto;
lmc->instance = instance;
lmc->keep = keep;
return lmc;
}
}
/* otherwise create a new one */
lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
if (list_isempty(lbl_mgr.lc_list))
lmc->start = MPLS_LABEL_UNRESERVED_MIN;
else
lmc->start = ((struct label_manager_chunk *)listgetdata(
listtail(lbl_mgr.lc_list)))
->end
+ 1;
if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS,
"Reached max labels. Start: %u, size: %u", lmc->start,
size);
XFREE(MTYPE_LM_CHUNK, lmc);
return NULL;
}
lmc->end = lmc->start + size - 1;
lmc->proto = proto;
lmc->instance = instance;
lmc->keep = keep;
listnode_add(lbl_mgr.lc_list, lmc);
return lmc;
}
/**
* Core function, release no longer used label cunks
*
* @param proto Daemon protocol of client, to identify the owner
* @param instance Instance, to identify the owner
* @param start First label of the chunk
* @param end Last label of the chunk
* @return 0 on success, -1 otherwise
*/
int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start,
uint32_t end)
{
struct listnode *node;
struct label_manager_chunk *lmc;
int ret = -1;
/* check that size matches */
zlog_debug("Releasing label chunk: %u - %u", start, end);
/* find chunk and disown */
for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
if (lmc->start != start)
continue;
if (lmc->end != end)
continue;
if (lmc->proto != proto || lmc->instance != instance) {
flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH,
"%s: Daemon mismatch!!", __func__);
continue;
}
lmc->proto = NO_PROTO;
lmc->instance = 0;
lmc->keep = 0;
ret = 0;
break;
}
if (ret != 0)
flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK,
"%s: Label chunk not released!!", __func__);
return ret;
}
void label_manager_close()
{
list_delete_and_null(&lbl_mgr.lc_list);
stream_free(obuf);
}
|