]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mlag_private.c
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"
39 #include "zebra/zebra_mlag_private.h"
45 * This file will have platform specific apis to communicate with MCLAG.
51 static struct thread_master
*zmlag_master
;
52 static int mlag_socket
;
54 static int zebra_mlag_connect(struct thread
*thread
);
55 static int zebra_mlag_read(struct thread
*thread
);
58 * Write the data to MLAGD
60 int zebra_mlag_private_write_data(uint8_t *data
, uint32_t len
)
64 if (IS_ZEBRA_DEBUG_MLAG
) {
65 zlog_debug("%s: Writing %d length Data to clag", __func__
, len
);
66 zlog_hexdump(data
, len
);
68 rc
= write(mlag_socket
, data
, len
);
72 static void zebra_mlag_sched_read(void)
74 thread_add_read(zmlag_master
, zebra_mlag_read
, NULL
, mlag_socket
,
75 &zrouter
.mlag_info
.t_read
);
78 static int zebra_mlag_read(struct thread
*thread
)
82 uint32_t tot_len
, curr_len
= mlag_rd_buf_offset
;
85 * Received message in sock_stream looks like below
86 * | len-1 (4 Bytes) | payload-1 (len-1) |
87 * len-2 (4 Bytes) | payload-2 (len-2) | ..
89 * Idea is read one message completely, then process, until message is
90 * read completely, keep on reading from the socket
92 if (curr_len
< ZEBRA_MLAG_LEN_SIZE
) {
95 data_len
= read(mlag_socket
, mlag_rd_buffer
+ curr_len
,
96 ZEBRA_MLAG_LEN_SIZE
- curr_len
);
97 if (data_len
== 0 || data_len
== -1) {
98 if (IS_ZEBRA_DEBUG_MLAG
)
99 zlog_debug("MLAG connection closed socket : %d",
102 zebra_mlag_handle_process_state(MLAG_DOWN
);
105 mlag_rd_buf_offset
+= data_len
;
106 if (data_len
!= (ssize_t
)ZEBRA_MLAG_LEN_SIZE
- curr_len
) {
107 /* Try again later */
108 zebra_mlag_sched_read();
111 curr_len
= ZEBRA_MLAG_LEN_SIZE
;
114 /* Get the actual packet length */
115 msglen
= (uint32_t *)mlag_rd_buffer
;
116 h_msglen
= ntohl(*msglen
);
118 /* This will be the actual length of the packet */
119 tot_len
= h_msglen
+ ZEBRA_MLAG_LEN_SIZE
;
121 if (curr_len
< tot_len
) {
124 data_len
= read(mlag_socket
, mlag_rd_buffer
+ curr_len
,
126 if (data_len
== 0 || data_len
== -1) {
127 if (IS_ZEBRA_DEBUG_MLAG
)
128 zlog_debug("MLAG connection closed socket : %d",
131 zebra_mlag_handle_process_state(MLAG_DOWN
);
134 mlag_rd_buf_offset
+= data_len
;
135 if (data_len
!= (ssize_t
)tot_len
- curr_len
) {
136 /* Try again later */
137 zebra_mlag_sched_read();
142 if (IS_ZEBRA_DEBUG_MLAG
) {
143 zlog_debug("Received a MLAG Message from socket: %d, len:%u ",
144 mlag_socket
, tot_len
);
145 zlog_hexdump(mlag_rd_buffer
, tot_len
);
148 tot_len
-= ZEBRA_MLAG_LEN_SIZE
;
150 /* Process the packet */
151 zebra_mlag_process_mlag_data(mlag_rd_buffer
+ ZEBRA_MLAG_LEN_SIZE
,
154 /* Register read thread. */
155 zebra_mlag_reset_read_buffer();
156 zebra_mlag_sched_read();
160 static int zebra_mlag_connect(struct thread
*thread
)
162 struct sockaddr_un svr
= {0};
166 /* Reset the Timer-running flag */
167 zrouter
.mlag_info
.timer_running
= false;
169 svr
.sun_family
= AF_UNIX
;
170 #define MLAG_SOCK_NAME "/var/run/clag-zebra.socket"
171 strlcpy(svr
.sun_path
, MLAG_SOCK_NAME
, sizeof(MLAG_SOCK_NAME
) + 1);
173 mlag_socket
= socket(svr
.sun_family
, SOCK_STREAM
, 0);
177 if (connect(mlag_socket
, (struct sockaddr
*)&svr
, sizeof(svr
)) == -1) {
178 if (IS_ZEBRA_DEBUG_MLAG
)
180 "Unable to connect to %s try again in 10 secs",
183 zrouter
.mlag_info
.timer_running
= true;
184 thread_add_timer(zmlag_master
, zebra_mlag_connect
, NULL
, 10,
185 &zrouter
.mlag_info
.t_read
);
188 len
= sizeof(struct ucred
);
189 ucred
.pid
= getpid();
191 set_nonblocking(mlag_socket
);
192 setsockopt(mlag_socket
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, len
);
194 if (IS_ZEBRA_DEBUG_MLAG
)
195 zlog_debug("%s: Connection with MLAG is established ",
198 thread_add_read(zmlag_master
, zebra_mlag_read
, NULL
, mlag_socket
,
199 &zrouter
.mlag_info
.t_read
);
201 * Connection is established with MLAGD, post to clients
203 zebra_mlag_handle_process_state(MLAG_UP
);
208 * Currently we are doing polling later we will look for better options
210 void zebra_mlag_private_monitor_state(void)
212 thread_add_event(zmlag_master
, zebra_mlag_connect
, NULL
, 0,
213 &zrouter
.mlag_info
.t_read
);
216 int zebra_mlag_private_open_channel(void)
218 zmlag_master
= zrouter
.mlag_info
.th_master
;
220 if (zrouter
.mlag_info
.connected
== true) {
221 if (IS_ZEBRA_DEBUG_MLAG
)
222 zlog_debug("%s: Zebra already connected to MLAGD",
227 if (zrouter
.mlag_info
.timer_running
== true) {
228 if (IS_ZEBRA_DEBUG_MLAG
)
230 "%s: Connection retry is in progress for MLAGD",
235 if (zrouter
.mlag_info
.clients_interested_cnt
) {
237 * Connect only if any clients are showing interest
239 thread_add_event(zmlag_master
, zebra_mlag_connect
, NULL
, 0,
240 &zrouter
.mlag_info
.t_read
);
245 int zebra_mlag_private_close_channel(void)
247 if (zmlag_master
== NULL
)
250 if (zrouter
.mlag_info
.clients_interested_cnt
) {
251 if (IS_ZEBRA_DEBUG_MLAG
)
252 zlog_debug("%s: still %d clients are connected, skip",
254 zrouter
.mlag_info
.clients_interested_cnt
);
259 * Post the De-register to MLAG, so that it can do necesasry cleanup
261 zebra_mlag_send_deregister();
266 void zebra_mlag_private_cleanup_data(void)
269 zrouter
.mlag_info
.connected
= false;
270 zrouter
.mlag_info
.timer_running
= false;
275 #else /*HAVE_CUMULUS */
277 int zebra_mlag_private_write_data(uint8_t *data
, uint32_t len
)
282 void zebra_mlag_private_monitor_state(void)
286 int zebra_mlag_private_open_channel(void)
291 int zebra_mlag_private_close_channel(void)
296 void zebra_mlag_private_cleanup_data(void)
299 #endif /*HAVE_CUMULUS*/