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