coqos_man.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * coqos_man.c - Manage MCSMA streams
  44. *
  45. *
  46. * Contributor(s):
  47. * Bill Wike
  48. * Nathaniel Houghton
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. /*====================================================================*"
  53. * system header files;
  54. *--------------------------------------------------------------------*/
  55. #include <unistd.h>
  56. #include <stdlib.h>
  57. #include <stdint.h>
  58. #include <limits.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/getoptv.h"
  63. #include "../tools/putoptv.h"
  64. #include "../tools/memory.h"
  65. #include "../tools/number.h"
  66. #include "../tools/symbol.h"
  67. #include "../tools/types.h"
  68. #include "../tools/flags.h"
  69. #include "../tools/files.h"
  70. #include "../tools/error.h"
  71. #include "../plc/plc.h"
  72. #include "../plc/coqos.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../tools/getoptv.c"
  78. #include "../tools/putoptv.c"
  79. #include "../tools/version.c"
  80. #include "../tools/uintspec.c"
  81. #include "../tools/hexdump.c"
  82. #include "../tools/hexencode.c"
  83. #include "../tools/hexdecode.c"
  84. #include "../tools/todigit.c"
  85. #include "../tools/typename.c"
  86. #include "../tools/error.c"
  87. #include "../tools/synonym.c"
  88. #endif
  89. #ifndef MAKEFILE
  90. #include "../plc/Request.c"
  91. #include "../plc/Confirm.c"
  92. #include "../plc/Failure.c"
  93. #include "../plc/Display.c"
  94. #include "../plc/ReadMME.c"
  95. #include "../plc/SendMME.c"
  96. #include "../plc/Devices.c"
  97. #endif
  98. #ifndef MAKEFILE
  99. #include "../ether/openchannel.c"
  100. #include "../ether/closechannel.c"
  101. #include "../ether/readpacket.c"
  102. #include "../ether/sendpacket.c"
  103. #include "../ether/channel.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../mme/MMECode.c"
  107. #include "../mme/EthernetHeader.c"
  108. #include "../mme/QualcommHeader.c"
  109. #include "../mme/UnwantedMessage.c"
  110. #endif
  111. /*====================================================================*
  112. *
  113. * signed con_man (struct plc * plc, uint16_t TOT_BW_USED);
  114. *
  115. *
  116. *
  117. * Contributor(s):
  118. * Bill Wike
  119. *
  120. *--------------------------------------------------------------------*/
  121. signed con_man (struct plc * plc, uint16_t TOT_BW_USED)
  122. {
  123. struct channel * channel = (struct channel *)(plc->channel);
  124. struct message * message = (struct message *)(plc->message);
  125. #ifndef __GNUC__
  126. #pragma pack (push,1)
  127. #endif
  128. struct __packed vs_con_info_req
  129. {
  130. struct ethernet_hdr ethernet;
  131. struct qualcomm_hdr qualcomm;
  132. uint8_t REQ_TYPE;
  133. uint32_t RSVD;
  134. uint16_t CSPEC_VER;
  135. uint16_t CID;
  136. uint16_t TOT_BW_USED;
  137. }
  138. * request = (struct vs_con_info_req *)(message);
  139. struct __packed vs_con_info_cnf
  140. {
  141. struct ethernet_hdr ethernet;
  142. struct qualcomm_hdr qualcomm;
  143. uint8_t REQ_TYPE;
  144. uint8_t MSTATUS;
  145. uint16_t ERR_REC_CODE;
  146. uint32_t RSVD;
  147. uint8_t NUM_CONN;
  148. struct conn_info CONN_INFO [64];
  149. }
  150. * confirm = (struct vs_con_info_cnf *)(message);
  151. struct conn_info * conn_info =(struct conn_info*)(confirm->CONN_INFO);
  152. struct vs_con_info_cnf *indicate = (struct vs_con_info_cnf *)(message);
  153. struct __packed vs_mod_conn_req
  154. {
  155. struct ethernet_hdr ethernet;
  156. struct qualcomm_hdr qualcomm;
  157. uint32_t REQ_ID;
  158. uint32_t RSVD;
  159. uint16_t CID;
  160. uint8_t MOD_CTRL;
  161. }
  162. * mc_request = (struct vs_mod_conn_req *)(message);
  163. struct __packed vs_mod_conn_cnf
  164. {
  165. struct ethernet_hdr ethernet;
  166. struct qualcomm_hdr qualcomm;
  167. uint32_t REQ_ID;
  168. uint8_t MSTATUS;
  169. uint16_t ERR_REC_CODE;
  170. uint32_t RSVD;
  171. }
  172. * mc_confirm = (struct vs_mod_conn_cnf *)(message);
  173. #ifndef __GNUC__
  174. #pragma pack (pop)
  175. #endif
  176. int i;
  177. int bwused;
  178. int lowest;
  179. uint16_t lowcid;
  180. while (1)
  181. {
  182. Request (plc, "COQOS connection information (requesting bandwidth notification)");
  183. memset (message, 0, sizeof (* message));
  184. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  185. QualcommHeader (&request->qualcomm, 0, (VS_CONN_INFO | MMTYPE_REQ));
  186. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  187. request->REQ_TYPE = 0x04;
  188. request->CID = 0x00;
  189. request->CSPEC_VER = 0x01;
  190. request->TOT_BW_USED = TOT_BW_USED;
  191. if (SendMME (plc) <= 0)
  192. {
  193. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  194. return (-1);
  195. }
  196. while (ReadMME (plc, 0, (VS_CONN_INFO | MMTYPE_CNF)) <= 0);
  197. if (confirm->MSTATUS)
  198. {
  199. Failure (plc, "Could not set up bandwidth notification.");
  200. }
  201. else
  202. {
  203. Confirm (plc, "Set up bandwidth monitoring.");
  204. break;
  205. }
  206. sleep (1);
  207. }
  208. while (1)
  209. {
  210. /*
  211. * Wait for indications saying too much bandwidth is
  212. * in use.
  213. */
  214. while (ReadMME (plc, 0, (VS_CONN_INFO | MMTYPE_IND)) <= 0)
  215. {
  216. continue;
  217. }
  218. if (indicate->MSTATUS)
  219. {
  220. Failure (plc, "Connection Info Indicate error");
  221. continue;
  222. }
  223. printf ("Number of Connections: %d\n", indicate->NUM_CONN);
  224. conn_info = (struct conn_info *)(confirm->CONN_INFO);
  225. bwused = 0;
  226. lowest = conn_info->CONN_COQOS_PRIO;
  227. lowcid = conn_info->CID;
  228. for (i = 0; i < indicate->NUM_CONN; i++)
  229. {
  230. bwused += conn_info->BW_USED;
  231. if (conn_info->CONN_COQOS_PRIO < lowest)
  232. {
  233. lowest = conn_info->CONN_COQOS_PRIO;
  234. lowcid = conn_info->CID;
  235. }
  236. conn_info++;
  237. }
  238. printf ("Total bandwidth used: %d\n", bwused);
  239. if (bwused <= TOT_BW_USED)
  240. {
  241. continue;
  242. }
  243. printf ("Bandwidth limit exceeded dropping: %04X\n", lowcid);
  244. Request (plc, "COQOS Modify Connection (suspend)");
  245. memset (message, 0, sizeof (* message));
  246. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  247. QualcommHeader (&request->qualcomm, 0, (VS_CONN_MOD | MMTYPE_REQ));
  248. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  249. mc_request->MOD_CTRL = 0x00;
  250. mc_request->CID = lowcid;
  251. if (SendMME (plc) <= 0)
  252. {
  253. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  254. return (-1);
  255. }
  256. while (ReadMME (plc, 0, (VS_CONN_MOD | MMTYPE_CNF)) <= 0);
  257. if (mc_confirm->MSTATUS)
  258. {
  259. Failure (plc, "Could not suspend stream.");
  260. continue;
  261. }
  262. printf ("Connection ID: %04X suspended\n", lowcid);
  263. sleep (5);
  264. /* flush out old data */
  265. while (ReadMME (plc, 0, (VS_CONN_INFO | MMTYPE_IND)) > 0);
  266. }
  267. return (0);
  268. }
  269. /*====================================================================*
  270. *
  271. * int main (int argc, char const * argv[]);
  272. *
  273. *
  274. *--------------------------------------------------------------------*/
  275. int main (int argc, char const * argv [])
  276. {
  277. extern struct channel channel;
  278. static char const * optv [] =
  279. {
  280. "ei:qv",
  281. "limit [device] [...]",
  282. "CoQos Stream Utility",
  283. "e\tredirect stderr to stdout",
  284. #if defined (WINPCAP) || defined (LIBPCAP)
  285. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  286. #else
  287. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  288. #endif
  289. "q\tquiet mode",
  290. "v\tverbose mode",
  291. (char const *) (0)
  292. };
  293. #include "../plc/plc.c"
  294. uint16_t limit;
  295. signed c;
  296. if (getenv (PLCDEVICE))
  297. {
  298. #if defined (WINPCAP) || defined (LIBPCAP)
  299. channel.ifindex = atoi (getenv (PLCDEVICE));
  300. #else
  301. channel.ifname = strdup (getenv (PLCDEVICE));
  302. #endif
  303. }
  304. optind = 1;
  305. while ((c = getoptv (argc, argv, optv)) != -1)
  306. {
  307. switch (c)
  308. {
  309. case 'e':
  310. dup2 (STDOUT_FILENO, STDERR_FILENO);
  311. break;
  312. case 'i':
  313. #if defined (WINPCAP) || defined (LIBPCAP)
  314. channel.ifindex = atoi (optarg);
  315. #else
  316. channel.ifname = optarg;
  317. #endif
  318. break;
  319. case 'q':
  320. _setbits (channel.flags, CHANNEL_SILENCE);
  321. _setbits (plc.flags, PLC_SILENCE);
  322. break;
  323. case 'v':
  324. _setbits (channel.flags, CHANNEL_VERBOSE);
  325. _setbits (plc.flags, PLC_VERBOSE);
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. argc -= optind;
  332. argv += optind;
  333. if (!argc)
  334. {
  335. error (1, ECANCELED, "No limit given");
  336. }
  337. limit = (uint16_t)(uintspec (* argv++, 10, 90000));
  338. argc--;
  339. openchannel (&channel);
  340. if (!(plc.message = malloc (sizeof (* plc.message))))
  341. {
  342. error (1, errno, PLC_NOMEMORY);
  343. }
  344. if (!argc)
  345. {
  346. con_man (&plc, limit);
  347. }
  348. while ((argc) && (* argv))
  349. {
  350. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  351. {
  352. error (1, errno, PLC_BAD_MAC, * argv);
  353. }
  354. con_man (&plc, limit);
  355. argc--;
  356. argv++;
  357. }
  358. free (plc.message);
  359. closechannel (&channel);
  360. exit (0);
  361. }