SSID: Respect ASCII character Label.
[tomato.git] / release / src-rt / emf / emfconf / emfu.c
blob6e2c82911085b728762828eee2f0055bfc07221d
1 /*
2 * EMFL Command Line Utility: This utility can be used to start/stop
3 * EMF, enable/disable BSS forwarding, add/delete/list the Static
4 * MFDB entries.
6 * Copyright (C) 2010, Broadcom Corporation
7 * All Rights Reserved.
8 *
9 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
10 * the contents of this file may not be disclosed to third parties, copied
11 * or duplicated in any form, in whole or in part, without the prior
12 * written permission of Broadcom Corporation.
14 * $Id: emfu.c 241182 2011-02-17 21:50:03Z gmo $
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <typedefs.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <netinet/in.h>
25 #include <shutils.h>
26 #include <emf_cfg.h>
27 #include "emfu.h"
28 #if defined(linux)
29 #include "emfu_linux.h"
30 #include <arpa/inet.h>
31 #else /* defined(vxworks) */
32 #error "Unsupported osl"
33 #endif
35 #define MAX_DATA_SIZE sizeof(emf_cfg_request_t)
37 static int emf_cfg_enable(char *argv[]);
38 static int emf_cfg_disable(char *argv[]);
39 static int emf_cfg_status_get(char *argv[]);
40 static int emf_cfg_bridge_add(char *argv[]);
41 static int emf_cfg_bridge_del(char *argv[]);
42 static int emf_cfg_iface_add(char *argv[]);
43 static int emf_cfg_iface_del(char *argv[]);
44 static int emf_cfg_iface_list(char *argv[]);
45 static int emf_cfg_uffp_add(char *argv[]);
46 static int emf_cfg_uffp_del(char *argv[]);
47 static int emf_cfg_uffp_list(char *argv[]);
48 static int emf_cfg_mfdb_add(char *argv[]);
49 static int emf_cfg_mfdb_del(char *argv[]);
50 static int emf_cfg_mfdb_clear(char *argv[]);
51 static int emf_cfg_mfdb_list(char *argv[]);
52 static int emf_cfg_stats_show(char *argv[]);
54 static emf_cmd_arg_t cmd_args[] =
57 "start",
58 NULL,
59 emf_cfg_enable,
60 EMF_ARGC_ENABLE_FWD
63 "stop",
64 NULL,
65 emf_cfg_disable,
66 EMF_ARGC_DISABLE_FWD
69 "status",
70 NULL,
71 emf_cfg_status_get,
72 EMF_ARGC_GET_FWD
75 "add",
76 "bridge",
77 emf_cfg_bridge_add,
78 EMF_ARGC_ADD_BRIDGE
81 "del",
82 "bridge",
83 emf_cfg_bridge_del,
84 EMF_ARGC_DEL_BRIDGE
87 "add",
88 "iface",
89 emf_cfg_iface_add,
90 EMF_ARGC_ADD_IF
93 "del",
94 "iface",
95 emf_cfg_iface_del,
96 EMF_ARGC_DEL_IF
99 "list",
100 "iface",
101 emf_cfg_iface_list,
102 EMF_ARGC_LIST_IF
105 "add",
106 "uffp",
107 emf_cfg_uffp_add,
108 EMF_ARGC_ADD_UFFP
111 "del",
112 "uffp",
113 emf_cfg_uffp_del,
114 EMF_ARGC_DEL_UFFP
117 "list",
118 "uffp",
119 emf_cfg_uffp_list,
120 EMF_ARGC_LIST_UFFP
123 "add",
124 "rtport",
125 emf_cfg_uffp_add,
126 EMF_ARGC_ADD_RTPORT
129 "del",
130 "rtport",
131 emf_cfg_uffp_del,
132 EMF_ARGC_DEL_RTPORT
135 "list",
136 "rtport",
137 emf_cfg_uffp_list,
138 EMF_ARGC_LIST_RTPORT
141 "add",
142 "mfdb",
143 emf_cfg_mfdb_add,
144 EMF_ARGC_ADD_MFDB
147 "del",
148 "mfdb",
149 emf_cfg_mfdb_del,
150 EMF_ARGC_DEL_MFDB
153 "clear",
154 "mfdb",
155 emf_cfg_mfdb_clear,
156 EMF_ARGC_CLEAR_MFDB
159 "list",
160 "mfdb",
161 emf_cfg_mfdb_list,
162 EMF_ARGC_LIST_MFDB
165 "show",
166 "stats",
167 emf_cfg_stats_show,
168 EMF_ARGC_SHOW_STATS
172 static void
173 emf_usage(FILE *fid)
175 fprintf(fid, EMF_USAGE);
176 return;
179 static int
180 emf_cfg_enable(char *argv[])
182 emf_cfg_request_t req;
184 bzero((char *)&req, sizeof(emf_cfg_request_t));
186 strcpy((char *)req.inst_id, argv[2]);
187 req.command_id = EMFCFG_CMD_EMF_ENABLE;
188 req.size = sizeof(bool);
189 req.oper_type = EMFCFG_OPER_TYPE_SET;
190 *(bool *)req.arg = TRUE;
192 /* Send request to kernel */
193 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
195 fprintf(stderr, "Unable to send request to EMF\n");
196 return (FAILURE);
199 if (req.status != EMFCFG_STATUS_SUCCESS)
201 fprintf(stderr, "%s\n", req.arg);
202 return (FAILURE);
205 return (SUCCESS);
208 static int
209 emf_cfg_disable(char *argv[])
211 emf_cfg_request_t req;
213 bzero((char *)&req, sizeof(emf_cfg_request_t));
215 strcpy((char *)req.inst_id, argv[2]);
216 req.command_id = EMFCFG_CMD_EMF_ENABLE;
217 req.size = sizeof(bool);
218 req.oper_type = EMFCFG_OPER_TYPE_SET;
219 *(bool *)req.arg = FALSE;
221 /* Send request to kernel */
222 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
224 fprintf(stderr, "Unable to send request to EMF\n");
225 return (FAILURE);
228 if (req.status != EMFCFG_STATUS_SUCCESS)
230 fprintf(stderr, "%s\n", req.arg);
231 return (FAILURE);
234 return (SUCCESS);
237 static int
238 emf_cfg_status_get(char *argv[])
240 emf_cfg_request_t req;
242 bzero((char *)&req, sizeof(emf_cfg_request_t));
244 strcpy((char *)req.inst_id, argv[2]);
245 req.command_id = EMFCFG_CMD_EMF_ENABLE;
246 req.oper_type = EMFCFG_OPER_TYPE_GET;
248 /* Send request to kernel */
249 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
251 fprintf(stderr, "Unable to send request to EMF\n");
252 return (FAILURE);
255 if (req.status != EMFCFG_STATUS_SUCCESS)
257 fprintf(stderr, "%sUnable to read Multicast forwarding status\n", req.arg);
258 return (FAILURE);
261 fprintf(stdout, "Multicast forwarding status: %s",
262 (*(bool *)req.arg ? "ENABLED\n" : "DISABLED\n"));
264 return (SUCCESS);
267 static int
268 emf_cfg_bridge_add(char *argv[])
270 emf_cfg_request_t req;
272 bzero((char *)&req, sizeof(emf_cfg_request_t));
274 strcpy((char *)req.inst_id, argv[3]);
275 req.command_id = EMFCFG_CMD_BR_ADD;
276 req.oper_type = EMFCFG_OPER_TYPE_SET;
278 /* Send request to kernel */
279 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
281 fprintf(stderr, "Unable to send request to EMF\n");
282 return (FAILURE);
285 if (req.status != EMFCFG_STATUS_SUCCESS)
287 fprintf(stderr, "%s\n", req.arg);
288 return (FAILURE);
291 return (SUCCESS);
294 static int
295 emf_cfg_bridge_del(char *argv[])
297 emf_cfg_request_t req;
299 bzero((char *)&req, sizeof(emf_cfg_request_t));
301 strcpy((char *)req.inst_id, argv[3]);
302 req.command_id = EMFCFG_CMD_BR_DEL;
303 req.oper_type = EMFCFG_OPER_TYPE_SET;
305 /* Send request to kernel */
306 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
308 fprintf(stderr, "Unable to send request to EMF\n");
309 return (FAILURE);
312 if (req.status != EMFCFG_STATUS_SUCCESS)
314 fprintf(stderr, "%s\n", req.arg);
315 return (FAILURE);
318 return (SUCCESS);
321 static int
322 emf_cfg_iface_add(char *argv[])
324 emf_cfg_request_t req;
325 emf_cfg_if_t *cfg_if;
327 bzero((char *)&req, sizeof(emf_cfg_request_t));
329 strcpy((char *)req.inst_id, argv[3]);
330 req.command_id = EMFCFG_CMD_IF_ADD;
331 req.oper_type = EMFCFG_OPER_TYPE_SET;
333 cfg_if = (emf_cfg_if_t *)req.arg;
334 strncpy((char *)cfg_if->if_name, argv[4], 16);
335 cfg_if->if_name[15] = 0;
337 /* Send request to kernel */
338 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
340 fprintf(stderr, "Unable to send request to EMF\n");
341 return (FAILURE);
344 if (req.status != EMFCFG_STATUS_SUCCESS)
346 fprintf(stderr, "%s\n", req.arg);
347 return (FAILURE);
350 return (SUCCESS);
353 static int
354 emf_cfg_iface_del(char *argv[])
356 emf_cfg_request_t req;
357 emf_cfg_if_t *cfg_if;
359 bzero((char *)&req, sizeof(emf_cfg_request_t));
361 strcpy((char *)req.inst_id, argv[3]);
362 req.command_id = EMFCFG_CMD_IF_DEL;
363 req.oper_type = EMFCFG_OPER_TYPE_SET;
365 cfg_if = (emf_cfg_if_t *)req.arg;
366 strncpy((char *)cfg_if->if_name, argv[4], 16);
367 cfg_if->if_name[15] = 0;
369 /* Send request to kernel */
370 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
372 fprintf(stderr, "Unable to send request to EMF\n");
373 return (FAILURE);
376 if (req.status != EMFCFG_STATUS_SUCCESS)
378 fprintf(stderr, "%s\n", req.arg);
379 return (FAILURE);
382 return (SUCCESS);
385 static int
386 emf_cfg_iface_list(char *argv[])
388 emf_cfg_request_t req;
389 emf_cfg_if_list_t *list;
390 int32 i;
392 bzero((char *)&req, sizeof(emf_cfg_request_t));
394 strcpy((char *)req.inst_id, argv[3]);
395 req.command_id = EMFCFG_CMD_IF_LIST;
396 req.oper_type = EMFCFG_OPER_TYPE_GET;
397 req.size = sizeof(req.arg);
399 /* Send request to kernel */
400 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
402 fprintf(stderr, "Unable to send request to EMF\n");
403 return (FAILURE);
406 if (req.status != EMFCFG_STATUS_SUCCESS)
408 fprintf(stderr, "%s\n", req.arg);
409 return (FAILURE);
412 fprintf(stdout, "EMF enabled interfaces on %s: ", argv[3]);
414 list = (emf_cfg_if_list_t *)req.arg;
415 for (i = 0; i < list->num_entries; i++)
417 fprintf(stdout, "%s ", list->if_entry[i].if_name);
420 fprintf(stdout, "\n");
422 return (SUCCESS);
425 static int
426 emf_cfg_uffp_add(char *argv[])
428 emf_cfg_request_t req;
429 emf_cfg_uffp_t *cfg_uffp;
430 emf_cfg_rtport_t *cfg_rtport;
432 bzero((char *)&req, sizeof(emf_cfg_request_t));
434 strcpy((char *)req.inst_id, argv[3]);
436 if (strncmp(argv[2], "uffp", 4) == 0)
438 req.command_id = EMFCFG_CMD_UFFP_ADD;
439 cfg_uffp = (emf_cfg_uffp_t *)req.arg;
440 strncpy((char *)cfg_uffp->if_name, argv[4], 16);
441 cfg_uffp->if_name[15] = 0;
443 else
445 req.command_id = EMFCFG_CMD_RTPORT_ADD;
446 cfg_rtport = (emf_cfg_rtport_t *)req.arg;
447 strncpy((char *)cfg_rtport->if_name, argv[4], 16);
448 cfg_rtport->if_name[15] = 0;
451 req.oper_type = EMFCFG_OPER_TYPE_SET;
453 /* Send request to kernel */
454 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
456 fprintf(stderr, "Unable to send request to EMF\n");
457 return (FAILURE);
460 if (req.status != EMFCFG_STATUS_SUCCESS)
462 fprintf(stderr, "%s\n", req.arg);
463 return (FAILURE);
466 return (SUCCESS);
469 static int
470 emf_cfg_uffp_del(char *argv[])
472 emf_cfg_request_t req;
473 emf_cfg_uffp_t *cfg_uffp;
474 emf_cfg_rtport_t *cfg_rtport;
476 bzero((char *)&req, sizeof(emf_cfg_request_t));
478 strcpy((char *)req.inst_id, argv[3]);
480 if (strncmp(argv[2], "uffp", 4) == 0)
482 req.command_id = EMFCFG_CMD_UFFP_DEL;
483 cfg_uffp = (emf_cfg_uffp_t *)req.arg;
484 strncpy((char *)cfg_uffp->if_name, argv[4], 16);
485 cfg_uffp->if_name[15] = 0;
487 else
489 req.command_id = EMFCFG_CMD_RTPORT_DEL;
490 cfg_rtport = (emf_cfg_rtport_t *)req.arg;
491 strncpy((char *)cfg_rtport->if_name, argv[4], 16);
492 cfg_rtport->if_name[15] = 0;
495 req.oper_type = EMFCFG_OPER_TYPE_SET;
497 /* Send request to kernel */
498 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
500 fprintf(stderr, "Unable to send request to EMF\n");
501 return (FAILURE);
504 if (req.status != EMFCFG_STATUS_SUCCESS)
506 fprintf(stderr, "%s\n", req.arg);
507 return (FAILURE);
510 return (SUCCESS);
513 static int
514 emf_cfg_uffp_list(char *argv[])
516 emf_cfg_request_t req;
517 emf_cfg_uffp_list_t *uffp_list;
518 emf_cfg_rtport_list_t *rtport_list;
519 int32 i;
521 bzero((char *)&req, sizeof(emf_cfg_request_t));
523 strcpy((char *)req.inst_id, argv[3]);
525 if (strncmp(argv[2], "uffp", 4) == 0)
526 req.command_id = EMFCFG_CMD_UFFP_LIST;
527 else
528 req.command_id = EMFCFG_CMD_RTPORT_LIST;
530 req.oper_type = EMFCFG_OPER_TYPE_GET;
531 req.size = sizeof(req.arg);
533 /* Send request to kernel */
534 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
536 fprintf(stderr, "Unable to send request to EMF\n");
537 return (FAILURE);
540 if (req.status != EMFCFG_STATUS_SUCCESS)
542 fprintf(stderr, "%s\n", req.arg);
543 return (FAILURE);
546 if (strncmp(argv[2], "uffp", 4) == 0)
548 fprintf(stdout, "UFFP list of %s: ", argv[3]);
550 uffp_list = (emf_cfg_uffp_list_t *)req.arg;
551 for (i = 0; i < uffp_list->num_entries; i++)
553 fprintf(stdout, "%s ", uffp_list->uffp_entry[i].if_name);
556 else
558 fprintf(stdout, "RTPORT list of %s: ", argv[3]);
560 rtport_list = (emf_cfg_rtport_list_t *)req.arg;
561 for (i = 0; i < rtport_list->num_entries; i++)
563 fprintf(stdout, "%s ", rtport_list->rtport_entry[i].if_name);
567 fprintf(stdout, "\n");
569 return (SUCCESS);
572 static int
573 emf_cfg_mfdb_add(char *argv[])
575 emf_cfg_mfdb_t *mfdb_entry;
576 emf_cfg_request_t req;
577 struct in_addr mgrp_ip;
579 bzero((char *)&req, sizeof(emf_cfg_request_t));
581 strcpy((char *)req.inst_id, argv[3]);
582 req.command_id = EMFCFG_CMD_MFDB_ADD;
583 req.oper_type = EMFCFG_OPER_TYPE_SET;
585 /* Convert the multicast group address to binary data */
586 mfdb_entry = (emf_cfg_mfdb_t *)req.arg;
587 if (inet_aton(argv[4], &mgrp_ip) == 0)
589 fprintf(stderr, "Invalid group address\n");
590 return (FAILURE);
593 mfdb_entry->mgrp_ip = ntohl(mgrp_ip.s_addr);
594 strncpy((char *)mfdb_entry->if_name, argv[5], 16);
596 /* Send request to kernel */
597 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
599 fprintf(stderr, "Unable to send request to EMF\n");
600 return (FAILURE);
603 if (req.status != EMFCFG_STATUS_SUCCESS)
605 fprintf(stderr, "%sMFDB add failed\n", req.arg);
606 return (FAILURE);
609 return (SUCCESS);
612 static int
613 emf_cfg_mfdb_del(char *argv[])
615 emf_cfg_mfdb_t *mfdb_entry;
616 emf_cfg_request_t req;
617 struct in_addr mgrp_ip;
619 bzero((char *)&req, sizeof(emf_cfg_request_t));
621 strcpy((char *)req.inst_id, argv[3]);
622 req.command_id = EMFCFG_CMD_MFDB_DEL;
623 req.oper_type = EMFCFG_OPER_TYPE_SET;
625 /* Convert the multicast group address to binary data */
626 mfdb_entry = (emf_cfg_mfdb_t *)req.arg;
627 if (inet_aton(argv[4], &mgrp_ip) == 0)
629 fprintf(stderr, "Invalid group address\n");
630 return (FAILURE);
633 mfdb_entry->mgrp_ip = ntohl(mgrp_ip.s_addr);
634 strncpy((char *)mfdb_entry->if_name, argv[5], 16);
636 /* Send request to kernel */
637 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
639 fprintf(stderr, "Unable to send request to EMF\n");
640 return (FAILURE);
643 if (req.status != EMFCFG_STATUS_SUCCESS)
645 fprintf(stderr, "%sMFDB delete failed\n", req.arg);
646 return (FAILURE);
649 return (SUCCESS);
652 static int
653 emf_cfg_mfdb_clear(char *argv[])
655 emf_cfg_request_t req;
657 bzero((char *)&req, sizeof(emf_cfg_request_t));
659 strcpy((char *)req.inst_id, argv[3]);
660 req.command_id = EMFCFG_CMD_MFDB_CLEAR;
661 req.oper_type = EMFCFG_OPER_TYPE_SET;
663 /* Send request to kernel */
664 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
666 fprintf(stderr, "Unable to send request to EMF\n");
667 return (FAILURE);
670 if (req.status != EMFCFG_STATUS_SUCCESS)
672 fprintf(stderr, "%sMFDB clear failed\n", req.arg);
673 return (FAILURE);
676 return (SUCCESS);
679 static int
680 emf_cfg_mfdb_list(char *argv[])
682 emf_cfg_request_t req;
683 emf_cfg_mfdb_list_t *list;
684 int32 i;
686 strcpy((char *)req.inst_id, argv[3]);
687 req.command_id = EMFCFG_CMD_MFDB_LIST;
688 req.oper_type = EMFCFG_OPER_TYPE_GET;
689 req.size = sizeof(req.arg);
690 list = (emf_cfg_mfdb_list_t *)req.arg;
692 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
694 fprintf(stderr, "Unable to send request to EMF\n");
695 return (FAILURE);
698 if (req.status != EMFCFG_STATUS_SUCCESS)
700 fprintf(stderr, "Unable to get the MFBD list\n");
701 return (FAILURE);
704 fprintf(stdout, "Group Interface Pkts\n");
706 for (i = 0; i < list->num_entries; i++)
708 fprintf(stdout, "%08x ", list->mfdb_entry[i].mgrp_ip);
709 fprintf(stdout, "%-15s", list->mfdb_entry[i].if_name);
710 fprintf(stdout, "%d\n", list->mfdb_entry[i].pkts_fwd);
713 return (SUCCESS);
716 static int
717 emf_cfg_stats_show(char *argv[])
719 emf_cfg_request_t req;
720 emf_stats_t *emfs;
722 strcpy((char *)req.inst_id, argv[3]);
723 req.command_id = EMFCFG_CMD_EMF_STATS;
724 req.oper_type = EMFCFG_OPER_TYPE_GET;
725 req.size = sizeof(emf_stats_t);
726 emfs = (emf_stats_t *)req.arg;
728 if (emf_cfg_request_send(&req, sizeof(emf_cfg_request_t)) < 0)
730 fprintf(stderr, "Unable to send request to EMF\n");
731 return (FAILURE);
734 if (req.status != EMFCFG_STATUS_SUCCESS)
736 fprintf(stderr, "Unable to get the EMF stats\n");
737 return (FAILURE);
740 fprintf(stdout, "McastDataPkts McastDataFwd McastFlooded "
741 "McastDataSentUp McastDataDropped\n");
742 fprintf(stdout, "%-15d %-15d %-15d %-15d %d\n",
743 emfs->mcast_data_frames, emfs->mcast_data_fwd,
744 emfs->mcast_data_flooded, emfs->mcast_data_sentup,
745 emfs->mcast_data_dropped);
746 fprintf(stdout, "IgmpPkts IgmpPktsFwd "
747 "IgmpPktsSentUp MFDBCacheHits MFDBCacheMisses\n");
748 fprintf(stdout, "%-15d %-15d %-15d %-15d %d\n",
749 emfs->igmp_frames, emfs->igmp_frames_fwd,
750 emfs->igmp_frames_sentup, emfs->mfdb_cache_hits,
751 emfs->mfdb_cache_misses);
753 return (SUCCESS);
757 #if defined(linux)
758 main(int argc, char *argv[])
759 #else /* defined(vxworks) */
760 #error "Unsupported osl"
761 #endif
763 int j, ret;
764 bool cmd_syntax = FALSE;
766 if (argc < 2)
768 emf_usage(stdout);
769 return (SUCCESS);
772 /* Find the command type */
773 for (j = 0; j < sizeof(cmd_args)/sizeof(cmd_args[0]); j++)
775 if ((strcmp(argv[1], cmd_args[j].cmd_oper_str) == 0) &&
776 (argc - 1 == cmd_args[j].arg_count) &&
777 ((cmd_args[j].cmd_id_str == NULL) ||
778 ((argv[2] != NULL) &&
779 (strcmp(argv[2], cmd_args[j].cmd_id_str) == 0))))
781 cmd_syntax = TRUE;
782 break;
786 if (!cmd_syntax)
788 emf_usage(stdout);
789 return (SUCCESS);
792 /* Call the command processing function. This function parses
793 * prepare command request and sends it kernel.
795 if ((ret = cmd_args[j].input(argv)) < 0)
797 return (FAILURE);
800 return (SUCCESS);