]> git.proxmox.com Git - mirror_frr.git/blame - vrrpd/vrrp_zebra.c
*: Switch all zclient->interface_add to interface create callback
[mirror_frr.git] / vrrpd / vrrp_zebra.c
CommitLineData
5435a2bf 1/*
63d4bd12
QY
2 * VRRP Zebra interfacing.
3 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
4 * Quentin Young
5435a2bf
QY
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include <zebra.h>
21
b6029d6a 22#include "lib/if.h"
63d4bd12 23#include "lib/linklist.h"
b6029d6a
QY
24#include "lib/log.h"
25#include "lib/prefix.h"
b6029d6a 26#include "lib/vty.h"
63d4bd12 27#include "lib/zclient.h"
5435a2bf 28
b6029d6a 29#include "vrrp.h"
dca8cfcc 30#include "vrrp_debug.h"
5435a2bf
QY
31#include "vrrp_zebra.h"
32
dca8cfcc
QY
33#define VRRP_LOGPFX "[ZEBRA] "
34
2fff50ec 35static struct zclient *zclient;
b6029d6a 36
dca8cfcc
QY
37static void vrrp_zebra_debug_if_state(struct interface *ifp, vrf_id_t vrf_id,
38 const char *func)
39{
40 DEBUGD(&vrrp_dbg_zebra,
76bcde9e
QY
41 "%s: %s index %d(%u) parent %d mac %02x:%02x:%02x:%02x:%02x:%02x flags %ld metric %d mtu %d operative %d",
42 func, ifp->name, vrf_id, ifp->link_ifindex, ifp->ifindex,
43 ifp->hw_addr[0], ifp->hw_addr[1], ifp->hw_addr[2],
44 ifp->hw_addr[3], ifp->hw_addr[4], ifp->hw_addr[5],
45 (long)ifp->flags, ifp->metric, ifp->mtu, if_is_operative(ifp));
dca8cfcc
QY
46}
47
48static void vrrp_zebra_debug_if_dump_address(struct interface *ifp,
49 const char *func)
50{
51 struct connected *ifc;
52 struct listnode *node;
53
54 DEBUGD(&vrrp_dbg_zebra, "%s: interface %s addresses:", func, ifp->name);
55
56 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
57 struct prefix *p = ifc->address;
58
dca8cfcc
QY
59 DEBUGD(&vrrp_dbg_zebra, "%s: interface %s address %s %s", func,
60 ifp->name, inet_ntoa(p->u.prefix4),
61 CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ? "secondary"
62 : "primary");
63 }
64}
65
66
b6029d6a
QY
67static void vrrp_zebra_connected(struct zclient *zclient)
68{
b6029d6a
QY
69 zclient_send_reg_requests(zclient, VRF_DEFAULT);
70}
71
72/* Router-id update message from zebra. */
73static int vrrp_router_id_update_zebra(int command, struct zclient *zclient,
74 zebra_size_t length, vrf_id_t vrf_id)
75{
76 struct prefix router_id;
77
78 zebra_router_id_update_read(zclient->ibuf, &router_id);
79
80 return 0;
81}
82
ef7bd2a3 83int vrrp_ifp_create(struct interface *ifp)
b6029d6a 84{
ef7bd2a3 85 vrrp_zebra_debug_if_state(ifp, ifp->vrf_id, __func__);
dca8cfcc 86
6e93585e 87 vrrp_if_add(ifp);
b6029d6a
QY
88
89 return 0;
90}
91
92static int vrrp_zebra_if_del(int command, struct zclient *zclient,
93 zebra_size_t length, vrf_id_t vrf_id)
94{
95 struct interface *ifp;
96
97 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
dca8cfcc 98
b6029d6a
QY
99 if (!ifp)
100 return 0;
101
dca8cfcc 102 vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);
27fd8827 103
dca8cfcc 104 vrrp_if_del(ifp);
b6029d6a 105
2d8cb4be
QY
106 if_set_index(ifp, IFINDEX_INTERNAL);
107
b6029d6a
QY
108 return 0;
109}
110
111static int vrrp_zebra_if_state_up(int command, struct zclient *zclient,
112 zebra_size_t length, vrf_id_t vrf_id)
113{
114 struct interface *ifp;
115
116 /*
117 * zebra api notifies interface up/down events by using the same call
118 * zebra_interface_state_read below, see comments in lib/zclient.c ifp =
119 * zebra_interface_state_read(zclient->ibuf, vrf_id);
120 */
121 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
dca8cfcc 122
b6029d6a
QY
123 if (!ifp)
124 return 0;
125
dca8cfcc 126 vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);
27fd8827 127
dca8cfcc 128 vrrp_if_up(ifp);
b6029d6a
QY
129
130 return 0;
131}
132
133static int vrrp_zebra_if_state_down(int command, struct zclient *zclient,
134 zebra_size_t length, vrf_id_t vrf_id)
135{
136 struct interface *ifp;
137
138 /*
139 * zebra api notifies interface up/down events by using the same call
140 * zebra_interface_state_read below, see comments in lib/zclient.c
141 */
142 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
dca8cfcc 143
b6029d6a
QY
144 if (!ifp)
145 return 0;
146
dca8cfcc 147 vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);
27fd8827 148
dca8cfcc 149 vrrp_if_down(ifp);
b6029d6a
QY
150
151 return 0;
152}
153
b6029d6a
QY
154static int vrrp_zebra_if_address_add(int command, struct zclient *zclient,
155 zebra_size_t length, vrf_id_t vrf_id)
156{
157 struct connected *c;
158
159 /*
160 * zebra api notifies address adds/dels events by using the same call
161 * interface_add_read below, see comments in lib/zclient.c
162 *
163 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
164 * will add address to interface list by calling
165 * connected_add_by_prefix()
166 */
167 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
dca8cfcc 168
b6029d6a
QY
169 if (!c)
170 return 0;
171
dca8cfcc
QY
172 vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
173 vrrp_zebra_debug_if_dump_address(c->ifp, __func__);
b6029d6a 174
dca8cfcc 175 vrrp_if_address_add(c->ifp);
b6029d6a
QY
176
177 return 0;
178}
179
180static int vrrp_zebra_if_address_del(int command, struct zclient *client,
181 zebra_size_t length, vrf_id_t vrf_id)
182{
183 struct connected *c;
184
185 /*
186 * zebra api notifies address adds/dels events by using the same call
187 * interface_add_read below, see comments in lib/zclient.c
188 *
189 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
190 * will remove address from interface list by calling
191 * connected_delete_by_prefix()
192 */
193 c = zebra_interface_address_read(command, client->ibuf, vrf_id);
dca8cfcc 194
b6029d6a
QY
195 if (!c)
196 return 0;
197
dca8cfcc
QY
198 vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
199 vrrp_zebra_debug_if_dump_address(c->ifp, __func__);
200
6e93585e 201 vrrp_if_address_del(c->ifp);
27fd8827 202
b6029d6a
QY
203 return 0;
204}
5435a2bf 205
f3fe0047
QY
206void vrrp_zebra_radv_set(struct vrrp_router *r, bool enable)
207{
dca8cfcc
QY
208 DEBUGD(&vrrp_dbg_zebra,
209 VRRP_LOGPFX VRRP_LOGPFX_VRID
210 "Requesting Zebra to turn router advertisements %s for %s",
211 r->vr->vrid, enable ? "on" : "off", r->mvl_ifp->name);
212
f3fe0047
QY
213 zclient_send_interface_radv_req(zclient, VRF_DEFAULT, r->mvl_ifp,
214 enable, VRRP_RADV_INT);
215}
216
c3bd894e
QY
217int vrrp_zclient_send_interface_protodown(struct interface *ifp, bool down)
218{
dca8cfcc
QY
219 DEBUGD(&vrrp_dbg_zebra,
220 VRRP_LOGPFX "Requesting Zebra to set %s protodown %s", ifp->name,
221 down ? "on" : "off");
222
c3bd894e
QY
223 return zclient_send_interface_protodown(zclient, VRF_DEFAULT, ifp,
224 down);
225}
226
138c5a74
DS
227int vrrp_ifp_up(struct interface *ifp)
228{
229 return 0;
230}
231
232int vrrp_ifp_down(struct interface *ifp)
233{
234 return 0;
235}
236
237int vrrp_ifp_destroy(struct interface *ifp)
238{
239 return 0;
240}
241
5435a2bf
QY
242void vrrp_zebra_init(void)
243{
138c5a74
DS
244 if_zapi_callbacks(vrrp_ifp_create, vrrp_ifp_up,
245 vrrp_ifp_down, vrrp_ifp_destroy);
246
b6029d6a
QY
247 /* Socket for receiving updates from Zebra daemon */
248 zclient = zclient_new(master, &zclient_options_default);
5435a2bf 249
b6029d6a
QY
250 zclient->zebra_connected = vrrp_zebra_connected;
251 zclient->router_id_update = vrrp_router_id_update_zebra;
b6029d6a
QY
252 zclient->interface_delete = vrrp_zebra_if_del;
253 zclient->interface_up = vrrp_zebra_if_state_up;
254 zclient->interface_down = vrrp_zebra_if_state_down;
255 zclient->interface_address_add = vrrp_zebra_if_address_add;
256 zclient->interface_address_delete = vrrp_zebra_if_address_del;
5435a2bf 257
4f576e75 258 zclient_init(zclient, ZEBRA_ROUTE_VRRP, 0, &vrrp_privs);
5435a2bf 259
b6029d6a
QY
260 zlog_notice("%s: zclient socket initialized", __PRETTY_FUNCTION__);
261}