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