]> git.proxmox.com Git - mirror_frr.git/blob - sharpd/sharp_zebra.c
*: Convert zapi->interface_delete to ifp callback
[mirror_frr.git] / sharpd / sharp_zebra.c
1 /*
2 * Zebra connect code.
3 * Copyright (C) Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This file is part of FRR.
7 *
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #include <zebra.h>
23
24 #include "thread.h"
25 #include "command.h"
26 #include "network.h"
27 #include "prefix.h"
28 #include "routemap.h"
29 #include "table.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "zclient.h"
33 #include "filter.h"
34 #include "plist.h"
35 #include "log.h"
36 #include "nexthop.h"
37 #include "nexthop_group.h"
38
39 #include "sharp_globals.h"
40 #include "sharp_nht.h"
41 #include "sharp_zebra.h"
42
43 /* Zebra structure to hold current status. */
44 struct zclient *zclient = NULL;
45
46 /* For registering threads. */
47 extern struct thread_master *master;
48
49 /* Inteface addition message from zebra. */
50 static int sharp_ifp_create(struct interface *ifp)
51 {
52 return 0;
53 }
54
55 static int sharp_ifp_destroy(struct interface *ifp)
56 {
57 return 0;
58 }
59
60 static int interface_address_add(ZAPI_CALLBACK_ARGS)
61 {
62 zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
63
64 return 0;
65 }
66
67 static int interface_address_delete(ZAPI_CALLBACK_ARGS)
68 {
69 struct connected *c;
70
71 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
72
73 if (!c)
74 return 0;
75
76 connected_free(c);
77 return 0;
78 }
79
80 static int sharp_ifp_up(struct interface *ifp)
81 {
82 return 0;
83 }
84
85 static int sharp_ifp_down(struct interface *ifp)
86 {
87 return 0;
88 }
89
90 void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
91 uint8_t instance, struct nexthop_group *nhg,
92 uint32_t routes)
93 {
94 uint32_t temp, i;
95 bool v4 = false;
96
97 zlog_debug("Inserting %u routes", routes);
98
99 if (p->family == AF_INET) {
100 v4 = true;
101 temp = ntohl(p->u.prefix4.s_addr);
102 } else
103 temp = ntohl(p->u.val32[3]);
104
105 monotime(&sg.r.t_start);
106 for (i = 0; i < routes; i++) {
107 route_add(p, vrf_id, (uint8_t)instance, nhg);
108 if (v4)
109 p->u.prefix4.s_addr = htonl(++temp);
110 else
111 p->u.val32[3] = htonl(++temp);
112 }
113 }
114
115 void sharp_remove_routes_helper(struct prefix *p, vrf_id_t vrf_id,
116 uint8_t instance, uint32_t routes)
117 {
118 uint32_t temp, i;
119 bool v4 = false;
120
121 zlog_debug("Removing %u routes", routes);
122
123 if (p->family == AF_INET) {
124 v4 = true;
125 temp = ntohl(p->u.prefix4.s_addr);
126 } else
127 temp = ntohl(p->u.val32[3]);
128
129 monotime(&sg.r.t_start);
130 for (i = 0; i < routes; i++) {
131 route_delete(p, vrf_id, (uint8_t)instance);
132 if (v4)
133 p->u.prefix4.s_addr = htonl(++temp);
134 else
135 p->u.val32[3] = htonl(++temp);
136 }
137 }
138
139 static void handle_repeated(bool installed)
140 {
141 struct prefix p = sg.r.orig_prefix;
142 sg.r.repeat--;
143
144 if (sg.r.repeat <= 0)
145 return;
146
147 if (installed) {
148 sg.r.removed_routes = 0;
149 sharp_remove_routes_helper(&p, sg.r.vrf_id,
150 sg.r.inst, sg.r.total_routes);
151 }
152
153 if (!installed) {
154 sg.r.installed_routes = 0;
155 sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst,
156 &sg.r.nhop_group,
157 sg.r.total_routes);
158 }
159 }
160
161 static int route_notify_owner(ZAPI_CALLBACK_ARGS)
162 {
163 struct timeval r;
164 struct prefix p;
165 enum zapi_route_notify_owner note;
166 uint32_t table;
167
168 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
169 return -1;
170
171 switch (note) {
172 case ZAPI_ROUTE_INSTALLED:
173 sg.r.installed_routes++;
174 if (sg.r.total_routes == sg.r.installed_routes) {
175 monotime(&sg.r.t_end);
176 timersub(&sg.r.t_end, &sg.r.t_start, &r);
177 zlog_debug("Installed All Items %jd.%ld",
178 (intmax_t)r.tv_sec, (long)r.tv_usec);
179 handle_repeated(true);
180 }
181 break;
182 case ZAPI_ROUTE_FAIL_INSTALL:
183 zlog_debug("Failed install of route");
184 break;
185 case ZAPI_ROUTE_BETTER_ADMIN_WON:
186 zlog_debug("Better Admin Distance won over us");
187 break;
188 case ZAPI_ROUTE_REMOVED:
189 sg.r.removed_routes++;
190 if (sg.r.total_routes == sg.r.removed_routes) {
191 monotime(&sg.r.t_end);
192 timersub(&sg.r.t_end, &sg.r.t_start, &r);
193 zlog_debug("Removed all Items %jd.%ld",
194 (intmax_t)r.tv_sec, (long)r.tv_usec);
195 handle_repeated(false);
196 }
197 break;
198 case ZAPI_ROUTE_REMOVE_FAIL:
199 zlog_debug("Route removal Failure");
200 break;
201 }
202 return 0;
203 }
204
205 static void zebra_connected(struct zclient *zclient)
206 {
207 zclient_send_reg_requests(zclient, VRF_DEFAULT);
208 }
209
210 void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
211 {
212 zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
213 }
214
215 void route_add(struct prefix *p, vrf_id_t vrf_id,
216 uint8_t instance, struct nexthop_group *nhg)
217 {
218 struct zapi_route api;
219 struct zapi_nexthop *api_nh;
220 struct nexthop *nh;
221 int i = 0;
222
223 memset(&api, 0, sizeof(api));
224 api.vrf_id = vrf_id;
225 api.type = ZEBRA_ROUTE_SHARP;
226 api.instance = instance;
227 api.safi = SAFI_UNICAST;
228 memcpy(&api.prefix, p, sizeof(*p));
229
230 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
231 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
232
233 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
234 api_nh = &api.nexthops[i];
235 api_nh->vrf_id = nh->vrf_id;
236 api_nh->type = nh->type;
237 switch (nh->type) {
238 case NEXTHOP_TYPE_IPV4:
239 api_nh->gate = nh->gate;
240 break;
241 case NEXTHOP_TYPE_IPV4_IFINDEX:
242 api_nh->gate = nh->gate;
243 api_nh->ifindex = nh->ifindex;
244 break;
245 case NEXTHOP_TYPE_IFINDEX:
246 api_nh->ifindex = nh->ifindex;
247 break;
248 case NEXTHOP_TYPE_IPV6:
249 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16);
250 break;
251 case NEXTHOP_TYPE_IPV6_IFINDEX:
252 api_nh->ifindex = nh->ifindex;
253 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16);
254 break;
255 case NEXTHOP_TYPE_BLACKHOLE:
256 api_nh->bh_type = nh->bh_type;
257 break;
258 }
259 i++;
260 }
261 api.nexthop_num = i;
262
263 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
264 }
265
266 void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance)
267 {
268 struct zapi_route api;
269
270 memset(&api, 0, sizeof(api));
271 api.vrf_id = vrf_id;
272 api.type = ZEBRA_ROUTE_SHARP;
273 api.safi = SAFI_UNICAST;
274 api.instance = instance;
275 memcpy(&api.prefix, p, sizeof(*p));
276 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
277
278 return;
279 }
280
281 void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id, bool import,
282 bool watch, bool connected)
283 {
284 int command;
285
286 if (!import) {
287 command = ZEBRA_NEXTHOP_REGISTER;
288
289 if (!watch)
290 command = ZEBRA_NEXTHOP_UNREGISTER;
291 } else {
292 command = ZEBRA_IMPORT_ROUTE_REGISTER;
293
294 if (!watch)
295 command = ZEBRA_IMPORT_ROUTE_UNREGISTER;
296 }
297
298 if (zclient_send_rnh(zclient, command, p, connected, vrf_id) < 0)
299 zlog_warn("%s: Failure to send nexthop to zebra",
300 __PRETTY_FUNCTION__);
301 }
302
303 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS)
304 {
305 struct sharp_nh_tracker *nht;
306 struct zapi_route nhr;
307 char buf[PREFIX_STRLEN];
308 int i;
309
310 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
311 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__);
312
313 return 0;
314 }
315
316 zlog_debug("Received update for %s",
317 prefix2str(&nhr.prefix, buf, sizeof(buf)));
318
319 nht = sharp_nh_tracker_get(&nhr.prefix);
320 nht->nhop_num = nhr.nexthop_num;
321 nht->updates++;
322
323 for (i = 0; i < nhr.nexthop_num; i++) {
324 struct zapi_nexthop *znh = &nhr.nexthops[i];
325
326 switch (znh->type) {
327 case NEXTHOP_TYPE_IPV4_IFINDEX:
328 case NEXTHOP_TYPE_IPV4:
329 zlog_debug(
330 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
331 inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf,
332 sizeof(buf)),
333 znh->type, znh->ifindex, znh->vrf_id,
334 znh->label_num);
335 break;
336 case NEXTHOP_TYPE_IPV6_IFINDEX:
337 case NEXTHOP_TYPE_IPV6:
338 zlog_debug(
339 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
340 inet_ntop(AF_INET6, &znh->gate.ipv6, buf,
341 sizeof(buf)),
342 znh->type, znh->ifindex, znh->vrf_id,
343 znh->label_num);
344 break;
345 case NEXTHOP_TYPE_IFINDEX:
346 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
347 znh->type, znh->ifindex);
348 break;
349 case NEXTHOP_TYPE_BLACKHOLE:
350 zlog_debug("\tNexthop blackhole");
351 break;
352 }
353 }
354 return 0;
355 }
356
357 extern struct zebra_privs_t sharp_privs;
358
359 void sharp_zebra_init(void)
360 {
361 struct zclient_options opt = {.receive_notify = true};
362
363 if_zapi_callbacks(sharp_ifp_create, sharp_ifp_up,
364 sharp_ifp_down, sharp_ifp_destroy);
365
366 zclient = zclient_new(master, &opt);
367
368 zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs);
369 zclient->zebra_connected = zebra_connected;
370 zclient->interface_address_add = interface_address_add;
371 zclient->interface_address_delete = interface_address_delete;
372 zclient->route_notify_owner = route_notify_owner;
373 zclient->nexthop_update = sharp_nexthop_update;
374 zclient->import_check_update = sharp_nexthop_update;
375 }