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