-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsr_tlv_api.c
272 lines (230 loc) · 8.61 KB
/
sr_tlv_api.c
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
/*
* =====================================================================================
*
* Filename: sr_tlv_api.c
*
* Description: implements the API to manipulate SR related TLVs
*
* Version: 1.0
* Created: Monday 26 February 2018 01:49:08 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* This file is part of the SPFComputation distribution (https://github.com/sachinites).
* Copyright (c) 2017 Abhishek Sagar.
* 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* =====================================================================================
*/
#include "sr_tlv_api.h"
#include "instance.h"
#include "prefix.h"
#include "spfutil.h"
#include "bitsop.h"
#include "glthread.h"
#include "LinuxMemoryManager/uapi_mm.h"
void
diplay_prefix_sid(prefix_t *prefix){
char subnet[PREFIX_LEN_WITH_MASK + 1];
memset(subnet, 0, PREFIX_LEN_WITH_MASK + 1);
apply_mask2(prefix->prefix, prefix->mask, subnet);
prefix_sid_subtlv_t *prefix_sid = get_prefix_sid(prefix);
if(prefix_sid){
printf("\t%-18s : %-8u %s\n", subnet, prefix_sid->sid.sid,
IS_PREFIX_SR_ACTIVE(prefix) ? "ACTIVE" : "INACTIVE");
printf("\t\tFLAGS : R:%c N:%c P:%c E:%c V:%c L:%c\n",
IS_BIT_SET(prefix_sid->flags, RE_ADVERTISEMENT_R_FLAG) ? '1' : '0',
IS_BIT_SET(prefix_sid->flags, NODE_SID_N_FLAG) ? '1' : '0',
IS_BIT_SET(prefix_sid->flags, NO_PHP_P_FLAG) ? '1' : '0',
IS_BIT_SET(prefix_sid->flags, EXPLICIT_NULL_E_FLAG) ? '1' : '0',
IS_BIT_SET(prefix_sid->flags, VALUE_V_FLAG) ? '1' : '0',
IS_BIT_SET(prefix_sid->flags, LOCAL_SIGNIFICANCE_L_FLAG) ? '1' : '0');
}
else{
printf("\t%-18s : Not assigned\n", subnet);
}
}
boolean
set_node_sid(node_t *node, unsigned int node_sid_value){
LEVEL level_it;
boolean trigger_conflict_res = FALSE,
is_prefix_sid_updated = FALSE;
prefix_t *router_id = NULL;
if(!node->spring_enabled){
printf("Error : SPRING not enabled\n");
return FALSE;
}
if(is_srgb_index_in_use(node->srgb, node_sid_value)){
printf("Warning : Conflict Detected\n");
}
for(level_it = LEVEL1 ; level_it < MAX_LEVEL; level_it++){
router_id = node_local_prefix_search(node, level_it, node->router_id, 32);
assert(router_id);
is_prefix_sid_updated = update_prefix_sid(node, router_id, node_sid_value, level_it);
if(is_prefix_sid_updated)
trigger_conflict_res = TRUE;
}
return trigger_conflict_res;
}
boolean
unset_node_sid(node_t *node){
LEVEL level_it;
boolean trigger_conflict_res = FALSE;
prefix_t *router_id = NULL;
unsigned int index = 0 ;
if(!node->spring_enabled){
printf("Error : SPRING not enabled\n");
return FALSE;
}
for(level_it = LEVEL1 ; level_it < MAX_LEVEL; level_it++){
router_id = node_local_prefix_search(node, level_it, node->router_id, 32);
assert(router_id);
if(!router_id->psid_thread_ptr){
continue;
}
index = PREFIX_SID_INDEX(router_id);
mark_srgb_index_not_in_use(node->srgb, index);
free_prefix_sid(router_id);
trigger_conflict_res = TRUE;
MARK_PREFIX_SR_INACTIVE(router_id);
}
return trigger_conflict_res;
}
boolean
set_interface_address_prefix_sid(node_t *node, char *intf_name,
unsigned int prefix_sid_value){
LEVEL level_it;
boolean trigger_conflict_res = FALSE,
is_prefix_sid_updated = FALSE;
prefix_t *prefix = NULL,
*intf_prefix = NULL;
if(!node->spring_enabled){
printf("Error : SPRING not enabled\n");
return FALSE;
}
edge_end_t *interface = get_interface_from_intf_name(node, intf_name);\
if(!interface){
printf("Error : Node %s Interface %s do not exist\n", node->node_name, intf_name);
return FALSE;
}
if(is_srgb_index_in_use(node->srgb, prefix_sid_value)){
printf("Warning : Conflict Detected\n");
}
for(level_it = LEVEL1 ; level_it < MAX_LEVEL; level_it++){
intf_prefix = interface->prefix[level_it];
if(!intf_prefix) continue;
prefix = node_local_prefix_search(node, level_it, intf_prefix->prefix, intf_prefix->mask);
assert(prefix);
is_prefix_sid_updated = update_prefix_sid(node, prefix, prefix_sid_value, level_it);
if(is_prefix_sid_updated)
trigger_conflict_res = TRUE;
}
return trigger_conflict_res;
}
boolean
unset_interface_address_prefix_sid(node_t *node, char *intf_name){
LEVEL level_it;
boolean trigger_conflict_res = FALSE;
prefix_t *prefix = NULL, *intf_prefix = NULL;
edge_end_t *interface = NULL;
mpls_label_t label = 0;
if(!node->spring_enabled){
printf("Error : SPRING not enabled\n");
return FALSE;
}
interface = get_interface_from_intf_name(node, intf_name);
if(!interface){
printf("Error : Node %s Interface %s do not exist\n", node->node_name, intf_name);
return FALSE;
}
for(level_it = LEVEL1 ; level_it < MAX_LEVEL; level_it++){
intf_prefix = interface->prefix[level_it];
if(!intf_prefix) continue;
prefix = node_local_prefix_search(node, level_it, intf_prefix->prefix, intf_prefix->mask);
assert(prefix);
if(!prefix->psid_thread_ptr)
continue;
label = PREFIX_SID_INDEX(prefix);
mark_srgb_index_not_in_use(node->srgb, label);
free_prefix_sid(prefix);
MARK_PREFIX_SR_INACTIVE(prefix);
trigger_conflict_res = TRUE;
}
return trigger_conflict_res;
}
boolean
set_interface_adj_sid(node_t *node, char *interface, unsigned int adj_sid_value){
return TRUE;
}
boolean
unset_interface_adj_sid(node_t *node, char *interface){
return TRUE;
}
boolean
update_prefix_sid(node_t *node, prefix_t *prefix,
unsigned int prefix_sid_value, LEVEL level){
boolean trigger_conflict_res = FALSE;
prefix_sid_subtlv_t *prefix_sid = NULL;
if(!prefix->psid_thread_ptr){
prefix_sid = XCALLOC(1, prefix_sid_subtlv_t);
prefix_sid->type = PREFIX_SID_SUBTLV_TYPE;
prefix_sid->length = 0; /*never used*/
prefix_sid->flags = 0;
prefix_sid->algorithm = SHORTEST_PATH_FIRST;
prefix_sid->sid.type = SID_SUBTLV_TYPE;
prefix_sid->sid.sid = prefix_sid_value;
/*Bi-Directional association*/
prefix->psid_thread_ptr = &prefix_sid->glthread;
prefix_sid->prefix = prefix;
MARK_PREFIX_SR_ACTIVE(prefix);
trigger_conflict_res = TRUE;
mark_srgb_index_in_use(node->srgb, prefix_sid_value);
glthread_add_next(&node->prefix_sids_thread_lst[level], &prefix_sid->glthread);
return trigger_conflict_res;
}
if(prefix_sid_value != PREFIX_SID_INDEX(prefix)){
trigger_conflict_res = TRUE;
mark_srgb_index_in_use(node->srgb, prefix_sid_value);
mark_srgb_index_not_in_use(node->srgb, PREFIX_SID_INDEX(prefix));
}
prefix_sid->sid.sid = prefix_sid_value;
if(trigger_conflict_res)
MARK_PREFIX_SR_ACTIVE(prefix);
return trigger_conflict_res;
}
unsigned int
PREFIX_SID_INDEX(prefix_t *prefix){
assert(prefix->psid_thread_ptr);
return (glthread_to_prefix_sid(prefix->psid_thread_ptr))->sid.sid;
}
prefix_sid_subtlv_t *
get_prefix_sid(prefix_t *prefix){
if(!prefix->psid_thread_ptr)
return NULL;
return glthread_to_prefix_sid(prefix->psid_thread_ptr);
}
void
free_prefix_sid(prefix_t *prefix){
if(!prefix->psid_thread_ptr)
return;
/*de-associate the prefix SID and free it*/
glthread_t *glthread = prefix->psid_thread_ptr;
prefix->psid_thread_ptr = NULL;
prefix_sid_subtlv_t *prefix_sid = glthread_to_prefix_sid(glthread);
prefix_sid->prefix = NULL;
remove_glthread(glthread);
XFREE(prefix_sid);
}