3 * Copyright (C) Cumulus Networks, Inc.
6 * This file is part of FRR.
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
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.
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
38 #include "sharp_zebra.h"
40 /* Zebra structure to hold current status. */
41 struct zclient
*zclient
= NULL
;
43 /* For registering threads. */
44 extern struct thread_master
*master
;
46 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
48 char ifname_tmp
[INTERFACE_NAMSIZ
];
50 /* Read interface name. */
51 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
54 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
57 /* Inteface addition message from zebra. */
58 static int interface_add(int command
, struct zclient
*zclient
,
59 zebra_size_t length
, vrf_id_t vrf_id
)
61 struct interface
*ifp
;
63 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
71 static int interface_delete(int command
, struct zclient
*zclient
,
72 zebra_size_t length
, vrf_id_t vrf_id
)
74 struct interface
*ifp
;
78 /* zebra_interface_state_read () updates interface structure in iflist
80 ifp
= zebra_interface_state_read(s
, vrf_id
);
85 if_set_index(ifp
, IFINDEX_INTERNAL
);
90 static int interface_address_add(int command
, struct zclient
*zclient
,
91 zebra_size_t length
, vrf_id_t vrf_id
)
94 zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
99 static int interface_address_delete(int command
, struct zclient
*zclient
,
100 zebra_size_t length
, vrf_id_t vrf_id
)
104 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
113 static int interface_state_up(int command
, struct zclient
*zclient
,
114 zebra_size_t length
, vrf_id_t vrf_id
)
117 zebra_interface_if_lookup(zclient
->ibuf
);
122 static int interface_state_down(int command
, struct zclient
*zclient
,
123 zebra_size_t length
, vrf_id_t vrf_id
)
126 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
131 extern uint32_t total_routes
;
132 extern uint32_t installed_routes
;
133 extern uint32_t removed_routes
;
135 static int route_notify_owner(int command
, struct zclient
*zclient
,
136 zebra_size_t length
, vrf_id_t vrf_id
)
139 enum zapi_route_notify_owner note
;
142 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
146 case ZAPI_ROUTE_INSTALLED
:
148 if (total_routes
== installed_routes
)
149 zlog_debug("Installed All Items");
151 case ZAPI_ROUTE_FAIL_INSTALL
:
152 zlog_debug("Failed install of route");
154 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
155 zlog_debug("Better Admin Distance won over us");
157 case ZAPI_ROUTE_REMOVED
:
159 if (total_routes
== removed_routes
)
160 zlog_debug("Removed all Items");
162 case ZAPI_ROUTE_REMOVE_FAIL
:
163 zlog_debug("Route removal Failure");
169 static void zebra_connected(struct zclient
*zclient
)
171 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
174 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
176 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
179 void route_add(struct prefix
*p
, uint8_t instance
, struct nexthop
*nh
)
181 struct zapi_route api
;
182 struct zapi_nexthop
*api_nh
;
184 memset(&api
, 0, sizeof(api
));
185 api
.vrf_id
= VRF_DEFAULT
;
186 api
.type
= ZEBRA_ROUTE_SHARP
;
187 api
.instance
= instance
;
188 api
.safi
= SAFI_UNICAST
;
189 memcpy(&api
.prefix
, p
, sizeof(*p
));
191 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
192 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
194 api_nh
= &api
.nexthops
[0];
195 api_nh
->vrf_id
= VRF_DEFAULT
;
196 api_nh
->gate
= nh
->gate
;
197 api_nh
->type
= nh
->type
;
198 api_nh
->ifindex
= nh
->ifindex
;
201 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
204 void route_delete(struct prefix
*p
, uint8_t instance
)
206 struct zapi_route api
;
208 memset(&api
, 0, sizeof(api
));
209 api
.vrf_id
= VRF_DEFAULT
;
210 api
.type
= ZEBRA_ROUTE_SHARP
;
211 api
.safi
= SAFI_UNICAST
;
212 api
.instance
= instance
;
213 memcpy(&api
.prefix
, p
, sizeof(*p
));
214 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
219 void sharp_zebra_nexthop_watch(struct prefix
*p
, bool watch
)
221 int command
= ZEBRA_NEXTHOP_REGISTER
;
224 command
= ZEBRA_NEXTHOP_UNREGISTER
;
226 zclient_send_rnh(zclient
, command
, p
, true, VRF_DEFAULT
);
229 static int sharp_nexthop_update(int command
, struct zclient
*zclient
,
230 zebra_size_t length
, vrf_id_t vrf_id
)
232 struct zapi_route nhr
;
233 char buf
[PREFIX_STRLEN
];
236 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
237 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
242 zlog_debug("Received update for %s",
243 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
244 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
245 struct zapi_nexthop
*znh
= &nhr
.nexthops
[i
];
248 case NEXTHOP_TYPE_IPV4_IFINDEX
:
249 case NEXTHOP_TYPE_IPV4
:
251 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
252 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
254 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
257 case NEXTHOP_TYPE_IPV6_IFINDEX
:
258 case NEXTHOP_TYPE_IPV6
:
260 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
261 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
263 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
266 case NEXTHOP_TYPE_IFINDEX
:
267 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
268 znh
->type
, znh
->ifindex
);
270 case NEXTHOP_TYPE_BLACKHOLE
:
271 zlog_debug("\tNexthop blackhole");
278 extern struct zebra_privs_t sharp_privs
;
280 void sharp_zebra_init(void)
282 struct zclient_options opt
= {.receive_notify
= true};
284 zclient
= zclient_new(master
, &opt
);
286 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
287 zclient
->zebra_connected
= zebra_connected
;
288 zclient
->interface_add
= interface_add
;
289 zclient
->interface_delete
= interface_delete
;
290 zclient
->interface_up
= interface_state_up
;
291 zclient
->interface_down
= interface_state_down
;
292 zclient
->interface_address_add
= interface_address_add
;
293 zclient
->interface_address_delete
= interface_address_delete
;
294 zclient
->route_notify_owner
= route_notify_owner
;
295 zclient
->nexthop_update
= sharp_nexthop_update
;