LinkStatistics.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed LinkStatistics (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * this plugin for plcstat reads device link statistics using a
  48. * VS_LNK_STATS message and displays information on stdout; since
  49. * the output may not suite all requirements, users are encouraged
  50. * to modify as needed; there is way too much data displayed here;
  51. *
  52. * some code is duplicated for the sake of modularity;
  53. *
  54. *
  55. * Contributor(s):
  56. * Charles Maier
  57. *
  58. *--------------------------------------------------------------------*/
  59. #ifndef LINKSTATISTICS_SOURCE
  60. #define LINKSTATISTICS_SOURCE
  61. #include <stdint.h>
  62. #include <memory.h>
  63. #include <inttypes.h>
  64. #include "../plc/plc.h"
  65. #include "../tools/memory.h"
  66. #include "../tools/number.h"
  67. #include "../tools/error.h"
  68. /*====================================================================*
  69. * constants;
  70. *--------------------------------------------------------------------*/
  71. #define PAD ' '
  72. #define DIGITS 16
  73. /*====================================================================*
  74. * variables;
  75. *--------------------------------------------------------------------*/
  76. #ifndef __GNUC__
  77. #pragma pack (push,1)
  78. #endif
  79. typedef struct __packed transmit
  80. {
  81. uint64_t NUMTXMPDU_ACKD;
  82. uint64_t NUMTXMPDU_COLL;
  83. uint64_t NUMTXMPDU_FAIL;
  84. uint64_t NUMTXPBS_PASS;
  85. uint64_t NUMTXPBS_FAIL;
  86. }
  87. transmit;
  88. typedef struct __packed receive
  89. {
  90. uint64_t NUMRXMPDU_ACKD;
  91. uint64_t NUMRXMPDU_FAIL;
  92. uint64_t NUMRXPBS_PASS;
  93. uint64_t NUMRXPBS_FAIL;
  94. uint64_t SUMTURBOBER_PASS;
  95. uint64_t SUMTURBOBER_FAIL;
  96. uint8_t NUMRXINTERVALS;
  97. uint8_t RXINTERVALSTATS [1];
  98. }
  99. receive;
  100. typedef struct __packed interval
  101. {
  102. uint8_t RXPHYRATE_MBPS_0;
  103. uint64_t NUMRXPBS_PASS;
  104. uint64_t NUMRXPBS_FAIL;
  105. uint64_t SUMTURBOBER_PASS;
  106. uint64_t SUMTURBOBER_FAIL;
  107. }
  108. interval;
  109. #ifndef __GNUC__
  110. #pragma pack (pop)
  111. #endif
  112. /*====================================================================*
  113. *
  114. * unsigned error_rate (uint64_t passed, uint64_t failed);
  115. *
  116. * compute error rate for a given quantity; the error rate is the
  117. * ratio of failures to attempts;
  118. *
  119. *--------------------------------------------------------------------*/
  120. static float error_rate (uint64_t passed, uint64_t failed)
  121. {
  122. if ((passed) || (failed))
  123. {
  124. return ((float)(failed * 100) / (float)(passed + failed));
  125. }
  126. return (0);
  127. }
  128. /*====================================================================*
  129. *
  130. * float fec_bit_error_rate (struct receive * receive);
  131. *
  132. * compute the FEC-BER from the VS_LNK_STATS when DIRECTION=1 and
  133. * LID=0xF8;
  134. *
  135. *--------------------------------------------------------------------*/
  136. static float fec_bit_error_rate (struct receive * receive)
  137. {
  138. float FECBitErrorRate = 0;
  139. if (receive->SUMTURBOBER_PASS || receive->SUMTURBOBER_FAIL)
  140. {
  141. float TotalSumOfBitError = 100 * (float)(receive->SUMTURBOBER_PASS + receive->SUMTURBOBER_FAIL);
  142. float TotalSumOfBits = 8 * 520 * (float)(receive->NUMRXPBS_PASS + receive->NUMRXPBS_FAIL);
  143. FECBitErrorRate = TotalSumOfBitError / TotalSumOfBits;
  144. }
  145. return (FECBitErrorRate);
  146. }
  147. /*====================================================================*
  148. *
  149. * void transmit (struct transmit * transmit);
  150. *
  151. * display transmit statistics in fixed field format;
  152. *
  153. *--------------------------------------------------------------------*/
  154. static void TransmitStatistics (struct transmit * transmit)
  155. {
  156. printf (" TX");
  157. printf (" %20" PRId64, transmit->NUMTXPBS_PASS);
  158. printf (" %20" PRId64, transmit->NUMTXPBS_FAIL);
  159. printf (" %6.2f%%", error_rate (transmit->NUMTXPBS_PASS, transmit->NUMTXPBS_FAIL));
  160. printf (" %20" PRId64, transmit->NUMTXMPDU_ACKD);
  161. printf (" %20" PRId64, transmit->NUMTXMPDU_FAIL);
  162. printf (" %20" PRId64, transmit->NUMTXMPDU_COLL);
  163. printf (" %6.2f%%", error_rate (transmit->NUMTXMPDU_ACKD, transmit->NUMTXMPDU_FAIL));
  164. printf ("\n");
  165. return;
  166. }
  167. /*====================================================================*
  168. *
  169. * void Receive (struct receive * receive);
  170. *
  171. * display receive statistics in fixed field format;
  172. *
  173. *--------------------------------------------------------------------*/
  174. static void ReceiveStatistics (struct receive * receive)
  175. {
  176. printf (" RX");
  177. printf (" %20" PRId64, receive->NUMRXPBS_PASS);
  178. printf (" %20" PRId64, receive->NUMRXPBS_FAIL);
  179. printf (" %6.2f%%", error_rate (receive->NUMRXPBS_PASS, receive->NUMRXPBS_FAIL));
  180. printf (" %20" PRId64, receive->NUMRXMPDU_ACKD);
  181. printf (" %20" PRId64, receive->NUMRXMPDU_FAIL);
  182. printf (" %6.2f%%", error_rate (receive->NUMRXMPDU_ACKD, receive->NUMRXMPDU_FAIL));
  183. printf ("\n");
  184. return;
  185. }
  186. /*====================================================================*
  187. *
  188. * void Receive (struct receive * receive);
  189. *
  190. * display receive statistics in fixed field format for each slot;
  191. * the last line sumarizes results for all slots;
  192. *
  193. *--------------------------------------------------------------------*/
  194. static void Receive2 (struct receive * receive)
  195. {
  196. struct interval * interval = (struct interval *)(receive->RXINTERVALSTATS);
  197. uint8_t slot = 0;
  198. while (slot < receive->NUMRXINTERVALS)
  199. {
  200. printf (" %1d", slot);
  201. printf (" %3d", interval->RXPHYRATE_MBPS_0);
  202. printf (" %20" PRId64, interval->NUMRXPBS_PASS);
  203. printf (" %20" PRId64, interval->NUMRXPBS_FAIL);
  204. printf (" %6.2f%%", error_rate (interval->NUMRXPBS_PASS, interval->NUMRXPBS_FAIL));
  205. printf (" %20" PRId64, interval->SUMTURBOBER_PASS);
  206. printf (" %20" PRId64, interval->SUMTURBOBER_FAIL);
  207. printf (" %6.2f%%", error_rate (interval->SUMTURBOBER_PASS, interval->SUMTURBOBER_FAIL));
  208. printf ("\n");
  209. interval++;
  210. slot++;
  211. }
  212. printf (" ALL");
  213. printf (" %20" PRId64, receive->NUMRXPBS_PASS);
  214. printf (" %20" PRId64, receive->NUMRXPBS_FAIL);
  215. printf (" %6.2f%%", error_rate (receive->NUMRXPBS_PASS, receive->NUMRXPBS_FAIL));
  216. printf (" %20" PRId64, receive->SUMTURBOBER_PASS);
  217. printf (" %20" PRId64, receive->SUMTURBOBER_FAIL);
  218. printf (" %6.2f%%", error_rate (receive->SUMTURBOBER_PASS, receive->SUMTURBOBER_FAIL));
  219. printf (" %6.2f%%", fec_bit_error_rate (receive));
  220. printf ("\n");
  221. return;
  222. }
  223. /*====================================================================*
  224. *
  225. * signed LinkStatistics (struct plc * plc);
  226. *
  227. *
  228. *--------------------------------------------------------------------*/
  229. signed LinkStatistics (struct plc * plc)
  230. {
  231. struct channel * channel = (struct channel *)(plc->channel);
  232. struct message * message = (struct message *)(plc->message);
  233. #ifndef __GNUC__
  234. #pragma pack (push,1)
  235. #endif
  236. struct __packed vs_lnk_stats_request
  237. {
  238. struct ethernet_hdr ethernet;
  239. struct qualcomm_hdr qualcomm;
  240. uint8_t MCONTROL;
  241. uint8_t DIRECTION;
  242. uint8_t LID;
  243. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  244. }
  245. * request = (struct vs_lnk_stats_request *) (message);
  246. struct __packed vs_lnk_stats_confirm
  247. {
  248. struct ethernet_hdr ethernet;
  249. struct qualcomm_hdr qualcomm;
  250. uint8_t MSTATUS;
  251. uint8_t DIRECTION;
  252. uint8_t LID;
  253. uint8_t TEI;
  254. uint8_t LSTATS [1];
  255. }
  256. * confirm = (struct vs_lnk_stats_confirm *) (message);
  257. #ifndef __GNUC__
  258. #pragma pack (pop)
  259. #endif
  260. memset (message, 0, sizeof (* message));
  261. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  262. QualcommHeader (&request->qualcomm, 0, (VS_LNK_STATS | MMTYPE_REQ));
  263. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  264. request->MCONTROL = plc->pushbutton;
  265. request->DIRECTION = plc->module;
  266. request->LID = plc->action;
  267. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  268. if (SendMME (plc) <= 0)
  269. {
  270. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  271. return (-1);
  272. }
  273. if (ReadMME (plc, 0, (VS_LNK_STATS | MMTYPE_CNF)) <= 0)
  274. {
  275. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  276. return (-1);
  277. }
  278. if (confirm->MSTATUS)
  279. {
  280. Failure (plc, PLC_WONTDOIT);
  281. return (-1);
  282. }
  283. if (confirm->DIRECTION == 0)
  284. {
  285. printf (" DIR");
  286. printf (" ----------- PBs PASS");
  287. printf (" ----------- PBs FAIL");
  288. printf (" PBs ERR");
  289. printf (" ---------- MPDU ACKD");
  290. printf (" ---------- MPDU FAIL");
  291. printf ("\n");
  292. TransmitStatistics ((struct transmit *)(confirm->LSTATS));
  293. printf ("\n");
  294. }
  295. if (confirm->DIRECTION == 1)
  296. {
  297. printf (" DIR");
  298. printf (" ----------- PBs PASS");
  299. printf (" ----------- PBs FAIL");
  300. printf (" PBs ERR");
  301. printf (" ---------- MPDU ACKD");
  302. printf (" ---------- MPDU FAIL");
  303. printf ("\n");
  304. ReceiveStatistics ((struct receive *)(confirm->LSTATS));
  305. printf ("\n");
  306. printf (" PHY");
  307. printf (" ----------- PBs PASS");
  308. printf (" ----------- PBs FAIL");
  309. printf (" PBs ERR");
  310. printf (" ----------- BER PASS");
  311. printf (" ----------- BER FAIL");
  312. printf (" BER ERR");
  313. printf ("\n");
  314. Receive2 ((struct receive *)(confirm->LSTATS));
  315. printf ("\n");
  316. }
  317. if (confirm->DIRECTION == 2)
  318. {
  319. printf (" DIR");
  320. printf (" ----------- PBs PASS");
  321. printf (" ----------- PBs FAIL");
  322. printf (" PBs ERR");
  323. printf (" ---------- MPDU ACKD");
  324. printf (" ---------- MPDU FAIL");
  325. printf ("\n");
  326. TransmitStatistics ((struct transmit *)(confirm->LSTATS));
  327. ReceiveStatistics ((struct receive *)(confirm->LSTATS + sizeof (struct transmit)));
  328. printf ("\n");
  329. printf (" PHY");
  330. printf (" ----------- PBs PASS");
  331. printf (" ----------- PBs FAIL");
  332. printf (" PBs ERR");
  333. printf (" ----------- BER PASS");
  334. printf (" ----------- BER FAIL");
  335. printf (" BER ERR");
  336. printf ("\n");
  337. Receive2 ((struct receive *)(confirm->LSTATS + sizeof (struct transmit)));
  338. printf ("\n");
  339. }
  340. return (0);
  341. }
  342. #endif