]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mlag_private.c
Merge pull request #5778 from ton31337/fix/add_doc_for_ebgp_connected_route_check
[mirror_frr.git] / zebra / zebra_mlag_private.c
1 /*
2 * This is an implementation of MLAG Functionality
3 *
4 * Module name: Zebra MLAG
5 *
6 * Author: sathesh Kumar karra <sathk@cumulusnetworks.com>
7 *
8 * Copyright (C) 2019 Cumulus Networks http://www.cumulusnetworks.com
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 #include "zebra.h"
25
26 #include "hook.h"
27 #include "module.h"
28 #include "thread.h"
29 #include "frr_pthread.h"
30 #include "libfrr.h"
31 #include "version.h"
32 #include "network.h"
33
34 #include "lib/stream.h"
35
36 #include "zebra/debug.h"
37 #include "zebra/zebra_router.h"
38 #include "zebra/zebra_mlag.h"
39
40 #include <sys/un.h>
41
42
43 /*
44 * This file will have platform specific apis to communicate with MCLAG.
45 *
46 */
47
48 static struct thread_master *zmlag_master;
49 static int mlag_socket;
50
51 static int zebra_mlag_connect(struct thread *thread);
52 static int zebra_mlag_read(struct thread *thread);
53
54 /*
55 * Write the data to MLAGD
56 */
57 static int zebra_mlag_private_write_data(uint8_t *data, uint32_t len)
58 {
59 int rc = 0;
60
61 if (IS_ZEBRA_DEBUG_MLAG) {
62 zlog_debug("%s: Writing %d length Data to clag", __func__, len);
63 zlog_hexdump(data, len);
64 }
65 rc = write(mlag_socket, data, len);
66 return rc;
67 }
68
69 static void zebra_mlag_sched_read(void)
70 {
71 thread_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
72 &zrouter.mlag_info.t_read);
73 }
74
75 static int zebra_mlag_read(struct thread *thread)
76 {
77 uint32_t *msglen;
78 uint32_t h_msglen;
79 uint32_t tot_len, curr_len = mlag_rd_buf_offset;
80
81 /*
82 * Received message in sock_stream looks like below
83 * | len-1 (4 Bytes) | payload-1 (len-1) |
84 * len-2 (4 Bytes) | payload-2 (len-2) | ..
85 *
86 * Idea is read one message completely, then process, until message is
87 * read completely, keep on reading from the socket
88 */
89 if (curr_len < ZEBRA_MLAG_LEN_SIZE) {
90 ssize_t data_len;
91
92 data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
93 ZEBRA_MLAG_LEN_SIZE - curr_len);
94 if (data_len == 0 || data_len == -1) {
95 if (IS_ZEBRA_DEBUG_MLAG)
96 zlog_debug("MLAG connection closed socket : %d",
97 mlag_socket);
98 close(mlag_socket);
99 zebra_mlag_handle_process_state(MLAG_DOWN);
100 return -1;
101 }
102 mlag_rd_buf_offset += data_len;
103 if (data_len != (ssize_t)(ZEBRA_MLAG_LEN_SIZE - curr_len)) {
104 /* Try again later */
105 zebra_mlag_sched_read();
106 return 0;
107 }
108 curr_len = ZEBRA_MLAG_LEN_SIZE;
109 }
110
111 /* Get the actual packet length */
112 msglen = (uint32_t *)mlag_rd_buffer;
113 h_msglen = ntohl(*msglen);
114
115 /* This will be the actual length of the packet */
116 tot_len = h_msglen + ZEBRA_MLAG_LEN_SIZE;
117
118 if (curr_len < tot_len) {
119 ssize_t data_len;
120
121 data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
122 tot_len - curr_len);
123 if (data_len == 0 || data_len == -1) {
124 if (IS_ZEBRA_DEBUG_MLAG)
125 zlog_debug("MLAG connection closed socket : %d",
126 mlag_socket);
127 close(mlag_socket);
128 zebra_mlag_handle_process_state(MLAG_DOWN);
129 return -1;
130 }
131 mlag_rd_buf_offset += data_len;
132 if (data_len != (ssize_t)(tot_len - curr_len)) {
133 /* Try again later */
134 zebra_mlag_sched_read();
135 return 0;
136 }
137 }
138
139 if (IS_ZEBRA_DEBUG_MLAG) {
140 zlog_debug("Received a MLAG Message from socket: %d, len:%u ",
141 mlag_socket, tot_len);
142 zlog_hexdump(mlag_rd_buffer, tot_len);
143 }
144
145 tot_len -= ZEBRA_MLAG_LEN_SIZE;
146
147 /* Process the packet */
148 zebra_mlag_process_mlag_data(mlag_rd_buffer + ZEBRA_MLAG_LEN_SIZE,
149 tot_len);
150
151 /* Register read thread. */
152 zebra_mlag_reset_read_buffer();
153 zebra_mlag_sched_read();
154 return 0;
155 }
156
157 static int zebra_mlag_connect(struct thread *thread)
158 {
159 struct sockaddr_un svr = {0};
160 struct ucred ucred;
161 socklen_t len = 0;
162
163 /* Reset the Timer-running flag */
164 zrouter.mlag_info.timer_running = false;
165
166 svr.sun_family = AF_UNIX;
167 #define MLAG_SOCK_NAME "/var/run/clag-zebra.socket"
168 strlcpy(svr.sun_path, MLAG_SOCK_NAME, sizeof(MLAG_SOCK_NAME) + 1);
169
170 mlag_socket = socket(svr.sun_family, SOCK_STREAM, 0);
171 if (mlag_socket < 0)
172 return -1;
173
174 if (connect(mlag_socket, (struct sockaddr *)&svr, sizeof(svr)) == -1) {
175 if (IS_ZEBRA_DEBUG_MLAG)
176 zlog_debug(
177 "Unable to connect to %s try again in 10 secs",
178 svr.sun_path);
179 close(mlag_socket);
180 zrouter.mlag_info.timer_running = true;
181 thread_add_timer(zmlag_master, zebra_mlag_connect, NULL, 10,
182 &zrouter.mlag_info.t_read);
183 return 0;
184 }
185 len = sizeof(struct ucred);
186 ucred.pid = getpid();
187
188 set_nonblocking(mlag_socket);
189 setsockopt(mlag_socket, SOL_SOCKET, SO_PEERCRED, &ucred, len);
190
191 if (IS_ZEBRA_DEBUG_MLAG)
192 zlog_debug("%s: Connection with MLAG is established ",
193 __func__);
194
195 thread_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
196 &zrouter.mlag_info.t_read);
197 /*
198 * Connection is established with MLAGD, post to clients
199 */
200 zebra_mlag_handle_process_state(MLAG_UP);
201 return 0;
202 }
203
204 /*
205 * Currently we are doing polling later we will look for better options
206 */
207 static int zebra_mlag_private_monitor_state(void)
208 {
209 thread_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
210 &zrouter.mlag_info.t_read);
211 return 0;
212 }
213
214 static int zebra_mlag_private_open_channel(void)
215 {
216 zmlag_master = zrouter.mlag_info.th_master;
217
218 if (zrouter.mlag_info.connected == true) {
219 if (IS_ZEBRA_DEBUG_MLAG)
220 zlog_debug("%s: Zebra already connected to MLAGD",
221 __func__);
222 return 0;
223 }
224
225 if (zrouter.mlag_info.timer_running == true) {
226 if (IS_ZEBRA_DEBUG_MLAG)
227 zlog_debug(
228 "%s: Connection retry is in progress for MLAGD",
229 __func__);
230 return 0;
231 }
232
233 if (zrouter.mlag_info.clients_interested_cnt) {
234 /*
235 * Connect only if any clients are showing interest
236 */
237 thread_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
238 &zrouter.mlag_info.t_read);
239 }
240 return 0;
241 }
242
243 static int zebra_mlag_private_close_channel(void)
244 {
245 if (zmlag_master == NULL)
246 return -1;
247
248 if (zrouter.mlag_info.clients_interested_cnt) {
249 if (IS_ZEBRA_DEBUG_MLAG)
250 zlog_debug("%s: still %d clients are connected, skip",
251 __func__,
252 zrouter.mlag_info.clients_interested_cnt);
253 return -1;
254 }
255
256 /*
257 * Post the De-register to MLAG, so that it can do necesasry cleanup
258 */
259 zebra_mlag_send_deregister();
260
261 return 0;
262 }
263
264 static int zebra_mlag_private_cleanup_data(void)
265 {
266 zmlag_master = NULL;
267 zrouter.mlag_info.connected = false;
268 zrouter.mlag_info.timer_running = false;
269
270 close(mlag_socket);
271 return 0;
272 }
273
274 static int zebra_mlag_module_init(void)
275 {
276 hook_register(zebra_mlag_private_write_data,
277 zebra_mlag_private_write_data);
278 hook_register(zebra_mlag_private_monitor_state,
279 zebra_mlag_private_monitor_state);
280 hook_register(zebra_mlag_private_open_channel,
281 zebra_mlag_private_open_channel);
282 hook_register(zebra_mlag_private_close_channel,
283 zebra_mlag_private_close_channel);
284 hook_register(zebra_mlag_private_cleanup_data,
285 zebra_mlag_private_cleanup_data);
286 return 0;
287 }
288
289 FRR_MODULE_SETUP(
290 .name = "zebra_cumulus_mlag",
291 .version = FRR_VERSION,
292 .description = "zebra Cumulus MLAG interface",
293 .init = zebra_mlag_module_init,
294 )