]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_dt.c
isisd: implement the 'lsp-too-large' notification
[mirror_frr.git] / zebra / zebra_fpm_dt.c
1 /*
2 * zebra_fpm_dt.c
3 *
4 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.com>
7 *
8 * This file is part of GNU Zebra.
9 *
10 * GNU Zebra 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
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * GNU Zebra is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
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
23 */
24
25 /*
26 * Developer tests for the zebra code that interfaces with the
27 * forwarding plane manager.
28 *
29 * The functions here are built into developer builds of zebra (when
30 * DEV_BUILD is defined), and can be called via the 'invoke' cli
31 * command.
32 *
33 * For example:
34 *
35 * # invoke zebra function zfpm_dt_benchmark_protobuf_encode 100000
36 *
37 */
38
39 #include <zebra.h>
40 #include "log.h"
41 #include "vrf.h"
42
43 #include "zebra/rib.h"
44 #include "zebra/zserv.h"
45 #include "zebra/zebra_vrf.h"
46
47 #include "zebra_fpm_private.h"
48
49 #include "qpb/qpb_allocator.h"
50 #include "qpb/linear_allocator.h"
51
52 #ifdef HAVE_PROTOBUF
53 #include "qpb/qpb.h"
54 #include "fpm/fpm.pb-c.h"
55 #endif
56
57 /*
58 * Externs.
59 */
60 extern int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv);
61 extern int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv);
62 extern int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv);
63
64 /*
65 * zfpm_dt_find_route
66 *
67 * Selects a suitable rib destination for fpm interface tests.
68 */
69 static int zfpm_dt_find_route(rib_dest_t **dest_p, struct route_entry **re_p)
70 {
71 struct route_node *rnode;
72 route_table_iter_t iter;
73 struct route_table *table;
74 rib_dest_t *dest;
75 struct route_entry *re;
76 int ret;
77
78 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
79 if (!table)
80 return 0;
81
82 route_table_iter_init(&iter, table);
83 while ((rnode = route_table_iter_next(&iter))) {
84 dest = rib_dest_from_rnode(rnode);
85
86 if (!dest)
87 continue;
88
89 re = zfpm_route_for_update(dest);
90 if (!re)
91 continue;
92
93 if (re->nexthop_active_num <= 0)
94 continue;
95
96 *dest_p = dest;
97 *re_p = re;
98 ret = 1;
99 goto done;
100 }
101
102 ret = 0;
103
104 done:
105 route_table_iter_cleanup(&iter);
106 return ret;
107 }
108 #ifdef HAVE_NETLINK
109
110 /*
111 * zfpm_dt_benchmark_netlink_encode
112 */
113 int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv)
114 {
115 int times, i, len;
116 rib_dest_t *dest;
117 struct route_entry *re;
118 char buf[4096];
119
120 times = 100000;
121 if (argc > 0) {
122 times = atoi(argv[0]);
123 }
124
125 if (!zfpm_dt_find_route(&dest, &re)) {
126 return 1;
127 }
128
129 for (i = 0; i < times; i++) {
130 len = zfpm_netlink_encode_route(RTM_NEWROUTE, dest, re, buf,
131 sizeof(buf));
132 if (len <= 0) {
133 return 2;
134 }
135 }
136 return 0;
137 }
138
139 #endif /* HAVE_NETLINK */
140
141 #ifdef HAVE_PROTOBUF
142
143 /*
144 * zfpm_dt_benchmark_protobuf_encode
145 */
146 int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv)
147 {
148 int times, i, len;
149 rib_dest_t *dest;
150 struct route_entry *re;
151 uint8_t buf[4096];
152
153 times = 100000;
154 if (argc > 0) {
155 times = atoi(argv[0]);
156 }
157
158 if (!zfpm_dt_find_route(&dest, &re)) {
159 return 1;
160 }
161
162 for (i = 0; i < times; i++) {
163 len = zfpm_protobuf_encode_route(dest, re, buf, sizeof(buf));
164 if (len <= 0) {
165 return 2;
166 }
167 }
168 return 0;
169 }
170
171 /*
172 * zfpm_dt_log_fpm_message
173 */
174 static void zfpm_dt_log_fpm_message(Fpm__Message *msg)
175 {
176 Fpm__AddRoute *add_route;
177 Fpm__Nexthop *nexthop;
178 struct prefix prefix;
179 uint8_t family, nh_family;
180 uint if_index;
181 char *if_name;
182 size_t i;
183 char buf[INET6_ADDRSTRLEN];
184 union g_addr nh_addr;
185
186 if (msg->type != FPM__MESSAGE__TYPE__ADD_ROUTE)
187 return;
188
189 zfpm_debug("Add route message");
190 add_route = msg->add_route;
191
192 if (!qpb_address_family_get(add_route->address_family, &family))
193 return;
194
195 if (!qpb_l3_prefix_get(add_route->key->prefix, family, &prefix))
196 return;
197
198 zfpm_debug("Vrf id: %d, Prefix: %s/%d, Metric: %d", add_route->vrf_id,
199 inet_ntop(family, &prefix.u.prefix, buf, sizeof(buf)),
200 prefix.prefixlen, add_route->metric);
201
202 /*
203 * Go over nexthops.
204 */
205 for (i = 0; i < add_route->n_nexthops; i++) {
206 nexthop = add_route->nexthops[i];
207 if (!qpb_if_identifier_get(nexthop->if_id, &if_index, &if_name))
208 continue;
209
210 if (nexthop->address)
211 qpb_l3_address_get(nexthop->address, &nh_family,
212 &nh_addr);
213
214 zfpm_debug("Nexthop - if_index: %d (%s), gateway: %s, ",
215 if_index, if_name ? if_name : "name not specified",
216 nexthop->address ? inet_ntoa(nh_addr.ipv4) : "None");
217 }
218 }
219
220 /*
221 * zfpm_dt_benchmark_protobuf_decode
222 */
223 int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv)
224 {
225 int times, i, len;
226 rib_dest_t *dest;
227 struct route_entry *re;
228 uint8_t msg_buf[4096];
229 QPB_DECLARE_STACK_ALLOCATOR(allocator, 8192);
230 Fpm__Message *fpm_msg;
231
232 QPB_INIT_STACK_ALLOCATOR(allocator);
233
234 times = 100000;
235 if (argc > 0)
236 times = atoi(argv[0]);
237
238 if (!zfpm_dt_find_route(&dest, &re))
239 return 1;
240
241 /*
242 * Encode the route into the message buffer once only.
243 */
244 len = zfpm_protobuf_encode_route(dest, re, msg_buf, sizeof(msg_buf));
245 if (len <= 0)
246 return 2;
247
248 // Decode once, and display the decoded message
249 fpm_msg = fpm__message__unpack(&allocator, len, msg_buf);
250
251 if (fpm_msg) {
252 zfpm_dt_log_fpm_message(fpm_msg);
253 QPB_RESET_STACK_ALLOCATOR(allocator);
254 }
255
256 /*
257 * Decode encoded message the specified number of times.
258 */
259 for (i = 0; i < times; i++) {
260 fpm_msg = fpm__message__unpack(&allocator, len, msg_buf);
261
262 if (!fpm_msg)
263 return 3;
264
265 // fpm__message__free_unpacked(msg, NULL);
266 QPB_RESET_STACK_ALLOCATOR(allocator);
267 }
268 return 0;
269 }
270
271 #endif /* HAVE_PROTOBUF */