]>
Commit | Line | Data |
---|---|---|
b80f3b24 AS |
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 | * | |
896014f4 DL |
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 | |
b80f3b24 AS |
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" | |
b5a8526b DL |
44 | #include "zebra/zserv.h" |
45 | #include "zebra/zebra_vrf.h" | |
b80f3b24 AS |
46 | |
47 | #include "zebra_fpm_private.h" | |
48 | ||
49 | #include "qpb/qpb_allocator.h" | |
50 | #include "qpb/linear_allocator.h" | |
51 | ||
358336ef | 52 | #ifdef HAVE_PROTOBUF |
b80f3b24 AS |
53 | #include "qpb/qpb.h" |
54 | #include "fpm/fpm.pb-c.h" | |
358336ef | 55 | #endif |
b80f3b24 AS |
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 | |
f0f77c9a | 70 | zfpm_dt_find_route (rib_dest_t **dest_p, struct route_entry **re_p) |
b80f3b24 AS |
71 | { |
72 | struct route_node *rnode; | |
73 | route_table_iter_t iter; | |
74 | struct route_table *table; | |
75 | rib_dest_t *dest; | |
f0f77c9a | 76 | struct route_entry *re; |
b80f3b24 AS |
77 | int ret; |
78 | ||
79 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT); | |
80 | if (!table) | |
81 | return 0; | |
82 | ||
83 | route_table_iter_init(&iter, table); | |
84 | while ((rnode = route_table_iter_next(&iter))) | |
85 | { | |
86 | dest = rib_dest_from_rnode (rnode); | |
87 | ||
88 | if (!dest) | |
89 | continue; | |
90 | ||
f0f77c9a DS |
91 | re = zfpm_route_for_update(dest); |
92 | if (!re) | |
b80f3b24 AS |
93 | continue; |
94 | ||
f0f77c9a | 95 | if (re->nexthop_active_num <= 0) |
b80f3b24 AS |
96 | continue; |
97 | ||
98 | *dest_p = dest; | |
f0f77c9a | 99 | *re_p = re; |
b80f3b24 AS |
100 | ret = 1; |
101 | goto done; | |
102 | } | |
103 | ||
104 | ret = 0; | |
105 | ||
106 | done: | |
107 | route_table_iter_cleanup(&iter); | |
108 | return ret; | |
109 | } | |
110 | #ifdef HAVE_NETLINK | |
111 | ||
112 | /* | |
113 | * zfpm_dt_benchmark_netlink_encode | |
114 | */ | |
115 | int | |
116 | zfpm_dt_benchmark_netlink_encode (int argc, const char **argv) | |
117 | { | |
118 | int times, i, len; | |
119 | rib_dest_t *dest; | |
f0f77c9a | 120 | struct route_entry *re; |
b80f3b24 AS |
121 | char buf[4096]; |
122 | ||
123 | times = 100000; | |
124 | if (argc > 0) { | |
125 | times = atoi(argv[0]); | |
126 | } | |
127 | ||
f0f77c9a | 128 | if (!zfpm_dt_find_route(&dest, &re)) { |
b80f3b24 AS |
129 | return 1; |
130 | } | |
131 | ||
132 | for (i = 0; i < times; i++) { | |
f0f77c9a | 133 | len = zfpm_netlink_encode_route(RTM_NEWROUTE, dest, re, buf, sizeof(buf)); |
b80f3b24 AS |
134 | if (len <= 0) { |
135 | return 2; | |
136 | } | |
137 | } | |
138 | return 0; | |
139 | } | |
140 | ||
141 | #endif /* HAVE_NETLINK */ | |
142 | ||
143 | #ifdef HAVE_PROTOBUF | |
144 | ||
145 | /* | |
146 | * zfpm_dt_benchmark_protobuf_encode | |
147 | */ | |
148 | int | |
149 | zfpm_dt_benchmark_protobuf_encode (int argc, const char **argv) | |
150 | { | |
151 | int times, i, len; | |
152 | rib_dest_t *dest; | |
f0f77c9a | 153 | struct route_entry *re; |
b80f3b24 AS |
154 | uint8_t buf[4096]; |
155 | ||
156 | times = 100000; | |
157 | if (argc > 0) { | |
158 | times = atoi(argv[0]); | |
159 | } | |
160 | ||
f0f77c9a | 161 | if (!zfpm_dt_find_route(&dest, &re)) { |
b80f3b24 AS |
162 | return 1; |
163 | } | |
164 | ||
165 | for (i = 0; i < times; i++) { | |
f0f77c9a | 166 | len = zfpm_protobuf_encode_route(dest, re, buf, sizeof(buf)); |
b80f3b24 AS |
167 | if (len <= 0) { |
168 | return 2; | |
169 | } | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
174 | /* | |
175 | * zfpm_dt_log_fpm_message | |
176 | */ | |
177 | static void | |
178 | zfpm_dt_log_fpm_message (Fpm__Message *msg) | |
179 | { | |
180 | Fpm__AddRoute *add_route; | |
181 | Fpm__Nexthop *nexthop; | |
182 | struct prefix prefix; | |
183 | u_char family, nh_family; | |
184 | uint if_index; | |
185 | char *if_name; | |
186 | size_t i; | |
187 | char buf[INET6_ADDRSTRLEN]; | |
188 | union g_addr nh_addr; | |
189 | ||
190 | if (msg->type != FPM__MESSAGE__TYPE__ADD_ROUTE) | |
191 | return; | |
192 | ||
193 | zfpm_debug ("Add route message"); | |
194 | add_route = msg->add_route; | |
195 | ||
196 | if (!qpb_address_family_get (add_route->address_family, &family)) | |
197 | return; | |
198 | ||
199 | if (!qpb_l3_prefix_get (add_route->key->prefix, family, &prefix)) | |
200 | return; | |
201 | ||
202 | zfpm_debug ("Vrf id: %d, Prefix: %s/%d, Metric: %d", add_route->vrf_id, | |
203 | inet_ntop (family, &prefix.u.prefix, buf, sizeof (buf)), | |
204 | prefix.prefixlen, add_route->metric); | |
205 | ||
206 | /* | |
207 | * Go over nexthops. | |
208 | */ | |
209 | for (i = 0; i < add_route->n_nexthops; i++) | |
210 | { | |
211 | nexthop = add_route->nexthops[i]; | |
212 | if (!qpb_if_identifier_get (nexthop->if_id, &if_index, &if_name)) | |
213 | continue; | |
214 | ||
215 | if (nexthop->address) | |
216 | qpb_l3_address_get (nexthop->address, &nh_family, &nh_addr); | |
217 | ||
218 | zfpm_debug ("Nexthop - if_index: %d (%s), gateway: %s, ", if_index, | |
219 | if_name ? if_name : "name not specified", | |
220 | nexthop->address ? inet_ntoa (nh_addr.ipv4) : "None"); | |
221 | } | |
222 | } | |
223 | ||
224 | /* | |
225 | * zfpm_dt_benchmark_protobuf_decode | |
226 | */ | |
227 | int | |
228 | zfpm_dt_benchmark_protobuf_decode (int argc, const char **argv) | |
229 | { | |
230 | int times, i, len; | |
231 | rib_dest_t *dest; | |
f0f77c9a | 232 | struct route_entry *re; |
b80f3b24 AS |
233 | uint8_t msg_buf[4096]; |
234 | QPB_DECLARE_STACK_ALLOCATOR (allocator, 8192); | |
235 | Fpm__Message *fpm_msg; | |
236 | ||
237 | QPB_INIT_STACK_ALLOCATOR (allocator); | |
238 | ||
239 | times = 100000; | |
240 | if (argc > 0) | |
241 | times = atoi(argv[0]); | |
242 | ||
f0f77c9a | 243 | if (!zfpm_dt_find_route (&dest, &re)) |
b80f3b24 AS |
244 | return 1; |
245 | ||
246 | /* | |
247 | * Encode the route into the message buffer once only. | |
248 | */ | |
f0f77c9a | 249 | len = zfpm_protobuf_encode_route (dest, re, msg_buf, sizeof (msg_buf)); |
b80f3b24 AS |
250 | if (len <= 0) |
251 | return 2; | |
252 | ||
253 | // Decode once, and display the decoded message | |
254 | fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); | |
255 | ||
256 | if (fpm_msg) | |
257 | { | |
258 | zfpm_dt_log_fpm_message(fpm_msg); | |
259 | QPB_RESET_STACK_ALLOCATOR (allocator); | |
260 | } | |
261 | ||
262 | /* | |
263 | * Decode encoded message the specified number of times. | |
264 | */ | |
265 | for (i = 0; i < times; i++) | |
266 | { | |
267 | fpm_msg = fpm__message__unpack (&allocator, len, msg_buf); | |
268 | ||
269 | if (!fpm_msg) | |
270 | return 3; | |
271 | ||
272 | // fpm__message__free_unpacked(msg, NULL); | |
273 | QPB_RESET_STACK_ALLOCATOR (allocator); | |
274 | } | |
275 | return 0; | |
276 | } | |
277 | ||
278 | #endif /* HAVE_PROTOBUF */ |