2 * Copyright (C) 2016 Volta Networks, Inc.
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.
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.
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,
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"
36 DEFINE_MTYPE_STATIC(LIB
, PW
, "Pseudowire")
38 DEFINE_QOBJ_TYPE(zebra_pw
)
40 DEFINE_HOOK(pw_install
, (struct zebra_pw
* pw
), (pw
))
41 DEFINE_HOOK(pw_uninstall
, (struct zebra_pw
* pw
), (pw
))
43 #define MPLS_NO_LABEL MPLS_INVALID_LABEL
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);
52 static inline int zebra_pw_compare(const struct zebra_pw
*a
,
53 const struct zebra_pw
*b
)
55 return (strcmp(a
->ifname
, b
->ifname
));
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
)
61 struct zebra_pw
*zebra_pw_add(struct zebra_vrf
*zvrf
, const char *ifname
,
62 uint8_t protocol
, struct zserv
*client
)
66 if (IS_ZEBRA_DEBUG_PW
)
67 zlog_debug("%u: adding pseudowire %s protocol %s",
68 zvrf_id(zvrf
), ifname
, zebra_route_string(protocol
));
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
);
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
;
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
);
89 void zebra_pw_del(struct zebra_vrf
*zvrf
, struct zebra_pw
*pw
)
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
));
95 /* remove nexthop tracking */
96 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
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
);
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
);
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
)
116 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
118 pw
->ifindex
= ifindex
;
121 pw
->nexthop
= *nexthop
;
122 pw
->local_label
= local_label
;
123 pw
->remote_label
= remote_label
;
127 if (zebra_pw_enabled(pw
))
128 zebra_register_rnh_pseudowire(pw
->vrf_id
, pw
);
130 zebra_pw_uninstall(pw
);
133 struct zebra_pw
*zebra_pw_find(struct zebra_vrf
*zvrf
, const char *ifname
)
136 strlcpy(pw
.ifname
, ifname
, sizeof(pw
.ifname
));
137 return (RB_FIND(zebra_pw_head
, &zvrf
->pseudowires
, &pw
));
140 static int zebra_pw_enabled(struct zebra_pw
*pw
)
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
)
151 void zebra_pw_update(struct zebra_pw
*pw
)
153 if (zebra_pw_check_reachability(pw
) < 0) {
154 zebra_pw_uninstall(pw
);
155 /* wait for NHT and try again later */
158 * Install or reinstall the pseudowire (e.g. to update
159 * parameters like the nexthop or the use of the control word).
161 zebra_pw_install(pw
);
165 static void zebra_pw_install(struct zebra_pw
*pw
)
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
));
172 if (hook_call(pw_install
, pw
)) {
173 zebra_pw_install_failure(pw
);
177 if (pw
->status
== PW_STATUS_DOWN
)
178 zebra_pw_update_status(pw
, PW_STATUS_UP
);
181 static void zebra_pw_uninstall(struct zebra_pw
*pw
)
183 if (pw
->status
== PW_STATUS_DOWN
)
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
));
191 /* ignore any possible error */
192 hook_call(pw_uninstall
, pw
);
194 if (zebra_pw_enabled(pw
))
195 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
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.
204 void zebra_pw_install_failure(struct zebra_pw
*pw
)
206 if (IS_ZEBRA_DEBUG_PW
)
208 "%u: failed installing pseudowire %s, "
209 "scheduling retry in %u seconds",
210 pw
->vrf_id
, pw
->ifname
, PW_INSTALL_RETRY_INTERVAL
);
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
);
217 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
220 static int zebra_pw_install_retry(struct thread
*thread
)
222 struct zebra_pw
*pw
= THREAD_ARG(thread
);
224 pw
->install_retry_timer
= NULL
;
225 zebra_pw_install(pw
);
230 static void zebra_pw_update_status(struct zebra_pw
*pw
, int status
)
234 zsend_pw_update(pw
->client
, pw
);
237 static int zebra_pw_check_reachability(struct zebra_pw
*pw
)
239 struct route_entry
*re
;
240 struct nexthop
*nexthop
;
242 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
244 /* find route to the remote end of the pseudowire */
245 re
= rib_match(family2afi(pw
->af
), SAFI_UNICAST
, pw
->vrf_id
,
248 if (IS_ZEBRA_DEBUG_PW
)
249 zlog_debug("%s: no route found for %s", __func__
,
255 * Need to ensure that there's a label binding for all nexthops.
256 * Otherwise, ECMP for this route could render the pseudowire unusable.
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
);
270 static int zebra_pw_client_close(struct zserv
*client
)
273 struct zebra_vrf
*zvrf
;
274 struct zebra_pw
*pw
, *tmp
;
276 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
278 RB_FOREACH_SAFE (pw
, zebra_pw_head
, &zvrf
->pseudowires
, tmp
) {
279 if (pw
->client
!= client
)
281 zebra_pw_del(zvrf
, pw
);
288 void zebra_pw_init(struct zebra_vrf
*zvrf
)
290 RB_INIT(zebra_pw_head
, &zvrf
->pseudowires
);
291 RB_INIT(zebra_static_pw_head
, &zvrf
->static_pseudowires
);
293 hook_register(zserv_client_close
, zebra_pw_client_close
);
296 void zebra_pw_exit(struct zebra_vrf
*zvrf
)
300 while (!RB_EMPTY(zebra_pw_head
, &zvrf
->pseudowires
)) {
301 pw
= RB_ROOT(zebra_pw_head
, &zvrf
->pseudowires
);
303 zebra_pw_del(zvrf
, pw
);
307 DEFUN_NOSH (pseudowire_if
,
310 "Static pseudowire configuration\n"
313 struct zebra_vrf
*zvrf
;
318 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
322 argv_find(argv
, argc
, "IFNAME", &idx
);
323 ifname
= argv
[idx
]->arg
;
325 pw
= zebra_pw_find(zvrf
, ifname
);
326 if (pw
&& pw
->protocol
!= ZEBRA_ROUTE_STATIC
) {
327 vty_out(vty
, "%% Pseudowire is not static\n");
332 pw
= zebra_pw_add(zvrf
, ifname
, ZEBRA_ROUTE_STATIC
, NULL
);
333 VTY_PUSH_CONTEXT(PW_NODE
, pw
);
338 DEFUN (no_pseudowire_if
,
339 no_pseudowire_if_cmd
,
340 "no pseudowire IFNAME",
342 "Static pseudowire configuration\n"
345 struct zebra_vrf
*zvrf
;
350 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
354 argv_find(argv
, argc
, "IFNAME", &idx
);
355 ifname
= argv
[idx
]->arg
;
357 pw
= zebra_pw_find(zvrf
, ifname
);
359 if (pw
->protocol
!= ZEBRA_ROUTE_STATIC
) {
360 vty_out(vty
, "%% Pseudowire is not static\n");
363 zebra_pw_del(zvrf
, pw
);
369 DEFUN (pseudowire_labels
,
370 pseudowire_labels_cmd
,
371 "[no] mpls label local (16-1048575) remote (16-1048575)",
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")
380 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
382 mpls_label_t local_label
, remote_label
;
384 if (argv_find(argv
, argc
, "no", &idx
)) {
385 local_label
= MPLS_NO_LABEL
;
386 remote_label
= MPLS_NO_LABEL
;
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
);
394 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, pw
->af
, &pw
->nexthop
,
395 local_label
, remote_label
, pw
->flags
, &pw
->data
);
400 DEFUN (pseudowire_neighbor
,
401 pseudowire_neighbor_cmd
,
402 "[no] neighbor <A.B.C.D|X:X::X:X>",
404 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
408 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
412 union g_addr nexthop
;
415 memset(&nexthop
, 0, sizeof(nexthop
));
417 if (!argv_find(argv
, argc
, "no", &idx
)) {
418 argv_find(argv
, argc
, "neighbor", &idx
);
419 address
= argv
[idx
+ 1]->arg
;
421 if (inet_pton(AF_INET
, address
, &nexthop
.ipv4
) == 1)
423 else if (inet_pton(AF_INET6
, address
, &nexthop
.ipv6
) == 1)
426 vty_out(vty
, "%% Malformed address\n");
431 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, af
, &nexthop
,
432 pw
->local_label
, pw
->remote_label
, pw
->flags
,
438 DEFUN (pseudowire_control_word
,
439 pseudowire_control_word_cmd
,
440 "[no] control-word <exclude|include>",
442 "Control-word options\n"
443 "Exclude control-word in pseudowire packets\n"
444 "Include control-word in pseudowire packets\n")
446 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
450 if (argv_find(argv
, argc
, "no", &idx
))
451 flags
= F_PSEUDOWIRE_CWORD
;
453 argv_find(argv
, argc
, "control-word", &idx
);
454 if (argv
[idx
+ 1]->text
[0] == 'i')
455 flags
= F_PSEUDOWIRE_CWORD
;
458 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, pw
->af
, &pw
->nexthop
,
459 pw
->local_label
, pw
->remote_label
, flags
, &pw
->data
);
464 DEFUN (show_pseudowires
,
465 show_pseudowires_cmd
,
466 "show mpls pseudowires",
471 struct zebra_vrf
*zvrf
;
474 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
478 vty_out(vty
, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
479 "Labels", "Protocol", "Status");
481 RB_FOREACH (pw
, zebra_pw_head
, &zvrf
->pseudowires
) {
482 char buf_nbr
[INET6_ADDRSTRLEN
];
485 inet_ntop(pw
->af
, &pw
->nexthop
, buf_nbr
, sizeof(buf_nbr
));
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
);
492 snprintf(buf_labels
, sizeof(buf_labels
), "-");
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
)
505 /* Pseudowire configuration write function. */
506 static int zebra_pw_config(struct vty
*vty
)
509 struct zebra_vrf
*zvrf
;
512 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
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
);
524 " ! Incomplete config, specify the static "
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
);
533 " ! Incomplete config, specify a neighbor "
536 if (!(pw
->flags
& F_PSEUDOWIRE_CWORD
))
537 vty_out(vty
, " control-word exclude\n");
546 static struct cmd_node pw_node
= {
547 PW_NODE
, "%s(config-pw)# ", 1,
550 void zebra_pw_vty_init(void)
552 install_node(&pw_node
, zebra_pw_config
);
553 install_default(PW_NODE
);
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
);
561 install_element(VIEW_NODE
, &show_pseudowires_cmd
);