summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_mlag_private.c
blob: b1bba831d2dbf033c74e4740f8cb5cc6433e43af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * This is an implementation of MLAG Functionality
 *
 * Module name: Zebra MLAG
 *
 * Author: sathesh Kumar karra <sathk@cumulusnetworks.com>
 *
 * Copyright (C) 2019 Cumulus Networks http://www.cumulusnetworks.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "zebra.h"

#include "hook.h"
#include "module.h"
#include "thread.h"
#include "frr_pthread.h"
#include "libfrr.h"
#include "lib/version.h"
#include "network.h"

#include "lib/stream.h"

#include "zebra/debug.h"
#include "zebra/zebra_router.h"
#include "zebra/zebra_mlag.h"

#include <sys/un.h>


/*
 * This file will have platform specific apis to communicate with MCLAG.
 *
 */

static struct thread_master *zmlag_master;
static int mlag_socket;

static int zebra_mlag_connect(struct thread *thread);
static int zebra_mlag_read(struct thread *thread);

/*
 * Write the data to MLAGD
 */
static int zebra_mlag_private_write_data(uint8_t *data, uint32_t len)
{
	int rc = 0;

	if (IS_ZEBRA_DEBUG_MLAG) {
		zlog_debug("%s: Writing %d length Data to clag", __func__, len);
		zlog_hexdump(data, len);
	}
	rc = write(mlag_socket, data, len);
	return rc;
}

static void zebra_mlag_sched_read(void)
{
	thread_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
			&zrouter.mlag_info.t_read);
}

static int zebra_mlag_read(struct thread *thread)
{
	static uint32_t mlag_rd_buf_offset;
	uint32_t *msglen;
	uint32_t h_msglen;
	uint32_t tot_len, curr_len = mlag_rd_buf_offset;

	/*
	 * Received message in sock_stream looks like below
	 * | len-1 (4 Bytes) | payload-1 (len-1) |
	 *   len-2 (4 Bytes) | payload-2 (len-2) | ..
	 *
	 * Idea is read one message completely, then process, until message is
	 * read completely, keep on reading from the socket
	 */
	if (curr_len < ZEBRA_MLAG_LEN_SIZE) {
		ssize_t data_len;

		data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
				ZEBRA_MLAG_LEN_SIZE - curr_len);
		if (data_len == 0 || data_len == -1) {
			if (IS_ZEBRA_DEBUG_MLAG)
				zlog_debug("MLAG connection closed socket : %d",
					   mlag_socket);
			close(mlag_socket);
			zebra_mlag_handle_process_state(MLAG_DOWN);
			return -1;
		}
		mlag_rd_buf_offset += data_len;
		if (data_len != (ssize_t)(ZEBRA_MLAG_LEN_SIZE - curr_len)) {
			/* Try again later */
			zebra_mlag_sched_read();
			return 0;
		}
		curr_len = ZEBRA_MLAG_LEN_SIZE;
	}

	/* Get the actual packet length */
	msglen = (uint32_t *)mlag_rd_buffer;
	h_msglen = ntohl(*msglen);

	/* This will be the actual length of the packet */
	tot_len = h_msglen + ZEBRA_MLAG_LEN_SIZE;

	/*
	 * If the buffer read we are about to do is too large
	 * we are really really really not double plus good
	 *
	 * I'm not sure what to do here other than to bail
	 * We'll need to revisit this in the future.
	 */
	assert(tot_len < ZEBRA_MLAG_BUF_LIMIT);

	if (curr_len < tot_len) {
		ssize_t data_len;

		data_len = read(mlag_socket, mlag_rd_buffer + curr_len,
				tot_len - curr_len);
		if (data_len == 0 || data_len == -1) {
			if (IS_ZEBRA_DEBUG_MLAG)
				zlog_debug("MLAG connection closed socket : %d",
					   mlag_socket);
			close(mlag_socket);
			zebra_mlag_handle_process_state(MLAG_DOWN);
			return -1;
		}
		mlag_rd_buf_offset += data_len;
		if (data_len != (ssize_t)(tot_len - curr_len)) {
			/* Try again later */
			zebra_mlag_sched_read();
			return 0;
		}
	}

	if (IS_ZEBRA_DEBUG_MLAG) {
		zlog_debug("Received a MLAG Message from socket: %d, len:%u ",
			   mlag_socket, tot_len);
		zlog_hexdump(mlag_rd_buffer, tot_len);
	}

	tot_len -= ZEBRA_MLAG_LEN_SIZE;

	/* Process the packet */
	zebra_mlag_process_mlag_data(mlag_rd_buffer + ZEBRA_MLAG_LEN_SIZE,
				     tot_len);

	/* Register read thread. */
	zebra_mlag_reset_read_buffer();
	mlag_rd_buf_offset = 0;
	zebra_mlag_sched_read();
	return 0;
}

static int zebra_mlag_connect(struct thread *thread)
{
	struct sockaddr_un svr = {0};

	/* Reset the Timer-running flag */
	zrouter.mlag_info.timer_running = false;

	svr.sun_family = AF_UNIX;
#define MLAG_SOCK_NAME "/var/run/clag-zebra.socket"
	strlcpy(svr.sun_path, MLAG_SOCK_NAME, sizeof(MLAG_SOCK_NAME) + 1);

	mlag_socket = socket(svr.sun_family, SOCK_STREAM, 0);
	if (mlag_socket < 0)
		return -1;

	if (connect(mlag_socket, (struct sockaddr *)&svr, sizeof(svr)) == -1) {
		if (IS_ZEBRA_DEBUG_MLAG)
			zlog_debug(
				"Unable to connect to %s try again in 10 secs",
				svr.sun_path);
		close(mlag_socket);
		zrouter.mlag_info.timer_running = true;
		thread_add_timer(zmlag_master, zebra_mlag_connect, NULL, 10,
				 &zrouter.mlag_info.t_read);
		return 0;
	}

	set_nonblocking(mlag_socket);

	if (IS_ZEBRA_DEBUG_MLAG)
		zlog_debug("%s: Connection with MLAG is established ",
			   __func__);

	thread_add_read(zmlag_master, zebra_mlag_read, NULL, mlag_socket,
			&zrouter.mlag_info.t_read);
	/*
	 * Connection is established with MLAGD, post to clients
	 */
	zebra_mlag_handle_process_state(MLAG_UP);
	return 0;
}

/*
 * Currently we are doing polling later we will look for better options
 */
static int zebra_mlag_private_monitor_state(void)
{
	thread_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
			 &zrouter.mlag_info.t_read);
	return 0;
}

static int zebra_mlag_private_open_channel(void)
{
	zmlag_master = zrouter.mlag_info.th_master;

	if (zrouter.mlag_info.connected == true) {
		if (IS_ZEBRA_DEBUG_MLAG)
			zlog_debug("%s: Zebra already connected to MLAGD",
				   __func__);
		return 0;
	}

	if (zrouter.mlag_info.timer_running == true) {
		if (IS_ZEBRA_DEBUG_MLAG)
			zlog_debug(
				"%s: Connection retry is in progress for MLAGD",
				__func__);
		return 0;
	}

	if (zrouter.mlag_info.clients_interested_cnt) {
		/*
		 * Connect only if any clients are showing interest
		 */
		thread_add_event(zmlag_master, zebra_mlag_connect, NULL, 0,
				 &zrouter.mlag_info.t_read);
	}
	return 0;
}

static int zebra_mlag_private_close_channel(void)
{
	if (zmlag_master == NULL)
		return -1;

	if (zrouter.mlag_info.clients_interested_cnt) {
		if (IS_ZEBRA_DEBUG_MLAG)
			zlog_debug("%s: still %d clients are connected, skip",
				   __func__,
				   zrouter.mlag_info.clients_interested_cnt);
		return -1;
	}

	/*
	 * Post the De-register to MLAG, so that it can do necesasry cleanup
	 */
	zebra_mlag_send_deregister();

	return 0;
}

static int zebra_mlag_private_cleanup_data(void)
{
	zmlag_master = NULL;
	zrouter.mlag_info.connected = false;
	zrouter.mlag_info.timer_running = false;

	close(mlag_socket);
	return 0;
}

static int zebra_mlag_module_init(void)
{
	hook_register(zebra_mlag_private_write_data,
		      zebra_mlag_private_write_data);
	hook_register(zebra_mlag_private_monitor_state,
		      zebra_mlag_private_monitor_state);
	hook_register(zebra_mlag_private_open_channel,
		      zebra_mlag_private_open_channel);
	hook_register(zebra_mlag_private_close_channel,
		      zebra_mlag_private_close_channel);
	hook_register(zebra_mlag_private_cleanup_data,
		      zebra_mlag_private_cleanup_data);
	return 0;
}

FRR_MODULE_SETUP(
	.name = "zebra_cumulus_mlag",
	.version = FRR_VERSION,
	.description = "zebra Cumulus MLAG interface",
	.init = zebra_mlag_module_init,
);