PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/mv_eth_tool.c

https://bitbucket.org/davecheney/2.6.22.18-netgear
C | 487 lines | 453 code | 34 blank | 0 comment | 147 complexity | aa2795fada12379c620a46594d39a16d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0, CC-BY-SA-3.0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mv_eth_proc.h"
  4. extern char **environ; /* all environments */
  5. static unsigned int port = 0, q = 0, weight = 0, status = 0, mac[6] = {0,};
  6. static unsigned int policy =0, command = 0, packet = 0;
  7. static unsigned int value = 0;
  8. static unsigned int inport, outport, dip, sip, da[6] = {0, }, sa[6] = {0, };
  9. static unsigned int db_type = 0;
  10. void show_usage(int badarg)
  11. {
  12. fprintf(stderr,
  13. "Usage: \n"
  14. " mv_eth_tool -h Display this help \n"
  15. " \n"
  16. " --- Global commands --- \n"
  17. " mv_eth_tool -txdone <quota> Set threshold to start tx_done operations \n"
  18. " mv_eth_tool -skb <0|1> SKB reuse support: 0 - disabled, 1 - enabled \n"
  19. " \n"
  20. " --- Port commands --- \n"
  21. " mv_eth_tool -rxcoal <port> <usec> Set RX interrupt coalescing value \n"
  22. " mv_eth_tool -txcoal <port> <usec> Set TX interrupt coalescing value \n"
  23. " mv_eth_tool -txen <port> <deep> Set deep of lookup for TX enable race. \n"
  24. " 0 - means disable. \n"
  25. " mv_eth_tool -ejp <port> <0|1> Set EJP mode: 0 - Disable, 1 - Enable \n"
  26. " mv_eth_tool -tx_noq <idx> <0|1> Set queuing discipline mode: 0 - Disable, 1 - Enable \n"
  27. " \n"
  28. " --- Port/Queue commands --- \n"
  29. " mv_eth_tool -tos <port> <rxq> <tos> Map TOS field to RX queue number \n"
  30. " \n"
  31. " mv_eth_tool -srq <port> <rxq> <bpdu|arp|tcp|udp> \n"
  32. " Set RX queue number for different packet types. \n"
  33. " rxq==8 means no special treatment. \n"
  34. " rxq==8 for Arp packets means drop. \n"
  35. " \n"
  36. " mv_eth_tool -sq <port> <rxq> <%%2x:%%2x:%%2x:%%2x:%%2x:%%2x> \n"
  37. " Set RX queue number for a Multicast MAC \n"
  38. " rxq==8 indicates delete entry. \n"
  39. " mv_eth_tool -srp <port> <WRR|FIXED> Set the Rx Policy to WRR or Fixed \n"
  40. " \n"
  41. " --- NFP commands --- \n"
  42. " mv_eth_tool -fprs <inp> <outp> <DIP> <SIP> <DA> <SA> \n"
  43. " Set NFP rule for IPv4 routing \n"
  44. " where DA, SA - MAC addresses xx:xx:xx:xx:xx:xx \n"
  45. " mv_eth_tool -fprd <DIP> <SIP> Delete NFP Rule for IPv4 routing \n"
  46. " mv_eth_tool -fp_dis Disable Network Fast Processing \n"
  47. " mv_eth_tool -fp_en Enable Network Fast Processing \n"
  48. " mv_eth_tool -fp_st Display NFP Status (enabled/disabled) \n"
  49. " mv_eth_tool -fp_print <DB> Print NFP Rule Database, \n"
  50. " where DB is routing, nat or bridge \n"
  51. " \n"
  52. " mv_eth_tool -St <option> <port> \n"
  53. " Display different status information of the port through the kernel printk. \n"
  54. " OPTIONS: \n"
  55. " p Display General port information. \n"
  56. " mac Display MAC addresses information \n"
  57. " q <0..7> Display specific q information. \n"
  58. " rxp Display Rx Policy information. \n"
  59. " txp Display Tx Policy information. \n"
  60. " cntrs Display the HW MIB counters \n"
  61. " regs Display a dump of the giga registers \n"
  62. " stats Display port statistics information. \n"
  63. " netdev Display net_device status information. \n"
  64. " nfp Display port NFP statistics \n"
  65. " tos Display port TOS to RXQ mapping \n"
  66. " switch Display switch statistics \n"
  67. "\n"
  68. );
  69. exit(badarg);
  70. }
  71. static void parse_pt(char *src)
  72. {
  73. if (!strcmp(src, "bpdu"))
  74. packet = PT_BPDU;
  75. else if(!strcmp(src, "arp"))
  76. packet = PT_ARP;
  77. else if(!strcmp(src, "tcp"))
  78. packet = PT_TCP;
  79. else if(!strcmp(src, "udp"))
  80. packet = PT_UDP;
  81. else {
  82. fprintf(stderr, "Illegall packet type, packet type should be bpdu/arp/tcp/udp. \n");
  83. exit(-1);
  84. }
  85. return;
  86. }
  87. static void parse_db_name(char *src)
  88. {
  89. if (!strcmp(src, "routing"))
  90. db_type = DB_ROUTING;
  91. else if (!strcmp(src, "nat"))
  92. db_type = DB_NAT;
  93. else if (!strcmp(src, "fdb") || !strcmp(src, "bridge"))
  94. db_type = DB_FDB;
  95. else {
  96. fprintf(stderr, "Illegall DB name, expected routing | nat | bridge. \n");
  97. exit(-1);
  98. }
  99. return;
  100. }
  101. static void parse_port(char *src)
  102. {
  103. int count;
  104. count = sscanf(src, "%x",&port);
  105. if (count != 1) {
  106. fprintf(stderr, "Port parsing error: count=%d\n", count);
  107. exit(-1);
  108. }
  109. return;
  110. }
  111. static void parse_q(char *src)
  112. {
  113. int count;
  114. count = sscanf(src, "%x",&q);
  115. if (count != 1) {
  116. fprintf(stderr, "Queue parsing error: count=%d\n", count);
  117. exit(-1);
  118. }
  119. return;
  120. }
  121. static void parse_policy(char *src)
  122. {
  123. if (!strcmp(src, "WRR"))
  124. policy = WRR;
  125. else if (!strcmp(src, "FIXED"))
  126. policy = FIXED;
  127. else {
  128. fprintf(stderr, "Illegall policy, policy can be WRR or Fixed.\n");
  129. exit(-1);
  130. }
  131. return;
  132. }
  133. static void parse_status(char *src)
  134. {
  135. if (!strcmp(src, "p")) {
  136. status = STS_PORT;
  137. }
  138. else if (!strcmp(src, "mac")) {
  139. status = STS_PORT_MAC;
  140. }
  141. else if(!strcmp(src, "q")) {
  142. status = STS_PORT_Q;
  143. }
  144. else if(!strcmp(src, "rxp")) {
  145. status = STS_PORT_RXP;
  146. }
  147. else if(!strcmp(src, "txp")) {
  148. status = STS_PORT_TXP;
  149. }
  150. else if(!strcmp(src, "cntrs")) {
  151. status = STS_PORT_MIB;
  152. }
  153. else if(!strcmp(src, "regs")) {
  154. status = STS_PORT_REGS;
  155. }
  156. else if(!strcmp(src, "stats")) {
  157. status = STS_PORT_STATS;
  158. }
  159. else if(!strcmp(src, "nfp")) {
  160. status = STS_PORT_NFP_STATS;
  161. }
  162. else if(!strcmp(src, "netdev")) {
  163. status = STS_NETDEV;
  164. }
  165. else if(!strcmp(src, "tos")) {
  166. status = STS_PORT_TOS_MAP;
  167. }
  168. else if(!strcmp(src, "switch")) {
  169. status = STS_SWITCH_STATS;
  170. }
  171. else {
  172. fprintf(stderr, "Illegall ststus %d.\n");
  173. exit(-1);
  174. }
  175. return;
  176. }
  177. static void parse_dec_val(char *src, unsigned int* val_ptr)
  178. {
  179. int i, count;
  180. count = sscanf(src, "%d", val_ptr);
  181. if(count != 1) {
  182. fprintf(stderr, "Illegall value - should be decimal.\n");
  183. exit(-1);
  184. }
  185. return;
  186. }
  187. static void parse_hex_val(char *src, unsigned int* hex_val_ptr)
  188. {
  189. int i, count;
  190. count = sscanf(src, "%x", hex_val_ptr);
  191. if(count != 1) {
  192. fprintf(stderr, "Illegall value - should be hex.\n");
  193. exit(-1);
  194. }
  195. return;
  196. }
  197. static int parse_mac(char *src, unsigned int macaddr[])
  198. {
  199. int count;
  200. int i;
  201. int buf[6];
  202. count = sscanf(src, "%2x:%2x:%2x:%2x:%2x:%2x",
  203. &buf[0], &buf[1], &buf[2], &buf[3], &buf[4], &buf[5]);
  204. if (count != 6) {
  205. fprintf(stderr, "MAC parsing error: Expected %%2x:%%2x:%%2x:%%2x:%%2x:%%2x.\n");
  206. exit(-1);
  207. }
  208. for (i = 0; i < count; i++) {
  209. macaddr[i] = buf[i];
  210. }
  211. return 0;
  212. }
  213. static int parse_ip(char *src, unsigned int* ip)
  214. {
  215. int count, i;
  216. int buf[4];
  217. count = sscanf(src, "%d.%d.%d.%d",
  218. &buf[0], &buf[1], &buf[2], &buf[3]);
  219. if (count != 4) {
  220. fprintf(stderr, "Illegall IP address (should be %%d.%%d.%%d.%%d)\n");
  221. exit(-1);
  222. }
  223. *ip = (((buf[0] & 0xFF) << 24) | ((buf[1] & 0xFF) << 16) |
  224. ((buf[2] & 0xFF) << 8) | ((buf[3] & 0xFF) << 0));
  225. return 0;
  226. }
  227. static void parse_cmdline(int argc, char **argp)
  228. {
  229. unsigned int i = 1;
  230. if(argc < 2) {
  231. show_usage(1);
  232. }
  233. if (!strcmp(argp[i], "-h")) {
  234. show_usage(0);
  235. }
  236. else if (!strcmp(argp[i], "-srq")) {
  237. command = COM_SRQ;
  238. i++;
  239. if(argc != 5)
  240. show_usage(1);
  241. parse_port(argp[i++]);
  242. parse_q(argp[i++]);
  243. parse_pt(argp[i++]);
  244. }
  245. else if (!strcmp(argp[i], "-sq")) {
  246. command = COM_SQ;
  247. i++;
  248. if(argc != 6)
  249. show_usage(1);
  250. parse_port(argp[i++]);
  251. parse_q(argp[i++]);
  252. parse_mac(argp[i++], mac);
  253. }
  254. else if (!strcmp(argp[i], "-srp")) {
  255. command = COM_SRP;
  256. i++;
  257. if(argc != 4)
  258. show_usage(1);
  259. parse_port(argp[i++]);
  260. parse_policy(argp[i++]);
  261. }
  262. else if (!strcmp(argp[i], "-srqw")) {
  263. command = COM_SRQW;
  264. i++;
  265. if(argc != 5)
  266. show_usage(1);
  267. parse_port(argp[i++]);
  268. parse_q(argp[i++]);
  269. parse_hex_val(argp[i++], &weight);
  270. }
  271. else if (!strcmp(argp[i], "-stp")) {
  272. command = COM_STP;
  273. i++;
  274. if(argc != 6)
  275. show_usage(1);
  276. parse_port(argp[i++]);
  277. parse_q(argp[i++]);
  278. parse_hex_val(argp[i++], &weight);
  279. parse_policy(argp[i++]);
  280. }
  281. else if (!strcmp(argp[i], "-fprs")) {
  282. command = COM_IP_RULE_SET;
  283. i++;
  284. if(argc != 8)
  285. show_usage(1);
  286. parse_dec_val(argp[i++], &inport);
  287. parse_dec_val(argp[i++], &outport);
  288. parse_ip(argp[i++], &dip);
  289. parse_ip(argp[i++], &sip);
  290. parse_mac(argp[i++], da);
  291. parse_mac(argp[i++], sa);
  292. }
  293. else if (!strcmp(argp[i], "-fprd")) {
  294. command = COM_IP_RULE_DEL;
  295. i++;
  296. if(argc != 4)
  297. show_usage(1);
  298. parse_ip(argp[i++], &dip);
  299. parse_ip(argp[i++], &sip);
  300. }
  301. else if (!strcmp(argp[i], "-fp_dis")) {
  302. command = COM_FP_DISABLE;
  303. if(argc != 2)
  304. show_usage(1);
  305. }
  306. else if (!strcmp(argp[i], "-fp_en")) {
  307. command = COM_FP_ENABLE;
  308. if(argc != 2)
  309. show_usage(1);
  310. }
  311. else if (!strcmp(argp[i], "-fp_st")) {
  312. command = COM_FP_STATUS;
  313. if(argc != 2)
  314. show_usage(1);
  315. }
  316. else if (!strcmp(argp[i], "-fp_print")) {
  317. command = COM_FP_PRINT;
  318. i++;
  319. if (argc != 3)
  320. show_usage(1);
  321. parse_db_name(argp[i++]);
  322. }
  323. else if (!strcmp(argp[i], "-txdone")) {
  324. command = COM_TXDONE_Q;
  325. i++;
  326. if(argc != 3)
  327. show_usage(1);
  328. parse_dec_val(argp[i++], &value);
  329. }
  330. else if (!strcmp(argp[i], "-txen")) {
  331. command = COM_TX_EN;
  332. i++;
  333. if(argc != 4)
  334. show_usage(1);
  335. parse_port(argp[i++]);
  336. parse_dec_val(argp[i++], &value);
  337. }
  338. else if (!strcmp(argp[i], "-skb")) {
  339. command = COM_SKB_REUSE;
  340. i++;
  341. if(argc != 3)
  342. show_usage(1);
  343. parse_dec_val(argp[i++], &value);
  344. }
  345. else if (!strcmp(argp[i], "-rxcoal")) {
  346. command = COM_RX_COAL;
  347. i++;
  348. if(argc != 4)
  349. show_usage(1);
  350. parse_port(argp[i++]);
  351. parse_dec_val(argp[i++], &value);
  352. }
  353. else if (!strcmp(argp[i], "-txcoal")) {
  354. command = COM_TX_COAL;
  355. i++;
  356. if(argc != 4)
  357. show_usage(1);
  358. parse_port(argp[i++]);
  359. parse_dec_val(argp[i++], &value);
  360. }
  361. else if (!strcmp(argp[i], "-ejp")) {
  362. command = COM_EJP_MODE;
  363. i++;
  364. if(argc != 4)
  365. show_usage(1);
  366. parse_port(argp[i++]);
  367. parse_dec_val(argp[i++], &value);
  368. }
  369. else if (!strcmp(argp[i], "-tos")) {
  370. command = COM_TOS_MAP;
  371. i++;
  372. if(argc != 5)
  373. show_usage(1);
  374. parse_port(argp[i++]);
  375. parse_q(argp[i++]);
  376. parse_hex_val(argp[i++], &value);
  377. }
  378. else if (!strcmp(argp[i], "-tx_noq")) {
  379. command = COM_TX_NOQUEUE;
  380. i++;
  381. if(argc != 4)
  382. show_usage(1);
  383. parse_port(argp[i++]);
  384. parse_dec_val(argp[i++], &value);
  385. }
  386. else if (!strcmp(argp[i], "-St")) {
  387. command = COM_STS;
  388. i++;
  389. if(argc < 4)
  390. show_usage(1);
  391. parse_status(argp[i++]);
  392. if( status == STS_PORT_Q ) {
  393. if(argc != 5)
  394. show_usage(1);
  395. parse_q(argp[i++]);
  396. }
  397. else if(argc != 4)
  398. show_usage(1);
  399. parse_port(argp[i++]);
  400. }
  401. else {
  402. show_usage(i++);
  403. }
  404. }
  405. static int procit(void)
  406. {
  407. FILE *mvethproc;
  408. mvethproc = fopen(FILE_PATH FILE_NAME, "w");
  409. if (!mvethproc) {
  410. printf ("Eror opening file %s/%s\n",FILE_PATH,FILE_NAME);
  411. exit(-1);
  412. }
  413. switch (command) {
  414. case COM_TXDONE_Q:
  415. case COM_SKB_REUSE:
  416. fprintf (mvethproc, ETH_CMD_STRING, ETH_PRINTF_LIST);
  417. break;
  418. case COM_RX_COAL:
  419. case COM_TX_COAL:
  420. case COM_TX_EN:
  421. case COM_EJP_MODE:
  422. case COM_TX_NOQUEUE:
  423. fprintf (mvethproc, PORT_CMD_STRING, PORT_PRINTF_LIST);
  424. break;
  425. case COM_TOS_MAP:
  426. fprintf (mvethproc, QUEUE_CMD_STRING, QUEUE_PRINTF_LIST);
  427. break;
  428. case COM_IP_RULE_SET:
  429. fprintf(mvethproc, IP_RULE_STRING, IP_RULE_PRINT_LIST);
  430. break;
  431. case COM_IP_RULE_DEL:
  432. fprintf(mvethproc, IP_RULE_DEL_STRING, IP_RULE_DEL_PRINT_LIST);
  433. break;
  434. case COM_FP_DISABLE:
  435. case COM_FP_ENABLE:
  436. case COM_FP_STATUS:
  437. fprintf(mvethproc, FP_EN_DIS_STRING, FP_EN_DIS_PRINT_LIST);
  438. break;
  439. case COM_FP_PRINT:
  440. fprintf(mvethproc, FP_DB_PRINT_STRING, FP_DB_PRINT_PRINT_LIST);
  441. break;
  442. default:
  443. fprintf (mvethproc, PROC_STRING, PROC_PRINT_LIST);
  444. break;
  445. }
  446. fclose (mvethproc);
  447. return 0;
  448. }
  449. int main(int argc, char **argp, char **envp)
  450. {
  451. parse_cmdline(argc, argp);
  452. return procit();
  453. }