]>
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 | */ | |
ac4d0be5 | 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); | |
b80f3b24 AS |
64 | |
65 | /* | |
66 | * zfpm_dt_find_route | |
67 | * | |
68 | * Selects a suitable rib destination for fpm interface tests. | |
69 | */ | |
ac4d0be5 | 70 | static int zfpm_dt_find_route(rib_dest_t **dest_p, struct rib **rib_p) |
b80f3b24 | 71 | { |
ac4d0be5 | 72 | struct route_node *rnode; |
73 | route_table_iter_t iter; | |
74 | struct route_table *table; | |
75 | rib_dest_t *dest; | |
76 | struct rib *rib; | |
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 | 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; | |
b80f3b24 AS |
108 | } |
109 | #ifdef HAVE_NETLINK | |
110 | ||
111 | /* | |
112 | * zfpm_dt_benchmark_netlink_encode | |
113 | */ | |
ac4d0be5 | 114 | int zfpm_dt_benchmark_netlink_encode(int argc, const char **argv) |
b80f3b24 | 115 | { |
ac4d0be5 | 116 | int times, i, len; |
117 | rib_dest_t *dest; | |
118 | struct rib *rib; | |
119 | char buf[4096]; | |
120 | ||
121 | times = 100000; | |
122 | if (argc > 0) { | |
123 | times = atoi(argv[0]); | |
124 | } | |
125 | ||
126 | if (!zfpm_dt_find_route(&dest, &rib)) { | |
127 | return 1; | |
128 | } | |
129 | ||
130 | for (i = 0; i < times; i++) { | |
131 | len = zfpm_netlink_encode_route(RTM_NEWROUTE, dest, rib, buf, | |
132 | sizeof(buf)); | |
133 | if (len <= 0) { | |
134 | return 2; | |
135 | } | |
136 | } | |
137 | return 0; | |
b80f3b24 AS |
138 | } |
139 | ||
140 | #endif /* HAVE_NETLINK */ | |
141 | ||
142 | #ifdef HAVE_PROTOBUF | |
143 | ||
144 | /* | |
145 | * zfpm_dt_benchmark_protobuf_encode | |
146 | */ | |
ac4d0be5 | 147 | int zfpm_dt_benchmark_protobuf_encode(int argc, const char **argv) |
b80f3b24 | 148 | { |
ac4d0be5 | 149 | int times, i, len; |
150 | rib_dest_t *dest; | |
151 | struct rib *rib; | |
152 | uint8_t buf[4096]; | |
153 | ||
154 | times = 100000; | |
155 | if (argc > 0) { | |
156 | times = atoi(argv[0]); | |
157 | } | |
158 | ||
159 | if (!zfpm_dt_find_route(&dest, &rib)) { | |
160 | return 1; | |
161 | } | |
162 | ||
163 | for (i = 0; i < times; i++) { | |
164 | len = zfpm_protobuf_encode_route(dest, rib, buf, sizeof(buf)); | |
165 | if (len <= 0) { | |
166 | return 2; | |
167 | } | |
168 | } | |
169 | return 0; | |
b80f3b24 AS |
170 | } |
171 | ||
172 | /* | |
173 | * zfpm_dt_log_fpm_message | |
174 | */ | |
ac4d0be5 | 175 | static void zfpm_dt_log_fpm_message(Fpm__Message *msg) |
b80f3b24 | 176 | { |
ac4d0be5 | 177 | Fpm__AddRoute *add_route; |
178 | Fpm__Nexthop *nexthop; | |
179 | struct prefix prefix; | |
180 | u_char family, nh_family; | |
181 | uint if_index; | |
182 | char *if_name; | |
183 | size_t i; | |
184 | char buf[INET6_ADDRSTRLEN]; | |
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", | |
217 | nexthop->address ? inet_ntoa(nh_addr.ipv4) : "None"); | |
218 | } | |
b80f3b24 AS |
219 | } |
220 | ||
221 | /* | |
222 | * zfpm_dt_benchmark_protobuf_decode | |
223 | */ | |
ac4d0be5 | 224 | int zfpm_dt_benchmark_protobuf_decode(int argc, const char **argv) |
b80f3b24 | 225 | { |
ac4d0be5 | 226 | int times, i, len; |
227 | rib_dest_t *dest; | |
228 | struct rib *rib; | |
229 | uint8_t msg_buf[4096]; | |
230 | QPB_DECLARE_STACK_ALLOCATOR(allocator, 8192); | |
231 | Fpm__Message *fpm_msg; | |
232 | ||
233 | QPB_INIT_STACK_ALLOCATOR(allocator); | |
234 | ||
235 | times = 100000; | |
236 | if (argc > 0) | |
237 | times = atoi(argv[0]); | |
238 | ||
239 | if (!zfpm_dt_find_route(&dest, &rib)) | |
240 | return 1; | |
241 | ||
242 | /* | |
243 | * Encode the route into the message buffer once only. | |
244 | */ | |
245 | len = zfpm_protobuf_encode_route(dest, rib, msg_buf, sizeof(msg_buf)); | |
246 | if (len <= 0) | |
247 | return 2; | |
248 | ||
249 | // Decode once, and display the decoded message | |
250 | fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); | |
251 | ||
252 | if (fpm_msg) { | |
253 | zfpm_dt_log_fpm_message(fpm_msg); | |
254 | QPB_RESET_STACK_ALLOCATOR(allocator); | |
255 | } | |
256 | ||
257 | /* | |
258 | * Decode encoded message the specified number of times. | |
259 | */ | |
260 | for (i = 0; i < times; i++) { | |
261 | fpm_msg = fpm__message__unpack(&allocator, len, msg_buf); | |
262 | ||
263 | if (!fpm_msg) | |
264 | return 3; | |
265 | ||
266 | // fpm__message__free_unpacked(msg, NULL); | |
267 | QPB_RESET_STACK_ALLOCATOR(allocator); | |
268 | } | |
269 | return 0; | |
b80f3b24 AS |
270 | } |
271 | ||
272 | #endif /* HAVE_PROTOBUF */ |