]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_pw.c
zebra: add pseudowire manager
[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_vrf.h"
31 #include "zebra/zebra_pw.h"
32
33 DEFINE_MTYPE_STATIC(LIB, PW, "Pseudowire")
34
35 DEFINE_HOOK(pw_install, (struct zebra_pw *pw), (pw))
36 DEFINE_HOOK(pw_uninstall, (struct zebra_pw *pw), (pw))
37
38 extern struct zebra_t zebrad;
39
40 static void zebra_pw_install(struct zebra_pw *);
41 static void zebra_pw_uninstall(struct zebra_pw *);
42 static int zebra_pw_install_retry(struct thread *);
43 static int zebra_pw_check_reachability(struct zebra_pw *);
44 static void zebra_pw_update_status(struct zebra_pw *, int);
45
46 static inline int zebra_pw_compare(const struct zebra_pw *a,
47 const struct zebra_pw *b)
48 {
49 return (strcmp(a->ifname, b->ifname));
50 }
51
52 RB_GENERATE(zebra_pw_head, zebra_pw, entry, zebra_pw_compare)
53
54 struct zebra_pw *zebra_pw_add(struct zebra_vrf *zvrf, const char *ifname,
55 uint8_t protocol, struct zserv *client)
56 {
57 struct zebra_pw *pw;
58
59 if (IS_ZEBRA_DEBUG_PW)
60 zlog_debug("%u: adding pseudowire %s protocol %s",
61 zvrf_id(zvrf), ifname, zebra_route_string(protocol));
62
63 pw = XCALLOC(MTYPE_PW, sizeof(*pw));
64 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
65 pw->protocol = protocol;
66 pw->vrf_id = zvrf_id(zvrf);
67 pw->client = client;
68 pw->status = PW_STATUS_UP;
69
70 RB_INSERT(zebra_pw_head, &zvrf->pseudowires, pw);
71
72 return pw;
73 }
74
75 void zebra_pw_del(struct zebra_vrf *zvrf, struct zebra_pw *pw)
76 {
77 if (IS_ZEBRA_DEBUG_PW)
78 zlog_debug("%u: deleting pseudowire %s protocol %s", pw->vrf_id,
79 pw->ifname, zebra_route_string(pw->protocol));
80
81 /* uninstall */
82 if (pw->status == PW_STATUS_UP)
83 hook_call(pw_uninstall, pw);
84 else if (pw->install_retry_timer)
85 THREAD_TIMER_OFF(pw->install_retry_timer);
86
87 /* unlink and release memory */
88 RB_REMOVE(zebra_pw_head, &zvrf->pseudowires, pw);
89 XFREE(MTYPE_PW, pw);
90 }
91
92 void zebra_pw_change(struct zebra_pw *pw, ifindex_t ifindex, int type, int af,
93 union g_addr *nexthop, uint32_t local_label,
94 uint32_t remote_label, uint8_t flags,
95 union pw_protocol_fields *data)
96 {
97 pw->ifindex = ifindex;
98 pw->type = type;
99 pw->af = af;
100 pw->nexthop = *nexthop;
101 pw->local_label = local_label;
102 pw->remote_label = remote_label;
103 pw->flags = flags;
104 pw->data = *data;
105
106 if (pw->enabled)
107 zebra_pw_update(pw);
108 else
109 zebra_pw_uninstall(pw);
110 }
111
112 struct zebra_pw *zebra_pw_find(struct zebra_vrf *zvrf, const char *ifname)
113 {
114 struct zebra_pw pw;
115 strlcpy(pw.ifname, ifname, sizeof(pw.ifname));
116 return (RB_FIND(zebra_pw_head, &zvrf->pseudowires, &pw));
117 }
118
119 void zebra_pw_update(struct zebra_pw *pw)
120 {
121 if (zebra_pw_check_reachability(pw) < 0) {
122 zebra_pw_uninstall(pw);
123 } else {
124 /*
125 * Install or reinstall the pseudowire (e.g. to update
126 * parameters like the nexthop or the use of the control word).
127 */
128 zebra_pw_install(pw);
129 }
130 }
131
132 static void zebra_pw_install(struct zebra_pw *pw)
133 {
134 if (IS_ZEBRA_DEBUG_PW)
135 zlog_debug("%u: installing pseudowire %s protocol %s",
136 pw->vrf_id, pw->ifname,
137 zebra_route_string(pw->protocol));
138
139 if (hook_call(pw_install, pw)) {
140 zebra_pw_install_failure(pw);
141 return;
142 }
143
144 if (pw->status == PW_STATUS_DOWN)
145 zebra_pw_update_status(pw, PW_STATUS_UP);
146 }
147
148 static void zebra_pw_uninstall(struct zebra_pw *pw)
149 {
150 if (pw->status == PW_STATUS_DOWN)
151 return;
152
153 if (IS_ZEBRA_DEBUG_PW)
154 zlog_debug("%u: uninstalling pseudowire %s protocol %s",
155 pw->vrf_id, pw->ifname,
156 zebra_route_string(pw->protocol));
157
158 /* ignore any possible error */
159 hook_call(pw_uninstall, pw);
160
161 if (pw->enabled)
162 zebra_pw_update_status(pw, PW_STATUS_DOWN);
163 }
164
165 /*
166 * Installation of the pseudowire in the kernel or hardware has failed. This
167 * function will notify the pseudowire client about the failure and schedule
168 * to retry the installation later. This function can be called by an external
169 * agent that performs the pseudowire installation in an asynchronous way.
170 */
171 void zebra_pw_install_failure(struct zebra_pw *pw)
172 {
173 if (IS_ZEBRA_DEBUG_PW)
174 zlog_debug("%u: failed installing pseudowire %s, "
175 "scheduling retry in %u seconds", pw->vrf_id,
176 pw->ifname, PW_INSTALL_RETRY_INTERVAL);
177
178 /* schedule to retry later */
179 THREAD_TIMER_OFF(pw->install_retry_timer);
180 pw->install_retry_timer =
181 thread_add_timer(zebrad.master, zebra_pw_install_retry,
182 pw, PW_INSTALL_RETRY_INTERVAL);
183
184 zebra_pw_update_status(pw, PW_STATUS_DOWN);
185 }
186
187 static int zebra_pw_install_retry(struct thread *thread)
188 {
189 struct zebra_pw *pw = THREAD_ARG(thread);
190
191 pw->install_retry_timer = NULL;
192 zebra_pw_install(pw);
193
194 return 0;
195 }
196
197 static void zebra_pw_update_status(struct zebra_pw *pw, int status)
198 {
199 pw->status = status;
200 if (pw->client)
201 zsend_pw_update(pw->client, pw);
202 }
203
204 static int zebra_pw_check_reachability(struct zebra_pw *pw)
205 {
206 struct rib *rib;
207 struct nexthop *nexthop, *tnexthop;
208 int recursing;
209
210 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
211
212 /* find route to the remote end of the pseudowire */
213 rib = rib_match(family2afi(pw->af), SAFI_UNICAST, pw->vrf_id,
214 &pw->nexthop, NULL);
215 if (!rib) {
216 if (IS_ZEBRA_DEBUG_PW)
217 zlog_warn("%s: no route found for %s", __func__,
218 pw->ifname);
219 return -1;
220 }
221
222 /*
223 * Need to ensure that there's a label binding for all nexthops.
224 * Otherwise, ECMP for this route could render the pseudowire unusable.
225 */
226 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) {
227 if (!nexthop->nh_label) {
228 if (IS_ZEBRA_DEBUG_PW)
229 zlog_warn("%s: unlabeled route for %s",
230 __func__, pw->ifname);
231 return -1;
232 }
233 }
234
235 return 0;
236 }
237
238 void
239 zebra_pw_client_close(struct zserv *client)
240 {
241 struct vrf *vrf;
242 struct zebra_vrf *zvrf;
243 struct zebra_pw *pw, *tmp;
244
245 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id) {
246 zvrf = vrf->info;
247 RB_FOREACH_SAFE(pw, zebra_pw_head, &zvrf->pseudowires, tmp) {
248 if (pw->client != client)
249 continue;
250 zebra_pw_del(zvrf, pw);
251 }
252 }
253 }
254
255 void zebra_pw_init(struct zebra_vrf *zvrf)
256 {
257 RB_INIT(&zvrf->pseudowires);
258 }
259
260 void zebra_pw_exit(struct zebra_vrf *zvrf)
261 {
262 struct zebra_pw *pw;
263
264 while ((pw = RB_ROOT(&zvrf->pseudowires)) != NULL)
265 zebra_pw_del(zvrf, pw);
266 }