]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pw.c
*: Make assignment from RB_ROOT in while loop work better
[mirror_frr.git] / zebra / zebra_pw.c
1 /* Zebra PW code
2 * Copyright (C) 2016 Volta Networks, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include "log.h"
23 #include "memory.h"
24 #include "thread.h"
25 #include "command.h"
26 #include "vrf.h"
27
28 #include "zebra/debug.h"
29 #include "zebra/rib.h"
30 #include "zebra/zserv.h"
31 #include "zebra/zebra_rnh.h"
32 #include "zebra/zebra_vrf.h"
33 #include "zebra/zebra_pw.h"
34
35 DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire")
36
37 DEFINE_QOBJ_TYPE(zebra_pw)
38
39 DEFINE_HOOK(pw_install, (struct zebra_pw * pw), (pw))
40 DEFINE_HOOK(pw_uninstall, (struct zebra_pw * pw), (pw))
41
42 #define MPLS_NO_LABEL MPLS_INVALID_LABEL
43
44 extern struct zebra_t zebrad;
45
46 static int zebra_pw_enabled(struct zebra_pw *);
47 static void zebra_pw_install(struct zebra_pw *);
48 static void zebra_pw_uninstall(struct zebra_pw *);
49 static int zebra_pw_install_retry(struct thread *);
50 static int zebra_pw_check_reachability(struct zebra_pw *);
51 static void zebra_pw_update_status(struct zebra_pw *, int);
52
53 static inline int zebra_pw_compare(const struct zebra_pw *a,
54 const struct zebra_pw *b)
55 {
56 return (strcmp(a->ifname, b->ifname));
57 }
58
59 RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
60 RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)
61
62 struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
63 uint8_t protocol, struct zserv *client)
64 {
65 struct zebra_pw *pw;
66
67 if (IS_ZEBRA_DEBUG_PW)
68 zlog_debug("%u: adding pseudowire %s protocol %s",
69 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
70
71 pw = XCALLOC(MTYPE_PW, sizeof(*pw));
72 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
73 pw->protocol = protocol;
74 pw->vrf_id = zvrf_id(zvrf);
75 pw->client = client;
76 pw->status = PW_STATUS_DOWN;
77 pw->local_label = MPLS_NO_LABEL;
78 pw->remote_label = MPLS_NO_LABEL;
79 pw->flags = F_PSEUDOWIRE_CWORD;
80
81 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
82 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
83 RB_INSERT(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
84 QOBJ_REG(pw, zebra_pw);
85 }
86
87 return pw;
88 }
89
90 void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
91 {
92 if (IS_ZEBRA_DEBUG_PW)
93 zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
94 pw->ifname, zebra_route_string(pw->protocol));
95
96 /* remove nexthop tracking */
97 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
98
99 /* uninstall */
100 if (pw->status == PW_STATUS_UP)
101 hook_call(pw_uninstall, pw);
102 else if (pw->install_retry_timer)
103 THREAD_TIMER_OFF(pw->install_retry_timer);
104
105 /* unlink and release memory */
106 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
107 if (pw->protocol == ZEBRA_ROUTE_STATIC)
108 RB_REMOVE(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
109 XFREE(MTYPE_PW, pw);
110 }
111
112 void zebra_pw_change(struct zebra_pw *pw, ifindex_t ifindex, int type, int af,
113 union g_addr *nexthop, uint32_t local_label,
114 uint32_t remote_label, uint8_t flags,
115 union pw_protocol_fields *data)
116 {
117 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
118
119 pw->ifindex = ifindex;
120 pw->type = type;
121 pw->af = af;
122 pw->nexthop = *nexthop;
123 pw->local_label = local_label;
124 pw->remote_label = remote_label;
125 pw->flags = flags;
126 pw->data = *data;
127
128 if (zebra_pw_enabled(pw))
129 zebra_register_rnh_pseudowire(pw->vrf_id, pw);
130 else
131 zebra_pw_uninstall(pw);
132 }
133
134 struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
135 {
136 struct zebra_pw pw;
137 strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
138 return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
139 }
140
141 static int zebra_pw_enabled(struct zebra_pw *pw)
142 {
143 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
144 if (pw->local_label == MPLS_NO_LABEL
145 || pw->remote_label == MPLS_NO_LABEL || pw->af == AF_UNSPEC)
146 return 0;
147 return 1;
148 } else
149 return pw->enabled;
150 }
151
152 void zebra_pw_update(struct zebra_pw *pw)
153 {
154 if (zebra_pw_check_reachability(pw) < 0) {
155 zebra_pw_uninstall(pw);
156 /* wait for NHT and try again later */
157 } else {
158 /*
159 * Install or reinstall the pseudowire (e.g. to update
160 * parameters like the nexthop or the use of the control word).
161 */
162 zebra_pw_install(pw);
163 }
164 }
165
166 static void zebra_pw_install(struct zebra_pw *pw)
167 {
168 if (IS_ZEBRA_DEBUG_PW)
169 zlog_debug("%u: installing pseudowire %s protocol %s",
170 pw->vrf_id, pw->ifname,
171 zebra_route_string(pw->protocol));
172
173 if (hook_call(pw_install, pw)) {
174 zebra_pw_install_failure(pw);
175 return;
176 }
177
178 if (pw->status == PW_STATUS_DOWN)
179 zebra_pw_update_status(pw, PW_STATUS_UP);
180 }
181
182 static void zebra_pw_uninstall(struct zebra_pw *pw)
183 {
184 if (pw->status == PW_STATUS_DOWN)
185 return;
186
187 if (IS_ZEBRA_DEBUG_PW)
188 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
189 pw->vrf_id, pw->ifname,
190 zebra_route_string(pw->protocol));
191
192 /* ignore any possible error */
193 hook_call(pw_uninstall, pw);
194
195 if (zebra_pw_enabled(pw))
196 zebra_pw_update_status(pw, PW_STATUS_DOWN);
197 }
198
199 /*
200 * Installation of the pseudowire in the kernel or hardware has failed. This
201 * function will notify the pseudowire client about the failure and schedule
202 * to retry the installation later. This function can be called by an external
203 * agent that performs the pseudowire installation in an asynchronous way.
204 */
205 void zebra_pw_install_failure(struct zebra_pw *pw)
206 {
207 if (IS_ZEBRA_DEBUG_PW)
208 zlog_debug(
209 "%u: failed installing pseudowire %s, "
210 "scheduling retry in %u seconds",
211 pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);
212
213 /* schedule to retry later */
214 THREAD_TIMER_OFF(pw->install_retry_timer);
215 thread_add_timer(zebrad.master, zebra_pw_install_retry, pw,
216 PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);
217
218 zebra_pw_update_status(pw, PW_STATUS_DOWN);
219 }
220
221 static int zebra_pw_install_retry(struct thread *thread)
222 {
223 struct zebra_pw *pw = THREAD_ARG(thread);
224
225 pw->install_retry_timer = NULL;
226 zebra_pw_install(pw);
227
228 return 0;
229 }
230
231 static void zebra_pw_update_status(struct zebra_pw *pw, int status)
232 {
233 pw->status = status;
234 if (pw->client)
235 zsend_pw_update(pw->client, pw);
236 }
237
238 static int zebra_pw_check_reachability(struct zebra_pw *pw)
239 {
240 struct route_entry *re;
241 struct nexthop *nexthop;
242
243 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
244
245 /* find route to the remote end of the pseudowire */
246 re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
247 &pw->nexthop, NULL);
248 if (!re) {
249 if (IS_ZEBRA_DEBUG_PW)
250 zlog_warn("%s: no route found for %s", __func__,
251 pw->ifname);
252 return -1;
253 }
254
255 /*
256 * Need to ensure that there's a label binding for all nexthops.
257 * Otherwise, ECMP for this route could render the pseudowire unusable.
258 */
259 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
260 if (!nexthop->nh_label) {
261 if (IS_ZEBRA_DEBUG_PW)
262 zlog_warn("%s: unlabeled route for %s",
263 __func__, pw->ifname);
264 return -1;
265 }
266 }
267
268 return 0;
269 }
270
271 void zebra_pw_client_close(struct zserv *client)
272 {
273 struct vrf *vrf;
274 struct zebra_vrf *zvrf;
275 struct zebra_pw *pw, *tmp;
276
277 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
278 zvrf = vrf->info;
279 RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
280 if (pw->client != client)
281 continue;
282 zebra_pw_del(zvrf, pw);
283 }
284 }
285 }
286
287 void zebra_pw_init(struct zebra_vrf *zvrf)
288 {
289 RB_INIT(zebra_pw_head, &zvrf->pseudowires);
290 RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
291 }
292
293 void zebra_pw_exit(struct zebra_vrf *zvrf)
294 {
295 struct zebra_pw *pw;
296
297 while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) {
298 pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires);
299
300 zebra_pw_del(zvrf, pw);
301 }
302 }
303
304 DEFUN_NOSH (pseudowire_if,
305 pseudowire_if_cmd,
306 "[no] pseudowire IFNAME",
307 NO_STR
308 "Static pseudowire configuration\n"
309 "Pseudowire name\n")
310 {
311 struct zebra_vrf *zvrf;
312 struct zebra_pw *pw;
313 int idx = 0;
314 const char *ifname;
315
316 zvrf = vrf_info_lookup(VRF_DEFAULT);
317 if (!zvrf)
318 return CMD_WARNING;
319
320 argv_find(argv, argc, "IFNAME", &idx);
321 ifname = argv[idx]->arg;
322 pw = zebra_pw_find(zvrf, ifname);
323 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
324 vty_out(vty, "%% Pseudowire is not static\n");
325 return CMD_WARNING;
326 }
327
328 if (argv_find(argv, argc, "no", &idx)) {
329 if (!pw)
330 return CMD_SUCCESS;
331 zebra_pw_del(zvrf, pw);
332 return CMD_SUCCESS;
333 }
334
335 if (!pw)
336 pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
337 VTY_PUSH_CONTEXT(PW_NODE, pw);
338
339 return CMD_SUCCESS;
340 }
341
342 DEFUN (pseudowire_labels,
343 pseudowire_labels_cmd,
344 "[no] mpls label local (16-1048575) remote (16-1048575)",
345 NO_STR
346 "MPLS L2VPN PW command\n"
347 "MPLS L2VPN static labels\n"
348 "Local pseudowire label\n"
349 "Local pseudowire label\n"
350 "Remote pseudowire label\n"
351 "Remote pseudowire label\n")
352 {
353 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
354 int idx = 0;
355 mpls_label_t local_label, remote_label;
356
357 if (argv_find(argv, argc, "no", &idx)) {
358 local_label = MPLS_NO_LABEL;
359 remote_label = MPLS_NO_LABEL;
360 } else {
361 argv_find(argv, argc, "local", &idx);
362 local_label = atoi(argv[idx + 1]->arg);
363 argv_find(argv, argc, "remote", &idx);
364 remote_label = atoi(argv[idx + 1]->arg);
365 }
366
367 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
368 local_label, remote_label, pw->flags, &pw->data);
369
370 return CMD_SUCCESS;
371 }
372
373 DEFUN (pseudowire_neighbor,
374 pseudowire_neighbor_cmd,
375 "[no] neighbor <A.B.C.D|X:X::X:X>",
376 NO_STR
377 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
378 "IPv4 address\n"
379 "IPv6 address\n")
380 {
381 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
382 int idx = 0;
383 const char *address;
384 int af;
385 union g_addr nexthop;
386
387 af = AF_UNSPEC;
388 memset(&nexthop, 0, sizeof(nexthop));
389
390 if (!argv_find(argv, argc, "no", &idx)) {
391 argv_find(argv, argc, "neighbor", &idx);
392 address = argv[idx + 1]->arg;
393
394 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
395 af = AF_INET;
396 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
397 af = AF_INET6;
398 else {
399 vty_out(vty, "%% Malformed address\n");
400 return CMD_WARNING;
401 }
402 }
403
404 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
405 pw->local_label, pw->remote_label, pw->flags,
406 &pw->data);
407
408 return CMD_SUCCESS;
409 }
410
411 DEFUN (pseudowire_control_word,
412 pseudowire_control_word_cmd,
413 "[no] control-word <exclude|include>",
414 NO_STR
415 "Control-word options\n"
416 "Exclude control-word in pseudowire packets\n"
417 "Include control-word in pseudowire packets\n")
418 {
419 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
420 int idx = 0;
421 uint8_t flags = 0;
422
423 if (argv_find(argv, argc, "no", &idx))
424 flags = F_PSEUDOWIRE_CWORD;
425 else {
426 argv_find(argv, argc, "control-word", &idx);
427 if (argv[idx + 1]->text[0] == 'i')
428 flags = F_PSEUDOWIRE_CWORD;
429 }
430
431 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
432 pw->local_label, pw->remote_label, flags, &pw->data);
433
434 return CMD_SUCCESS;
435 }
436
437 DEFUN (show_pseudowires,
438 show_pseudowires_cmd,
439 "show mpls pseudowires",
440 SHOW_STR
441 MPLS_STR
442 "Pseudowires\n")
443 {
444 struct zebra_vrf *zvrf;
445 struct zebra_pw *pw;
446
447 zvrf = vrf_info_lookup(VRF_DEFAULT);
448 if (!zvrf)
449 return 0;
450
451 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
452 "Labels", "Protocol", "Status");
453
454 RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
455 char buf_nbr[INET6_ADDRSTRLEN];
456 char buf_labels[64];
457
458 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
459
460 if (pw->local_label != MPLS_NO_LABEL
461 && pw->remote_label != MPLS_NO_LABEL)
462 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
463 pw->local_label, pw->remote_label);
464 else
465 snprintf(buf_labels, sizeof(buf_labels), "-");
466
467 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
468 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
469 zebra_route_string(pw->protocol),
470 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
471 ? "UP"
472 : "DOWN");
473 }
474
475 return CMD_SUCCESS;
476 }
477
478 /* Pseudowire configuration write function. */
479 static int zebra_pw_config(struct vty *vty)
480 {
481 int write = 0;
482 struct zebra_vrf *zvrf;
483 struct zebra_pw *pw;
484
485 zvrf = vrf_info_lookup(VRF_DEFAULT);
486 if (!zvrf)
487 return 0;
488
489 RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
490 vty_out(vty, "pseudowire %s\n", pw->ifname);
491 if (pw->local_label != MPLS_NO_LABEL
492 && pw->remote_label != MPLS_NO_LABEL)
493 vty_out(vty, " mpls label local %u remote %u\n",
494 pw->local_label, pw->remote_label);
495 else
496 vty_out(vty,
497 " ! Incomplete config, specify the static "
498 "MPLS labels\n");
499
500 if (pw->af != AF_UNSPEC) {
501 char buf[INET6_ADDRSTRLEN];
502 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
503 vty_out(vty, " neighbor %s\n", buf);
504 } else
505 vty_out(vty,
506 " ! Incomplete config, specify a neighbor "
507 "address\n");
508
509 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
510 vty_out(vty, " control-word exclude\n");
511
512 vty_out(vty, "!\n");
513 write = 1;
514 }
515
516 return write;
517 }
518
519 static struct cmd_node pw_node = {
520 PW_NODE, "%s(config-pw)# ", 1,
521 };
522
523 void zebra_pw_vty_init(void)
524 {
525 install_node(&pw_node, zebra_pw_config);
526 install_default(PW_NODE);
527
528 install_element(CONFIG_NODE, &pseudowire_if_cmd);
529 install_element(PW_NODE, &pseudowire_labels_cmd);
530 install_element(PW_NODE, &pseudowire_neighbor_cmd);
531 install_element(PW_NODE, &pseudowire_control_word_cmd);
532
533 install_element(VIEW_NODE, &show_pseudowires_cmd);
534 }