summaryrefslogtreecommitdiffstats
path: root/pimd/pim_zpthread.c
blob: e6fb3b3a17274ea2d027cd061c481d4a9081806b (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
/*
 * PIM for Quagga
 * Copyright (C) 2008  Everton da Silva Marques
 *
 * 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 <lib/log.h>
#include <lib/lib_errors.h>

#include "pimd.h"
#include "pim_instance.h"
#include "pim_mlag.h"
#include "pim_zebra.h"

extern struct zclient *zclient;

#define PIM_MLAG_POST_LIMIT 100

int32_t mlag_bulk_cnt;

static void pim_mlag_zebra_fill_header(enum mlag_msg_type msg_type)
{
	uint32_t fill_msg_type = msg_type;
	uint16_t data_len;
	uint16_t msg_cnt = 1;

	if (msg_type == MLAG_MSG_NONE)
		return;

	switch (msg_type) {
	case MLAG_REGISTER:
	case MLAG_DEREGISTER:
		data_len = sizeof(struct mlag_msg);
		break;
	case MLAG_MROUTE_ADD:
		data_len = sizeof(struct mlag_mroute_add);
		fill_msg_type = MLAG_MROUTE_ADD_BULK;
		break;
	case MLAG_MROUTE_DEL:
		data_len = sizeof(struct mlag_mroute_del);
		fill_msg_type = MLAG_MROUTE_DEL_BULK;
		break;
	default:
		data_len = 0;
		break;
	}

	stream_reset(router->mlag_stream);
	/* ADD Hedaer */
	stream_putl(router->mlag_stream, fill_msg_type);
	/*
	 * In case of Bulk actual size & msg_cnt will be updated
	 * just before writing onto zebra
	 */
	stream_putw(router->mlag_stream, data_len);
	stream_putw(router->mlag_stream, msg_cnt);

	if (PIM_DEBUG_MLAG)
		zlog_debug(":%s: msg_type: %d/%d len %d",
			   __func__, msg_type, fill_msg_type, data_len);
}

static void pim_mlag_zebra_flush_buffer(void)
{
	uint32_t msg_type;

	/* Stream had bulk messages update the Hedaer */
	if (mlag_bulk_cnt > 1) {
		/*
		 * No need to reset the pointer, below api reads from data[0]
		 */
		STREAM_GETL(router->mlag_stream, msg_type);
		if (msg_type == MLAG_MROUTE_ADD_BULK) {
			stream_putw_at(
				router->mlag_stream, 4,
				(mlag_bulk_cnt * sizeof(struct mlag_mroute_add)));
			stream_putw_at(router->mlag_stream, 6, mlag_bulk_cnt);
		} else if (msg_type == MLAG_MROUTE_DEL_BULK) {
			stream_putw_at(
				router->mlag_stream, 4,
				(mlag_bulk_cnt * sizeof(struct mlag_mroute_del)));
			stream_putw_at(router->mlag_stream, 6, mlag_bulk_cnt);
		} else {
			flog_err(EC_LIB_ZAPI_ENCODE,
				"unknown bulk message type %d bulk_count %d",
				msg_type, mlag_bulk_cnt);
			stream_reset(router->mlag_stream);
			mlag_bulk_cnt = 0;
			return;
		}
	}

	zclient_send_mlag_data(zclient, router->mlag_stream);
stream_failure:
	stream_reset(router->mlag_stream);
	mlag_bulk_cnt = 0;
}

/*
 * Only ROUTE add & Delete will be bulked.
 * Buffer will be flushed, when
 * 1) there were no messages in the queue
 * 2) Curr_msg_type != prev_msg_type
 */

static void pim_mlag_zebra_check_for_buffer_flush(uint32_t curr_msg_type,
						  uint32_t prev_msg_type)
{
	/* First Message, keep bulking */
	if (prev_msg_type == MLAG_MSG_NONE) {
		mlag_bulk_cnt = 1;
		return;
	}

	/*msg type is route add & delete, keep bulking */
	if (curr_msg_type == prev_msg_type
	    && (curr_msg_type == MLAG_MROUTE_ADD
		|| curr_msg_type == MLAG_MROUTE_DEL)) {
		mlag_bulk_cnt++;
		return;
	}

	pim_mlag_zebra_flush_buffer();
}

/*
 * Thsi thread reads the clients data from the Gloabl queue and encodes with
 * protobuf and pass on to the MLAG socket.
 */
static void pim_mlag_zthread_handler(struct thread *event)
{
	struct stream *read_s;
	uint32_t wr_count = 0;
	uint32_t prev_msg_type = MLAG_MSG_NONE;
	uint32_t curr_msg_type = MLAG_MSG_NONE;

	router->zpthread_mlag_write = NULL;
	wr_count = stream_fifo_count_safe(router->mlag_fifo);

	if (PIM_DEBUG_MLAG)
		zlog_debug(":%s: Processing MLAG write, %d messages in queue",
			   __func__, wr_count);

	if (wr_count == 0)
		return;

	for (wr_count = 0; wr_count < PIM_MLAG_POST_LIMIT; wr_count++) {
		/* FIFO is empty,wait for teh message to be add */
		if (stream_fifo_count_safe(router->mlag_fifo) == 0)
			break;

		read_s = stream_fifo_pop_safe(router->mlag_fifo);
		if (!read_s) {
			zlog_debug(":%s: Got a NULL Messages, some thing wrong",
				   __func__);
			break;
		}
		STREAM_GETL(read_s, curr_msg_type);
		/*
		 * Check for Buffer Overflow,
		 * MLAG Can't process more than 'PIM_MLAG_BUF_LIMIT' bytes
		 */
		if (router->mlag_stream->endp + read_s->endp + ZEBRA_HEADER_SIZE
		    > MLAG_BUF_LIMIT)
			pim_mlag_zebra_flush_buffer();

		pim_mlag_zebra_check_for_buffer_flush(curr_msg_type,
						      prev_msg_type);

		/*
		 * First message to Buffer, fill the Header
		 */
		if (router->mlag_stream->endp == 0)
			pim_mlag_zebra_fill_header(curr_msg_type);

		/*
		 * add the data now
		 */
		stream_put(router->mlag_stream, read_s->data + read_s->getp,
			   read_s->endp - read_s->getp);

		stream_free(read_s);
		prev_msg_type = curr_msg_type;
	}

stream_failure:
	/*
	 * we are here , because
	 * 1. Queue might be empty
	 * 2. we crossed the max Q Read limit
	 * In any acse flush the buffer towards zebra
	 */
	pim_mlag_zebra_flush_buffer();

	if (wr_count >= PIM_MLAG_POST_LIMIT)
		pim_mlag_signal_zpthread();
}


int pim_mlag_signal_zpthread(void)
{
	if (router->master) {
		if (PIM_DEBUG_MLAG)
			zlog_debug(":%s: Scheduling PIM MLAG write Thread",
				   __func__);
		thread_add_event(router->master, pim_mlag_zthread_handler, NULL,
				 0, &router->zpthread_mlag_write);
	}
	return (0);
}