gen_stats.c 6.5 KB

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