2 * This is an implementation of MLAG Functionality
4 * Module name: Zebra MLAG
6 * Author: sathesh Kumar karra <sathk@cumulusnetworks.com>
8 * Copyright (C) 2019 Cumulus Networks http://www.cumulusnetworks.com
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)
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
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
29 #include "frr_pthread.h"
34 #include "lib/stream.h"
36 #include "zebra/debug.h"
37 #include "zebra/zebra_router.h"
38 #include "zebra/zebra_mlag.h"
44 * This file will have platform specific apis to communicate with MCLAG.
48 static struct thread_master
*zmlag_master
;
49 static int mlag_socket
;
51 static int zebra_mlag_connect(struct thread
*thread
);
52 static int zebra_mlag_read(struct thread
*thread
);
55 * Write the data to MLAGD
57 static int zebra_mlag_private_write_data(uint8_t *data
, uint32_t len
)
61 if (IS_ZEBRA_DEBUG_MLAG
) {
62 zlog_debug("%s: Writing %d length Data to clag", __func__
, len
);
63 zlog_hexdump(data
, len
);
65 rc
= write(mlag_socket
, data
, len
);
69 static void zebra_mlag_sched_read(void)
71 thread_add_read(zmlag_master
, zebra_mlag_read
, NULL
, mlag_socket
,
72 &zrouter
.mlag_info
.t_read
);
75 static int zebra_mlag_read(struct thread
*thread
)
79 uint32_t tot_len
, curr_len
= mlag_rd_buf_offset
;
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) | ..
86 * Idea is read one message completely, then process, until message is
87 * read completely, keep on reading from the socket
89 if (curr_len
< ZEBRA_MLAG_LEN_SIZE
) {
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",
99 zebra_mlag_handle_process_state(MLAG_DOWN
);
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();
108 curr_len
= ZEBRA_MLAG_LEN_SIZE
;
111 /* Get the actual packet length */
112 msglen
= (uint32_t *)mlag_rd_buffer
;
113 h_msglen
= ntohl(*msglen
);
115 /* This will be the actual length of the packet */
116 tot_len
= h_msglen
+ ZEBRA_MLAG_LEN_SIZE
;
118 if (curr_len
< tot_len
) {
121 data_len
= read(mlag_socket
, mlag_rd_buffer
+ curr_len
,
123 if (data_len
== 0 || data_len
== -1) {
124 if (IS_ZEBRA_DEBUG_MLAG
)
125 zlog_debug("MLAG connection closed socket : %d",
128 zebra_mlag_handle_process_state(MLAG_DOWN
);
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();
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
);
145 tot_len
-= ZEBRA_MLAG_LEN_SIZE
;
147 /* Process the packet */
148 zebra_mlag_process_mlag_data(mlag_rd_buffer
+ ZEBRA_MLAG_LEN_SIZE
,
151 /* Register read thread. */
152 zebra_mlag_reset_read_buffer();
153 zebra_mlag_sched_read();
157 static int zebra_mlag_connect(struct thread
*thread
)
159 struct sockaddr_un svr
= {0};
163 /* Reset the Timer-running flag */
164 zrouter
.mlag_info
.timer_running
= false;
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);
170 mlag_socket
= socket(svr
.sun_family
, SOCK_STREAM
, 0);
174 if (connect(mlag_socket
, (struct sockaddr
*)&svr
, sizeof(svr
)) == -1) {
175 if (IS_ZEBRA_DEBUG_MLAG
)
177 "Unable to connect to %s try again in 10 secs",
180 zrouter
.mlag_info
.timer_running
= true;
181 thread_add_timer(zmlag_master
, zebra_mlag_connect
, NULL
, 10,
182 &zrouter
.mlag_info
.t_read
);
185 len
= sizeof(struct ucred
);
186 ucred
.pid
= getpid();
188 set_nonblocking(mlag_socket
);
189 setsockopt(mlag_socket
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, len
);
191 if (IS_ZEBRA_DEBUG_MLAG
)
192 zlog_debug("%s: Connection with MLAG is established ",
195 thread_add_read(zmlag_master
, zebra_mlag_read
, NULL
, mlag_socket
,
196 &zrouter
.mlag_info
.t_read
);
198 * Connection is established with MLAGD, post to clients
200 zebra_mlag_handle_process_state(MLAG_UP
);
205 * Currently we are doing polling later we will look for better options
207 static int zebra_mlag_private_monitor_state(void)
209 thread_add_event(zmlag_master
, zebra_mlag_connect
, NULL
, 0,
210 &zrouter
.mlag_info
.t_read
);
214 static int zebra_mlag_private_open_channel(void)
216 zmlag_master
= zrouter
.mlag_info
.th_master
;
218 if (zrouter
.mlag_info
.connected
== true) {
219 if (IS_ZEBRA_DEBUG_MLAG
)
220 zlog_debug("%s: Zebra already connected to MLAGD",
225 if (zrouter
.mlag_info
.timer_running
== true) {
226 if (IS_ZEBRA_DEBUG_MLAG
)
228 "%s: Connection retry is in progress for MLAGD",
233 if (zrouter
.mlag_info
.clients_interested_cnt
) {
235 * Connect only if any clients are showing interest
237 thread_add_event(zmlag_master
, zebra_mlag_connect
, NULL
, 0,
238 &zrouter
.mlag_info
.t_read
);
243 static int zebra_mlag_private_close_channel(void)
245 if (zmlag_master
== NULL
)
248 if (zrouter
.mlag_info
.clients_interested_cnt
) {
249 if (IS_ZEBRA_DEBUG_MLAG
)
250 zlog_debug("%s: still %d clients are connected, skip",
252 zrouter
.mlag_info
.clients_interested_cnt
);
257 * Post the De-register to MLAG, so that it can do necesasry cleanup
259 zebra_mlag_send_deregister();
264 static int zebra_mlag_private_cleanup_data(void)
267 zrouter
.mlag_info
.connected
= false;
268 zrouter
.mlag_info
.timer_running
= false;
274 static int zebra_mlag_module_init(void)
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
);
290 .name
= "zebra_cumulus_mlag",
291 .version
= FRR_VERSION
,
292 .description
= "zebra Cumulus MLAG interface",
293 .init
= zebra_mlag_module_init
,