]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pw.c
zebra: add nexthop tracking for pseudowires
[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 "vrf.h"
26
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"
33
34 DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire")
35
36 DEFINE_HOOK(pw_install, (struct zebra_pw *pw), (pw))
37 DEFINE_HOOK(pw_uninstall, (struct zebra_pw *pw), (pw))
38
39 extern struct zebra_t zebrad;
40
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);
46
47 static inline int zebra_pw_compare(const struct zebra_pw *a,
48 const struct zebra_pw *b)
49 {
50 return (strcmp(a->ifname, b->ifname));
51 }
52
53 RB_GENERATE(zebra_pw_head, zebra_pw, entry, zebra_pw_compare)
54
55 struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
56 uint8_t protocol, struct zserv *client)
57 {
58 struct zebra_pw *pw;
59
60 if (IS_ZEBRA_DEBUG_PW)
61 zlog_debug("%u: adding pseudowire %s protocol %s",
62 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
63
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);
68 pw->client = client;
69 pw->status = PW_STATUS_UP;
70
71 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
72
73 return pw;
74 }
75
76 void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
77 {
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));
81
82 /* remove nexthop tracking */
83 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
84
85 /* uninstall */
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);
90
91 /* unlink and release memory */
92 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
93 XFREE(MTYPE_PW, pw);
94 }
95
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)
100 {
101 zebra_deregister_rnh_pseudowire(pw->vrf_id, pw);
102
103 pw->ifindex = ifindex;
104 pw->type = type;
105 pw->af = af;
106 pw->nexthop = *nexthop;
107 pw->local_label = local_label;
108 pw->remote_label = remote_label;
109 pw->flags = flags;
110 pw->data = *data;
111
112 if (pw->enabled)
113 zebra_register_rnh_pseudowire(pw->vrf_id, pw);
114 else
115 zebra_pw_uninstall(pw);
116 }
117
118 struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
119 {
120 struct zebra_pw pw;
121 strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
122 return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
123 }
124
125 void zebra_pw_update(struct zebra_pw *pw)
126 {
127 if (zebra_pw_check_reachability(pw) < 0) {
128 zebra_pw_uninstall(pw);
129 /* wait for NHT and try again later */
130 } else {
131 /*
132 * Install or reinstall the pseudowire (e.g. to update
133 * parameters like the nexthop or the use of the control word).
134 */
135 zebra_pw_install(pw);
136 }
137 }
138
139 static void zebra_pw_install(struct zebra_pw *pw)
140 {
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));
145
146 if (hook_call(pw_install, pw)) {
147 zebra_pw_install_failure(pw);
148 return;
149 }
150
151 if (pw->status == PW_STATUS_DOWN)
152 zebra_pw_update_status(pw, PW_STATUS_UP);
153 }
154
155 static void zebra_pw_uninstall(struct zebra_pw *pw)
156 {
157 if (pw->status == PW_STATUS_DOWN)
158 return;
159
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));
164
165 /* ignore any possible error */
166 hook_call(pw_uninstall, pw);
167
168 if (pw->enabled)
169 zebra_pw_update_status(pw, PW_STATUS_DOWN);
170 }
171
172 /*
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.
177 */
178 void zebra_pw_install_failure(struct zebra_pw *pw)
179 {
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);
184
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);
190
191 zebra_pw_update_status(pw, PW_STATUS_DOWN);
192 }
193
194 static int zebra_pw_install_retry(struct thread *thread)
195 {
196 struct zebra_pw *pw = THREAD_ARG(thread);
197
198 pw->install_retry_timer = NULL;
199 zebra_pw_install(pw);
200
201 return 0;
202 }
203
204 static void zebra_pw_update_status(struct zebra_pw *pw, int status)
205 {
206 pw->status = status;
207 if (pw->client)
208 zsend_pw_update(pw->client, pw);
209 }
210
211 static int zebra_pw_check_reachability(struct zebra_pw *pw)
212 {
213 struct rib *rib;
214 struct nexthop *nexthop, *tnexthop;
215 int recursing;
216
217 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
218
219 /* find route to the remote end of the pseudowire */
220 rib = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
221 &pw->nexthop, NULL);
222 if (!rib) {
223 if (IS_ZEBRA_DEBUG_PW)
224 zlog_warn("%s: no route found for %s", __func__,
225 pw->ifname);
226 return -1;
227 }
228
229 /*
230 * Need to ensure that there's a label binding for all nexthops.
231 * Otherwise, ECMP for this route could render the pseudowire unusable.
232 */
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);
238 return -1;
239 }
240 }
241
242 return 0;
243 }
244
245 void
246 zebra_pw_client_close(struct zserv *client)
247 {
248 struct vrf *vrf;
249 struct zebra_vrf *zvrf;
250 struct zebra_pw *pw, *tmp;
251
252 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id) {
253 zvrf = vrf->info;
254 RB_FOREACH_SAFE(pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
255 if (pw->client != client)
256 continue;
257 zebra_pw_del(zvrf, pw);
258 }
259 }
260 }
261
262 void zebra_pw_init(struct zebra_vrf *zvrf)
263 {
264 RB_INIT(&zvrf->pseudowires);
265 }
266
267 void zebra_pw_exit(struct zebra_vrf *zvrf)
268 {
269 struct zebra_pw *pw;
270
271 while ((pw = RB_ROOT(&zvrf->pseudowires)) != NULL)
272 zebra_pw_del(zvrf, pw);
273 }