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