gen_stats.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * net/core/gen_stats.c
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. * Jamal Hadi Salim
  11. * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  12. *
  13. * See Documentation/networking/gen_stats.txt
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/socket.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/gen_stats.h>
  22. #include <net/netlink.h>
  23. #include <net/gen_stats.h>
  24. static inline int
  25. gnet_stats_copy(struct gnet_dump *d, int type, void *buf, int size)
  26. {
  27. if (nla_put(d->skb, type, size, buf))
  28. goto nla_put_failure;
  29. return 0;
  30. nla_put_failure:
  31. spin_unlock_bh(d->lock);
  32. return -1;
  33. }
  34. /**
  35. * gnet_stats_start_copy_compat - start dumping procedure in compatibility mode
  36. * @skb: socket buffer to put statistics TLVs into
  37. * @type: TLV type for top level statistic TLV
  38. * @tc_stats_type: TLV type for backward compatibility struct tc_stats TLV
  39. * @xstats_type: TLV type for backward compatibility xstats TLV
  40. * @lock: statistics lock
  41. * @d: dumping handle
  42. *
  43. * Initializes the dumping handle, grabs the statistic lock and appends
  44. * an empty TLV header to the socket buffer for use a container for all
  45. * other statistic TLVS.
  46. *
  47. * The dumping handle is marked to be in backward compatibility mode telling
  48. * all gnet_stats_copy_XXX() functions to fill a local copy of struct tc_stats.
  49. *
  50. * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
  51. */
  52. int
  53. gnet_stats_start_copy_compat(struct sk_buff *skb, int type, int tc_stats_type,
  54. int xstats_type, spinlock_t *lock, struct gnet_dump *d)
  55. __acquires(lock)
  56. {
  57. memset(d, 0, sizeof(*d));
  58. spin_lock_bh(lock);
  59. d->lock = lock;
  60. if (type)
  61. d->tail = (struct nlattr *)skb_tail_pointer(skb);
  62. d->skb = skb;
  63. d->compat_tc_stats = tc_stats_type;
  64. d->compat_xstats = xstats_type;
  65. if (d->tail)
  66. return gnet_stats_copy(d, type, NULL, 0);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL(gnet_stats_start_copy_compat);
  70. /**
  71. * gnet_stats_start_copy_compat - start dumping procedure in compatibility mode
  72. * @skb: socket buffer to put statistics TLVs into
  73. * @type: TLV type for top level statistic TLV
  74. * @lock: statistics lock
  75. * @d: dumping handle
  76. *
  77. * Initializes the dumping handle, grabs the statistic lock and appends
  78. * an empty TLV header to the socket buffer for use a container for all
  79. * other statistic TLVS.
  80. *
  81. * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
  82. */
  83. int
  84. gnet_stats_start_copy(struct sk_buff *skb, int type, spinlock_t *lock,
  85. struct gnet_dump *d)
  86. {
  87. return gnet_stats_start_copy_compat(skb, type, 0, 0, lock, d);
  88. }
  89. EXPORT_SYMBOL(gnet_stats_start_copy);
  90. /**
  91. * gnet_stats_copy_basic - copy basic statistics into statistic TLV
  92. * @d: dumping handle
  93. * @b: basic statistics
  94. *
  95. * Appends the basic statistics to the top level TLV created by
  96. * gnet_stats_start_copy().
  97. *
  98. * Returns 0 on success or -1 with the statistic lock released
  99. * if the room in the socket buffer was not sufficient.
  100. */
  101. int
  102. gnet_stats_copy_basic(struct gnet_dump *d, struct gnet_stats_basic_packed *b)
  103. {
  104. if (d->compat_tc_stats) {
  105. d->tc_stats.bytes = b->bytes;
  106. d->tc_stats.packets = b->packets;
  107. }
  108. if (d->tail) {
  109. struct gnet_stats_basic sb;
  110. memset(&sb, 0, sizeof(sb));
  111. sb.bytes = b->bytes;
  112. sb.packets = b->packets;
  113. return gnet_stats_copy(d, TCA_STATS_BASIC, &sb, sizeof(sb));
  114. }
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(gnet_stats_copy_basic);
  118. /**
  119. * gnet_stats_copy_rate_est - copy rate estimator statistics into statistics TLV
  120. * @d: dumping handle
  121. * @b: basic statistics
  122. * @r: rate estimator statistics
  123. *
  124. * Appends the rate estimator statistics to the top level TLV created by
  125. * gnet_stats_start_copy().
  126. *
  127. * Returns 0 on success or -1 with the statistic lock released
  128. * if the room in the socket buffer was not sufficient.
  129. */
  130. int
  131. gnet_stats_copy_rate_est(struct gnet_dump *d,
  132. const struct gnet_stats_basic_packed *b,
  133. struct gnet_stats_rate_est64 *r)
  134. {
  135. struct gnet_stats_rate_est est;
  136. int res;
  137. if (b && !gen_estimator_active(b, r))
  138. return 0;
  139. est.bps = min_t(u64, UINT_MAX, r->bps);
  140. /* we have some time before reaching 2^32 packets per second */
  141. est.pps = r->pps;
  142. if (d->compat_tc_stats) {
  143. d->tc_stats.bps = est.bps;
  144. d->tc_stats.pps = est.pps;
  145. }
  146. if (d->tail) {
  147. res = gnet_stats_copy(d, TCA_STATS_RATE_EST, &est, sizeof(est));
  148. if (res < 0 || est.bps == r->bps)
  149. return res;
  150. /* emit 64bit stats only if needed */
  151. return gnet_stats_copy(d, TCA_STATS_RATE_EST64, r, sizeof(*r));
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(gnet_stats_copy_rate_est);
  156. /**
  157. * gnet_stats_copy_queue - copy queue statistics into statistics TLV
  158. * @d: dumping handle
  159. * @q: queue statistics
  160. *
  161. * Appends the queue statistics to the top level TLV created by
  162. * gnet_stats_start_copy().
  163. *
  164. * Returns 0 on success or -1 with the statistic lock released
  165. * if the room in the socket buffer was not sufficient.
  166. */
  167. int
  168. gnet_stats_copy_queue(struct gnet_dump *d, struct gnet_stats_queue *q)
  169. {
  170. if (d->compat_tc_stats) {
  171. d->tc_stats.drops = q->drops;
  172. d->tc_stats.qlen = q->qlen;
  173. d->tc_stats.backlog = q->backlog;
  174. d->tc_stats.overlimits = q->overlimits;
  175. }
  176. if (d->tail)
  177. return gnet_stats_copy(d, TCA_STATS_QUEUE, q, sizeof(*q));
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(gnet_stats_copy_queue);
  181. /**
  182. * gnet_stats_copy_app - copy application specific statistics into statistics TLV
  183. * @d: dumping handle
  184. * @st: application specific statistics data
  185. * @len: length of data
  186. *
  187. * Appends the application sepecific statistics to the top level TLV created by
  188. * gnet_stats_start_copy() and remembers the data for XSTATS if the dumping
  189. * handle is in backward compatibility mode.
  190. *
  191. * Returns 0 on success or -1 with the statistic lock released
  192. * if the room in the socket buffer was not sufficient.
  193. */
  194. int
  195. gnet_stats_copy_app(struct gnet_dump *d, void *st, int len)
  196. {
  197. if (d->compat_xstats) {
  198. d->xstats = st;
  199. d->xstats_len = len;
  200. }
  201. if (d->tail)
  202. return gnet_stats_copy(d, TCA_STATS_APP, st, len);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(gnet_stats_copy_app);
  206. /**
  207. * gnet_stats_finish_copy - finish dumping procedure
  208. * @d: dumping handle
  209. *
  210. * Corrects the length of the top level TLV to include all TLVs added
  211. * by gnet_stats_copy_XXX() calls. Adds the backward compatibility TLVs
  212. * if gnet_stats_start_copy_compat() was used and releases the statistics
  213. * lock.
  214. *
  215. * Returns 0 on success or -1 with the statistic lock released
  216. * if the room in the socket buffer was not sufficient.
  217. */
  218. int
  219. gnet_stats_finish_copy(struct gnet_dump *d)
  220. {
  221. if (d->tail)
  222. d->tail->nla_len = skb_tail_pointer(d->skb) - (u8 *)d->tail;
  223. if (d->compat_tc_stats)
  224. if (gnet_stats_copy(d, d->compat_tc_stats, &d->tc_stats,
  225. sizeof(d->tc_stats)) < 0)
  226. return -1;
  227. if (d->compat_xstats && d->xstats) {
  228. if (gnet_stats_copy(d, d->compat_xstats, d->xstats,
  229. d->xstats_len) < 0)
  230. return -1;
  231. }
  232. spin_unlock_bh(d->lock);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(gnet_stats_finish_copy);