]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pw.c
Merge pull request #3723 from slrz/zebra-rtadv-add-rfc8106-support
[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 dplane_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 hook_call(pw_install, pw);
174 if (dplane_pw_install(pw) == ZEBRA_DPLANE_REQUEST_FAILURE) {
175 zebra_pw_install_failure(pw);
176 return;
177 }
178
179 if (pw->status == PW_STATUS_DOWN)
180 zebra_pw_update_status(pw, PW_STATUS_UP);
181 }
182
183 static void zebra_pw_uninstall(struct zebra_pw *pw)
184 {
185 if (pw->status == PW_STATUS_DOWN)
186 return;
187
188 if (IS_ZEBRA_DEBUG_PW)
189 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
190 pw->vrf_id, pw->ifname,
191 zebra_route_string(pw->protocol));
192
193 /* ignore any possible error */
194 hook_call(pw_uninstall, pw);
195 dplane_pw_uninstall(pw);
196
197 if (zebra_pw_enabled(pw))
198 zebra_pw_update_status(pw, PW_STATUS_DOWN);
199 }
200
201 /*
202 * Installation of the pseudowire in the kernel or hardware has failed. This
203 * function will notify the pseudowire client about the failure and schedule
204 * to retry the installation later. This function can be called by an external
205 * agent that performs the pseudowire installation in an asynchronous way.
206 */
207 void zebra_pw_install_failure(struct zebra_pw *pw)
208 {
209 if (IS_ZEBRA_DEBUG_PW)
210 zlog_debug(
211 "%u: failed installing pseudowire %s, "
212 "scheduling retry in %u seconds",
213 pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);
214
215 /* schedule to retry later */
216 THREAD_TIMER_OFF(pw->install_retry_timer);
217 thread_add_timer(zrouter.master, zebra_pw_install_retry, pw,
218 PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);
219
220 zebra_pw_update_status(pw, PW_STATUS_DOWN);
221 }
222
223 static int zebra_pw_install_retry(struct thread *thread)
224 {
225 struct zebra_pw *pw = THREAD_ARG(thread);
226
227 pw->install_retry_timer = NULL;
228 zebra_pw_install(pw);
229
230 return 0;
231 }
232
233 static void zebra_pw_update_status(struct zebra_pw *pw, int status)
234 {
235 pw->status = status;
236 if (pw->client)
237 zsend_pw_update(pw->client, pw);
238 }
239
240 static int zebra_pw_check_reachability(struct zebra_pw *pw)
241 {
242 struct route_entry *re;
243 struct nexthop *nexthop;
244
245 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
246
247 /* find route to the remote end of the pseudowire */
248 re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
249 &pw->nexthop, NULL);
250 if (!re) {
251 if (IS_ZEBRA_DEBUG_PW)
252 zlog_debug("%s: no route found for %s", __func__,
253 pw->ifname);
254 return -1;
255 }
256
257 /*
258 * Need to ensure that there's a label binding for all nexthops.
259 * Otherwise, ECMP for this route could render the pseudowire unusable.
260 */
261 for (ALL_NEXTHOPS(re->ng, nexthop)) {
262 if (!nexthop->nh_label) {
263 if (IS_ZEBRA_DEBUG_PW)
264 zlog_debug("%s: unlabeled route for %s",
265 __func__, pw->ifname);
266 return -1;
267 }
268 }
269
270 return 0;
271 }
272
273 static int zebra_pw_client_close(struct zserv *client)
274 {
275 struct vrf *vrf;
276 struct zebra_vrf *zvrf;
277 struct zebra_pw *pw, *tmp;
278
279 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
280 zvrf = vrf->info;
281 RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
282 if (pw->client != client)
283 continue;
284 zebra_pw_del(zvrf, pw);
285 }
286 }
287
288 return 0;
289 }
290
291 void zebra_pw_init(struct zebra_vrf *zvrf)
292 {
293 RB_INIT(zebra_pw_head, &zvrf->pseudowires);
294 RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
295
296 hook_register(zserv_client_close, zebra_pw_client_close);
297 }
298
299 void zebra_pw_exit(struct zebra_vrf *zvrf)
300 {
301 struct zebra_pw *pw;
302
303 while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) {
304 pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires);
305
306 zebra_pw_del(zvrf, pw);
307 }
308 }
309
310 DEFUN_NOSH (pseudowire_if,
311 pseudowire_if_cmd,
312 "pseudowire IFNAME",
313 "Static pseudowire configuration\n"
314 "Pseudowire name\n")
315 {
316 struct zebra_vrf *zvrf;
317 struct zebra_pw *pw;
318 const char *ifname;
319 int idx = 0;
320
321 zvrf = vrf_info_lookup(VRF_DEFAULT);
322 if (!zvrf)
323 return CMD_WARNING;
324
325 argv_find(argv, argc, "IFNAME", &idx);
326 ifname = argv[idx]->arg;
327
328 pw = zebra_pw_find(zvrf, ifname);
329 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
330 vty_out(vty, "%% Pseudowire is not static\n");
331 return CMD_WARNING;
332 }
333
334 if (!pw)
335 pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
336 VTY_PUSH_CONTEXT(PW_NODE, pw);
337
338 return CMD_SUCCESS;
339 }
340
341 DEFUN (no_pseudowire_if,
342 no_pseudowire_if_cmd,
343 "no pseudowire IFNAME",
344 NO_STR
345 "Static pseudowire configuration\n"
346 "Pseudowire name\n")
347 {
348 struct zebra_vrf *zvrf;
349 struct zebra_pw *pw;
350 const char *ifname;
351 int idx = 0;
352
353 zvrf = vrf_info_lookup(VRF_DEFAULT);
354 if (!zvrf)
355 return CMD_WARNING;
356
357 argv_find(argv, argc, "IFNAME", &idx);
358 ifname = argv[idx]->arg;
359
360 pw = zebra_pw_find(zvrf, ifname);
361 if (pw) {
362 if (pw->protocol != ZEBRA_ROUTE_STATIC) {
363 vty_out(vty, "%% Pseudowire is not static\n");
364 return CMD_WARNING;
365 }
366 zebra_pw_del(zvrf, pw);
367 }
368
369 return CMD_SUCCESS;
370 }
371
372 DEFUN (pseudowire_labels,
373 pseudowire_labels_cmd,
374 "[no] mpls label local (16-1048575) remote (16-1048575)",
375 NO_STR
376 "MPLS L2VPN PW command\n"
377 "MPLS L2VPN static labels\n"
378 "Local pseudowire label\n"
379 "Local pseudowire label\n"
380 "Remote pseudowire label\n"
381 "Remote pseudowire label\n")
382 {
383 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
384 int idx = 0;
385 mpls_label_t local_label, remote_label;
386
387 if (argv_find(argv, argc, "no", &idx)) {
388 local_label = MPLS_NO_LABEL;
389 remote_label = MPLS_NO_LABEL;
390 } else {
391 argv_find(argv, argc, "local", &idx);
392 local_label = atoi(argv[idx + 1]->arg);
393 argv_find(argv, argc, "remote", &idx);
394 remote_label = atoi(argv[idx + 1]->arg);
395 }
396
397 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
398 local_label, remote_label, pw->flags, &pw->data);
399
400 return CMD_SUCCESS;
401 }
402
403 DEFUN (pseudowire_neighbor,
404 pseudowire_neighbor_cmd,
405 "[no] neighbor <A.B.C.D|X:X::X:X>",
406 NO_STR
407 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
408 "IPv4 address\n"
409 "IPv6 address\n")
410 {
411 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
412 int idx = 0;
413 const char *address;
414 int af;
415 union g_addr nexthop;
416
417 af = AF_UNSPEC;
418 memset(&nexthop, 0, sizeof(nexthop));
419
420 if (!argv_find(argv, argc, "no", &idx)) {
421 argv_find(argv, argc, "neighbor", &idx);
422 address = argv[idx + 1]->arg;
423
424 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
425 af = AF_INET;
426 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
427 af = AF_INET6;
428 else {
429 vty_out(vty, "%% Malformed address\n");
430 return CMD_WARNING;
431 }
432 }
433
434 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
435 pw->local_label, pw->remote_label, pw->flags,
436 &pw->data);
437
438 return CMD_SUCCESS;
439 }
440
441 DEFUN (pseudowire_control_word,
442 pseudowire_control_word_cmd,
443 "[no] control-word <exclude|include>",
444 NO_STR
445 "Control-word options\n"
446 "Exclude control-word in pseudowire packets\n"
447 "Include control-word in pseudowire packets\n")
448 {
449 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
450 int idx = 0;
451 uint8_t flags = 0;
452
453 if (argv_find(argv, argc, "no", &idx))
454 flags = F_PSEUDOWIRE_CWORD;
455 else {
456 argv_find(argv, argc, "control-word", &idx);
457 if (argv[idx + 1]->text[0] == 'i')
458 flags = F_PSEUDOWIRE_CWORD;
459 }
460
461 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
462 pw->local_label, pw->remote_label, flags, &pw->data);
463
464 return CMD_SUCCESS;
465 }
466
467 DEFUN (show_pseudowires,
468 show_pseudowires_cmd,
469 "show mpls pseudowires",
470 SHOW_STR
471 MPLS_STR
472 "Pseudowires\n")
473 {
474 struct zebra_vrf *zvrf;
475 struct zebra_pw *pw;
476
477 zvrf = vrf_info_lookup(VRF_DEFAULT);
478 if (!zvrf)
479 return 0;
480
481 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
482 "Labels", "Protocol", "Status");
483
484 RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
485 char buf_nbr[INET6_ADDRSTRLEN];
486 char buf_labels[64];
487
488 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
489
490 if (pw->local_label != MPLS_NO_LABEL
491 && pw->remote_label != MPLS_NO_LABEL)
492 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
493 pw->local_label, pw->remote_label);
494 else
495 snprintf(buf_labels, sizeof(buf_labels), "-");
496
497 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
498 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
499 zebra_route_string(pw->protocol),
500 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
501 ? "UP"
502 : "DOWN");
503 }
504
505 return CMD_SUCCESS;
506 }
507
508 /* Pseudowire configuration write function. */
509 static int zebra_pw_config(struct vty *vty)
510 {
511 int write = 0;
512 struct zebra_vrf *zvrf;
513 struct zebra_pw *pw;
514
515 zvrf = vrf_info_lookup(VRF_DEFAULT);
516 if (!zvrf)
517 return 0;
518
519 RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
520 vty_out(vty, "pseudowire %s\n", pw->ifname);
521 if (pw->local_label != MPLS_NO_LABEL
522 && pw->remote_label != MPLS_NO_LABEL)
523 vty_out(vty, " mpls label local %u remote %u\n",
524 pw->local_label, pw->remote_label);
525 else
526 vty_out(vty,
527 " ! Incomplete config, specify the static "
528 "MPLS labels\n");
529
530 if (pw->af != AF_UNSPEC) {
531 char buf[INET6_ADDRSTRLEN];
532 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
533 vty_out(vty, " neighbor %s\n", buf);
534 } else
535 vty_out(vty,
536 " ! Incomplete config, specify a neighbor "
537 "address\n");
538
539 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
540 vty_out(vty, " control-word exclude\n");
541
542 vty_out(vty, "!\n");
543 write = 1;
544 }
545
546 return write;
547 }
548
549 static struct cmd_node pw_node = {
550 PW_NODE, "%s(config-pw)# ", 1,
551 };
552
553 void zebra_pw_vty_init(void)
554 {
555 install_node(&pw_node, zebra_pw_config);
556 install_default(PW_NODE);
557
558 install_element(CONFIG_NODE, &pseudowire_if_cmd);
559 install_element(CONFIG_NODE, &no_pseudowire_if_cmd);
560 install_element(PW_NODE, &pseudowire_labels_cmd);
561 install_element(PW_NODE, &pseudowire_neighbor_cmd);
562 install_element(PW_NODE, &pseudowire_control_word_cmd);
563
564 install_element(VIEW_NODE, &show_pseudowires_cmd);
565 }