]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_pw.c
zebra: include zebra nexthop debug in show runn
[mirror_frr.git] / zebra / zebra_pw.c
CommitLineData
6833ae01 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"
2dd0d726 25#include "command.h"
6833ae01 26#include "vrf.h"
27
28#include "zebra/debug.h"
29#include "zebra/rib.h"
3801e764 30#include "zebra/zebra_router.h"
bf094f69 31#include "zebra/zapi_msg.h"
731a75fe 32#include "zebra/zebra_rnh.h"
6833ae01 33#include "zebra/zebra_vrf.h"
34#include "zebra/zebra_pw.h"
35
36DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire")
37
2dd0d726
RW
38DEFINE_QOBJ_TYPE(zebra_pw)
39
6833ae01 40DEFINE_HOOK(pw_install, (struct zebra_pw * pw), (pw))
41DEFINE_HOOK(pw_uninstall, (struct zebra_pw * pw), (pw))
42
69965f53
DL
43#define MPLS_NO_LABEL MPLS_INVALID_LABEL
44
2dd0d726 45static int zebra_pw_enabled(struct zebra_pw *);
6833ae01 46static void zebra_pw_install(struct zebra_pw *);
47static void zebra_pw_uninstall(struct zebra_pw *);
48static int zebra_pw_install_retry(struct thread *);
49static int zebra_pw_check_reachability(struct zebra_pw *);
50static void zebra_pw_update_status(struct zebra_pw *, int);
51
52static inline int zebra_pw_compare(const struct zebra_pw *a,
53 const struct zebra_pw *b)
54{
55 return (strcmp(a->ifname, b->ifname));
56}
57
2dd0d726
RW
58RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
59RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)
6833ae01 60
61struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
62 uint8_t protocol, struct zserv *client)
63{
64 struct zebra_pw *pw;
65
66 if (IS_ZEBRA_DEBUG_PW)
67 zlog_debug("%u: adding pseudowire %s protocol %s",
68 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
69
70 pw = XCALLOC(MTYPE_PW, sizeof(*pw));
71 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
72 pw->protocol = protocol;
73 pw->vrf_id = zvrf_id(zvrf);
74 pw->client = client;
3c5b5220 75 pw->status = PW_STATUS_DOWN;
2dd0d726
RW
76 pw->local_label = MPLS_NO_LABEL;
77 pw->remote_label = MPLS_NO_LABEL;
78 pw->flags = F_PSEUDOWIRE_CWORD;
6833ae01 79
80 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
2dd0d726
RW
81 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
82 RB_INSERT(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
83 QOBJ_REG(pw, zebra_pw);
84 }
6833ae01 85
86 return pw;
87}
88
89void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
90{
91 if (IS_ZEBRA_DEBUG_PW)
92 zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
93 pw->ifname, zebra_route_string(pw->protocol));
94
731a75fe
RW
95 /* remove nexthop tracking */
96 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
97
6833ae01 98 /* uninstall */
97d8d05a 99 if (pw->status == PW_STATUS_UP) {
6833ae01 100 hook_call(pw_uninstall, pw);
97d8d05a
MS
101 dplane_pw_uninstall(pw);
102 } else if (pw->install_retry_timer)
6833ae01 103 THREAD_TIMER_OFF(pw->install_retry_timer);
104
105 /* unlink and release memory */
106 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
2dd0d726
RW
107 if (pw->protocol == ZEBRA_ROUTE_STATIC)
108 RB_REMOVE(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
6833ae01 109 XFREE(MTYPE_PW, pw);
110}
111
112void 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{
731a75fe
RW
117 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
118
6833ae01 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
2dd0d726 128 if (zebra_pw_enabled(pw))
731a75fe 129 zebra_register_rnh_pseudowire(pw->vrf_id, pw);
6833ae01 130 else
131 zebra_pw_uninstall(pw);
132}
133
134struct 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
2dd0d726
RW
141static 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
6833ae01 152void zebra_pw_update(struct zebra_pw *pw)
153{
154 if (zebra_pw_check_reachability(pw) < 0) {
155 zebra_pw_uninstall(pw);
72b1ee34 156 zebra_pw_install_failure(pw);
731a75fe 157 /* wait for NHT and try again later */
6833ae01 158 } else {
159 /*
160 * Install or reinstall the pseudowire (e.g. to update
161 * parameters like the nexthop or the use of the control word).
162 */
163 zebra_pw_install(pw);
164 }
165}
166
167static void zebra_pw_install(struct zebra_pw *pw)
168{
169 if (IS_ZEBRA_DEBUG_PW)
170 zlog_debug("%u: installing pseudowire %s protocol %s",
171 pw->vrf_id, pw->ifname,
172 zebra_route_string(pw->protocol));
173
97d8d05a
MS
174 hook_call(pw_install, pw);
175 if (dplane_pw_install(pw) == ZEBRA_DPLANE_REQUEST_FAILURE) {
6833ae01 176 zebra_pw_install_failure(pw);
177 return;
178 }
179
180 if (pw->status == PW_STATUS_DOWN)
181 zebra_pw_update_status(pw, PW_STATUS_UP);
182}
183
184static void zebra_pw_uninstall(struct zebra_pw *pw)
185{
186 if (pw->status == PW_STATUS_DOWN)
187 return;
188
189 if (IS_ZEBRA_DEBUG_PW)
190 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
191 pw->vrf_id, pw->ifname,
192 zebra_route_string(pw->protocol));
193
194 /* ignore any possible error */
195 hook_call(pw_uninstall, pw);
97d8d05a 196 dplane_pw_uninstall(pw);
6833ae01 197
2dd0d726 198 if (zebra_pw_enabled(pw))
6833ae01 199 zebra_pw_update_status(pw, PW_STATUS_DOWN);
200}
201
202/*
203 * Installation of the pseudowire in the kernel or hardware has failed. This
204 * function will notify the pseudowire client about the failure and schedule
205 * to retry the installation later. This function can be called by an external
206 * agent that performs the pseudowire installation in an asynchronous way.
207 */
208void zebra_pw_install_failure(struct zebra_pw *pw)
209{
210 if (IS_ZEBRA_DEBUG_PW)
211 zlog_debug(
212 "%u: failed installing pseudowire %s, "
213 "scheduling retry in %u seconds",
214 pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);
215
216 /* schedule to retry later */
217 THREAD_TIMER_OFF(pw->install_retry_timer);
3801e764 218 thread_add_timer(zrouter.master, zebra_pw_install_retry, pw,
69965f53 219 PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);
6833ae01 220
221 zebra_pw_update_status(pw, PW_STATUS_DOWN);
222}
223
224static int zebra_pw_install_retry(struct thread *thread)
225{
226 struct zebra_pw *pw = THREAD_ARG(thread);
227
228 pw->install_retry_timer = NULL;
229 zebra_pw_install(pw);
230
231 return 0;
232}
233
234static void zebra_pw_update_status(struct zebra_pw *pw, int status)
235{
236 pw->status = status;
237 if (pw->client)
238 zsend_pw_update(pw->client, pw);
239}
240
241static int zebra_pw_check_reachability(struct zebra_pw *pw)
242{
69965f53
DL
243 struct route_entry *re;
244 struct nexthop *nexthop;
6833ae01 245
246 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
247
248 /* find route to the remote end of the pseudowire */
69965f53
DL
249 re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
250 &pw->nexthop, NULL);
251 if (!re) {
6833ae01 252 if (IS_ZEBRA_DEBUG_PW)
9df414fe
QY
253 zlog_debug("%s: no route found for %s", __func__,
254 pw->ifname);
6833ae01 255 return -1;
256 }
257
258 /*
259 * Need to ensure that there's a label binding for all nexthops.
260 * Otherwise, ECMP for this route could render the pseudowire unusable.
261 */
0eb97b86 262 for (ALL_NEXTHOPS_PTR(re->nhe->nhg, nexthop)) {
6833ae01 263 if (!nexthop->nh_label) {
264 if (IS_ZEBRA_DEBUG_PW)
9df414fe
QY
265 zlog_debug("%s: unlabeled route for %s",
266 __func__, pw->ifname);
6833ae01 267 return -1;
268 }
269 }
270
271 return 0;
272}
273
453844ab 274static int zebra_pw_client_close(struct zserv *client)
6833ae01 275{
276 struct vrf *vrf;
277 struct zebra_vrf *zvrf;
278 struct zebra_pw *pw, *tmp;
279
a2addae8 280 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
6833ae01 281 zvrf = vrf->info;
a2addae8 282 RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
6833ae01 283 if (pw->client != client)
284 continue;
285 zebra_pw_del(zvrf, pw);
286 }
287 }
453844ab
QY
288
289 return 0;
6833ae01 290}
291
292void zebra_pw_init(struct zebra_vrf *zvrf)
293{
69965f53
DL
294 RB_INIT(zebra_pw_head, &zvrf->pseudowires);
295 RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
453844ab 296
21ccc0cf 297 hook_register(zserv_client_close, zebra_pw_client_close);
6833ae01 298}
299
300void zebra_pw_exit(struct zebra_vrf *zvrf)
301{
302 struct zebra_pw *pw;
303
55cd0f61
DS
304 while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) {
305 pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires);
306
6833ae01 307 zebra_pw_del(zvrf, pw);
55cd0f61 308 }
6833ae01 309}
2dd0d726
RW
310
311DEFUN_NOSH (pseudowire_if,
312 pseudowire_if_cmd,
1e9d1183 313 "pseudowire IFNAME",
2dd0d726
RW
314 "Static pseudowire configuration\n"
315 "Pseudowire name\n")
316{
317 struct zebra_vrf *zvrf;
318 struct zebra_pw *pw;
2dd0d726 319 const char *ifname;
1e9d1183 320 int idx = 0;
2dd0d726
RW
321
322 zvrf = vrf_info_lookup(VRF_DEFAULT);
323 if (!zvrf)
324 return CMD_WARNING;
325
326 argv_find(argv, argc, "IFNAME", &idx);
327 ifname = argv[idx]->arg;
1e9d1183 328
2dd0d726
RW
329 pw = zebra_pw_find(zvrf, ifname);
330 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
69965f53 331 vty_out(vty, "%% Pseudowire is not static\n");
2dd0d726
RW
332 return CMD_WARNING;
333 }
334
2dd0d726
RW
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
1e9d1183
RW
342DEFUN (no_pseudowire_if,
343 no_pseudowire_if_cmd,
344 "no pseudowire IFNAME",
345 NO_STR
346 "Static pseudowire configuration\n"
347 "Pseudowire name\n")
348{
349 struct zebra_vrf *zvrf;
350 struct zebra_pw *pw;
351 const char *ifname;
352 int idx = 0;
353
354 zvrf = vrf_info_lookup(VRF_DEFAULT);
355 if (!zvrf)
356 return CMD_WARNING;
357
358 argv_find(argv, argc, "IFNAME", &idx);
359 ifname = argv[idx]->arg;
360
361 pw = zebra_pw_find(zvrf, ifname);
362 if (pw) {
363 if (pw->protocol != ZEBRA_ROUTE_STATIC) {
364 vty_out(vty, "%% Pseudowire is not static\n");
365 return CMD_WARNING;
366 }
367 zebra_pw_del(zvrf, pw);
368 }
369
370 return CMD_SUCCESS;
371}
372
2dd0d726
RW
373DEFUN (pseudowire_labels,
374 pseudowire_labels_cmd,
375 "[no] mpls label local (16-1048575) remote (16-1048575)",
376 NO_STR
377 "MPLS L2VPN PW command\n"
378 "MPLS L2VPN static labels\n"
379 "Local pseudowire label\n"
380 "Local pseudowire label\n"
381 "Remote pseudowire label\n"
382 "Remote pseudowire label\n")
383{
384 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
385 int idx = 0;
386 mpls_label_t local_label, remote_label;
387
388 if (argv_find(argv, argc, "no", &idx)) {
389 local_label = MPLS_NO_LABEL;
390 remote_label = MPLS_NO_LABEL;
391 } else {
392 argv_find(argv, argc, "local", &idx);
393 local_label = atoi(argv[idx + 1]->arg);
394 argv_find(argv, argc, "remote", &idx);
395 remote_label = atoi(argv[idx + 1]->arg);
396 }
397
398 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
399 local_label, remote_label, pw->flags, &pw->data);
400
401 return CMD_SUCCESS;
402}
403
404DEFUN (pseudowire_neighbor,
405 pseudowire_neighbor_cmd,
406 "[no] neighbor <A.B.C.D|X:X::X:X>",
407 NO_STR
408 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
409 "IPv4 address\n"
410 "IPv6 address\n")
411{
412 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
413 int idx = 0;
414 const char *address;
415 int af;
416 union g_addr nexthop;
417
418 af = AF_UNSPEC;
419 memset(&nexthop, 0, sizeof(nexthop));
420
421 if (!argv_find(argv, argc, "no", &idx)) {
422 argv_find(argv, argc, "neighbor", &idx);
423 address = argv[idx + 1]->arg;
424
425 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
426 af = AF_INET;
427 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
428 af = AF_INET6;
429 else {
69965f53 430 vty_out(vty, "%% Malformed address\n");
2dd0d726
RW
431 return CMD_WARNING;
432 }
433 }
434
435 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
436 pw->local_label, pw->remote_label, pw->flags,
437 &pw->data);
438
439 return CMD_SUCCESS;
440}
441
442DEFUN (pseudowire_control_word,
443 pseudowire_control_word_cmd,
444 "[no] control-word <exclude|include>",
445 NO_STR
446 "Control-word options\n"
447 "Exclude control-word in pseudowire packets\n"
448 "Include control-word in pseudowire packets\n")
449{
450 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
451 int idx = 0;
452 uint8_t flags = 0;
453
454 if (argv_find(argv, argc, "no", &idx))
455 flags = F_PSEUDOWIRE_CWORD;
456 else {
457 argv_find(argv, argc, "control-word", &idx);
458 if (argv[idx + 1]->text[0] == 'i')
459 flags = F_PSEUDOWIRE_CWORD;
460 }
461
462 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
463 pw->local_label, pw->remote_label, flags, &pw->data);
464
465 return CMD_SUCCESS;
466}
467
468DEFUN (show_pseudowires,
469 show_pseudowires_cmd,
d261dd7e 470 "show mpls pseudowires",
2dd0d726 471 SHOW_STR
d261dd7e 472 MPLS_STR
efd7904e 473 "Pseudowires\n")
2dd0d726
RW
474{
475 struct zebra_vrf *zvrf;
476 struct zebra_pw *pw;
477
478 zvrf = vrf_info_lookup(VRF_DEFAULT);
479 if (!zvrf)
480 return 0;
481
69965f53
DL
482 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
483 "Labels", "Protocol", "Status");
2dd0d726 484
a2addae8 485 RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
2dd0d726
RW
486 char buf_nbr[INET6_ADDRSTRLEN];
487 char buf_labels[64];
488
489 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
490
491 if (pw->local_label != MPLS_NO_LABEL
492 && pw->remote_label != MPLS_NO_LABEL)
493 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
494 pw->local_label, pw->remote_label);
495 else
496 snprintf(buf_labels, sizeof(buf_labels), "-");
497
69965f53 498 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
2dd0d726
RW
499 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
500 zebra_route_string(pw->protocol),
501 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
502 ? "UP"
69965f53 503 : "DOWN");
2dd0d726
RW
504 }
505
506 return CMD_SUCCESS;
507}
508
509/* Pseudowire configuration write function. */
510static int zebra_pw_config(struct vty *vty)
511{
512 int write = 0;
513 struct zebra_vrf *zvrf;
514 struct zebra_pw *pw;
515
516 zvrf = vrf_info_lookup(VRF_DEFAULT);
517 if (!zvrf)
518 return 0;
519
a2addae8 520 RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
69965f53 521 vty_out(vty, "pseudowire %s\n", pw->ifname);
2dd0d726
RW
522 if (pw->local_label != MPLS_NO_LABEL
523 && pw->remote_label != MPLS_NO_LABEL)
69965f53
DL
524 vty_out(vty, " mpls label local %u remote %u\n",
525 pw->local_label, pw->remote_label);
2dd0d726
RW
526 else
527 vty_out(vty,
528 " ! Incomplete config, specify the static "
69965f53 529 "MPLS labels\n");
2dd0d726
RW
530
531 if (pw->af != AF_UNSPEC) {
532 char buf[INET6_ADDRSTRLEN];
533 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
69965f53 534 vty_out(vty, " neighbor %s\n", buf);
2dd0d726
RW
535 } else
536 vty_out(vty,
537 " ! Incomplete config, specify a neighbor "
69965f53 538 "address\n");
2dd0d726
RW
539
540 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
69965f53 541 vty_out(vty, " control-word exclude\n");
2dd0d726 542
69965f53 543 vty_out(vty, "!\n");
2dd0d726
RW
544 write = 1;
545 }
546
547 return write;
548}
549
550static struct cmd_node pw_node = {
551 PW_NODE, "%s(config-pw)# ", 1,
552};
553
554void zebra_pw_vty_init(void)
555{
556 install_node(&pw_node, zebra_pw_config);
557 install_default(PW_NODE);
558
559 install_element(CONFIG_NODE, &pseudowire_if_cmd);
1e9d1183 560 install_element(CONFIG_NODE, &no_pseudowire_if_cmd);
2dd0d726
RW
561 install_element(PW_NODE, &pseudowire_labels_cmd);
562 install_element(PW_NODE, &pseudowire_neighbor_cmd);
563 install_element(PW_NODE, &pseudowire_control_word_cmd);
564
565 install_element(VIEW_NODE, &show_pseudowires_cmd);
566}