]> git.proxmox.com Git - mirror_frr.git/blame - sharpd/sharp_zebra.c
sharpd: Allow route install/removal of v6 routes.
[mirror_frr.git] / sharpd / sharp_zebra.c
CommitLineData
8a71d93d
DS
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"
694b242f 37#include "nexthop_group.h"
8a71d93d
DS
38
39#include "sharp_zebra.h"
40
41/* Zebra structure to hold current status. */
42struct zclient *zclient = NULL;
43
44/* For registering threads. */
45extern struct thread_master *master;
46
47static struct interface *zebra_interface_if_lookup(struct stream *s)
48{
49 char ifname_tmp[INTERFACE_NAMSIZ];
50
51 /* Read interface name. */
52 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
53
54 /* And look it up. */
55 return if_lookup_by_name(ifname_tmp, VRF_DEFAULT);
56}
57
58/* Inteface addition message from zebra. */
59static int interface_add(int command, struct zclient *zclient,
996c9314 60 zebra_size_t length, vrf_id_t vrf_id)
8a71d93d
DS
61{
62 struct interface *ifp;
63
64 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
65
66 if (!ifp->info)
67 return 0;
68
69 return 0;
70}
71
72static int interface_delete(int command, struct zclient *zclient,
73 zebra_size_t length, vrf_id_t vrf_id)
74{
75 struct interface *ifp;
76 struct stream *s;
77
78 s = zclient->ibuf;
79 /* zebra_interface_state_read () updates interface structure in iflist
80 */
81 ifp = zebra_interface_state_read(s, vrf_id);
82
83 if (ifp == NULL)
84 return 0;
85
86 if_set_index(ifp, IFINDEX_INTERNAL);
87
88 return 0;
89}
90
91static int interface_address_add(int command, struct zclient *zclient,
92 zebra_size_t length, vrf_id_t vrf_id)
93{
94
95 zebra_interface_address_read(command, zclient->ibuf, vrf_id);
96
97 return 0;
98}
99
100static int interface_address_delete(int command, struct zclient *zclient,
101 zebra_size_t length, vrf_id_t vrf_id)
102{
103 struct connected *c;
104
105 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
106
107 if (!c)
108 return 0;
109
110 connected_free(c);
111 return 0;
112}
113
114static int interface_state_up(int command, struct zclient *zclient,
115 zebra_size_t length, vrf_id_t vrf_id)
116{
117
118 zebra_interface_if_lookup(zclient->ibuf);
119
120 return 0;
121}
122
123static int interface_state_down(int command, struct zclient *zclient,
124 zebra_size_t length, vrf_id_t vrf_id)
125{
126
127 zebra_interface_state_read(zclient->ibuf, vrf_id);
128
129 return 0;
130}
131
132extern uint32_t total_routes;
133extern uint32_t installed_routes;
5e54c602 134extern uint32_t removed_routes;
b939f6ff
DS
135extern int32_t repeat;
136extern struct prefix orig_prefix;
137extern struct nexthop_group nhop_group;
138extern uint8_t inst;
8a71d93d 139
6b98d34f
DS
140void sharp_install_routes_helper(struct prefix *p, uint8_t instance,
141 struct nexthop_group *nhg,
142 uint32_t routes)
143{
144 uint32_t temp, i;
dbc1bf46 145 bool v4 = false;
6b98d34f
DS
146
147 zlog_debug("Inserting %u routes", routes);
148
dbc1bf46
DS
149 if (p->family == AF_INET) {
150 v4 = true;
151 temp = ntohl(p->u.prefix4.s_addr);
152 } else
153 temp = ntohl(p->u.val32[3]);
154
6b98d34f
DS
155 for (i = 0; i < routes; i++) {
156 route_add(p, (uint8_t)instance, nhg);
dbc1bf46
DS
157 if (v4)
158 p->u.prefix4.s_addr = htonl(++temp);
159 else
160 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
161 }
162}
163
164void sharp_remove_routes_helper(struct prefix *p, uint8_t instance,
165 uint32_t routes)
166{
167 uint32_t temp, i;
dbc1bf46 168 bool v4 = false;
6b98d34f
DS
169
170 zlog_debug("Removing %u routes", routes);
171
dbc1bf46
DS
172 if (p->family == AF_INET) {
173 v4 = true;
174 temp = ntohl(p->u.prefix4.s_addr);
175 } else
176 temp = ntohl(p->u.val32[3]);
177
6b98d34f
DS
178 for (i = 0; i < routes; i++) {
179 route_delete(p, (uint8_t)instance);
dbc1bf46
DS
180 if (v4)
181 p->u.prefix4.s_addr = htonl(++temp);
182 else
183 p->u.val32[3] = htonl(++temp);
6b98d34f
DS
184 }
185}
186
b939f6ff 187static void handle_repeated(bool installed)
6b98d34f 188{
b939f6ff 189 struct prefix p = orig_prefix;
6b98d34f
DS
190 repeat--;
191
192 if (repeat <= 0)
193 return;
194
195 if (installed) {
196 removed_routes = 0;
b939f6ff 197 sharp_remove_routes_helper(&p, inst, total_routes);
6b98d34f
DS
198 }
199
200 if (!installed) {
201 installed_routes = 0;
b939f6ff
DS
202 sharp_install_routes_helper(&p, inst, &nhop_group,
203 total_routes);
6b98d34f
DS
204 }
205}
206
28b11f81
DS
207static int route_notify_owner(int command, struct zclient *zclient,
208 zebra_size_t length, vrf_id_t vrf_id)
8a71d93d
DS
209{
210 struct prefix p;
211 enum zapi_route_notify_owner note;
28610f7e 212 uint32_t table;
8a71d93d 213
28610f7e 214 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
8a71d93d
DS
215 return -1;
216
5e54c602
DS
217 switch (note) {
218 case ZAPI_ROUTE_INSTALLED:
219 installed_routes++;
6b98d34f 220 if (total_routes == installed_routes) {
5e54c602 221 zlog_debug("Installed All Items");
6b98d34f
DS
222 handle_repeated(true);
223 }
5e54c602
DS
224 break;
225 case ZAPI_ROUTE_FAIL_INSTALL:
226 zlog_debug("Failed install of route");
227 break;
228 case ZAPI_ROUTE_BETTER_ADMIN_WON:
229 zlog_debug("Better Admin Distance won over us");
230 break;
231 case ZAPI_ROUTE_REMOVED:
232 removed_routes++;
6b98d34f 233 if (total_routes == removed_routes) {
5e54c602 234 zlog_debug("Removed all Items");
6b98d34f
DS
235 handle_repeated(false);
236 }
5e54c602
DS
237 break;
238 case ZAPI_ROUTE_REMOVE_FAIL:
239 zlog_debug("Route removal Failure");
240 break;
241 }
8a71d93d
DS
242 return 0;
243}
244
245static void zebra_connected(struct zclient *zclient)
246{
247 zclient_send_reg_requests(zclient, VRF_DEFAULT);
248}
249
7d061b3c 250void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
ab18a495 251{
7d061b3c 252 zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
ab18a495
DS
253}
254
694b242f 255void route_add(struct prefix *p, uint8_t instance, struct nexthop_group *nhg)
8a71d93d
DS
256{
257 struct zapi_route api;
258 struct zapi_nexthop *api_nh;
694b242f
DS
259 struct nexthop *nh;
260 int i = 0;
8a71d93d
DS
261
262 memset(&api, 0, sizeof(api));
263 api.vrf_id = VRF_DEFAULT;
264 api.type = ZEBRA_ROUTE_SHARP;
ae252c02 265 api.instance = instance;
8a71d93d
DS
266 api.safi = SAFI_UNICAST;
267 memcpy(&api.prefix, p, sizeof(*p));
268
a3896844 269 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
8a71d93d
DS
270 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
271
694b242f
DS
272 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
273 api_nh = &api.nexthops[i];
274 api_nh->vrf_id = VRF_DEFAULT;
275 api_nh->type = nh->type;
276 switch (nh->type) {
277 case NEXTHOP_TYPE_IPV4:
278 api_nh->gate = nh->gate;
279 break;
280 case NEXTHOP_TYPE_IPV4_IFINDEX:
281 api_nh->gate = nh->gate;
282 api_nh->ifindex = nh->ifindex;
283 break;
284 case NEXTHOP_TYPE_IFINDEX:
285 api_nh->ifindex = nh->ifindex;
286 break;
287 case NEXTHOP_TYPE_IPV6:
288 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16);
289 break;
290 case NEXTHOP_TYPE_IPV6_IFINDEX:
291 api_nh->ifindex = nh->ifindex;
292 memcpy(&api_nh->gate.ipv6, &nh->gate.ipv6, 16);
293 break;
294 case NEXTHOP_TYPE_BLACKHOLE:
295 api_nh->bh_type = nh->bh_type;
296 break;
297 }
298 i++;
299 }
300 api.nexthop_num = i;
8a71d93d
DS
301
302 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
303}
304
ae252c02 305void route_delete(struct prefix *p, uint8_t instance)
8a71d93d
DS
306{
307 struct zapi_route api;
308
309 memset(&api, 0, sizeof(api));
310 api.vrf_id = VRF_DEFAULT;
311 api.type = ZEBRA_ROUTE_SHARP;
312 api.safi = SAFI_UNICAST;
ae252c02 313 api.instance = instance;
8a71d93d
DS
314 memcpy(&api.prefix, p, sizeof(*p));
315 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
316
317 return;
318}
319
0ae8130d
DS
320void sharp_zebra_nexthop_watch(struct prefix *p, bool watch)
321{
322 int command = ZEBRA_NEXTHOP_REGISTER;
323
324 if (!watch)
325 command = ZEBRA_NEXTHOP_UNREGISTER;
326
b3beaea0
A
327 if (zclient_send_rnh(zclient, command, p, true, VRF_DEFAULT) < 0)
328 zlog_warn("%s: Failure to send nexthop to zebra",
329 __PRETTY_FUNCTION__);
0ae8130d
DS
330}
331
332static int sharp_nexthop_update(int command, struct zclient *zclient,
333 zebra_size_t length, vrf_id_t vrf_id)
334{
335 struct zapi_route nhr;
336 char buf[PREFIX_STRLEN];
337 int i;
338
339 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
340 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__);
341
342 return 0;
343 }
344
345 zlog_debug("Received update for %s",
346 prefix2str(&nhr.prefix, buf, sizeof(buf)));
347 for (i = 0; i < nhr.nexthop_num; i++) {
348 struct zapi_nexthop *znh = &nhr.nexthops[i];
349
350 switch (znh->type) {
351 case NEXTHOP_TYPE_IPV4_IFINDEX:
352 case NEXTHOP_TYPE_IPV4:
353 zlog_debug(
354 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
355 inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf,
356 sizeof(buf)),
357 znh->type, znh->ifindex, znh->vrf_id,
358 znh->label_num);
359 break;
360 case NEXTHOP_TYPE_IPV6_IFINDEX:
361 case NEXTHOP_TYPE_IPV6:
362 zlog_debug(
363 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
364 inet_ntop(AF_INET6, &znh->gate.ipv6, buf,
365 sizeof(buf)),
366 znh->type, znh->ifindex, znh->vrf_id,
367 znh->label_num);
368 break;
369 case NEXTHOP_TYPE_IFINDEX:
370 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
371 znh->type, znh->ifindex);
372 break;
373 case NEXTHOP_TYPE_BLACKHOLE:
374 zlog_debug("\tNexthop blackhole");
375 break;
376 }
377 }
378 return 0;
379}
380
8a71d93d
DS
381extern struct zebra_privs_t sharp_privs;
382
383void sharp_zebra_init(void)
384{
996c9314 385 struct zclient_options opt = {.receive_notify = true};
8a71d93d 386
26f63a1e 387 zclient = zclient_new(master, &opt);
8a71d93d
DS
388
389 zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs);
390 zclient->zebra_connected = zebra_connected;
391 zclient->interface_add = interface_add;
392 zclient->interface_delete = interface_delete;
393 zclient->interface_up = interface_state_up;
394 zclient->interface_down = interface_state_down;
395 zclient->interface_address_add = interface_address_add;
396 zclient->interface_address_delete = interface_address_delete;
28b11f81 397 zclient->route_notify_owner = route_notify_owner;
0ae8130d 398 zclient->nexthop_update = sharp_nexthop_update;
8a71d93d 399}