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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Zebra SRv6 definitions
* Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
* Copyright (C) 2020 Masakazu Asama
*/
#include <zebra.h>
#include "network.h"
#include "prefix.h"
#include "stream.h"
#include "srv6.h"
#include "zebra/debug.h"
#include "zebra/zapi_msg.h"
#include "zebra/zserv.h"
#include "zebra/zebra_router.h"
#include "zebra/zebra_srv6.h"
#include "zebra/zebra_errors.h"
#include "zebra/ge_netlink.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
DEFINE_MGROUP(SRV6_MGR, "SRv6 Manager");
DEFINE_MTYPE_STATIC(SRV6_MGR, SRV6M_CHUNK, "SRv6 Manager Chunk");
/* define hooks for the basic API, so that it can be specialized or served
* externally
*/
DEFINE_HOOK(srv6_manager_client_connect,
(struct zserv *client, vrf_id_t vrf_id),
(client, vrf_id));
DEFINE_HOOK(srv6_manager_client_disconnect,
(struct zserv *client), (client));
DEFINE_HOOK(srv6_manager_get_chunk,
(struct srv6_locator **loc,
struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id),
(loc, client, locator_name, vrf_id));
DEFINE_HOOK(srv6_manager_release_chunk,
(struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id),
(client, locator_name, vrf_id));
/* define wrappers to be called in zapi_msg.c (as hooks must be called in
* source file where they were defined)
*/
void srv6_manager_client_connect_call(struct zserv *client, vrf_id_t vrf_id)
{
hook_call(srv6_manager_client_connect, client, vrf_id);
}
void srv6_manager_get_locator_chunk_call(struct srv6_locator **loc,
struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id)
{
hook_call(srv6_manager_get_chunk, loc, client, locator_name, vrf_id);
}
void srv6_manager_release_locator_chunk_call(struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id)
{
hook_call(srv6_manager_release_chunk, client, locator_name, vrf_id);
}
int srv6_manager_client_disconnect_cb(struct zserv *client)
{
hook_call(srv6_manager_client_disconnect, client);
return 0;
}
static int zebra_srv6_cleanup(struct zserv *client)
{
return 0;
}
/* --- Zebra SRv6 SID format management functions --------------------------- */
void srv6_sid_format_register(struct srv6_sid_format *format)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
/* Ensure that the format is registered only once */
assert(!srv6_sid_format_lookup(format->name));
listnode_add(srv6->sid_formats, format);
}
void srv6_sid_format_unregister(struct srv6_sid_format *format)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
listnode_delete(srv6->sid_formats, format);
}
struct srv6_sid_format *srv6_sid_format_lookup(const char *name)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
struct srv6_sid_format *format;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(srv6->sid_formats, node, format))
if (!strncmp(name, format->name, sizeof(format->name)))
return format;
return NULL;
}
/*
* Helper function to create the SRv6 compressed format `usid-f3216`.
*/
static struct srv6_sid_format *create_srv6_sid_format_usid_f3216(void)
{
struct srv6_sid_format *format = NULL;
format = srv6_sid_format_alloc(SRV6_SID_FORMAT_USID_F3216_NAME);
format->type = SRV6_SID_FORMAT_TYPE_USID;
/* Define block/node/function length */
format->block_len = SRV6_SID_FORMAT_USID_F3216_BLOCK_LEN;
format->node_len = SRV6_SID_FORMAT_USID_F3216_NODE_LEN;
format->function_len = SRV6_SID_FORMAT_USID_F3216_FUNCTION_LEN;
format->argument_len = SRV6_SID_FORMAT_USID_F3216_ARGUMENT_LEN;
/* Define the ranges from which the SID function can be allocated */
format->config.usid.lib_start = SRV6_SID_FORMAT_USID_F3216_LIB_START;
format->config.usid.elib_start = SRV6_SID_FORMAT_USID_F3216_ELIB_START;
format->config.usid.elib_end = SRV6_SID_FORMAT_USID_F3216_ELIB_END;
format->config.usid.wlib_start = SRV6_SID_FORMAT_USID_F3216_WLIB_START;
format->config.usid.wlib_end = SRV6_SID_FORMAT_USID_F3216_WLIB_END;
format->config.usid.ewlib_start = SRV6_SID_FORMAT_USID_F3216_EWLIB_START;
return format;
}
/*
* Helper function to create the SRv6 uncompressed format.
*/
static struct srv6_sid_format *create_srv6_sid_format_uncompressed(void)
{
struct srv6_sid_format *format = NULL;
format = srv6_sid_format_alloc(SRV6_SID_FORMAT_UNCOMPRESSED_F4024_NAME);
format->type = SRV6_SID_FORMAT_TYPE_UNCOMPRESSED;
/* Define block/node/function length */
format->block_len = SRV6_SID_FORMAT_UNCOMPRESSED_F4024_BLOCK_LEN;
format->node_len = SRV6_SID_FORMAT_UNCOMPRESSED_F4024_NODE_LEN;
format->function_len = SRV6_SID_FORMAT_UNCOMPRESSED_F4024_FUNCTION_LEN;
format->argument_len = SRV6_SID_FORMAT_UNCOMPRESSED_F4024_ARGUMENT_LEN;
/* Define the ranges from which the SID function can be allocated */
format->config.uncompressed.explicit_start =
SRV6_SID_FORMAT_UNCOMPRESSED_F4024_EXPLICIT_RANGE_START;
return format;
}
void zebra_srv6_locator_add(struct srv6_locator *locator)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
struct srv6_locator *tmp;
struct listnode *node;
struct zserv *client;
tmp = zebra_srv6_locator_lookup(locator->name);
if (!tmp)
listnode_add(srv6->locators, locator);
/*
* Notify new locator info to zclients.
*
* The srv6 locators and their prefixes are managed by zserv(zebra).
* And an actual configuration the srv6 sid in the srv6 locator is done
* by zclient(bgpd, isisd, etc). The configuration of each locator
* allocation and specify it by zserv and zclient should be
* asynchronous. For that, zclient should be received the event via
* ZAPI when a srv6 locator is added on zebra.
* Basically, in SRv6, adding/removing SRv6 locators is performed less
* frequently than adding rib entries, so a broad to all zclients will
* not degrade the overall performance of FRRouting.
*/
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
zsend_zebra_srv6_locator_add(client, locator);
}
void zebra_srv6_locator_delete(struct srv6_locator *locator)
{
struct listnode *n;
struct srv6_locator_chunk *c;
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
struct zserv *client;
/*
* Notify deleted locator info to zclients if needed.
*
* zclient(bgpd,isisd,etc) allocates a sid from srv6 locator chunk and
* uses it for its own purpose. For example, in the case of BGP L3VPN,
* the SID assigned to vpn unicast rib will be given.
* And when the locator is deleted by zserv(zebra), those SIDs need to
* be withdrawn. The zclient must initiate the withdrawal of the SIDs
* by ZEBRA_SRV6_LOCATOR_DELETE, and this notification is sent to the
* owner of each chunk.
*/
for (ALL_LIST_ELEMENTS_RO((struct list *)locator->chunks, n, c)) {
if (c->proto == ZEBRA_ROUTE_SYSTEM)
continue;
client = zserv_find_client(c->proto, c->instance);
if (!client) {
zlog_warn(
"%s: Not found zclient(proto=%u, instance=%u).",
__func__, c->proto, c->instance);
continue;
}
zsend_zebra_srv6_locator_delete(client, locator);
}
listnode_delete(srv6->locators, locator);
srv6_locator_free(locator);
}
struct srv6_locator *zebra_srv6_locator_lookup(const char *name)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
struct srv6_locator *locator;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator))
if (!strncmp(name, locator->name, SRV6_LOCNAME_SIZE))
return locator;
return NULL;
}
void zebra_notify_srv6_locator_add(struct srv6_locator *locator)
{
struct listnode *node;
struct zserv *client;
/*
* Notify new locator info to zclients.
*
* The srv6 locators and their prefixes are managed by zserv(zebra).
* And an actual configuration the srv6 sid in the srv6 locator is done
* by zclient(bgpd, isisd, etc). The configuration of each locator
* allocation and specify it by zserv and zclient should be
* asynchronous. For that, zclient should be received the event via
* ZAPI when a srv6 locator is added on zebra.
* Basically, in SRv6, adding/removing SRv6 locators is performed less
* frequently than adding rib entries, so a broad to all zclients will
* not degrade the overall performance of FRRouting.
*/
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
zsend_zebra_srv6_locator_add(client, locator);
}
void zebra_notify_srv6_locator_delete(struct srv6_locator *locator)
{
struct listnode *n;
struct srv6_locator_chunk *c;
struct zserv *client;
/*
* Notify deleted locator info to zclients if needed.
*
* zclient(bgpd,isisd,etc) allocates a sid from srv6 locator chunk and
* uses it for its own purpose. For example, in the case of BGP L3VPN,
* the SID assigned to vpn unicast rib will be given.
* And when the locator is deleted by zserv(zebra), those SIDs need to
* be withdrawn. The zclient must initiate the withdrawal of the SIDs
* by ZEBRA_SRV6_LOCATOR_DELETE, and this notification is sent to the
* owner of each chunk.
*/
for (ALL_LIST_ELEMENTS_RO((struct list *)locator->chunks, n, c)) {
if (c->proto == ZEBRA_ROUTE_SYSTEM)
continue;
client = zserv_find_client(c->proto, c->instance);
if (!client) {
zlog_warn("Not found zclient(proto=%u, instance=%u).",
c->proto, c->instance);
continue;
}
zsend_zebra_srv6_locator_delete(client, locator);
}
}
struct zebra_srv6 srv6;
struct zebra_srv6 *zebra_srv6_get_default(void)
{
static bool first_execution = true;
struct srv6_sid_format *format_usidf3216;
struct srv6_sid_format *format_uncompressed;
if (first_execution) {
first_execution = false;
srv6.locators = list_new();
/* Initialize list of SID formats */
srv6.sid_formats = list_new();
srv6.sid_formats->del = delete_srv6_sid_format;
/* Create SID format `usid-f3216` */
format_usidf3216 = create_srv6_sid_format_usid_f3216();
srv6_sid_format_register(format_usidf3216);
/* Create SID format `uncompressed` */
format_uncompressed = create_srv6_sid_format_uncompressed();
srv6_sid_format_register(format_uncompressed);
}
return &srv6;
}
/**
* Core function, assigns srv6-locator chunks
*
* 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 session_id SessionID of client
* @param name Name of SRv6-locator
* @return Pointer to the assigned srv6-locator chunk,
* or NULL if the request could not be satisfied
*/
static struct srv6_locator *
assign_srv6_locator_chunk(uint8_t proto,
uint16_t instance,
uint32_t session_id,
const char *locator_name)
{
bool chunk_found = false;
struct listnode *node = NULL;
struct srv6_locator *loc = NULL;
struct srv6_locator_chunk *chunk = NULL;
loc = zebra_srv6_locator_lookup(locator_name);
if (!loc) {
zlog_info("%s: locator %s was not found",
__func__, locator_name);
return NULL;
}
for (ALL_LIST_ELEMENTS_RO((struct list *)loc->chunks, node, chunk)) {
if (chunk->proto != NO_PROTO && chunk->proto != proto)
continue;
chunk_found = true;
break;
}
if (!chunk_found) {
zlog_info("%s: locator is already owned", __func__);
return NULL;
}
chunk->proto = proto;
chunk->instance = instance;
chunk->session_id = session_id;
return loc;
}
static int zebra_srv6_manager_get_locator_chunk(struct srv6_locator **loc,
struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id)
{
int ret = 0;
*loc = assign_srv6_locator_chunk(client->proto, client->instance,
client->session_id, locator_name);
if (!*loc)
zlog_err("Unable to assign locator chunk to %s instance %u",
zebra_route_string(client->proto), client->instance);
else if (IS_ZEBRA_DEBUG_PACKET)
zlog_info("Assigned locator chunk %s to %s instance %u",
(*loc)->name, zebra_route_string(client->proto),
client->instance);
if (*loc && (*loc)->status_up)
ret = zsend_srv6_manager_get_locator_chunk_response(client,
vrf_id,
*loc);
return ret;
}
/**
* Core function, release no longer used srv6-locator chunks
*
* @param proto Daemon protocol of client, to identify the owner
* @param instance Instance, to identify the owner
* @param session_id Zclient session ID, to identify the zclient session
* @param locator_name SRv6-locator name, to identify the actual locator
* @return 0 on success, -1 otherwise
*/
static int release_srv6_locator_chunk(uint8_t proto, uint16_t instance,
uint32_t session_id,
const char *locator_name)
{
int ret = -1;
struct listnode *node;
struct srv6_locator_chunk *chunk;
struct srv6_locator *loc = NULL;
loc = zebra_srv6_locator_lookup(locator_name);
if (!loc)
return -1;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Releasing srv6-locator on %s", __func__,
locator_name);
for (ALL_LIST_ELEMENTS_RO((struct list *)loc->chunks, node, chunk)) {
if (chunk->proto != proto ||
chunk->instance != instance ||
chunk->session_id != session_id)
continue;
chunk->proto = NO_PROTO;
chunk->instance = 0;
chunk->session_id = 0;
chunk->keep = 0;
ret = 0;
break;
}
if (ret != 0)
flog_err(EC_ZEBRA_SRV6M_UNRELEASED_LOCATOR_CHUNK,
"%s: SRv6 locator chunk not released", __func__);
return ret;
}
static int zebra_srv6_manager_release_locator_chunk(struct zserv *client,
const char *locator_name,
vrf_id_t vrf_id)
{
if (vrf_id != VRF_DEFAULT) {
zlog_err("SRv6 locator doesn't support vrf");
return -1;
}
return release_srv6_locator_chunk(client->proto, client->instance,
client->session_id, locator_name);
}
/**
* Release srv6-locator 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_srv6_locator_chunks(struct zserv *client)
{
int ret;
int count = 0;
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
struct listnode *loc_node;
struct listnode *chunk_node;
struct srv6_locator *loc;
struct srv6_locator_chunk *chunk;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Releasing chunks for client proto %s, instance %d, session %u",
__func__, zebra_route_string(client->proto),
client->instance, client->session_id);
for (ALL_LIST_ELEMENTS_RO(srv6->locators, loc_node, loc)) {
for (ALL_LIST_ELEMENTS_RO(loc->chunks, chunk_node, chunk)) {
if (chunk->proto == client->proto &&
chunk->instance == client->instance &&
chunk->session_id == client->session_id &&
chunk->keep == 0) {
ret = release_srv6_locator_chunk(
chunk->proto, chunk->instance,
chunk->session_id, loc->name);
if (ret == 0)
count++;
}
}
}
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Released %d srv6-locator chunks",
__func__, count);
return count;
}
void zebra_srv6_encap_src_addr_set(struct in6_addr *encap_src_addr)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
if (!encap_src_addr)
return;
memcpy(&srv6->encap_src_addr, encap_src_addr, sizeof(struct in6_addr));
}
void zebra_srv6_encap_src_addr_unset(void)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
memset(&srv6->encap_src_addr, 0, sizeof(struct in6_addr));
}
void zebra_srv6_terminate(void)
{
struct srv6_locator *locator;
struct srv6_sid_format *format;
if (srv6.locators) {
while (listcount(srv6.locators)) {
locator = listnode_head(srv6.locators);
listnode_delete(srv6.locators, locator);
srv6_locator_free(locator);
}
list_delete(&srv6.locators);
}
/* Free SRv6 SID formats */
if (srv6.sid_formats) {
while (listcount(srv6.sid_formats)) {
format = listnode_head(srv6.sid_formats);
srv6_sid_format_unregister(format);
srv6_sid_format_free(format);
}
list_delete(&srv6.sid_formats);
}
}
void zebra_srv6_init(void)
{
hook_register(zserv_client_close, zebra_srv6_cleanup);
hook_register(srv6_manager_get_chunk,
zebra_srv6_manager_get_locator_chunk);
hook_register(srv6_manager_release_chunk,
zebra_srv6_manager_release_locator_chunk);
}
bool zebra_srv6_is_enable(void)
{
struct zebra_srv6 *srv6 = zebra_srv6_get_default();
return listcount(srv6->locators);
}
|