]>
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 | */ | |
d62a17ae | 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); | |
b80f3b24 AS |
63 | |
64 | /* | |
65 | * zfpm_dt_find_route | |
66 | * | |
67 | * Selects a suitable rib destination for fpm interface tests. | |
68 | */ | |
d62a17ae | 69 | static int zfpm_dt_find_route(rib_dest_t **dest_p, struct route_entry **re_p) |
b80f3b24 | 70 | { |
d62a17ae | 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 | ||
97cd9bfc | 93 | if (nexthop_group_active_nexthop_num(&(re->nhe->nhg)) == 0) |
d62a17ae | 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; | |
b80f3b24 AS |
107 | } |
108 | #ifdef HAVE_NETLINK | |
109 | ||
110 | /* | |
111 | * zfpm_dt_benchmark_netlink_encode | |
112 | */ | |
d62a17ae | 113 | int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv) |
b80f3b24 | 114 | { |
d62a17ae | 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; | |
b80f3b24 AS |
137 | } |
138 | ||
139 | #endif /* HAVE_NETLINK */ | |
140 | ||
141 | #ifdef HAVE_PROTOBUF | |
142 | ||
143 | /* | |
144 | * zfpm_dt_benchmark_protobuf_encode | |
145 | */ | |
d62a17ae | 146 | int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv) |
b80f3b24 | 147 | { |
d62a17ae | 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; | |
b80f3b24 AS |
169 | } |
170 | ||
171 | /* | |
172 | * zfpm_dt_log_fpm_message | |
173 | */ | |
d62a17ae | 174 | static void zfpm_dt_log_fpm_message(Fpm__Message *msg) |
b80f3b24 | 175 | { |
d62a17ae | 176 | Fpm__AddRoute *add_route; |
177 | Fpm__Nexthop *nexthop; | |
178 | struct prefix prefix; | |
d7c0a89a | 179 | uint8_t family, nh_family; |
d62a17ae | 180 | uint if_index; |
181 | char *if_name; | |
182 | size_t i; | |
183 | char buf[INET6_ADDRSTRLEN]; | |
9bcef951 | 184 | char addr_buf[PREFIX_STRLEN]; |
d62a17ae | 185 | union g_addr nh_addr; |
186 | ||
187 | if (msg->type != FPM__MESSAGE__TYPE__ADD_ROUTE) | |
188 | return; | |
189 | ||
190 | zfpm_debug("Add route message"); | |
191 | add_route = msg->add_route; | |
192 | ||
193 | if (!qpb_address_family_get(add_route->address_family, &family)) | |
194 | return; | |
195 | ||
196 | if (!qpb_l3_prefix_get(add_route->key->prefix, family, &prefix)) | |
197 | return; | |
198 | ||
199 | zfpm_debug("Vrf id: %d, Prefix: %s/%d, Metric: %d", add_route->vrf_id, | |
200 | inet_ntop(family, &prefix.u.prefix, buf, sizeof(buf)), | |
201 | prefix.prefixlen, add_route->metric); | |
202 | ||
203 | /* | |
204 | * Go over nexthops. | |
205 | */ | |
206 | for (i = 0; i < add_route->n_nexthops; i++) { | |
207 | nexthop = add_route->nexthops[i]; | |
208 | if (!qpb_if_identifier_get(nexthop->if_id, &if_index, &if_name)) | |
209 | continue; | |
210 | ||
211 | if (nexthop->address) | |
212 | qpb_l3_address_get(nexthop->address, &nh_family, | |
213 | &nh_addr); | |
214 | ||
215 | zfpm_debug("Nexthop - if_index: %d (%s), gateway: %s, ", | |
216 | if_index, if_name ? if_name : "name not specified", | |
9bcef951 MS |
217 | nexthop->address ? |
218 | inet_ntop(AF_INET, &nh_addr.ipv4, | |
219 | addr_buf, sizeof(addr_buf)) : "None"); | |
d62a17ae | 220 | } |
b80f3b24 AS |
221 | } |
222 | ||
223 | /* | |
224 | * zfpm_dt_benchmark_protobuf_decode | |
225 | */ | |
d62a17ae | 226 | int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv) |
b80f3b24 | 227 | { |
d62a17ae | 228 | int times, i, len; |
229 | rib_dest_t *dest; | |
230 | struct route_entry *re; | |
231 | uint8_t msg_buf[4096]; | |
232 | QPB_DECLARE_STACK_ALLOCATOR(allocator, 8192); | |
233 | Fpm__Message *fpm_msg; | |
234 | ||
235 | QPB_INIT_STACK_ALLOCATOR(allocator); | |
236 | ||
237 | times = 100000; | |
238 | if (argc > 0) | |
239 | times = atoi(argv[0]); | |
240 | ||
241 | if (!zfpm_dt_find_route(&dest, &re)) | |
242 | return 1; | |
243 | ||
244 | /* | |
245 | * Encode the route into the message buffer once only. | |
246 | */ | |
247 | len = zfpm_protobuf_encode_route(dest, re, msg_buf, sizeof(msg_buf)); | |
248 | if (len <= 0) | |
249 | return 2; | |
250 | ||
251 | // Decode once, and display the decoded message | |
252 | fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); | |
253 | ||
254 | if (fpm_msg) { | |
255 | zfpm_dt_log_fpm_message(fpm_msg); | |
256 | QPB_RESET_STACK_ALLOCATOR(allocator); | |
257 | } | |
258 | ||
259 | /* | |
260 | * Decode encoded message the specified number of times. | |
261 | */ | |
262 | for (i = 0; i < times; i++) { | |
263 | fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); | |
264 | ||
265 | if (!fpm_msg) | |
266 | return 3; | |
267 | ||
268 | // fpm__message__free_unpacked(msg, NULL); | |
269 | QPB_RESET_STACK_ALLOCATOR(allocator); | |
270 | } | |
271 | return 0; | |
b80f3b24 AS |
272 | } |
273 | ||
274 | #endif /* HAVE_PROTOBUF */ |