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