gen_stats.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/gen_stats.h>
  23. static inline int
  24. gnet_stats_copy(struct gnet_dump *d, int type, void *buf, int size)
  25. {
  26. RTA_PUT(d->skb, type, size, buf);
  27. return 0;
  28. rtattr_failure:
  29. spin_unlock_bh(d->lock);
  30. return -1;
  31. }
  32. /**
  33. * gnet_stats_start_copy_compat - start dumping procedure in compatibility mode
  34. * @skb: socket buffer to put statistics TLVs into
  35. * @type: TLV type for top level statistic TLV
  36. * @tc_stats_type: TLV type for backward compatibility struct tc_stats TLV
  37. * @xstats_type: TLV type for backward compatibility xstats TLV
  38. * @lock: statistics lock
  39. * @d: dumping handle
  40. *
  41. * Initializes the dumping handle, grabs the statistic lock and appends
  42. * an empty TLV header to the socket buffer for use a container for all
  43. * other statistic TLVS.
  44. *
  45. * The dumping handle is marked to be in backward compatibility mode telling
  46. * all gnet_stats_copy_XXX() functions to fill a local copy of struct tc_stats.
  47. *
  48. * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
  49. */
  50. int
  51. gnet_stats_start_copy_compat(struct sk_buff *skb, int type, int tc_stats_type,
  52. int xstats_type, spinlock_t *lock, struct gnet_dump *d)
  53. {
  54. memset(d, 0, sizeof(*d));
  55. spin_lock_bh(lock);
  56. d->lock = lock;
  57. if (type)
  58. d->tail = (struct rtattr *) skb->tail;
  59. d->skb = skb;
  60. d->compat_tc_stats = tc_stats_type;
  61. d->compat_xstats = xstats_type;
  62. if (d->tail)
  63. return gnet_stats_copy(d, type, NULL, 0);
  64. return 0;
  65. }
  66. /**
  67. * gnet_stats_start_copy_compat - start dumping procedure in compatibility mode
  68. * @skb: socket buffer to put statistics TLVs into
  69. * @type: TLV type for top level statistic TLV
  70. * @lock: statistics lock
  71. * @d: dumping handle
  72. *
  73. * Initializes the dumping handle, grabs the statistic lock and appends
  74. * an empty TLV header to the socket buffer for use a container for all
  75. * other statistic TLVS.
  76. *
  77. * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
  78. */
  79. int
  80. gnet_stats_start_copy(struct sk_buff *skb, int type, spinlock_t *lock,
  81. struct gnet_dump *d)
  82. {
  83. return gnet_stats_start_copy_compat(skb, type, 0, 0, lock, d);
  84. }
  85. /**
  86. * gnet_stats_copy_basic - copy basic statistics into statistic TLV
  87. * @d: dumping handle
  88. * @b: basic statistics
  89. *
  90. * Appends the basic statistics to the top level TLV created by
  91. * gnet_stats_start_copy().
  92. *
  93. * Returns 0 on success or -1 with the statistic lock released
  94. * if the room in the socket buffer was not sufficient.
  95. */
  96. int
  97. gnet_stats_copy_basic(struct gnet_dump *d, struct gnet_stats_basic *b)
  98. {
  99. if (d->compat_tc_stats) {
  100. d->tc_stats.bytes = b->bytes;
  101. d->tc_stats.packets = b->packets;
  102. }
  103. if (d->tail)
  104. return gnet_stats_copy(d, TCA_STATS_BASIC, b, sizeof(*b));
  105. return 0;
  106. }
  107. /**
  108. * gnet_stats_copy_rate_est - copy rate estimator statistics into statistics TLV
  109. * @d: dumping handle
  110. * @r: rate estimator statistics
  111. *
  112. * Appends the rate estimator statistics to the top level TLV created by
  113. * gnet_stats_start_copy().
  114. *
  115. * Returns 0 on success or -1 with the statistic lock released
  116. * if the room in the socket buffer was not sufficient.
  117. */
  118. int
  119. gnet_stats_copy_rate_est(struct gnet_dump *d, struct gnet_stats_rate_est *r)
  120. {
  121. if (d->compat_tc_stats) {
  122. d->tc_stats.bps = r->bps;
  123. d->tc_stats.pps = r->pps;
  124. }
  125. if (d->tail)
  126. return gnet_stats_copy(d, TCA_STATS_RATE_EST, r, sizeof(*r));
  127. return 0;
  128. }
  129. /**
  130. * gnet_stats_copy_queue - copy queue statistics into statistics TLV
  131. * @d: dumping handle
  132. * @q: queue statistics
  133. *
  134. * Appends the queue statistics to the top level TLV created by
  135. * gnet_stats_start_copy().
  136. *
  137. * Returns 0 on success or -1 with the statistic lock released
  138. * if the room in the socket buffer was not sufficient.
  139. */
  140. int
  141. gnet_stats_copy_queue(struct gnet_dump *d, struct gnet_stats_queue *q)
  142. {
  143. if (d->compat_tc_stats) {
  144. d->tc_stats.drops = q->drops;
  145. d->tc_stats.qlen = q->qlen;
  146. d->tc_stats.backlog = q->backlog;
  147. d->tc_stats.overlimits = q->overlimits;
  148. }
  149. if (d->tail)
  150. return gnet_stats_copy(d, TCA_STATS_QUEUE, q, sizeof(*q));
  151. return 0;
  152. }
  153. /**
  154. * gnet_stats_copy_app - copy application specific statistics into statistics TLV
  155. * @d: dumping handle
  156. * @st: application specific statistics data
  157. * @len: length of data
  158. *
  159. * Appends the application sepecific statistics to the top level TLV created by
  160. * gnet_stats_start_copy() and remembers the data for XSTATS if the dumping
  161. * handle is in backward compatibility mode.
  162. *
  163. * Returns 0 on success or -1 with the statistic lock released
  164. * if the room in the socket buffer was not sufficient.
  165. */
  166. int
  167. gnet_stats_copy_app(struct gnet_dump *d, void *st, int len)
  168. {
  169. if (d->compat_xstats) {
  170. d->xstats = st;
  171. d->xstats_len = len;
  172. }
  173. if (d->tail)
  174. return gnet_stats_copy(d, TCA_STATS_APP, st, len);
  175. return 0;
  176. }
  177. /**
  178. * gnet_stats_finish_copy - finish dumping procedure
  179. * @d: dumping handle
  180. *
  181. * Corrects the length of the top level TLV to include all TLVs added
  182. * by gnet_stats_copy_XXX() calls. Adds the backward compatibility TLVs
  183. * if gnet_stats_start_copy_compat() was used and releases the statistics
  184. * lock.
  185. *
  186. * Returns 0 on success or -1 with the statistic lock released
  187. * if the room in the socket buffer was not sufficient.
  188. */
  189. int
  190. gnet_stats_finish_copy(struct gnet_dump *d)
  191. {
  192. if (d->tail)
  193. d->tail->rta_len = d->skb->tail - (u8 *) d->tail;
  194. if (d->compat_tc_stats)
  195. if (gnet_stats_copy(d, d->compat_tc_stats, &d->tc_stats,
  196. sizeof(d->tc_stats)) < 0)
  197. return -1;
  198. if (d->compat_xstats && d->xstats) {
  199. if (gnet_stats_copy(d, d->compat_xstats, d->xstats,
  200. d->xstats_len) < 0)
  201. return -1;
  202. }
  203. spin_unlock_bh(d->lock);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL(gnet_stats_start_copy);
  207. EXPORT_SYMBOL(gnet_stats_start_copy_compat);
  208. EXPORT_SYMBOL(gnet_stats_copy_basic);
  209. EXPORT_SYMBOL(gnet_stats_copy_rate_est);
  210. EXPORT_SYMBOL(gnet_stats_copy_queue);
  211. EXPORT_SYMBOL(gnet_stats_copy_app);
  212. EXPORT_SYMBOL(gnet_stats_finish_copy);