]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_srte.c
*: Add camelCase JSON keys in addition to PascalCase
[mirror_frr.git] / zebra / zebra_srte.c
1 /* Zebra SR-TE code
2 * Copyright (C) 2020 NetDEF, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "lib/zclient.h"
24 #include "lib/lib_errors.h"
25
26 #include "zebra/zebra_srte.h"
27 #include "zebra/zebra_mpls.h"
28 #include "zebra/zebra_rnh.h"
29 #include "zebra/zapi_msg.h"
30
31 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_SR_POLICY, "SR Policy");
32
33 static void zebra_sr_policy_deactivate(struct zebra_sr_policy *policy);
34
35 /* Generate rb-tree of SR Policy instances. */
36 static inline int
37 zebra_sr_policy_instance_compare(const struct zebra_sr_policy *a,
38 const struct zebra_sr_policy *b)
39 {
40 return sr_policy_compare(&a->endpoint, &b->endpoint, a->color,
41 b->color);
42 }
43 RB_GENERATE(zebra_sr_policy_instance_head, zebra_sr_policy, entry,
44 zebra_sr_policy_instance_compare)
45
46 struct zebra_sr_policy_instance_head zebra_sr_policy_instances =
47 RB_INITIALIZER(&zebra_sr_policy_instances);
48
49 struct zebra_sr_policy *zebra_sr_policy_add(uint32_t color,
50 struct ipaddr *endpoint, char *name)
51 {
52 struct zebra_sr_policy *policy;
53
54 policy = XCALLOC(MTYPE_ZEBRA_SR_POLICY, sizeof(*policy));
55 policy->color = color;
56 policy->endpoint = *endpoint;
57 strlcpy(policy->name, name, sizeof(policy->name));
58 policy->status = ZEBRA_SR_POLICY_DOWN;
59 RB_INSERT(zebra_sr_policy_instance_head, &zebra_sr_policy_instances,
60 policy);
61
62 return policy;
63 }
64
65 void zebra_sr_policy_del(struct zebra_sr_policy *policy)
66 {
67 if (policy->status == ZEBRA_SR_POLICY_UP)
68 zebra_sr_policy_deactivate(policy);
69 RB_REMOVE(zebra_sr_policy_instance_head, &zebra_sr_policy_instances,
70 policy);
71 XFREE(MTYPE_ZEBRA_SR_POLICY, policy);
72 }
73
74 struct zebra_sr_policy *zebra_sr_policy_find(uint32_t color,
75 struct ipaddr *endpoint)
76 {
77 struct zebra_sr_policy policy = {};
78
79 policy.color = color;
80 policy.endpoint = *endpoint;
81 return RB_FIND(zebra_sr_policy_instance_head,
82 &zebra_sr_policy_instances, &policy);
83 }
84
85 struct zebra_sr_policy *zebra_sr_policy_find_by_name(char *name)
86 {
87 struct zebra_sr_policy *policy;
88
89 // TODO: create index for policy names
90 RB_FOREACH (policy, zebra_sr_policy_instance_head,
91 &zebra_sr_policy_instances) {
92 if (strcmp(policy->name, name) == 0)
93 return policy;
94 }
95
96 return NULL;
97 }
98
99 static int zebra_sr_policy_notify_update_client(struct zebra_sr_policy *policy,
100 struct zserv *client)
101 {
102 const struct zebra_nhlfe *nhlfe;
103 struct stream *s;
104 uint32_t message = 0;
105 unsigned long nump = 0;
106 uint8_t num;
107 struct zapi_nexthop znh;
108 int ret;
109
110 /* Get output stream. */
111 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
112
113 zclient_create_header(s, ZEBRA_NEXTHOP_UPDATE, zvrf_id(policy->zvrf));
114
115 /* Message flags. */
116 SET_FLAG(message, ZAPI_MESSAGE_SRTE);
117 stream_putl(s, message);
118
119 stream_putw(s, SAFI_UNICAST);
120 switch (policy->endpoint.ipa_type) {
121 case IPADDR_V4:
122 stream_putw(s, AF_INET);
123 stream_putc(s, IPV4_MAX_BITLEN);
124 stream_put_in_addr(s, &policy->endpoint.ipaddr_v4);
125 break;
126 case IPADDR_V6:
127 stream_putw(s, AF_INET6);
128 stream_putc(s, IPV6_MAX_BITLEN);
129 stream_put(s, &policy->endpoint.ipaddr_v6, IPV6_MAX_BYTELEN);
130 break;
131 default:
132 flog_warn(EC_LIB_DEVELOPMENT,
133 "%s: unknown policy endpoint address family: %u",
134 __func__, policy->endpoint.ipa_type);
135 exit(1);
136 }
137 stream_putl(s, policy->color);
138
139 num = 0;
140 frr_each (nhlfe_list_const, &policy->lsp->nhlfe_list, nhlfe) {
141 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
142 || CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
143 continue;
144
145 if (num == 0) {
146 stream_putc(s, re_type_from_lsp_type(nhlfe->type));
147 stream_putw(s, 0); /* instance - not available */
148 stream_putc(s, nhlfe->distance);
149 stream_putl(s, 0); /* metric - not available */
150 nump = stream_get_endp(s);
151 stream_putc(s, 0);
152 }
153
154 zapi_nexthop_from_nexthop(&znh, nhlfe->nexthop);
155 ret = zapi_nexthop_encode(s, &znh, 0, message);
156 if (ret < 0)
157 goto failure;
158
159 num++;
160 }
161 stream_putc_at(s, nump, num);
162 stream_putw_at(s, 0, stream_get_endp(s));
163
164 client->nh_last_upd_time = monotime(NULL);
165 return zserv_send_message(client, s);
166
167 failure:
168
169 stream_free(s);
170 return -1;
171 }
172
173 static void zebra_sr_policy_notify_update(struct zebra_sr_policy *policy)
174 {
175 struct rnh *rnh;
176 struct prefix p = {};
177 struct zebra_vrf *zvrf;
178 struct listnode *node;
179 struct zserv *client;
180
181 zvrf = policy->zvrf;
182 switch (policy->endpoint.ipa_type) {
183 case IPADDR_V4:
184 p.family = AF_INET;
185 p.prefixlen = IPV4_MAX_BITLEN;
186 p.u.prefix4 = policy->endpoint.ipaddr_v4;
187 break;
188 case IPADDR_V6:
189 p.family = AF_INET6;
190 p.prefixlen = IPV6_MAX_BITLEN;
191 p.u.prefix6 = policy->endpoint.ipaddr_v6;
192 break;
193 default:
194 flog_warn(EC_LIB_DEVELOPMENT,
195 "%s: unknown policy endpoint address family: %u",
196 __func__, policy->endpoint.ipa_type);
197 exit(1);
198 }
199
200 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), SAFI_UNICAST);
201 if (!rnh)
202 return;
203
204 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
205 if (policy->status == ZEBRA_SR_POLICY_UP)
206 zebra_sr_policy_notify_update_client(policy, client);
207 else
208 /* Fallback to the IGP shortest path. */
209 zebra_send_rnh_update(rnh, client, zvrf_id(zvrf),
210 policy->color);
211 }
212 }
213
214 static void zebra_sr_policy_activate(struct zebra_sr_policy *policy,
215 struct zebra_lsp *lsp)
216 {
217 policy->status = ZEBRA_SR_POLICY_UP;
218 policy->lsp = lsp;
219 (void)zebra_sr_policy_bsid_install(policy);
220 zsend_sr_policy_notify_status(policy->color, &policy->endpoint,
221 policy->name, ZEBRA_SR_POLICY_UP);
222 zebra_sr_policy_notify_update(policy);
223 }
224
225 static void zebra_sr_policy_update(struct zebra_sr_policy *policy,
226 struct zebra_lsp *lsp,
227 struct zapi_srte_tunnel *old_tunnel)
228 {
229 bool bsid_changed;
230 bool segment_list_changed;
231
232 policy->lsp = lsp;
233
234 bsid_changed =
235 policy->segment_list.local_label != old_tunnel->local_label;
236 segment_list_changed =
237 policy->segment_list.label_num != old_tunnel->label_num
238 || memcmp(policy->segment_list.labels, old_tunnel->labels,
239 sizeof(mpls_label_t)
240 * policy->segment_list.label_num);
241
242 /* Re-install label stack if necessary. */
243 if (bsid_changed || segment_list_changed) {
244 zebra_sr_policy_bsid_uninstall(policy, old_tunnel->local_label);
245 (void)zebra_sr_policy_bsid_install(policy);
246 }
247
248 zsend_sr_policy_notify_status(policy->color, &policy->endpoint,
249 policy->name, ZEBRA_SR_POLICY_UP);
250
251 /* Handle segment-list update. */
252 if (segment_list_changed)
253 zebra_sr_policy_notify_update(policy);
254 }
255
256 static void zebra_sr_policy_deactivate(struct zebra_sr_policy *policy)
257 {
258 policy->status = ZEBRA_SR_POLICY_DOWN;
259 policy->lsp = NULL;
260 zebra_sr_policy_bsid_uninstall(policy,
261 policy->segment_list.local_label);
262 zsend_sr_policy_notify_status(policy->color, &policy->endpoint,
263 policy->name, ZEBRA_SR_POLICY_DOWN);
264 zebra_sr_policy_notify_update(policy);
265 }
266
267 int zebra_sr_policy_validate(struct zebra_sr_policy *policy,
268 struct zapi_srte_tunnel *new_tunnel)
269 {
270 struct zapi_srte_tunnel old_tunnel = policy->segment_list;
271 struct zebra_lsp *lsp;
272
273 if (new_tunnel)
274 policy->segment_list = *new_tunnel;
275
276 /* Try to resolve the Binding-SID nexthops. */
277 lsp = mpls_lsp_find(policy->zvrf, policy->segment_list.labels[0]);
278 if (!lsp || !lsp->best_nhlfe
279 || lsp->addr_family != ipaddr_family(&policy->endpoint)) {
280 if (policy->status == ZEBRA_SR_POLICY_UP)
281 zebra_sr_policy_deactivate(policy);
282 return -1;
283 }
284
285 /* First label was resolved successfully. */
286 if (policy->status == ZEBRA_SR_POLICY_DOWN)
287 zebra_sr_policy_activate(policy, lsp);
288 else
289 zebra_sr_policy_update(policy, lsp, &old_tunnel);
290
291 return 0;
292 }
293
294 int zebra_sr_policy_bsid_install(struct zebra_sr_policy *policy)
295 {
296 struct zapi_srte_tunnel *zt = &policy->segment_list;
297 struct zebra_nhlfe *nhlfe;
298
299 if (zt->local_label == MPLS_LABEL_NONE)
300 return 0;
301
302 frr_each_safe (nhlfe_list, &policy->lsp->nhlfe_list, nhlfe) {
303 uint8_t num_out_labels;
304 mpls_label_t *out_labels;
305 mpls_label_t null_label = MPLS_LABEL_IMPLICIT_NULL;
306
307 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)
308 || CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
309 continue;
310
311 /*
312 * Don't push the first SID if the corresponding action in the
313 * LFIB is POP.
314 */
315 if (!nhlfe->nexthop->nh_label
316 || !nhlfe->nexthop->nh_label->num_labels
317 || nhlfe->nexthop->nh_label->label[0]
318 == MPLS_LABEL_IMPLICIT_NULL) {
319 if (zt->label_num > 1) {
320 num_out_labels = zt->label_num - 1;
321 out_labels = &zt->labels[1];
322 } else {
323 num_out_labels = 1;
324 out_labels = &null_label;
325 }
326 } else {
327 num_out_labels = zt->label_num;
328 out_labels = zt->labels;
329 }
330
331 if (mpls_lsp_install(
332 policy->zvrf, zt->type, zt->local_label,
333 num_out_labels, out_labels, nhlfe->nexthop->type,
334 &nhlfe->nexthop->gate, nhlfe->nexthop->ifindex)
335 < 0)
336 return -1;
337 }
338
339 return 0;
340 }
341
342 void zebra_sr_policy_bsid_uninstall(struct zebra_sr_policy *policy,
343 mpls_label_t old_bsid)
344 {
345 struct zapi_srte_tunnel *zt = &policy->segment_list;
346
347 mpls_lsp_uninstall_all_vrf(policy->zvrf, zt->type, old_bsid);
348 }
349
350 int zebra_sr_policy_label_update(mpls_label_t label,
351 enum zebra_sr_policy_update_label_mode mode)
352 {
353 struct zebra_sr_policy *policy;
354
355 RB_FOREACH (policy, zebra_sr_policy_instance_head,
356 &zebra_sr_policy_instances) {
357 mpls_label_t next_hop_label;
358
359 next_hop_label = policy->segment_list.labels[0];
360 if (next_hop_label != label)
361 continue;
362
363 switch (mode) {
364 case ZEBRA_SR_POLICY_LABEL_CREATED:
365 case ZEBRA_SR_POLICY_LABEL_UPDATED:
366 case ZEBRA_SR_POLICY_LABEL_REMOVED:
367 zebra_sr_policy_validate(policy, NULL);
368 break;
369 }
370 }
371
372 return 0;
373 }
374
375 void zebra_srte_init(void)
376 {
377 }