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_vrf.h"
31 #include "zebra/zebra_pw.h"
33 DEFINE_MTYPE_STATIC(LIB
, PW
, "Pseudowire")
35 DEFINE_HOOK(pw_install
, (struct zebra_pw
*pw
), (pw
))
36 DEFINE_HOOK(pw_uninstall
, (struct zebra_pw
*pw
), (pw
))
38 extern struct zebra_t zebrad
;
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);
46 static inline int zebra_pw_compare(const struct zebra_pw
*a
,
47 const struct zebra_pw
*b
)
49 return (strcmp(a
->ifname
, b
->ifname
));
52 RB_GENERATE(zebra_pw_head
, zebra_pw
, entry
, zebra_pw_compare
)
54 struct zebra_pw
*zebra_pw_add(struct zebra_vrf
*zvrf
, const char *ifname
,
55 uint8_t protocol
, struct zserv
*client
)
59 if (IS_ZEBRA_DEBUG_PW
)
60 zlog_debug("%u: adding pseudowire %s protocol %s",
61 zvrf_id(zvrf
), ifname
, zebra_route_string(protocol
));
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
);
68 pw
->status
= PW_STATUS_UP
;
70 RB_INSERT(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
75 void zebra_pw_del(struct zebra_vrf
*zvrf
, struct zebra_pw
*pw
)
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
));
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
);
87 /* unlink and release memory */
88 RB_REMOVE(zebra_pw_head
, &zvrf
->pseudowires
, pw
);
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
)
97 pw
->ifindex
= ifindex
;
100 pw
->nexthop
= *nexthop
;
101 pw
->local_label
= local_label
;
102 pw
->remote_label
= remote_label
;
109 zebra_pw_uninstall(pw
);
112 struct zebra_pw
*zebra_pw_find(struct zebra_vrf
*zvrf
, const char *ifname
)
115 strlcpy(pw
.ifname
, ifname
, sizeof(pw
.ifname
));
116 return (RB_FIND(zebra_pw_head
, &zvrf
->pseudowires
, &pw
));
119 void zebra_pw_update(struct zebra_pw
*pw
)
121 if (zebra_pw_check_reachability(pw
) < 0) {
122 zebra_pw_uninstall(pw
);
125 * Install or reinstall the pseudowire (e.g. to update
126 * parameters like the nexthop or the use of the control word).
128 zebra_pw_install(pw
);
132 static void zebra_pw_install(struct zebra_pw
*pw
)
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
));
139 if (hook_call(pw_install
, pw
)) {
140 zebra_pw_install_failure(pw
);
144 if (pw
->status
== PW_STATUS_DOWN
)
145 zebra_pw_update_status(pw
, PW_STATUS_UP
);
148 static void zebra_pw_uninstall(struct zebra_pw
*pw
)
150 if (pw
->status
== PW_STATUS_DOWN
)
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
));
158 /* ignore any possible error */
159 hook_call(pw_uninstall
, pw
);
162 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
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.
171 void zebra_pw_install_failure(struct zebra_pw
*pw
)
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
);
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
);
184 zebra_pw_update_status(pw
, PW_STATUS_DOWN
);
187 static int zebra_pw_install_retry(struct thread
*thread
)
189 struct zebra_pw
*pw
= THREAD_ARG(thread
);
191 pw
->install_retry_timer
= NULL
;
192 zebra_pw_install(pw
);
197 static void zebra_pw_update_status(struct zebra_pw
*pw
, int status
)
201 zsend_pw_update(pw
->client
, pw
);
204 static int zebra_pw_check_reachability(struct zebra_pw
*pw
)
207 struct nexthop
*nexthop
, *tnexthop
;
210 /* TODO: consider GRE/L2TPv3 tunnels in addition to MPLS LSPs */
212 /* find route to the remote end of the pseudowire */
213 rib
= rib_match(family2afi(pw
->af
), SAFI_UNICAST
, pw
->vrf_id
,
216 if (IS_ZEBRA_DEBUG_PW
)
217 zlog_warn("%s: no route found for %s", __func__
,
223 * Need to ensure that there's a label binding for all nexthops.
224 * Otherwise, ECMP for this route could render the pseudowire unusable.
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
);
239 zebra_pw_client_close(struct zserv
*client
)
242 struct zebra_vrf
*zvrf
;
243 struct zebra_pw
*pw
, *tmp
;
245 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
) {
247 RB_FOREACH_SAFE(pw
, zebra_pw_head
, &zvrf
->pseudowires
, tmp
) {
248 if (pw
->client
!= client
)
250 zebra_pw_del(zvrf
, pw
);
255 void zebra_pw_init(struct zebra_vrf
*zvrf
)
257 RB_INIT(&zvrf
->pseudowires
);
260 void zebra_pw_exit(struct zebra_vrf
*zvrf
)
264 while ((pw
= RB_ROOT(&zvrf
->pseudowires
)) != NULL
)
265 zebra_pw_del(zvrf
, pw
);