]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_mlag_private.c
Merge pull request #13445 from donaldsharp/lua_scripting_mem_leak
[mirror_frr.git] / zebra / zebra_mlag_private.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
ee235396
SK
2/*
3 * This is an implementation of MLAG Functionality
4 *
5 * Module name: Zebra MLAG
6 *
7 * Author: sathesh Kumar karra <sathk@cumulusnetworks.com>
8 *
9 * Copyright (C) 2019 Cumulus Networks http://www.cumulusnetworks.com
ee235396
SK
10 */
11#include "zebra.h"
12
13#include "hook.h"
14#include "module.h"
24a58196 15#include "frrevent.h"
67fa73f2 16#include "frr_pthread.h"
ee235396 17#include "libfrr.h"
09781197 18#include "lib/version.h"
ee235396
SK
19#include "network.h"
20
21#include "lib/stream.h"
22
23#include "zebra/debug.h"
24#include "zebra/zebra_router.h"
25#include "zebra/zebra_mlag.h"
ee235396
SK
26
27#include <sys/un.h>
28
29
30/*
31 * This file will have platform specific apis to communicate with MCLAG.
32 *
33 */
34
cd9d0537 35static struct event_loop *zmlag_master;
ee235396
SK
36static int mlag_socket;
37
e6685141
DS
38static void zebra_mlag_connect(struct event *thread);
39static void zebra_mlag_read(struct event *thread);
ee235396
SK
40
41/*
42 * Write the data to MLAGD
43 */
d621815a 44static int zebra_mlag_private_write_data(uint8_t *data, uint32_t len)
ee235396
SK
45{
46 int rc = 0;
47
48 if (IS_ZEBRA_DEBUG_MLAG) {
49 zlog_debug("%s: Writing %d length Data to clag", __func__, len);
50 zlog_hexdump(data, len);
51 }
52 rc = write(mlag_socket, data, len);
53 return rc;
54}
55
56static void zebra_mlag_sched_read(void)
57{
907a2395
DS
58 event_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
59 &zrouter.mlag_info.t_read);
ee235396
SK
60}
61
e6685141 62static void zebra_mlag_read(struct event *thread)
ee235396 63{
ca349126 64 static uint32_t mlag_rd_buf_offset;
ee235396
SK
65 uint32_t *msglen;
66 uint32_t h_msglen;
ded3e3e3 67 uint32_t tot_len, curr_len = mlag_rd_buf_offset;
ee235396 68
ee235396
SK
69 /*
70 * Received message in sock_stream looks like below
71 * | len-1 (4 Bytes) | payload-1 (len-1) |
72 * len-2 (4 Bytes) | payload-2 (len-2) | ..
73 *
74 * Idea is read one message completely, then process, until message is
75 * read completely, keep on reading from the socket
76 */
77 if (curr_len < ZEBRA_MLAG_LEN_SIZE) {
78 ssize_t data_len;
79
80 data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
81 ZEBRA_MLAG_LEN_SIZE - curr_len);
82 if (data_len == 0 || data_len == -1) {
83 if (IS_ZEBRA_DEBUG_MLAG)
84 zlog_debug("MLAG connection closed socket : %d",
85 mlag_socket);
86 close(mlag_socket);
87 zebra_mlag_handle_process_state(MLAG_DOWN);
cc9f21da 88 return;
ee235396 89 }
ded3e3e3 90 mlag_rd_buf_offset += data_len;
668b6011 91 if (data_len != (ssize_t)(ZEBRA_MLAG_LEN_SIZE - curr_len)) {
ee235396
SK
92 /* Try again later */
93 zebra_mlag_sched_read();
cc9f21da 94 return;
ee235396
SK
95 }
96 curr_len = ZEBRA_MLAG_LEN_SIZE;
97 }
98
99 /* Get the actual packet length */
100 msglen = (uint32_t *)mlag_rd_buffer;
101 h_msglen = ntohl(*msglen);
102
103 /* This will be the actual length of the packet */
104 tot_len = h_msglen + ZEBRA_MLAG_LEN_SIZE;
105
7692744f
DS
106 /*
107 * If the buffer read we are about to do is too large
108 * we are really really really not double plus good
109 *
110 * I'm not sure what to do here other than to bail
111 * We'll need to revisit this in the future.
112 */
113 assert(tot_len < ZEBRA_MLAG_BUF_LIMIT);
114
ee235396
SK
115 if (curr_len < tot_len) {
116 ssize_t data_len;
117
118 data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
119 tot_len - curr_len);
120 if (data_len == 0 || data_len == -1) {
121 if (IS_ZEBRA_DEBUG_MLAG)
122 zlog_debug("MLAG connection closed socket : %d",
123 mlag_socket);
124 close(mlag_socket);
125 zebra_mlag_handle_process_state(MLAG_DOWN);
cc9f21da 126 return;
ee235396 127 }
ded3e3e3 128 mlag_rd_buf_offset += data_len;
668b6011 129 if (data_len != (ssize_t)(tot_len - curr_len)) {
ee235396
SK
130 /* Try again later */
131 zebra_mlag_sched_read();
cc9f21da 132 return;
ee235396
SK
133 }
134 }
135
136 if (IS_ZEBRA_DEBUG_MLAG) {
137 zlog_debug("Received a MLAG Message from socket: %d, len:%u ",
138 mlag_socket, tot_len);
139 zlog_hexdump(mlag_rd_buffer, tot_len);
140 }
141
142 tot_len -= ZEBRA_MLAG_LEN_SIZE;
143
144 /* Process the packet */
145 zebra_mlag_process_mlag_data(mlag_rd_buffer + ZEBRA_MLAG_LEN_SIZE,
146 tot_len);
147
148 /* Register read thread. */
149 zebra_mlag_reset_read_buffer();
ca349126 150 mlag_rd_buf_offset = 0;
ee235396 151 zebra_mlag_sched_read();
ee235396
SK
152}
153
e6685141 154static void zebra_mlag_connect(struct event *thread)
ee235396
SK
155{
156 struct sockaddr_un svr = {0};
ee235396
SK
157
158 /* Reset the Timer-running flag */
159 zrouter.mlag_info.timer_running = false;
160
ee235396
SK
161 svr.sun_family = AF_UNIX;
162#define MLAG_SOCK_NAME "/var/run/clag-zebra.socket"
163 strlcpy(svr.sun_path, MLAG_SOCK_NAME, sizeof(MLAG_SOCK_NAME) + 1);
164
165 mlag_socket = socket(svr.sun_family, SOCK_STREAM, 0);
166 if (mlag_socket < 0)
cc9f21da 167 return;
ee235396
SK
168
169 if (connect(mlag_socket, (struct sockaddr *)&svr, sizeof(svr)) == -1) {
170 if (IS_ZEBRA_DEBUG_MLAG)
171 zlog_debug(
172 "Unable to connect to %s try again in 10 secs",
173 svr.sun_path);
174 close(mlag_socket);
175 zrouter.mlag_info.timer_running = true;
907a2395
DS
176 event_add_timer(zmlag_master, zebra_mlag_connect, NULL, 10,
177 &zrouter.mlag_info.t_read);
cc9f21da 178 return;
ee235396 179 }
ee235396
SK
180
181 set_nonblocking(mlag_socket);
ee235396
SK
182
183 if (IS_ZEBRA_DEBUG_MLAG)
184 zlog_debug("%s: Connection with MLAG is established ",
185 __func__);
186
907a2395
DS
187 event_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
188 &zrouter.mlag_info.t_read);
ee235396
SK
189 /*
190 * Connection is established with MLAGD, post to clients
191 */
192 zebra_mlag_handle_process_state(MLAG_UP);
ee235396
SK
193}
194
195/*
196 * Currently we are doing polling later we will look for better options
197 */
d621815a 198static int zebra_mlag_private_monitor_state(void)
ee235396 199{
907a2395
DS
200 event_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
201 &zrouter.mlag_info.t_read);
d621815a 202 return 0;
ee235396
SK
203}
204
d621815a 205static int zebra_mlag_private_open_channel(void)
ee235396
SK
206{
207 zmlag_master = zrouter.mlag_info.th_master;
208
209 if (zrouter.mlag_info.connected == true) {
210 if (IS_ZEBRA_DEBUG_MLAG)
211 zlog_debug("%s: Zebra already connected to MLAGD",
212 __func__);
213 return 0;
214 }
215
216 if (zrouter.mlag_info.timer_running == true) {
217 if (IS_ZEBRA_DEBUG_MLAG)
218 zlog_debug(
219 "%s: Connection retry is in progress for MLAGD",
220 __func__);
221 return 0;
222 }
223
224 if (zrouter.mlag_info.clients_interested_cnt) {
225 /*
226 * Connect only if any clients are showing interest
227 */
907a2395
DS
228 event_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
229 &zrouter.mlag_info.t_read);
ee235396
SK
230 }
231 return 0;
232}
233
d621815a 234static int zebra_mlag_private_close_channel(void)
ee235396
SK
235{
236 if (zmlag_master == NULL)
237 return -1;
238
239 if (zrouter.mlag_info.clients_interested_cnt) {
240 if (IS_ZEBRA_DEBUG_MLAG)
241 zlog_debug("%s: still %d clients are connected, skip",
242 __func__,
243 zrouter.mlag_info.clients_interested_cnt);
244 return -1;
245 }
246
247 /*
248 * Post the De-register to MLAG, so that it can do necesasry cleanup
249 */
250 zebra_mlag_send_deregister();
251
252 return 0;
253}
254
d621815a 255static int zebra_mlag_private_cleanup_data(void)
ee235396
SK
256{
257 zmlag_master = NULL;
258 zrouter.mlag_info.connected = false;
259 zrouter.mlag_info.timer_running = false;
260
261 close(mlag_socket);
ee235396
SK
262 return 0;
263}
264
d621815a 265static int zebra_mlag_module_init(void)
ee235396 266{
d621815a
DL
267 hook_register(zebra_mlag_private_write_data,
268 zebra_mlag_private_write_data);
269 hook_register(zebra_mlag_private_monitor_state,
270 zebra_mlag_private_monitor_state);
271 hook_register(zebra_mlag_private_open_channel,
272 zebra_mlag_private_open_channel);
273 hook_register(zebra_mlag_private_close_channel,
274 zebra_mlag_private_close_channel);
275 hook_register(zebra_mlag_private_cleanup_data,
276 zebra_mlag_private_cleanup_data);
ee235396
SK
277 return 0;
278}
279
d621815a
DL
280FRR_MODULE_SETUP(
281 .name = "zebra_cumulus_mlag",
282 .version = FRR_VERSION,
283 .description = "zebra Cumulus MLAG interface",
284 .init = zebra_mlag_module_init,
80413c20 285);