]> git.proxmox.com Git - mirror_frr.git/blame - vrrpd/vrrp_zebra.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[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
3c3c3252 92int vrrp_ifp_destroy(struct interface *ifp)
b6029d6a 93{
3c3c3252 94 vrrp_zebra_debug_if_state(ifp, ifp->vrf_id, __func__);
27fd8827 95
dca8cfcc 96 vrrp_if_del(ifp);
b6029d6a
QY
97
98 return 0;
99}
100
ddbf3e60 101int vrrp_ifp_up(struct interface *ifp)
b6029d6a 102{
ddbf3e60 103 vrrp_zebra_debug_if_state(ifp, ifp->vrf_id, __func__);
27fd8827 104
dca8cfcc 105 vrrp_if_up(ifp);
b6029d6a
QY
106
107 return 0;
108}
109
b0b69e59 110int vrrp_ifp_down(struct interface *ifp)
b6029d6a 111{
b0b69e59 112 vrrp_zebra_debug_if_state(ifp, ifp->vrf_id, __func__);
27fd8827 113
dca8cfcc 114 vrrp_if_down(ifp);
b6029d6a
QY
115
116 return 0;
117}
118
b6029d6a
QY
119static int vrrp_zebra_if_address_add(int command, struct zclient *zclient,
120 zebra_size_t length, vrf_id_t vrf_id)
121{
122 struct connected *c;
123
124 /*
125 * zebra api notifies address adds/dels events by using the same call
126 * interface_add_read below, see comments in lib/zclient.c
127 *
128 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
129 * will add address to interface list by calling
130 * connected_add_by_prefix()
131 */
132 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
dca8cfcc 133
b6029d6a
QY
134 if (!c)
135 return 0;
136
dca8cfcc
QY
137 vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
138 vrrp_zebra_debug_if_dump_address(c->ifp, __func__);
b6029d6a 139
dca8cfcc 140 vrrp_if_address_add(c->ifp);
b6029d6a
QY
141
142 return 0;
143}
144
145static int vrrp_zebra_if_address_del(int command, struct zclient *client,
146 zebra_size_t length, vrf_id_t vrf_id)
147{
148 struct connected *c;
149
150 /*
151 * zebra api notifies address adds/dels events by using the same call
152 * interface_add_read below, see comments in lib/zclient.c
153 *
154 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
155 * will remove address from interface list by calling
156 * connected_delete_by_prefix()
157 */
158 c = zebra_interface_address_read(command, client->ibuf, vrf_id);
dca8cfcc 159
b6029d6a
QY
160 if (!c)
161 return 0;
162
dca8cfcc
QY
163 vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
164 vrrp_zebra_debug_if_dump_address(c->ifp, __func__);
165
6e93585e 166 vrrp_if_address_del(c->ifp);
27fd8827 167
b6029d6a
QY
168 return 0;
169}
5435a2bf 170
f3fe0047
QY
171void vrrp_zebra_radv_set(struct vrrp_router *r, bool enable)
172{
dca8cfcc
QY
173 DEBUGD(&vrrp_dbg_zebra,
174 VRRP_LOGPFX VRRP_LOGPFX_VRID
175 "Requesting Zebra to turn router advertisements %s for %s",
176 r->vr->vrid, enable ? "on" : "off", r->mvl_ifp->name);
177
490810d5 178 zclient_send_interface_radv_req(zclient, r->mvl_ifp->vrf_id, r->mvl_ifp,
f3fe0047
QY
179 enable, VRRP_RADV_INT);
180}
181
c3bd894e
QY
182int vrrp_zclient_send_interface_protodown(struct interface *ifp, bool down)
183{
dca8cfcc
QY
184 DEBUGD(&vrrp_dbg_zebra,
185 VRRP_LOGPFX "Requesting Zebra to set %s protodown %s", ifp->name,
186 down ? "on" : "off");
187
490810d5 188 return zclient_send_interface_protodown(zclient, ifp->vrf_id, ifp,
c3bd894e
QY
189 down);
190}
191
5435a2bf
QY
192void vrrp_zebra_init(void)
193{
138c5a74
DS
194 if_zapi_callbacks(vrrp_ifp_create, vrrp_ifp_up,
195 vrrp_ifp_down, vrrp_ifp_destroy);
196
b6029d6a
QY
197 /* Socket for receiving updates from Zebra daemon */
198 zclient = zclient_new(master, &zclient_options_default);
5435a2bf 199
b6029d6a
QY
200 zclient->zebra_connected = vrrp_zebra_connected;
201 zclient->router_id_update = vrrp_router_id_update_zebra;
b6029d6a
QY
202 zclient->interface_address_add = vrrp_zebra_if_address_add;
203 zclient->interface_address_delete = vrrp_zebra_if_address_del;
5435a2bf 204
4f576e75 205 zclient_init(zclient, ZEBRA_ROUTE_VRRP, 0, &vrrp_privs);
5435a2bf 206
b6029d6a
QY
207 zlog_notice("%s: zclient socket initialized", __PRETTY_FUNCTION__);
208}