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,
27 #include "zebra/debug.h"
28 #include "zebra/rib.h"
29 #include "zebra/zserv.h"
30 #include "zebra/zebra_rnh.h"
31 #include "zebra/zebra_vrf.h"
32 #include "zebra/zebra_pw.h"
34 DEFINE_MTYPE_STATIC(LIB
, PW
, "Pseudowire")
36 DEFINE_HOOK(pw_install
, (struct zebra_pw
*pw
), (pw
))
37 DEFINE_HOOK(pw_uninstall
, (struct zebra_pw
*pw
), (pw
))
39 extern struct zebra_t zebrad
;
41 static void zebra_pw_install(struct zebra_pw
*);
42 static void zebra_pw_uninstall(struct zebra_pw
*);
43 static int zebra_pw_install_retry(struct thread
*);
44 static int zebra_pw_check_reachability(struct zebra_pw
*);
45 static void zebra_pw_update_status(struct zebra_pw
*, int);
47 static inline int zebra_pw_compare(const struct zebra_pw
*a
,
48 const struct zebra_pw
*b
)
50 return (strcmp(a
->ifname
, b
->ifname
));
53 RB_GENERATE(zebra_pw_head
, zebra_pw
, entry
, zebra_pw_compare
)
55 struct zebra_pw
*zebra_pw_add(struct zebra_vrf
*zvrf
, const char *ifname
,
56 uint8_t protocol
, struct zserv
*client
)
60 if (IS_ZEBRA_DEBUG_PW
)
61 zlog_debug("%u: adding pseudowire %s protocol %s",
62 zvrf_id(zvrf
), ifname
, zebra_route_string(protocol
));
64 pw
= XCALLOC(MTYPE_PW
, sizeof(*pw
));
65 strlcpy(pw
->ifname
, ifname
, sizeof(pw
->ifname
));
66 pw
->protocol
= protocol
;
67 pw
->vrf_id
= zvrf_id(zvrf
);
69 pw
->status
= PW_STATUS_UP
;
71 RB_INSERT(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
76 void zebra_pw_del(struct zebra_vrf
*zvrf
, struct zebra_pw
*pw
)
78 if (IS_ZEBRA_DEBUG_PW
)
79 zlog_debug("%u: deleting pseudowire %s protocol %s", pw
->vrf_id
,
80 pw
->ifname
, zebra_route_string(pw
->protocol
));
82 /* remove nexthop tracking */
83 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
86 if (pw
->status
== PW_STATUS_UP
)
87 hook_call(pw_uninstall
, pw
);
88 else if (pw
->install_retry_timer
)
89 THREAD_TIMER_OFF(pw
->install_retry_timer
);
91 /* unlink and release memory */
92 RB_REMOVE(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
96 void zebra_pw_change(struct zebra_pw
*pw
, ifindex_t ifindex
, int type
, int af
,
97 union g_addr
*nexthop
, uint32_t local_label
,
98 uint32_t remote_label
, uint8_t flags
,
99 union pw_protocol_fields
*data
)
101 zebra_deregister_rnh_pseudowire(pw
->vrf_id
, pw
);
103 pw
->ifindex
= ifindex
;
106 pw
->nexthop
= *nexthop
;
107 pw
->local_label
= local_label
;
108 pw
->remote_label
= remote_label
;
113 zebra_register_rnh_pseudowire(pw
->vrf_id
, pw
);
115 zebra_pw_uninstall(pw
);
118 struct zebra_pw
*zebra_pw_find(struct zebra_vrf
*zvrf
, const char *ifname
)
121 strlcpy(pw
.ifname
, ifname
, sizeof(pw
.ifname
));
122 return (RB_FIND(zebra_pw_head
, &zvrf
->pseudowires
, &pw
));
125 void zebra_pw_update(struct zebra_pw
*pw
)
127 if (zebra_pw_check_reachability(pw
) < 0) {
128 zebra_pw_uninstall(pw
);
129 /* wait for NHT and try again later */
132 * Install or reinstall the pseudowire (e.g. to update
133 * parameters like the nexthop or the use of the control word).
135 zebra_pw_install(pw
);
139 static void zebra_pw_install(struct zebra_pw
*pw
)
141 if (IS_ZEBRA_DEBUG_PW
)
142 zlog_debug("%u: installing pseudowire %s protocol %s",
143 pw
->vrf_id
, pw
->ifname
,
144 zebra_route_string(pw
->protocol
));
146 if (hook_call(pw_install
, pw
)) {
147 zebra_pw_install_failure(pw
);
151 if (pw
->status
== PW_STATUS_DOWN
)
152 zebra_pw_update_status(pw
, PW_STATUS_UP
);
155 static void zebra_pw_uninstall(struct zebra_pw
*pw
)
157 if (pw
->status
== PW_STATUS_DOWN
)
160 if (IS_ZEBRA_DEBUG_PW
)
161 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
162 pw
->vrf_id
, pw
->ifname
,
163 zebra_route_string(pw
->protocol
));
165 /* ignore any possible error */
166 hook_call(pw_uninstall
, pw
);
169 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
173 * Installation of the pseudowire in the kernel or hardware has failed. This
174 * function will notify the pseudowire client about the failure and schedule
175 * to retry the installation later. This function can be called by an external
176 * agent that performs the pseudowire installation in an asynchronous way.
178 void zebra_pw_install_failure(struct zebra_pw
*pw
)
180 if (IS_ZEBRA_DEBUG_PW
)
181 zlog_debug("%u: failed installing pseudowire %s, "
182 "scheduling retry in %u seconds", pw
->vrf_id
,
183 pw
->ifname
, PW_INSTALL_RETRY_INTERVAL
);
185 /* schedule to retry later */
186 THREAD_TIMER_OFF(pw
->install_retry_timer
);
187 pw
->install_retry_timer
=
188 thread_add_timer(zebrad
.master
, zebra_pw_install_retry
,
189 pw
, PW_INSTALL_RETRY_INTERVAL
);
191 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
194 static int zebra_pw_install_retry(struct thread
*thread
)
196 struct zebra_pw
*pw
= THREAD_ARG(thread
);
198 pw
->install_retry_timer
= NULL
;
199 zebra_pw_install(pw
);
204 static void zebra_pw_update_status(struct zebra_pw
*pw
, int status
)
208 zsend_pw_update(pw
->client
, pw
);
211 static int zebra_pw_check_reachability(struct zebra_pw
*pw
)
214 struct nexthop
*nexthop
, *tnexthop
;
217 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
219 /* find route to the remote end of the pseudowire */
220 rib
= rib_match(family2afi(pw
->af
), SAFI_UNICAST
, pw
->vrf_id
,
223 if (IS_ZEBRA_DEBUG_PW
)
224 zlog_warn("%s: no route found for %s", __func__
,
230 * Need to ensure that there's a label binding for all nexthops.
231 * Otherwise, ECMP for this route could render the pseudowire unusable.
233 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
)) {
234 if (!nexthop
->nh_label
) {
235 if (IS_ZEBRA_DEBUG_PW
)
236 zlog_warn("%s: unlabeled route for %s",
237 __func__
, pw
->ifname
);
246 zebra_pw_client_close(struct zserv
*client
)
249 struct zebra_vrf
*zvrf
;
250 struct zebra_pw
*pw
, *tmp
;
252 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
) {
254 RB_FOREACH_SAFE(pw
, zebra_pw_head
, &zvrf
->pseudowires
, tmp
) {
255 if (pw
->client
!= client
)
257 zebra_pw_del(zvrf
, pw
);
262 void zebra_pw_init(struct zebra_vrf
*zvrf
)
264 RB_INIT(&zvrf
->pseudowires
);
267 void zebra_pw_exit(struct zebra_vrf
*zvrf
)
271 while ((pw
= RB_ROOT(&zvrf
->pseudowires
)) != NULL
)
272 zebra_pw_del(zvrf
, pw
);