]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_pw.c
zebra: reorganize zserv.c by pthread affinity
[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"
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
6833ae01 45extern struct zebra_t zebrad;
46
2dd0d726 47static int zebra_pw_enabled(struct zebra_pw *);
6833ae01 48static void zebra_pw_install(struct zebra_pw *);
49static void zebra_pw_uninstall(struct zebra_pw *);
50static int zebra_pw_install_retry(struct thread *);
51static int zebra_pw_check_reachability(struct zebra_pw *);
52static void zebra_pw_update_status(struct zebra_pw *, int);
53
54static inline int zebra_pw_compare(const struct zebra_pw *a,
55 const struct zebra_pw *b)
56{
57 return (strcmp(a->ifname, b->ifname));
58}
59
2dd0d726
RW
60RB_GENERATE(zebra_pw_head, zebra_pw, pw_entry, zebra_pw_compare)
61RB_GENERATE(zebra_static_pw_head, zebra_pw, static_pw_entry, zebra_pw_compare)
6833ae01 62
63struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
64 uint8_t protocol, struct zserv *client)
65{
66 struct zebra_pw *pw;
67
68 if (IS_ZEBRA_DEBUG_PW)
69 zlog_debug("%u: adding pseudowire %s protocol %s",
70 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
71
72 pw = XCALLOC(MTYPE_PW, sizeof(*pw));
73 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
74 pw->protocol = protocol;
75 pw->vrf_id = zvrf_id(zvrf);
76 pw->client = client;
3c5b5220 77 pw->status = PW_STATUS_DOWN;
2dd0d726
RW
78 pw->local_label = MPLS_NO_LABEL;
79 pw->remote_label = MPLS_NO_LABEL;
80 pw->flags = F_PSEUDOWIRE_CWORD;
6833ae01 81
82 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
2dd0d726
RW
83 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
84 RB_INSERT(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
85 QOBJ_REG(pw, zebra_pw);
86 }
6833ae01 87
88 return pw;
89}
90
91void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
92{
93 if (IS_ZEBRA_DEBUG_PW)
94 zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
95 pw->ifname, zebra_route_string(pw->protocol));
96
731a75fe
RW
97 /* remove nexthop tracking */
98 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
99
6833ae01 100 /* uninstall */
101 if (pw->status == PW_STATUS_UP)
102 hook_call(pw_uninstall, pw);
103 else if (pw->install_retry_timer)
104 THREAD_TIMER_OFF(pw->install_retry_timer);
105
106 /* unlink and release memory */
107 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
2dd0d726
RW
108 if (pw->protocol == ZEBRA_ROUTE_STATIC)
109 RB_REMOVE(zebra_static_pw_head, &zvrf->static_pseudowires, pw);
6833ae01 110 XFREE(MTYPE_PW, pw);
111}
112
113void zebra_pw_change(struct zebra_pw *pw, ifindex_t ifindex, int type, int af,
114 union g_addr *nexthop, uint32_t local_label,
115 uint32_t remote_label, uint8_t flags,
116 union pw_protocol_fields *data)
117{
731a75fe
RW
118 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
119
6833ae01 120 pw->ifindex = ifindex;
121 pw->type = type;
122 pw->af = af;
123 pw->nexthop = *nexthop;
124 pw->local_label = local_label;
125 pw->remote_label = remote_label;
126 pw->flags = flags;
127 pw->data = *data;
128
2dd0d726 129 if (zebra_pw_enabled(pw))
731a75fe 130 zebra_register_rnh_pseudowire(pw->vrf_id, pw);
6833ae01 131 else
132 zebra_pw_uninstall(pw);
133}
134
135struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
136{
137 struct zebra_pw pw;
138 strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
139 return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
140}
141
2dd0d726
RW
142static int zebra_pw_enabled(struct zebra_pw *pw)
143{
144 if (pw->protocol == ZEBRA_ROUTE_STATIC) {
145 if (pw->local_label == MPLS_NO_LABEL
146 || pw->remote_label == MPLS_NO_LABEL || pw->af == AF_UNSPEC)
147 return 0;
148 return 1;
149 } else
150 return pw->enabled;
151}
152
6833ae01 153void zebra_pw_update(struct zebra_pw *pw)
154{
155 if (zebra_pw_check_reachability(pw) < 0) {
156 zebra_pw_uninstall(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
174 if (hook_call(pw_install, pw)) {
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
183static 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
2dd0d726 196 if (zebra_pw_enabled(pw))
6833ae01 197 zebra_pw_update_status(pw, PW_STATUS_DOWN);
198}
199
200/*
201 * Installation of the pseudowire in the kernel or hardware has failed. This
202 * function will notify the pseudowire client about the failure and schedule
203 * to retry the installation later. This function can be called by an external
204 * agent that performs the pseudowire installation in an asynchronous way.
205 */
206void zebra_pw_install_failure(struct zebra_pw *pw)
207{
208 if (IS_ZEBRA_DEBUG_PW)
209 zlog_debug(
210 "%u: failed installing pseudowire %s, "
211 "scheduling retry in %u seconds",
212 pw->vrf_id, pw->ifname, PW_INSTALL_RETRY_INTERVAL);
213
214 /* schedule to retry later */
215 THREAD_TIMER_OFF(pw->install_retry_timer);
69965f53
DL
216 thread_add_timer(zebrad.master, zebra_pw_install_retry, pw,
217 PW_INSTALL_RETRY_INTERVAL, &pw->install_retry_timer);
6833ae01 218
219 zebra_pw_update_status(pw, PW_STATUS_DOWN);
220}
221
222static int zebra_pw_install_retry(struct thread *thread)
223{
224 struct zebra_pw *pw = THREAD_ARG(thread);
225
226 pw->install_retry_timer = NULL;
227 zebra_pw_install(pw);
228
229 return 0;
230}
231
232static void zebra_pw_update_status(struct zebra_pw *pw, int status)
233{
234 pw->status = status;
235 if (pw->client)
236 zsend_pw_update(pw->client, pw);
237}
238
239static int zebra_pw_check_reachability(struct zebra_pw *pw)
240{
69965f53
DL
241 struct route_entry *re;
242 struct nexthop *nexthop;
6833ae01 243
244 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
245
246 /* find route to the remote end of the pseudowire */
69965f53
DL
247 re = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
248 &pw->nexthop, NULL);
249 if (!re) {
6833ae01 250 if (IS_ZEBRA_DEBUG_PW)
251 zlog_warn("%s: no route found for %s", __func__,
252 pw->ifname);
253 return -1;
254 }
255
256 /*
257 * Need to ensure that there's a label binding for all nexthops.
258 * Otherwise, ECMP for this route could render the pseudowire unusable.
259 */
7ee30f28 260 for (ALL_NEXTHOPS(re->ng, nexthop)) {
6833ae01 261 if (!nexthop->nh_label) {
262 if (IS_ZEBRA_DEBUG_PW)
263 zlog_warn("%s: unlabeled route for %s",
264 __func__, pw->ifname);
265 return -1;
266 }
267 }
268
269 return 0;
270}
271
453844ab 272static int zebra_pw_client_close(struct zserv *client)
6833ae01 273{
274 struct vrf *vrf;
275 struct zebra_vrf *zvrf;
276 struct zebra_pw *pw, *tmp;
277
a2addae8 278 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
6833ae01 279 zvrf = vrf->info;
a2addae8 280 RB_FOREACH_SAFE (pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
6833ae01 281 if (pw->client != client)
282 continue;
283 zebra_pw_del(zvrf, pw);
284 }
285 }
453844ab
QY
286
287 return 0;
6833ae01 288}
289
290void zebra_pw_init(struct zebra_vrf *zvrf)
291{
69965f53
DL
292 RB_INIT(zebra_pw_head, &zvrf->pseudowires);
293 RB_INIT(zebra_static_pw_head, &zvrf->static_pseudowires);
453844ab 294
d8647095 295 hook_register(zapi_client_close, zebra_pw_client_close);
6833ae01 296}
297
298void zebra_pw_exit(struct zebra_vrf *zvrf)
299{
300 struct zebra_pw *pw;
301
55cd0f61
DS
302 while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) {
303 pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires);
304
6833ae01 305 zebra_pw_del(zvrf, pw);
55cd0f61 306 }
6833ae01 307}
2dd0d726
RW
308
309DEFUN_NOSH (pseudowire_if,
310 pseudowire_if_cmd,
311 "[no] pseudowire IFNAME",
312 NO_STR
313 "Static pseudowire configuration\n"
314 "Pseudowire name\n")
315{
316 struct zebra_vrf *zvrf;
317 struct zebra_pw *pw;
318 int idx = 0;
319 const char *ifname;
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 pw = zebra_pw_find(zvrf, ifname);
328 if (pw && pw->protocol != ZEBRA_ROUTE_STATIC) {
69965f53 329 vty_out(vty, "%% Pseudowire is not static\n");
2dd0d726
RW
330 return CMD_WARNING;
331 }
332
333 if (argv_find(argv, argc, "no", &idx)) {
334 if (!pw)
335 return CMD_SUCCESS;
336 zebra_pw_del(zvrf, pw);
0af35d90 337 return CMD_SUCCESS;
2dd0d726
RW
338 }
339
340 if (!pw)
341 pw = zebra_pw_add(zvrf, ifname, ZEBRA_ROUTE_STATIC, NULL);
342 VTY_PUSH_CONTEXT(PW_NODE, pw);
343
344 return CMD_SUCCESS;
345}
346
347DEFUN (pseudowire_labels,
348 pseudowire_labels_cmd,
349 "[no] mpls label local (16-1048575) remote (16-1048575)",
350 NO_STR
351 "MPLS L2VPN PW command\n"
352 "MPLS L2VPN static labels\n"
353 "Local pseudowire label\n"
354 "Local pseudowire label\n"
355 "Remote pseudowire label\n"
356 "Remote pseudowire label\n")
357{
358 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
359 int idx = 0;
360 mpls_label_t local_label, remote_label;
361
362 if (argv_find(argv, argc, "no", &idx)) {
363 local_label = MPLS_NO_LABEL;
364 remote_label = MPLS_NO_LABEL;
365 } else {
366 argv_find(argv, argc, "local", &idx);
367 local_label = atoi(argv[idx + 1]->arg);
368 argv_find(argv, argc, "remote", &idx);
369 remote_label = atoi(argv[idx + 1]->arg);
370 }
371
372 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
373 local_label, remote_label, pw->flags, &pw->data);
374
375 return CMD_SUCCESS;
376}
377
378DEFUN (pseudowire_neighbor,
379 pseudowire_neighbor_cmd,
380 "[no] neighbor <A.B.C.D|X:X::X:X>",
381 NO_STR
382 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
383 "IPv4 address\n"
384 "IPv6 address\n")
385{
386 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
387 int idx = 0;
388 const char *address;
389 int af;
390 union g_addr nexthop;
391
392 af = AF_UNSPEC;
393 memset(&nexthop, 0, sizeof(nexthop));
394
395 if (!argv_find(argv, argc, "no", &idx)) {
396 argv_find(argv, argc, "neighbor", &idx);
397 address = argv[idx + 1]->arg;
398
399 if (inet_pton(AF_INET, address, &nexthop.ipv4) == 1)
400 af = AF_INET;
401 else if (inet_pton(AF_INET6, address, &nexthop.ipv6) == 1)
402 af = AF_INET6;
403 else {
69965f53 404 vty_out(vty, "%% Malformed address\n");
2dd0d726
RW
405 return CMD_WARNING;
406 }
407 }
408
409 zebra_pw_change(pw, pw->ifindex, pw->type, af, &nexthop,
410 pw->local_label, pw->remote_label, pw->flags,
411 &pw->data);
412
413 return CMD_SUCCESS;
414}
415
416DEFUN (pseudowire_control_word,
417 pseudowire_control_word_cmd,
418 "[no] control-word <exclude|include>",
419 NO_STR
420 "Control-word options\n"
421 "Exclude control-word in pseudowire packets\n"
422 "Include control-word in pseudowire packets\n")
423{
424 VTY_DECLVAR_CONTEXT(zebra_pw, pw);
425 int idx = 0;
426 uint8_t flags = 0;
427
428 if (argv_find(argv, argc, "no", &idx))
429 flags = F_PSEUDOWIRE_CWORD;
430 else {
431 argv_find(argv, argc, "control-word", &idx);
432 if (argv[idx + 1]->text[0] == 'i')
433 flags = F_PSEUDOWIRE_CWORD;
434 }
435
436 zebra_pw_change(pw, pw->ifindex, pw->type, pw->af, &pw->nexthop,
437 pw->local_label, pw->remote_label, flags, &pw->data);
438
439 return CMD_SUCCESS;
440}
441
442DEFUN (show_pseudowires,
443 show_pseudowires_cmd,
d261dd7e 444 "show mpls pseudowires",
2dd0d726 445 SHOW_STR
d261dd7e 446 MPLS_STR
efd7904e 447 "Pseudowires\n")
2dd0d726
RW
448{
449 struct zebra_vrf *zvrf;
450 struct zebra_pw *pw;
451
452 zvrf = vrf_info_lookup(VRF_DEFAULT);
453 if (!zvrf)
454 return 0;
455
69965f53
DL
456 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
457 "Labels", "Protocol", "Status");
2dd0d726 458
a2addae8 459 RB_FOREACH (pw, zebra_pw_head, &zvrf->pseudowires) {
2dd0d726
RW
460 char buf_nbr[INET6_ADDRSTRLEN];
461 char buf_labels[64];
462
463 inet_ntop(pw->af, &pw->nexthop, buf_nbr, sizeof(buf_nbr));
464
465 if (pw->local_label != MPLS_NO_LABEL
466 && pw->remote_label != MPLS_NO_LABEL)
467 snprintf(buf_labels, sizeof(buf_labels), "%u/%u",
468 pw->local_label, pw->remote_label);
469 else
470 snprintf(buf_labels, sizeof(buf_labels), "-");
471
69965f53 472 vty_out(vty, "%-16s %-24s %-12s %-8s %-10s\n", pw->ifname,
2dd0d726
RW
473 (pw->af != AF_UNSPEC) ? buf_nbr : "-", buf_labels,
474 zebra_route_string(pw->protocol),
475 (zebra_pw_enabled(pw) && pw->status == PW_STATUS_UP)
476 ? "UP"
69965f53 477 : "DOWN");
2dd0d726
RW
478 }
479
480 return CMD_SUCCESS;
481}
482
483/* Pseudowire configuration write function. */
484static int zebra_pw_config(struct vty *vty)
485{
486 int write = 0;
487 struct zebra_vrf *zvrf;
488 struct zebra_pw *pw;
489
490 zvrf = vrf_info_lookup(VRF_DEFAULT);
491 if (!zvrf)
492 return 0;
493
a2addae8 494 RB_FOREACH (pw, zebra_static_pw_head, &zvrf->static_pseudowires) {
69965f53 495 vty_out(vty, "pseudowire %s\n", pw->ifname);
2dd0d726
RW
496 if (pw->local_label != MPLS_NO_LABEL
497 && pw->remote_label != MPLS_NO_LABEL)
69965f53
DL
498 vty_out(vty, " mpls label local %u remote %u\n",
499 pw->local_label, pw->remote_label);
2dd0d726
RW
500 else
501 vty_out(vty,
502 " ! Incomplete config, specify the static "
69965f53 503 "MPLS labels\n");
2dd0d726
RW
504
505 if (pw->af != AF_UNSPEC) {
506 char buf[INET6_ADDRSTRLEN];
507 inet_ntop(pw->af, &pw->nexthop, buf, sizeof(buf));
69965f53 508 vty_out(vty, " neighbor %s\n", buf);
2dd0d726
RW
509 } else
510 vty_out(vty,
511 " ! Incomplete config, specify a neighbor "
69965f53 512 "address\n");
2dd0d726
RW
513
514 if (!(pw->flags & F_PSEUDOWIRE_CWORD))
69965f53 515 vty_out(vty, " control-word exclude\n");
2dd0d726 516
69965f53 517 vty_out(vty, "!\n");
2dd0d726
RW
518 write = 1;
519 }
520
521 return write;
522}
523
524static struct cmd_node pw_node = {
525 PW_NODE, "%s(config-pw)# ", 1,
526};
527
528void zebra_pw_vty_init(void)
529{
530 install_node(&pw_node, zebra_pw_config);
531 install_default(PW_NODE);
532
533 install_element(CONFIG_NODE, &pseudowire_if_cmd);
534 install_element(PW_NODE, &pseudowire_labels_cmd);
535 install_element(PW_NODE, &pseudowire_neighbor_cmd);
536 install_element(PW_NODE, &pseudowire_control_word_cmd);
537
538 install_element(VIEW_NODE, &show_pseudowires_cmd);
539}