zebra_fpm_dt.c 5.9 KB

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