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/zserv.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 extern struct zebra_t zebrad
;
47 static int zebra_pw_enabled(struct zebra_pw
*);
48 static void zebra_pw_install(struct zebra_pw
*);
49 static void zebra_pw_uninstall(struct zebra_pw
*);
50 static int zebra_pw_install_retry(struct thread
*);
51 static int zebra_pw_check_reachability(struct zebra_pw
*);
52 static void zebra_pw_update_status(struct zebra_pw
*, int);
54 static inline int zebra_pw_compare(const struct zebra_pw
*a
,
55 const struct zebra_pw
*b
)
57 return (strcmp(a
->ifname
, b
->ifname
));
60 RB_GENERATE(zebra_pw_head
, zebra_pw
, pw_entry
, zebra_pw_compare
)
61 RB_GENERATE(zebra_static_pw_head
, zebra_pw
, static_pw_entry
, zebra_pw_compare
)
63 struct zebra_pw
*zebra_pw_add(struct zebra_vrf
*zvrf
, const char *ifname
,
64 uint8_t protocol
, struct zserv
*client
)
68 if (IS_ZEBRA_DEBUG_PW
)
69 zlog_debug("%u: adding pseudowire %s protocol %s",
70 zvrf_id(zvrf
), ifname
, zebra_route_string(protocol
));
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
);
77 pw
->status
= PW_STATUS_DOWN
;
78 pw
->local_label
= MPLS_NO_LABEL
;
79 pw
->remote_label
= MPLS_NO_LABEL
;
80 pw
->flags
= F_PSEUDOWIRE_CWORD
;
82 RB_INSERT(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
83 if (pw
->protocol
== ZEBRA_ROUTE_STATIC
) {
84 RB_INSERT(zebra_static_pw_head
, &zvrf
->static_pseudowires
, pw
);
85 QOBJ_REG(pw
, zebra_pw
);
91 void zebra_pw_del(struct zebra_vrf
*zvrf
, struct zebra_pw
*pw
)
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
));
97 /* remove nexthop tracking */
98 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
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
);
106 /* unlink and release memory */
107 RB_REMOVE(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
108 if (pw
->protocol
== ZEBRA_ROUTE_STATIC
)
109 RB_REMOVE(zebra_static_pw_head
, &zvrf
->static_pseudowires
, pw
);
113 void 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
)
118 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
120 pw
->ifindex
= ifindex
;
123 pw
->nexthop
= *nexthop
;
124 pw
->local_label
= local_label
;
125 pw
->remote_label
= remote_label
;
129 if (zebra_pw_enabled(pw
))
130 zebra_register_rnh_pseudowire(pw
->vrf_id
, pw
);
132 zebra_pw_uninstall(pw
);
135 struct zebra_pw
*zebra_pw_find(struct zebra_vrf
*zvrf
, const char *ifname
)
138 strlcpy(pw
.ifname
, ifname
, sizeof(pw
.ifname
));
139 return (RB_FIND(zebra_pw_head
, &zvrf
->pseudowires
, &pw
));
142 static int zebra_pw_enabled(struct zebra_pw
*pw
)
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
)
153 void zebra_pw_update(struct zebra_pw
*pw
)
155 if (zebra_pw_check_reachability(pw
) < 0) {
156 zebra_pw_uninstall(pw
);
157 /* wait for NHT and try again later */
160 * Install or reinstall the pseudowire (e.g. to update
161 * parameters like the nexthop or the use of the control word).
163 zebra_pw_install(pw
);
167 static void zebra_pw_install(struct zebra_pw
*pw
)
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
));
174 if (hook_call(pw_install
, pw
)) {
175 zebra_pw_install_failure(pw
);
179 if (pw
->status
== PW_STATUS_DOWN
)
180 zebra_pw_update_status(pw
, PW_STATUS_UP
);
183 static void zebra_pw_uninstall(struct zebra_pw
*pw
)
185 if (pw
->status
== PW_STATUS_DOWN
)
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
));
193 /* ignore any possible error */
194 hook_call(pw_uninstall
, pw
);
196 if (zebra_pw_enabled(pw
))
197 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
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.
206 void zebra_pw_install_failure(struct zebra_pw
*pw
)
208 if (IS_ZEBRA_DEBUG_PW
)
210 "%u: failed installing pseudowire %s, "
211 "scheduling retry in %u seconds",
212 pw
->vrf_id
, pw
->ifname
, PW_INSTALL_RETRY_INTERVAL
);
214 /* schedule to retry later */
215 THREAD_TIMER_OFF(pw
->install_retry_timer
);
216 thread_add_timer(zebrad
.master
, zebra_pw_install_retry
, pw
,
217 PW_INSTALL_RETRY_INTERVAL
, &pw
->install_retry_timer
);
219 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
222 static int zebra_pw_install_retry(struct thread
*thread
)
224 struct zebra_pw
*pw
= THREAD_ARG(thread
);
226 pw
->install_retry_timer
= NULL
;
227 zebra_pw_install(pw
);
232 static void zebra_pw_update_status(struct zebra_pw
*pw
, int status
)
236 zsend_pw_update(pw
->client
, pw
);
239 static int zebra_pw_check_reachability(struct zebra_pw
*pw
)
241 struct route_entry
*re
;
242 struct nexthop
*nexthop
;
244 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
246 /* find route to the remote end of the pseudowire */
247 re
= rib_match(family2afi(pw
->af
), SAFI_UNICAST
, pw
->vrf_id
,
250 if (IS_ZEBRA_DEBUG_PW
)
251 zlog_warn("%s: no route found for %s", __func__
,
257 * Need to ensure that there's a label binding for all nexthops.
258 * Otherwise, ECMP for this route could render the pseudowire unusable.
260 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
261 if (!nexthop
->nh_label
) {
262 if (IS_ZEBRA_DEBUG_PW
)
263 zlog_warn("%s: unlabeled route for %s",
264 __func__
, pw
->ifname
);
272 static int zebra_pw_client_close(struct zserv
*client
)
275 struct zebra_vrf
*zvrf
;
276 struct zebra_pw
*pw
, *tmp
;
278 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
280 RB_FOREACH_SAFE (pw
, zebra_pw_head
, &zvrf
->pseudowires
, tmp
) {
281 if (pw
->client
!= client
)
283 zebra_pw_del(zvrf
, pw
);
290 void zebra_pw_init(struct zebra_vrf
*zvrf
)
292 RB_INIT(zebra_pw_head
, &zvrf
->pseudowires
);
293 RB_INIT(zebra_static_pw_head
, &zvrf
->static_pseudowires
);
295 hook_register(zserv_client_close
, zebra_pw_client_close
);
298 void zebra_pw_exit(struct zebra_vrf
*zvrf
)
302 while (!RB_EMPTY(zebra_pw_head
, &zvrf
->pseudowires
)) {
303 pw
= RB_ROOT(zebra_pw_head
, &zvrf
->pseudowires
);
305 zebra_pw_del(zvrf
, pw
);
309 DEFUN_NOSH (pseudowire_if
,
312 "Static pseudowire configuration\n"
315 struct zebra_vrf
*zvrf
;
320 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
324 argv_find(argv
, argc
, "IFNAME", &idx
);
325 ifname
= argv
[idx
]->arg
;
327 pw
= zebra_pw_find(zvrf
, ifname
);
328 if (pw
&& pw
->protocol
!= ZEBRA_ROUTE_STATIC
) {
329 vty_out(vty
, "%% Pseudowire is not static\n");
334 pw
= zebra_pw_add(zvrf
, ifname
, ZEBRA_ROUTE_STATIC
, NULL
);
335 VTY_PUSH_CONTEXT(PW_NODE
, pw
);
340 DEFUN (no_pseudowire_if
,
341 no_pseudowire_if_cmd
,
342 "no pseudowire IFNAME",
344 "Static pseudowire configuration\n"
347 struct zebra_vrf
*zvrf
;
352 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
356 argv_find(argv
, argc
, "IFNAME", &idx
);
357 ifname
= argv
[idx
]->arg
;
359 pw
= zebra_pw_find(zvrf
, ifname
);
361 if (pw
->protocol
!= ZEBRA_ROUTE_STATIC
) {
362 vty_out(vty
, "%% Pseudowire is not static\n");
365 zebra_pw_del(zvrf
, pw
);
371 DEFUN (pseudowire_labels
,
372 pseudowire_labels_cmd
,
373 "[no] mpls label local (16-1048575) remote (16-1048575)",
375 "MPLS L2VPN PW command\n"
376 "MPLS L2VPN static labels\n"
377 "Local pseudowire label\n"
378 "Local pseudowire label\n"
379 "Remote pseudowire label\n"
380 "Remote pseudowire label\n")
382 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
384 mpls_label_t local_label
, remote_label
;
386 if (argv_find(argv
, argc
, "no", &idx
)) {
387 local_label
= MPLS_NO_LABEL
;
388 remote_label
= MPLS_NO_LABEL
;
390 argv_find(argv
, argc
, "local", &idx
);
391 local_label
= atoi(argv
[idx
+ 1]->arg
);
392 argv_find(argv
, argc
, "remote", &idx
);
393 remote_label
= atoi(argv
[idx
+ 1]->arg
);
396 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, pw
->af
, &pw
->nexthop
,
397 local_label
, remote_label
, pw
->flags
, &pw
->data
);
402 DEFUN (pseudowire_neighbor
,
403 pseudowire_neighbor_cmd
,
404 "[no] neighbor <A.B.C.D|X:X::X:X>",
406 "Specify the IPv4 or IPv6 address of the remote endpoint\n"
410 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
414 union g_addr nexthop
;
417 memset(&nexthop
, 0, sizeof(nexthop
));
419 if (!argv_find(argv
, argc
, "no", &idx
)) {
420 argv_find(argv
, argc
, "neighbor", &idx
);
421 address
= argv
[idx
+ 1]->arg
;
423 if (inet_pton(AF_INET
, address
, &nexthop
.ipv4
) == 1)
425 else if (inet_pton(AF_INET6
, address
, &nexthop
.ipv6
) == 1)
428 vty_out(vty
, "%% Malformed address\n");
433 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, af
, &nexthop
,
434 pw
->local_label
, pw
->remote_label
, pw
->flags
,
440 DEFUN (pseudowire_control_word
,
441 pseudowire_control_word_cmd
,
442 "[no] control-word <exclude|include>",
444 "Control-word options\n"
445 "Exclude control-word in pseudowire packets\n"
446 "Include control-word in pseudowire packets\n")
448 VTY_DECLVAR_CONTEXT(zebra_pw
, pw
);
452 if (argv_find(argv
, argc
, "no", &idx
))
453 flags
= F_PSEUDOWIRE_CWORD
;
455 argv_find(argv
, argc
, "control-word", &idx
);
456 if (argv
[idx
+ 1]->text
[0] == 'i')
457 flags
= F_PSEUDOWIRE_CWORD
;
460 zebra_pw_change(pw
, pw
->ifindex
, pw
->type
, pw
->af
, &pw
->nexthop
,
461 pw
->local_label
, pw
->remote_label
, flags
, &pw
->data
);
466 DEFUN (show_pseudowires
,
467 show_pseudowires_cmd
,
468 "show mpls pseudowires",
473 struct zebra_vrf
*zvrf
;
476 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
480 vty_out(vty
, "%-16s %-24s %-12s %-8s %-10s\n", "Interface", "Neighbor",
481 "Labels", "Protocol", "Status");
483 RB_FOREACH (pw
, zebra_pw_head
, &zvrf
->pseudowires
) {
484 char buf_nbr
[INET6_ADDRSTRLEN
];
487 inet_ntop(pw
->af
, &pw
->nexthop
, buf_nbr
, sizeof(buf_nbr
));
489 if (pw
->local_label
!= MPLS_NO_LABEL
490 && pw
->remote_label
!= MPLS_NO_LABEL
)
491 snprintf(buf_labels
, sizeof(buf_labels
), "%u/%u",
492 pw
->local_label
, pw
->remote_label
);
494 snprintf(buf_labels
, sizeof(buf_labels
), "-");
496 vty_out(vty
, "%-16s %-24s %-12s %-8s %-10s\n", pw
->ifname
,
497 (pw
->af
!= AF_UNSPEC
) ? buf_nbr
: "-", buf_labels
,
498 zebra_route_string(pw
->protocol
),
499 (zebra_pw_enabled(pw
) && pw
->status
== PW_STATUS_UP
)
507 /* Pseudowire configuration write function. */
508 static int zebra_pw_config(struct vty
*vty
)
511 struct zebra_vrf
*zvrf
;
514 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
518 RB_FOREACH (pw
, zebra_static_pw_head
, &zvrf
->static_pseudowires
) {
519 vty_out(vty
, "pseudowire %s\n", pw
->ifname
);
520 if (pw
->local_label
!= MPLS_NO_LABEL
521 && pw
->remote_label
!= MPLS_NO_LABEL
)
522 vty_out(vty
, " mpls label local %u remote %u\n",
523 pw
->local_label
, pw
->remote_label
);
526 " ! Incomplete config, specify the static "
529 if (pw
->af
!= AF_UNSPEC
) {
530 char buf
[INET6_ADDRSTRLEN
];
531 inet_ntop(pw
->af
, &pw
->nexthop
, buf
, sizeof(buf
));
532 vty_out(vty
, " neighbor %s\n", buf
);
535 " ! Incomplete config, specify a neighbor "
538 if (!(pw
->flags
& F_PSEUDOWIRE_CWORD
))
539 vty_out(vty
, " control-word exclude\n");
548 static struct cmd_node pw_node
= {
549 PW_NODE
, "%s(config-pw)# ", 1,
552 void zebra_pw_vty_init(void)
554 install_node(&pw_node
, zebra_pw_config
);
555 install_default(PW_NODE
);
557 install_element(CONFIG_NODE
, &pseudowire_if_cmd
);
558 install_element(CONFIG_NODE
, &no_pseudowire_if_cmd
);
559 install_element(PW_NODE
, &pseudowire_labels_cmd
);
560 install_element(PW_NODE
, &pseudowire_neighbor_cmd
);
561 install_element(PW_NODE
, &pseudowire_control_word_cmd
);
563 install_element(VIEW_NODE
, &show_pseudowires_cmd
);