]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_dt.c
Merge branch 'master' into working/master/patch-set/4-misc-fixes
[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
21 * along with GNU Zebra; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26 /*
27 * Developer tests for the zebra code that interfaces with the
28 * forwarding plane manager.
29 *
30 * The functions here are built into developer builds of zebra (when
31 * DEV_BUILD is defined), and can be called via the 'invoke' cli
32 * command.
33 *
34 * For example:
35 *
36 * # invoke zebra function zfpm_dt_benchmark_protobuf_encode 100000
37 *
38 */
39
40 #include <zebra.h>
41 #include "log.h"
42 #include "vrf.h"
43
44 #include "zebra/rib.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_vrf.h"
47
48 #include "zebra_fpm_private.h"
49
50 #include "qpb/qpb_allocator.h"
51 #include "qpb/linear_allocator.h"
52
53 #include "qpb/qpb.h"
54 #include "fpm/fpm.pb-c.h"
55
56 /*
57 * Externs.
58 */
59 extern int zfpm_dt_benchmark_netlink_encode (int argc, const char **argv);
60 extern int zfpm_dt_benchmark_protobuf_encode (int argc, const char **argv);
61 extern int zfpm_dt_benchmark_protobuf_decode (int argc, const char **argv);
62
63 /*
64 * zfpm_dt_find_route
65 *
66 * Selects a suitable rib destination for fpm interface tests.
67 */
68 static int
69 zfpm_dt_find_route (rib_dest_t **dest_p, struct rib **rib_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 rib *rib;
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 {
85 dest = rib_dest_from_rnode (rnode);
86
87 if (!dest)
88 continue;
89
90 rib = zfpm_route_for_update(dest);
91 if (!rib)
92 continue;
93
94 if (rib->nexthop_active_num <= 0)
95 continue;
96
97 *dest_p = dest;
98 *rib_p = rib;
99 ret = 1;
100 goto done;
101 }
102
103 ret = 0;
104
105 done:
106 route_table_iter_cleanup(&iter);
107 return ret;
108 }
109 #ifdef HAVE_NETLINK
110
111 /*
112 * zfpm_dt_benchmark_netlink_encode
113 */
114 int
115 zfpm_dt_benchmark_netlink_encode (int argc, const char **argv)
116 {
117 int times, i, len;
118 rib_dest_t *dest;
119 struct rib *rib;
120 char buf[4096];
121
122 times = 100000;
123 if (argc > 0) {
124 times = atoi(argv[0]);
125 }
126
127 if (!zfpm_dt_find_route(&dest, &rib)) {
128 return 1;
129 }
130
131 for (i = 0; i < times; i++) {
132 len = zfpm_netlink_encode_route(RTM_NEWROUTE, dest, rib, buf, sizeof(buf));
133 if (len <= 0) {
134 return 2;
135 }
136 }
137 return 0;
138 }
139
140 #endif /* HAVE_NETLINK */
141
142 #ifdef HAVE_PROTOBUF
143
144 /*
145 * zfpm_dt_benchmark_protobuf_encode
146 */
147 int
148 zfpm_dt_benchmark_protobuf_encode (int argc, const char **argv)
149 {
150 int times, i, len;
151 rib_dest_t *dest;
152 struct rib *rib;
153 uint8_t buf[4096];
154
155 times = 100000;
156 if (argc > 0) {
157 times = atoi(argv[0]);
158 }
159
160 if (!zfpm_dt_find_route(&dest, &rib)) {
161 return 1;
162 }
163
164 for (i = 0; i < times; i++) {
165 len = zfpm_protobuf_encode_route(dest, rib, buf, sizeof(buf));
166 if (len <= 0) {
167 return 2;
168 }
169 }
170 return 0;
171 }
172
173 /*
174 * zfpm_dt_log_fpm_message
175 */
176 static void
177 zfpm_dt_log_fpm_message (Fpm__Message *msg)
178 {
179 Fpm__AddRoute *add_route;
180 Fpm__Nexthop *nexthop;
181 struct prefix prefix;
182 u_char family, nh_family;
183 uint if_index;
184 char *if_name;
185 size_t i;
186 char buf[INET6_ADDRSTRLEN];
187 union g_addr nh_addr;
188
189 if (msg->type != FPM__MESSAGE__TYPE__ADD_ROUTE)
190 return;
191
192 zfpm_debug ("Add route message");
193 add_route = msg->add_route;
194
195 if (!qpb_address_family_get (add_route->address_family, &family))
196 return;
197
198 if (!qpb_l3_prefix_get (add_route->key->prefix, family, &prefix))
199 return;
200
201 zfpm_debug ("Vrf id: %d, Prefix: %s/%d, Metric: %d", add_route->vrf_id,
202 inet_ntop (family, &prefix.u.prefix, buf, sizeof (buf)),
203 prefix.prefixlen, add_route->metric);
204
205 /*
206 * Go over nexthops.
207 */
208 for (i = 0; i < add_route->n_nexthops; i++)
209 {
210 nexthop = add_route->nexthops[i];
211 if (!qpb_if_identifier_get (nexthop->if_id, &if_index, &if_name))
212 continue;
213
214 if (nexthop->address)
215 qpb_l3_address_get (nexthop->address, &nh_family, &nh_addr);
216
217 zfpm_debug ("Nexthop - if_index: %d (%s), gateway: %s, ", if_index,
218 if_name ? if_name : "name not specified",
219 nexthop->address ? inet_ntoa (nh_addr.ipv4) : "None");
220 }
221 }
222
223 /*
224 * zfpm_dt_benchmark_protobuf_decode
225 */
226 int
227 zfpm_dt_benchmark_protobuf_decode (int argc, const char **argv)
228 {
229 int times, i, len;
230 rib_dest_t *dest;
231 struct rib *rib;
232 uint8_t msg_buf[4096];
233 QPB_DECLARE_STACK_ALLOCATOR (allocator, 8192);
234 Fpm__Message *fpm_msg;
235
236 QPB_INIT_STACK_ALLOCATOR (allocator);
237
238 times = 100000;
239 if (argc > 0)
240 times = atoi(argv[0]);
241
242 if (!zfpm_dt_find_route (&dest, &rib))
243 return 1;
244
245 /*
246 * Encode the route into the message buffer once only.
247 */
248 len = zfpm_protobuf_encode_route (dest, rib, msg_buf, sizeof (msg_buf));
249 if (len <= 0)
250 return 2;
251
252 // Decode once, and display the decoded message
253 fpm_msg = fpm__message__unpack(&allocator, len, msg_buf);
254
255 if (fpm_msg)
256 {
257 zfpm_dt_log_fpm_message(fpm_msg);
258 QPB_RESET_STACK_ALLOCATOR (allocator);
259 }
260
261 /*
262 * Decode encoded message the specified number of times.
263 */
264 for (i = 0; i < times; i++)
265 {
266 fpm_msg = fpm__message__unpack (&allocator, len, msg_buf);
267
268 if (!fpm_msg)
269 return 3;
270
271 // fpm__message__free_unpacked(msg, NULL);
272 QPB_RESET_STACK_ALLOCATOR (allocator);
273 }
274 return 0;
275 }
276
277 #endif /* HAVE_PROTOBUF */