gen_stats.c 6.4 KB

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