summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_fpm_dt.c
blob: e39272203032dfd77ec3735c8e414aa2ea060990 (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
/*
 * zebra_fpm_dt.c
 *
 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
 *
 * @author Avneesh Sachdev <avneesh@sproute.com>
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra 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, or (at your option) any
 * later version.
 *
 * GNU Zebra 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
 */

/*
 * Developer tests for the zebra code that interfaces with the
 * forwarding plane manager.
 *
 * The functions here are built into developer builds of zebra (when
 * DEV_BUILD is defined), and can be called via the 'invoke' cli
 * command.
 *
 * For example:
 *
 * # invoke zebra function zfpm_dt_benchmark_protobuf_encode 100000
 *
 */

#include <zebra.h>
#include "log.h"
#include "vrf.h"

#include "zebra/rib.h"
#include "zebra/zserv.h"
#include "zebra/zebra_vrf.h"

#include "zebra_fpm_private.h"

#include "qpb/qpb_allocator.h"
#include "qpb/linear_allocator.h"

#ifdef HAVE_PROTOBUF
#include "qpb/qpb.h"
#include "fpm/fpm.pb-c.h"
#endif

/*
 * Externs.
 */
extern int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv);
extern int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv);
extern int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv);

/*
 * zfpm_dt_find_route
 *
 * Selects a suitable rib destination for fpm interface tests.
 */
static int zfpm_dt_find_route(rib_dest_t **dest_p, struct route_entry **re_p)
{
	struct route_node *rnode;
	route_table_iter_t iter;
	struct route_table *table;
	rib_dest_t *dest;
	struct route_entry *re;
	int ret;

	table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
	if (!table)
		return 0;

	route_table_iter_init(&iter, table);
	while ((rnode = route_table_iter_next(&iter))) {
		dest = rib_dest_from_rnode(rnode);

		if (!dest)
			continue;

		re = zfpm_route_for_update(dest);
		if (!re)
			continue;

		if (nexthop_group_active_nexthop_num(&(re->nhe->nhg)) == 0)
			continue;

		*dest_p = dest;
		*re_p = re;
		ret = 1;
		goto done;
	}

	ret = 0;

done:
	route_table_iter_cleanup(&iter);
	return ret;
}
#ifdef HAVE_NETLINK

/*
 * zfpm_dt_benchmark_netlink_encode
 */
int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv)
{
	int times, i, len;
	rib_dest_t *dest;
	struct route_entry *re;
	char buf[4096];

	times = 100000;
	if (argc > 0) {
		times = atoi(argv[0]);
	}

	if (!zfpm_dt_find_route(&dest, &re)) {
		return 1;
	}

	for (i = 0; i < times; i++) {
		len = zfpm_netlink_encode_route(RTM_NEWROUTE, dest, re, buf,
						sizeof(buf));
		if (len <= 0) {
			return 2;
		}
	}
	return 0;
}

#endif /* HAVE_NETLINK */

#ifdef HAVE_PROTOBUF

/*
 * zfpm_dt_benchmark_protobuf_encode
 */
int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv)
{
	int times, i, len;
	rib_dest_t *dest;
	struct route_entry *re;
	uint8_t buf[4096];

	times = 100000;
	if (argc > 0) {
		times = atoi(argv[0]);
	}

	if (!zfpm_dt_find_route(&dest, &re)) {
		return 1;
	}

	for (i = 0; i < times; i++) {
		len = zfpm_protobuf_encode_route(dest, re, buf, sizeof(buf));
		if (len <= 0) {
			return 2;
		}
	}
	return 0;
}

/*
 * zfpm_dt_log_fpm_message
 */
static void zfpm_dt_log_fpm_message(Fpm__Message *msg)
{
	Fpm__AddRoute *add_route;
	Fpm__Nexthop *nexthop;
	struct prefix prefix;
	uint8_t family, nh_family;
	uint if_index;
	char *if_name;
	size_t i;
	char buf[INET6_ADDRSTRLEN];
	char addr_buf[PREFIX_STRLEN];
	union g_addr nh_addr;

	if (msg->type != FPM__MESSAGE__TYPE__ADD_ROUTE)
		return;

	zfpm_debug("Add route message");
	add_route = msg->add_route;

	if (!qpb_address_family_get(add_route->address_family, &family))
		return;

	if (!qpb_l3_prefix_get(add_route->key->prefix, family, &prefix))
		return;

	zfpm_debug("Vrf id: %d, Prefix: %s/%d, Metric: %d", add_route->vrf_id,
		   inet_ntop(family, &prefix.u.prefix, buf, sizeof(buf)),
		   prefix.prefixlen, add_route->metric);

	/*
	 * Go over nexthops.
	 */
	for (i = 0; i < add_route->n_nexthops; i++) {
		nexthop = add_route->nexthops[i];
		if (!qpb_if_identifier_get(nexthop->if_id, &if_index, &if_name))
			continue;

		if (nexthop->address)
			qpb_l3_address_get(nexthop->address, &nh_family,
					   &nh_addr);

		zfpm_debug("Nexthop - if_index: %d (%s), gateway: %s, ",
			   if_index, if_name ? if_name : "name not specified",
			   nexthop->address ?
			   inet_ntop(AF_INET, &nh_addr.ipv4,
				     addr_buf, sizeof(addr_buf)) : "None");
	}
}

/*
 * zfpm_dt_benchmark_protobuf_decode
 */
int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv)
{
	int times, i, len;
	rib_dest_t *dest;
	struct route_entry *re;
	uint8_t msg_buf[4096];
	QPB_DECLARE_STACK_ALLOCATOR(allocator, 8192);
	Fpm__Message *fpm_msg;

	QPB_INIT_STACK_ALLOCATOR(allocator);

	times = 100000;
	if (argc > 0)
		times = atoi(argv[0]);

	if (!zfpm_dt_find_route(&dest, &re))
		return 1;

	/*
	 * Encode the route into the message buffer once only.
	 */
	len = zfpm_protobuf_encode_route(dest, re, msg_buf, sizeof(msg_buf));
	if (len <= 0)
		return 2;

	// Decode once, and display the decoded message
	fpm_msg = fpm__message__unpack(&allocator, len, msg_buf);

	if (fpm_msg) {
		zfpm_dt_log_fpm_message(fpm_msg);
		QPB_RESET_STACK_ALLOCATOR(allocator);
	}

	/*
	 * Decode encoded message the specified number of times.
	 */
	for (i = 0; i < times; i++) {
		fpm_msg = fpm__message__unpack(&allocator, len, msg_buf);

		if (!fpm_msg)
			return 3;

		// fpm__message__free_unpacked(msg, NULL);
		QPB_RESET_STACK_ALLOCATOR(allocator);
	}
	return 0;
}

#endif /* HAVE_PROTOBUF */