]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pw.c
*: reindent
[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 extern struct zebra_t zebrad;
43
44 static int zebra_pw_enabled(struct zebra_pw *);
45 static void zebra_pw_install(struct zebra_pw *);
46 static void zebra_pw_uninstall(struct zebra_pw *);
47 static int zebra_pw_install_retry(struct thread *);
48 static int zebra_pw_check_reachability(struct zebra_pw *);
49 static void zebra_pw_update_status(struct zebra_pw *, int);
50
51 static inline int zebra_pw_compare(const struct zebra_pw *a,
52 const struct zebra_pw *b)
53 {
54 return (strcmp(a->ifname, b->ifname));
55 }
56
57 RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
58 RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)
59
60 struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
61 uint8_t protocol, struct zserv *client)
62 {
63 struct zebra_pw *pw;
64
65 if (IS_ZEBRA_DEBUG_PW)
66 zlog_debug("%u: adding pseudowire %s protocol %s",
67 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
68
69 pw = XCALLOC(MTYPE_PW, sizeof(*pw));
70 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
71 pw->protocol = protocol;
72 pw->vrf_id = zvrf_id(zvrf);
73 pw->client = client;
74 pw->status = PW_STATUS_UP;
75 pw->local_label = MPLS_NO_LABEL;
76 pw->remote_label = MPLS_NO_LABEL;
77 pw->flags = F_PSEUDOWIRE_CWORD;
78
79 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
80 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
81 RB_INSERT(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
82 QOBJ_REG(pw, zebra_pw);
83 }
84
85 return pw;
86 }
87
88 void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
89 {
90 if (IS_ZEBRA_DEBUG_PW)
91 zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
92 pw->ifname, zebra_route_string(pw->protocol));
93
94 /* remove nexthop tracking */
95 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
96
97 /* uninstall */
98 if (pw->status == PW_STATUS_UP)
99 hook_call(pw_uninstall, pw);
100 else if (pw->install_retry_timer)
101 THREAD_TIMER_OFF(pw->install_retry_timer);
102
103 /* unlink and release memory */
104 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
105 if (pw->protocol == ZEBRA_ROUTE_STATIC)
106 RB_REMOVE(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
107 XFREE(MTYPE_PW, pw);
108 }
109
110 void zebra_pw_change(struct zebra_pw *pw, ifindex_t ifindex, int type, int af,
111 union g_addr *nexthop, uint32_t local_label,
112 uint32_t remote_label, uint8_t flags,
113 union pw_protocol_fields *data)
114 {
115 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
116
117 pw->ifindex = ifindex;
118 pw->type = type;
119 pw->af = af;
120 pw->nexthop = *nexthop;
121 pw->local_label = local_label;
122 pw->remote_label = remote_label;
123 pw->flags = flags;
124 pw->data = *data;
125
126 if (zebra_pw_enabled(pw))
127 zebra_register_rnh_pseudowire(pw->vrf_id, pw);
128 else
129 zebra_pw_uninstall(pw);
130 }
131
132 struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
133 {
134 struct zebra_pw pw;
135 strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
136 return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
137 }
138
139 static int zebra_pw_enabled(struct zebra_pw *pw)
140 {
141 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
142 if (pw->local_label == MPLS_NO_LABEL
143 || pw->remote_label == MPLS_NO_LABEL || pw->af == AF_UNSPEC)
144 return 0;
145 return 1;
146 } else
147 return pw->enabled;
148 }
149
150 void zebra_pw_update(struct zebra_pw *pw)
151 {
152 if (zebra_pw_check_reachability(pw) < 0) {
153 zebra_pw_uninstall(pw);
154 /* wait for NHT and try again later */
155 } else {
156 /*
157 * Install or reinstall the pseudowire (e.g. to update
158 * parameters like the nexthop or the use of the control word).
159 */
160 zebra_pw_install(pw);
161 }
162 }
163
164 static void zebra_pw_install(struct zebra_pw *pw)
165 {
166 if (IS_ZEBRA_DEBUG_PW)
167 zlog_debug("%u: installing pseudowire %s protocol %s",
168 pw->vrf_id, pw->ifname,
169 zebra_route_string(pw->protocol));
170
171 if (hook_call(pw_install, pw)) {
172 zebra_pw_install_failure(pw);
173 return;
174 }
175
176 if (pw->status == PW_STATUS_DOWN)
177 zebra_pw_update_status(pw, PW_STATUS_UP);
178 }
179
180 static void zebra_pw_uninstall(struct zebra_pw *pw)
181 {
182 if (pw->status == PW_STATUS_DOWN)
183 return;
184
185 if (IS_ZEBRA_DEBUG_PW)
186 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
187 pw->vrf_id, pw->ifname,
188 zebra_route_string(pw->protocol));
189
190 /* ignore any possible error */
191 hook_call(pw_uninstall, pw);
192
193 if (zebra_pw_enabled(pw))
194 zebra_pw_update_status(pw, PW_STATUS_DOWN);
195 }
196
197 /*
198 * Installation of the pseudowire in the kernel or hardware has failed. This
199 * function will notify the pseudowire client about the failure and schedule
200 * to retry the installation later. This function can be called by an external
201 * agent that performs the pseudowire installation in an asynchronous way.
202 */
203 void zebra_pw_install_failure(struct zebra_pw *pw)
204 {
205 if (IS_ZEBRA_DEBUG_PW)
206 zlog_debug(
207 "%u: failed installing pseudowire %s, "
208 "scheduling retry in %u seconds",
209 pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);
210
211 /* schedule to retry later */
212 THREAD_TIMER_OFF(pw->install_retry_timer);
213 pw->install_retry_timer =
214 thread_add_timer(zebrad.master, zebra_pw_install_retry, pw,
215 PW_INSTALL_RETRY_INTERVAL);
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 rib *rib;
240 struct nexthop *nexthop, *tnexthop;
241 int recursing;
242
243 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
244
245 /* find route to the remote end of the pseudowire */
246 rib = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
247 &pw->nexthop, NULL);
248 if (!rib) {
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_RO(rib->nexthop, nexthop, tnexthop, recursing)) {
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 {
279 zvrf = vrf->info;
280 RB_FOREACH_SAFE(pw, zebra_pw_head, &zvrf->pseudowires, tmp)
281 {
282 if (pw->client != client)
283 continue;
284 zebra_pw_del(zvrf, pw);
285 }
286 }
287 }
288
289 void zebra_pw_init(struct zebra_vrf *zvrf)
290 {
291 RB_INIT(&zvrf->pseudowires);
292 RB_INIT(&zvrf->static_pseudowires);
293 }
294
295 void zebra_pw_exit(struct zebra_vrf *zvrf)
296 {
297 struct zebra_pw *pw;
298
299 while ((pw = RB_ROOT(&zvrf->pseudowires)) != NULL)
300 zebra_pw_del(zvrf, pw);
301 }
302
303 DEFUN_NOSH (pseudowire_if,
304 pseudowire_if_cmd,
305 "[no] pseudowire IFNAME",
306 NO_STR
307 "Static pseudowire configuration\n"
308 "Pseudowire name\n")
309 {
310 struct zebra_vrf *zvrf;
311 struct zebra_pw *pw;
312 int idx = 0;
313 const char *ifname;
314
315 zvrf = vrf_info_lookup(VRF_DEFAULT);
316 if (!zvrf)
317 return CMD_WARNING;
318
319 argv_find(argv, argc, "IFNAME", &idx);
320 ifname = argv[idx]->arg;
321 pw = zebra_pw_find(zvrf, ifname);
322 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
323 vty_out(vty, "%% Pseudowire is not static%s", VTY_NEWLINE);
324 return CMD_WARNING;
325 }
326
327 if (argv_find(argv, argc, "no", &idx)) {
328 if (!pw)
329 return CMD_SUCCESS;
330 zebra_pw_del(zvrf, pw);
331 }
332
333 if (!pw)
334 pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
335 VTY_PUSH_CONTEXT(PW_NODE, pw);
336
337 return CMD_SUCCESS;
338 }
339
340 DEFUN (pseudowire_labels,
341 pseudowire_labels_cmd,
342 "[no] mpls label local (16-1048575) remote (16-1048575)",
343 NO_STR
344 "MPLS L2VPN PW command\n"
345 "MPLS L2VPN static labels\n"
346 "Local pseudowire label\n"
347 "Local pseudowire label\n"
348 "Remote pseudowire label\n"
349 "Remote pseudowire label\n")
350 {
351 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
352 int idx = 0;
353 mpls_label_t local_label, remote_label;
354
355 if (argv_find(argv, argc, "no", &idx)) {
356 local_label = MPLS_NO_LABEL;
357 remote_label = MPLS_NO_LABEL;
358 } else {
359 argv_find(argv, argc, "local", &idx);
360 local_label = atoi(argv[idx + 1]->arg);
361 argv_find(argv, argc, "remote", &idx);
362 remote_label = atoi(argv[idx + 1]->arg);
363 }
364
365 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
366 local_label, remote_label, pw->flags, &pw->data);
367
368 return CMD_SUCCESS;
369 }
370
371 DEFUN (pseudowire_neighbor,
372 pseudowire_neighbor_cmd,
373 "[no] neighbor <A.B.C.D|X:X::X:X>",
374 NO_STR
375 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
376 "IPv4 address\n"
377 "IPv6 address\n")
378 {
379 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
380 int idx = 0;
381 const char *address;
382 int af;
383 union g_addr nexthop;
384
385 af = AF_UNSPEC;
386 memset(&nexthop, 0, sizeof(nexthop));
387
388 if (!argv_find(argv, argc, "no", &idx)) {
389 argv_find(argv, argc, "neighbor", &idx);
390 address = argv[idx + 1]->arg;
391
392 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
393 af = AF_INET;
394 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
395 af = AF_INET6;
396 else {
397 vty_out(vty, "%% Malformed address%s", VTY_NEWLINE);
398 return CMD_WARNING;
399 }
400 }
401
402 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
403 pw->local_label, pw->remote_label, pw->flags,
404 &pw->data);
405
406 return CMD_SUCCESS;
407 }
408
409 DEFUN (pseudowire_control_word,
410 pseudowire_control_word_cmd,
411 "[no] control-word <exclude|include>",
412 NO_STR
413 "Control-word options\n"
414 "Exclude control-word in pseudowire packets\n"
415 "Include control-word in pseudowire packets\n")
416 {
417 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
418 int idx = 0;
419 uint8_t flags = 0;
420
421 if (argv_find(argv, argc, "no", &idx))
422 flags = F_PSEUDOWIRE_CWORD;
423 else {
424 argv_find(argv, argc, "control-word", &idx);
425 if (argv[idx + 1]->text[0] == 'i')
426 flags = F_PSEUDOWIRE_CWORD;
427 }
428
429 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
430 pw->local_label, pw->remote_label, flags, &pw->data);
431
432 return CMD_SUCCESS;
433 }
434
435 DEFUN (show_pseudowires,
436 show_pseudowires_cmd,
437 "show pseudowires",
438 SHOW_STR
439 "Pseudowires")
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
448 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s%s", "Interface", "Neighbor",
449 "Labels", "Protocol", "Status", VTY_NEWLINE);
450
451 RB_FOREACH(pw, zebra_pw_head, &zvrf->pseudowires)
452 {
453 char buf_nbr[INET6_ADDRSTRLEN];
454 char buf_labels[64];
455
456 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
457
458 if (pw->local_label != MPLS_NO_LABEL
459 && pw->remote_label != MPLS_NO_LABEL)
460 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
461 pw->local_label, pw->remote_label);
462 else
463 snprintf(buf_labels, sizeof(buf_labels), "-");
464
465 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s%s", pw->ifname,
466 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
467 zebra_route_string(pw->protocol),
468 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
469 ? "UP"
470 : "DOWN",
471 VTY_NEWLINE);
472 }
473
474 return CMD_SUCCESS;
475 }
476
477 /* Pseudowire configuration write function. */
478 static int zebra_pw_config(struct vty *vty)
479 {
480 int write = 0;
481 struct zebra_vrf *zvrf;
482 struct zebra_pw *pw;
483
484 zvrf = vrf_info_lookup(VRF_DEFAULT);
485 if (!zvrf)
486 return 0;
487
488 RB_FOREACH(pw, zebra_static_pw_head, &zvrf->static_pseudowires)
489 {
490 vty_out(vty, "pseudowire %s%s", pw->ifname, VTY_NEWLINE);
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%s",
494 pw->local_label, pw->remote_label, VTY_NEWLINE);
495 else
496 vty_out(vty,
497 " ! Incomplete config, specify the static "
498 "MPLS labels%s",
499 VTY_NEWLINE);
500
501 if (pw->af != AF_UNSPEC) {
502 char buf[INET6_ADDRSTRLEN];
503 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
504 vty_out(vty, " neighbor %s%s", buf, VTY_NEWLINE);
505 } else
506 vty_out(vty,
507 " ! Incomplete config, specify a neighbor "
508 "address%s",
509 VTY_NEWLINE);
510
511 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
512 vty_out(vty, " control-word exclude%s", VTY_NEWLINE);
513
514 vty_out(vty, "!%s", VTY_NEWLINE);
515 write = 1;
516 }
517
518 return write;
519 }
520
521 static struct cmd_node pw_node = {
522 PW_NODE, "%s(config-pw)# ", 1,
523 };
524
525 void zebra_pw_vty_init(void)
526 {
527 install_node(&pw_node, zebra_pw_config);
528 install_default(PW_NODE);
529
530 install_element(CONFIG_NODE, &pseudowire_if_cmd);
531 install_element(PW_NODE, &pseudowire_labels_cmd);
532 install_element(PW_NODE, &pseudowire_neighbor_cmd);
533 install_element(PW_NODE, &pseudowire_control_word_cmd);
534
535 install_element(VIEW_NODE, &show_pseudowires_cmd);
536 }