]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
5435a2bf | 2 | /* |
63d4bd12 QY |
3 | * VRRP Zebra interfacing. |
4 | * Copyright (C) 2018-2019 Cumulus Networks, Inc. | |
5 | * Quentin Young | |
5435a2bf QY |
6 | */ |
7 | #include <zebra.h> | |
8 | ||
b6029d6a | 9 | #include "lib/if.h" |
63d4bd12 | 10 | #include "lib/linklist.h" |
b6029d6a QY |
11 | #include "lib/log.h" |
12 | #include "lib/prefix.h" | |
b6029d6a | 13 | #include "lib/vty.h" |
63d4bd12 | 14 | #include "lib/zclient.h" |
5435a2bf | 15 | |
b6029d6a | 16 | #include "vrrp.h" |
dca8cfcc | 17 | #include "vrrp_debug.h" |
5435a2bf QY |
18 | #include "vrrp_zebra.h" |
19 | ||
dca8cfcc QY |
20 | #define VRRP_LOGPFX "[ZEBRA] " |
21 | ||
2fff50ec | 22 | static struct zclient *zclient; |
b6029d6a | 23 | |
096f7609 | 24 | static void vrrp_zebra_debug_if_state(struct interface *ifp, const char *func) |
dca8cfcc QY |
25 | { |
26 | DEBUGD(&vrrp_dbg_zebra, | |
096f7609 IR |
27 | "%s: %s index %d vrf %s(%u) parent %d mac %02x:%02x:%02x:%02x:%02x:%02x flags %ld metric %d mtu %d operative %d", |
28 | func, ifp->name, ifp->ifindex, ifp->vrf->name, ifp->vrf->vrf_id, | |
29 | ifp->link_ifindex, ifp->hw_addr[0], ifp->hw_addr[1], | |
30 | ifp->hw_addr[2], ifp->hw_addr[3], ifp->hw_addr[4], | |
31 | ifp->hw_addr[5], (long)ifp->flags, ifp->metric, ifp->mtu, | |
32 | if_is_operative(ifp)); | |
dca8cfcc QY |
33 | } |
34 | ||
35 | static void vrrp_zebra_debug_if_dump_address(struct interface *ifp, | |
36 | const char *func) | |
37 | { | |
38 | struct connected *ifc; | |
39 | struct listnode *node; | |
40 | ||
41 | DEBUGD(&vrrp_dbg_zebra, "%s: interface %s addresses:", func, ifp->name); | |
42 | ||
43 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) { | |
44 | struct prefix *p = ifc->address; | |
45 | ||
26b6d034 PR |
46 | DEBUGD(&vrrp_dbg_zebra, "%s: interface %s address %pFX %s", |
47 | func, ifp->name, p, | |
dca8cfcc QY |
48 | CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ? "secondary" |
49 | : "primary"); | |
50 | } | |
51 | } | |
52 | ||
53 | ||
b6029d6a QY |
54 | static void vrrp_zebra_connected(struct zclient *zclient) |
55 | { | |
b6029d6a QY |
56 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
57 | } | |
58 | ||
59 | /* Router-id update message from zebra. */ | |
60 | static int vrrp_router_id_update_zebra(int command, struct zclient *zclient, | |
61 | zebra_size_t length, vrf_id_t vrf_id) | |
62 | { | |
63 | struct prefix router_id; | |
64 | ||
65 | zebra_router_id_update_read(zclient->ibuf, &router_id); | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
ef7bd2a3 | 70 | int vrrp_ifp_create(struct interface *ifp) |
b6029d6a | 71 | { |
096f7609 | 72 | vrrp_zebra_debug_if_state(ifp, __func__); |
dca8cfcc | 73 | |
6e93585e | 74 | vrrp_if_add(ifp); |
b6029d6a QY |
75 | |
76 | return 0; | |
77 | } | |
78 | ||
3c3c3252 | 79 | int vrrp_ifp_destroy(struct interface *ifp) |
b6029d6a | 80 | { |
096f7609 | 81 | vrrp_zebra_debug_if_state(ifp, __func__); |
27fd8827 | 82 | |
dca8cfcc | 83 | vrrp_if_del(ifp); |
b6029d6a QY |
84 | |
85 | return 0; | |
86 | } | |
87 | ||
ddbf3e60 | 88 | int vrrp_ifp_up(struct interface *ifp) |
b6029d6a | 89 | { |
096f7609 | 90 | vrrp_zebra_debug_if_state(ifp, __func__); |
27fd8827 | 91 | |
dca8cfcc | 92 | vrrp_if_up(ifp); |
b6029d6a QY |
93 | |
94 | return 0; | |
95 | } | |
96 | ||
b0b69e59 | 97 | int vrrp_ifp_down(struct interface *ifp) |
b6029d6a | 98 | { |
096f7609 | 99 | vrrp_zebra_debug_if_state(ifp, __func__); |
27fd8827 | 100 | |
dca8cfcc | 101 | vrrp_if_down(ifp); |
b6029d6a QY |
102 | |
103 | return 0; | |
104 | } | |
105 | ||
b6029d6a QY |
106 | static int vrrp_zebra_if_address_add(int command, struct zclient *zclient, |
107 | zebra_size_t length, vrf_id_t vrf_id) | |
108 | { | |
109 | struct connected *c; | |
110 | ||
111 | /* | |
112 | * zebra api notifies address adds/dels events by using the same call | |
113 | * interface_add_read below, see comments in lib/zclient.c | |
114 | * | |
115 | * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...) | |
116 | * will add address to interface list by calling | |
117 | * connected_add_by_prefix() | |
118 | */ | |
119 | c = zebra_interface_address_read(command, zclient->ibuf, vrf_id); | |
dca8cfcc | 120 | |
b6029d6a QY |
121 | if (!c) |
122 | return 0; | |
123 | ||
096f7609 | 124 | vrrp_zebra_debug_if_state(c->ifp, __func__); |
dca8cfcc | 125 | vrrp_zebra_debug_if_dump_address(c->ifp, __func__); |
b6029d6a | 126 | |
dca8cfcc | 127 | vrrp_if_address_add(c->ifp); |
b6029d6a QY |
128 | |
129 | return 0; | |
130 | } | |
131 | ||
132 | static int vrrp_zebra_if_address_del(int command, struct zclient *client, | |
133 | zebra_size_t length, vrf_id_t vrf_id) | |
134 | { | |
135 | struct connected *c; | |
136 | ||
137 | /* | |
138 | * zebra api notifies address adds/dels events by using the same call | |
139 | * interface_add_read below, see comments in lib/zclient.c | |
140 | * | |
141 | * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...) | |
142 | * will remove address from interface list by calling | |
143 | * connected_delete_by_prefix() | |
144 | */ | |
145 | c = zebra_interface_address_read(command, client->ibuf, vrf_id); | |
dca8cfcc | 146 | |
b6029d6a QY |
147 | if (!c) |
148 | return 0; | |
149 | ||
096f7609 | 150 | vrrp_zebra_debug_if_state(c->ifp, __func__); |
dca8cfcc QY |
151 | vrrp_zebra_debug_if_dump_address(c->ifp, __func__); |
152 | ||
6e93585e | 153 | vrrp_if_address_del(c->ifp); |
27fd8827 | 154 | |
b6029d6a QY |
155 | return 0; |
156 | } | |
5435a2bf | 157 | |
f3fe0047 QY |
158 | void vrrp_zebra_radv_set(struct vrrp_router *r, bool enable) |
159 | { | |
dca8cfcc QY |
160 | DEBUGD(&vrrp_dbg_zebra, |
161 | VRRP_LOGPFX VRRP_LOGPFX_VRID | |
162 | "Requesting Zebra to turn router advertisements %s for %s", | |
163 | r->vr->vrid, enable ? "on" : "off", r->mvl_ifp->name); | |
164 | ||
096f7609 IR |
165 | zclient_send_interface_radv_req(zclient, r->mvl_ifp->vrf->vrf_id, |
166 | r->mvl_ifp, enable, VRRP_RADV_INT); | |
f3fe0047 QY |
167 | } |
168 | ||
7cfdb485 | 169 | void vrrp_zclient_send_interface_protodown(struct interface *ifp, bool down) |
c3bd894e | 170 | { |
dca8cfcc QY |
171 | DEBUGD(&vrrp_dbg_zebra, |
172 | VRRP_LOGPFX "Requesting Zebra to set %s protodown %s", ifp->name, | |
173 | down ? "on" : "off"); | |
174 | ||
096f7609 | 175 | zclient_send_interface_protodown(zclient, ifp->vrf->vrf_id, ifp, down); |
c3bd894e QY |
176 | } |
177 | ||
a243d1db DL |
178 | static zclient_handler *const vrrp_handlers[] = { |
179 | [ZEBRA_ROUTER_ID_UPDATE] = vrrp_router_id_update_zebra, | |
180 | [ZEBRA_INTERFACE_ADDRESS_ADD] = vrrp_zebra_if_address_add, | |
181 | [ZEBRA_INTERFACE_ADDRESS_DELETE] = vrrp_zebra_if_address_del, | |
182 | }; | |
183 | ||
5435a2bf QY |
184 | void vrrp_zebra_init(void) |
185 | { | |
138c5a74 DS |
186 | if_zapi_callbacks(vrrp_ifp_create, vrrp_ifp_up, |
187 | vrrp_ifp_down, vrrp_ifp_destroy); | |
188 | ||
b6029d6a | 189 | /* Socket for receiving updates from Zebra daemon */ |
a243d1db DL |
190 | zclient = zclient_new(master, &zclient_options_default, vrrp_handlers, |
191 | array_size(vrrp_handlers)); | |
5435a2bf | 192 | |
b6029d6a | 193 | zclient->zebra_connected = vrrp_zebra_connected; |
5435a2bf | 194 | |
4f576e75 | 195 | zclient_init(zclient, ZEBRA_ROUTE_VRRP, 0, &vrrp_privs); |
5435a2bf | 196 | |
15569c58 | 197 | zlog_notice("%s: zclient socket initialized", __func__); |
b6029d6a | 198 | } |