net_mld.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /****************************************************************************
  2. * net/procfs/net_mld.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /* Output format:
  36. *
  37. * Joins: xxxx Leaves: xxxx
  38. * Sent Sched Sent
  39. * Queries: xxxx xxxx
  40. * Reports:
  41. * Ver 1: ---- xxxx
  42. * Ver 2: xxxx xxxx
  43. * Done: xxxx xxxx
  44. * Received:
  45. * Queries:
  46. * Gen: xxxx
  47. * MAS: xxxx
  48. * MASSQ: xxxx
  49. * Ucast: xxxx
  50. * Bad: xxxx
  51. * Reports:
  52. * Ver 1: xxxx
  53. * Ver 2: xxxx
  54. * Done: xxxx
  55. */
  56. /****************************************************************************
  57. * Included Files
  58. ****************************************************************************/
  59. #include <nuttx/config.h>
  60. #include <sys/types.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <debug.h>
  64. #include <nuttx/net/netstats.h>
  65. #include "procfs/procfs.h"
  66. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
  67. !defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && defined(CONFIG_NET_STATISTICS)
  68. #if defined(CONFIG_NET_ICMPv6) || defined(CONFIG_NET_MLD)
  69. /****************************************************************************
  70. * Private Function Prototypes
  71. ****************************************************************************/
  72. /* Line generating functions */
  73. static int netprocfs_joinleave(FAR struct netprocfs_file_s *netfile);
  74. static int netprocfs_queries_sent(FAR struct netprocfs_file_s *netfile);
  75. static int netprocfs_reports_sent(FAR struct netprocfs_file_s *netfile);
  76. static int netprocfs_done_sent(FAR struct netprocfs_file_s *netfile);
  77. static int netprocfs_queries_received_1(FAR struct netprocfs_file_s *netfile);
  78. static int netprocfs_queries_received_2(FAR struct netprocfs_file_s *netfile);
  79. static int netprocfs_reports_received(FAR struct netprocfs_file_s *netfile);
  80. static int netprocfs_done_received(FAR struct netprocfs_file_s *netfile);
  81. /****************************************************************************
  82. * Private Data
  83. ****************************************************************************/
  84. /* Line generating functions */
  85. static const linegen_t g_mld_linegen[] =
  86. {
  87. netprocfs_joinleave,
  88. netprocfs_queries_sent,
  89. netprocfs_reports_sent,
  90. netprocfs_done_sent,
  91. netprocfs_queries_received_1,
  92. netprocfs_queries_received_2,
  93. netprocfs_reports_received,
  94. netprocfs_done_received
  95. };
  96. #define NSTAT_LINES (sizeof(g_mld_linegen) / sizeof(linegen_t))
  97. /****************************************************************************
  98. * Private Functions
  99. ****************************************************************************/
  100. /****************************************************************************
  101. * Name: netprocfs_joinleave
  102. ****************************************************************************/
  103. static int netprocfs_joinleave(FAR struct netprocfs_file_s *netfile)
  104. {
  105. int len;
  106. len = snprintf(netfile->line, NET_LINELEN, "Joins: %04x ",
  107. g_netstats.mld.njoins);
  108. len += snprintf(&netfile->line[len], NET_LINELEN - len, "Leaves: %04x\n",
  109. g_netstats.mld.nleaves);
  110. return len;
  111. }
  112. /****************************************************************************
  113. * Name: netprocfs_queries_sent
  114. ****************************************************************************/
  115. static int netprocfs_queries_sent(FAR struct netprocfs_file_s *netfile)
  116. {
  117. int len;
  118. len = snprintf(netfile->line, NET_LINELEN, "Sent Sched Sent\n");
  119. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  120. " Queries: %04x %04x\n",
  121. g_netstats.mld.query_sched, g_netstats.mld.query_sent);
  122. return len;
  123. }
  124. /****************************************************************************
  125. * Name: netprocfs_reports_sent
  126. ****************************************************************************/
  127. static int netprocfs_reports_sent(FAR struct netprocfs_file_s *netfile)
  128. {
  129. int len;
  130. len = snprintf(netfile->line, NET_LINELEN, " Reports:\n");
  131. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  132. " Ver 1: ---- %04x\n",
  133. g_netstats.mld.v1report_sent);
  134. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  135. " Ver 2: %04x %04x\n",
  136. g_netstats.mld.report_sched, g_netstats.mld.v2report_sent);
  137. return len;
  138. }
  139. /****************************************************************************
  140. * Name: netprocfs_done_sent
  141. ****************************************************************************/
  142. static int netprocfs_done_sent(FAR struct netprocfs_file_s *netfile)
  143. {
  144. return snprintf(netfile->line, NET_LINELEN, " Done: %04x %04x\n",
  145. g_netstats.mld.done_sched, g_netstats.mld.done_sent);
  146. }
  147. /****************************************************************************
  148. * Name: netprocfs_queries_received_1 and _2
  149. ****************************************************************************/
  150. static int netprocfs_queries_received_1(FAR struct netprocfs_file_s *netfile)
  151. {
  152. int len;
  153. len = snprintf(netfile->line, NET_LINELEN, "Received:\n");
  154. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  155. " Queries:\n");
  156. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  157. " Gen: %04x\n",
  158. g_netstats.mld.gm_query_received);
  159. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  160. " MAS: %04x\n",
  161. g_netstats.mld.mas_query_received);
  162. return len;
  163. }
  164. static int netprocfs_queries_received_2(FAR struct netprocfs_file_s *netfile)
  165. {
  166. int len;
  167. len = snprintf(netfile->line, NET_LINELEN,
  168. " MASS: %04x\n",
  169. g_netstats.mld.mass_query_received);
  170. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  171. " Ucast: %04x\n",
  172. g_netstats.mld.ucast_query_received);
  173. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  174. " Bad: %04x\n",
  175. g_netstats.mld.bad_query_received);
  176. return len;
  177. }
  178. /****************************************************************************
  179. * Name: netprocfs_reports_received
  180. ****************************************************************************/
  181. static int netprocfs_reports_received(FAR struct netprocfs_file_s *netfile)
  182. {
  183. int len;
  184. len = snprintf(netfile->line, NET_LINELEN, " Reports:\n");
  185. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  186. " Ver 1: %04x\n",
  187. g_netstats.mld.v1report_received);
  188. len += snprintf(&netfile->line[len], NET_LINELEN - len,
  189. " Ver 2: %04x\n",
  190. g_netstats.mld.v2report_received);
  191. return len;
  192. }
  193. /****************************************************************************
  194. * Name: netprocfs_done_received
  195. ****************************************************************************/
  196. static int netprocfs_done_received(FAR struct netprocfs_file_s *netfile)
  197. {
  198. return snprintf(netfile->line, NET_LINELEN , " Done: %04x\n",
  199. g_netstats.mld.done_received);
  200. }
  201. /****************************************************************************
  202. * Public Functions
  203. ****************************************************************************/
  204. /****************************************************************************
  205. * Name: netprocfs_read_mldstats
  206. *
  207. * Description:
  208. * Read and format MLD statistics.
  209. *
  210. * Input Parameters:
  211. * priv - A reference to the network procfs file structure
  212. * buffer - The user-provided buffer into which network status will be
  213. * returned.
  214. * bulen - The size in bytes of the user provided buffer.
  215. *
  216. * Returned Value:
  217. * Zero (OK) is returned on success; a negated errno value is returned
  218. * on failure.
  219. *
  220. ****************************************************************************/
  221. ssize_t netprocfs_read_mldstats(FAR struct netprocfs_file_s *priv,
  222. FAR char *buffer, size_t buflen)
  223. {
  224. return netprocfs_read_linegen(priv, buffer, buflen, g_mld_linegen, NSTAT_LINES);
  225. }
  226. #endif /* CONFIG_NET_ICMPv6 || CONFIG_NET_MLD */
  227. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
  228. * !CONFIG_FS_PROCFS_EXCLUDE_NET */