]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_pw.c
zebra: On shutdown don't count removals
[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"
30#include "zebra/zserv.h"
731a75fe 31#include "zebra/zebra_rnh.h"
6833ae01 32#include "zebra/zebra_vrf.h"
33#include "zebra/zebra_pw.h"
34
35DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire")
36
2dd0d726
RW
37DEFINE_QOBJ_TYPE(zebra_pw)
38
6833ae01 39DEFINE_HOOK(pw_install, (struct zebra_pw * pw), (pw))
40DEFINE_HOOK(pw_uninstall, (struct zebra_pw * pw), (pw))
41
69965f53
DL
42#define MPLS_NO_LABEL MPLS_INVALID_LABEL
43
6833ae01 44extern struct zebra_t zebrad;
45
2dd0d726 46static int zebra_pw_enabled(struct zebra_pw *);
6833ae01 47static void zebra_pw_install(struct zebra_pw *);
48static void zebra_pw_uninstall(struct zebra_pw *);
49static int zebra_pw_install_retry(struct thread *);
50static int zebra_pw_check_reachability(struct zebra_pw *);
51static void zebra_pw_update_status(struct zebra_pw *, int);
52
53static 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
2dd0d726
RW
59RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
60RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)
6833ae01 61
62struct 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;
3c5b5220 76 pw->status = PW_STATUS_DOWN;
2dd0d726
RW
77 pw->local_label = MPLS_NO_LABEL;
78 pw->remote_label = MPLS_NO_LABEL;
79 pw->flags = F_PSEUDOWIRE_CWORD;
6833ae01 80
81 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
2dd0d726
RW
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 }
6833ae01 86
87 return pw;
88}
89
90void 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
731a75fe
RW
96 /* remove nexthop tracking */
97 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
98
6833ae01 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);
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);
731a75fe 156 /* wait for NHT and try again later */
6833ae01 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
166static 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
182static 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
2dd0d726 195 if (zebra_pw_enabled(pw))
6833ae01 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 */
205void 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);
69965f53
DL
215 thread_add_timer(zebrad.master, zebra_pw_install_retry, pw,
216 PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);
6833ae01 217
218 zebra_pw_update_status(pw, PW_STATUS_DOWN);
219}
220
221static 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
231static 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
238static int zebra_pw_check_reachability(struct zebra_pw *pw)
239{
69965f53
DL
240 struct route_entry *re;
241 struct nexthop *nexthop;
6833ae01 242
243 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
244
245 /* find route to the remote end of the pseudowire */
69965f53
DL
246 re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
247 &pw->nexthop, NULL);
248 if (!re) {
6833ae01 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 */
69965f53 259 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
6833ae01 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
271void zebra_pw_client_close(struct zserv *client)
272{
273 struct vrf *vrf;
274 struct zebra_vrf *zvrf;
275 struct zebra_pw *pw, *tmp;
276
a2addae8 277 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
6833ae01 278 zvrf = vrf->info;
a2addae8 279 RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
6833ae01 280 if (pw->client != client)
281 continue;
282 zebra_pw_del(zvrf, pw);
283 }
284 }
285}
286
287void zebra_pw_init(struct zebra_vrf *zvrf)
288{
69965f53
DL
289 RB_INIT(zebra_pw_head, &zvrf->pseudowires);
290 RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
6833ae01 291}
292
293void zebra_pw_exit(struct zebra_vrf *zvrf)
294{
295 struct zebra_pw *pw;
296
69965f53 297 while ((pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires)) != NULL)
6833ae01 298 zebra_pw_del(zvrf, pw);
299}
2dd0d726
RW
300
301DEFUN_NOSH (pseudowire_if,
302 pseudowire_if_cmd,
303 "[no] pseudowire IFNAME",
304 NO_STR
305 "Static pseudowire configuration\n"
306 "Pseudowire name\n")
307{
308 struct zebra_vrf *zvrf;
309 struct zebra_pw *pw;
310 int idx = 0;
311 const char *ifname;
312
313 zvrf = vrf_info_lookup(VRF_DEFAULT);
314 if (!zvrf)
315 return CMD_WARNING;
316
317 argv_find(argv, argc, "IFNAME", &idx);
318 ifname = argv[idx]->arg;
319 pw = zebra_pw_find(zvrf, ifname);
320 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
69965f53 321 vty_out(vty, "%% Pseudowire is not static\n");
2dd0d726
RW
322 return CMD_WARNING;
323 }
324
325 if (argv_find(argv, argc, "no", &idx)) {
326 if (!pw)
327 return CMD_SUCCESS;
328 zebra_pw_del(zvrf, pw);
0af35d90 329 return CMD_SUCCESS;
2dd0d726
RW
330 }
331
332 if (!pw)
333 pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
334 VTY_PUSH_CONTEXT(PW_NODE, pw);
335
336 return CMD_SUCCESS;
337}
338
339DEFUN (pseudowire_labels,
340 pseudowire_labels_cmd,
341 "[no] mpls label local (16-1048575) remote (16-1048575)",
342 NO_STR
343 "MPLS L2VPN PW command\n"
344 "MPLS L2VPN static labels\n"
345 "Local pseudowire label\n"
346 "Local pseudowire label\n"
347 "Remote pseudowire label\n"
348 "Remote pseudowire label\n")
349{
350 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
351 int idx = 0;
352 mpls_label_t local_label, remote_label;
353
354 if (argv_find(argv, argc, "no", &idx)) {
355 local_label = MPLS_NO_LABEL;
356 remote_label = MPLS_NO_LABEL;
357 } else {
358 argv_find(argv, argc, "local", &idx);
359 local_label = atoi(argv[idx + 1]->arg);
360 argv_find(argv, argc, "remote", &idx);
361 remote_label = atoi(argv[idx + 1]->arg);
362 }
363
364 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
365 local_label, remote_label, pw->flags, &pw->data);
366
367 return CMD_SUCCESS;
368}
369
370DEFUN (pseudowire_neighbor,
371 pseudowire_neighbor_cmd,
372 "[no] neighbor <A.B.C.D|X:X::X:X>",
373 NO_STR
374 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
375 "IPv4 address\n"
376 "IPv6 address\n")
377{
378 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
379 int idx = 0;
380 const char *address;
381 int af;
382 union g_addr nexthop;
383
384 af = AF_UNSPEC;
385 memset(&nexthop, 0, sizeof(nexthop));
386
387 if (!argv_find(argv, argc, "no", &idx)) {
388 argv_find(argv, argc, "neighbor", &idx);
389 address = argv[idx + 1]->arg;
390
391 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
392 af = AF_INET;
393 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
394 af = AF_INET6;
395 else {
69965f53 396 vty_out(vty, "%% Malformed address\n");
2dd0d726
RW
397 return CMD_WARNING;
398 }
399 }
400
401 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
402 pw->local_label, pw->remote_label, pw->flags,
403 &pw->data);
404
405 return CMD_SUCCESS;
406}
407
408DEFUN (pseudowire_control_word,
409 pseudowire_control_word_cmd,
410 "[no] control-word <exclude|include>",
411 NO_STR
412 "Control-word options\n"
413 "Exclude control-word in pseudowire packets\n"
414 "Include control-word in pseudowire packets\n")
415{
416 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
417 int idx = 0;
418 uint8_t flags = 0;
419
420 if (argv_find(argv, argc, "no", &idx))
421 flags = F_PSEUDOWIRE_CWORD;
422 else {
423 argv_find(argv, argc, "control-word", &idx);
424 if (argv[idx + 1]->text[0] == 'i')
425 flags = F_PSEUDOWIRE_CWORD;
426 }
427
428 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
429 pw->local_label, pw->remote_label, flags, &pw->data);
430
431 return CMD_SUCCESS;
432}
433
434DEFUN (show_pseudowires,
435 show_pseudowires_cmd,
d261dd7e 436 "show mpls pseudowires",
2dd0d726 437 SHOW_STR
d261dd7e 438 MPLS_STR
efd7904e 439 "Pseudowires\n")
2dd0d726
RW
440{
441 struct zebra_vrf *zvrf;
442 struct zebra_pw *pw;
443
444 zvrf = vrf_info_lookup(VRF_DEFAULT);
445 if (!zvrf)
446 return 0;
447
69965f53
DL
448 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
449 "Labels", "Protocol", "Status");
2dd0d726 450
a2addae8 451 RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
2dd0d726
RW
452 char buf_nbr[INET6_ADDRSTRLEN];
453 char buf_labels[64];
454
455 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
456
457 if (pw->local_label != MPLS_NO_LABEL
458 && pw->remote_label != MPLS_NO_LABEL)
459 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
460 pw->local_label, pw->remote_label);
461 else
462 snprintf(buf_labels, sizeof(buf_labels), "-");
463
69965f53 464 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
2dd0d726
RW
465 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
466 zebra_route_string(pw->protocol),
467 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
468 ? "UP"
69965f53 469 : "DOWN");
2dd0d726
RW
470 }
471
472 return CMD_SUCCESS;
473}
474
475/* Pseudowire configuration write function. */
476static int zebra_pw_config(struct vty *vty)
477{
478 int write = 0;
479 struct zebra_vrf *zvrf;
480 struct zebra_pw *pw;
481
482 zvrf = vrf_info_lookup(VRF_DEFAULT);
483 if (!zvrf)
484 return 0;
485
a2addae8 486 RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
69965f53 487 vty_out(vty, "pseudowire %s\n", pw->ifname);
2dd0d726
RW
488 if (pw->local_label != MPLS_NO_LABEL
489 && pw->remote_label != MPLS_NO_LABEL)
69965f53
DL
490 vty_out(vty, " mpls label local %u remote %u\n",
491 pw->local_label, pw->remote_label);
2dd0d726
RW
492 else
493 vty_out(vty,
494 " ! Incomplete config, specify the static "
69965f53 495 "MPLS labels\n");
2dd0d726
RW
496
497 if (pw->af != AF_UNSPEC) {
498 char buf[INET6_ADDRSTRLEN];
499 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
69965f53 500 vty_out(vty, " neighbor %s\n", buf);
2dd0d726
RW
501 } else
502 vty_out(vty,
503 " ! Incomplete config, specify a neighbor "
69965f53 504 "address\n");
2dd0d726
RW
505
506 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
69965f53 507 vty_out(vty, " control-word exclude\n");
2dd0d726 508
69965f53 509 vty_out(vty, "!\n");
2dd0d726
RW
510 write = 1;
511 }
512
513 return write;
514}
515
516static struct cmd_node pw_node = {
517 PW_NODE, "%s(config-pw)# ", 1,
518};
519
520void zebra_pw_vty_init(void)
521{
522 install_node(&pw_node, zebra_pw_config);
523 install_default(PW_NODE);
524
525 install_element(CONFIG_NODE, &pseudowire_if_cmd);
526 install_element(PW_NODE, &pseudowire_labels_cmd);
527 install_element(PW_NODE, &pseudowire_neighbor_cmd);
528 install_element(PW_NODE, &pseudowire_control_word_cmd);
529
530 install_element(VIEW_NODE, &show_pseudowires_cmd);
531}