xref: /f-stack/tools/knictl/knictl.c (revision 552bc48c)
1 #include <unistd.h>
2 #include <strings.h>
3 #include "ff_ipc.h"
4 
5 void
usage(void)6 usage(void)
7 {
8     printf("Usage:\n");
9     printf("  knictl [-p <f-stack proc_id>] [-P <max proc_id>] "
10         "[-a alltokni/alltoff/default][-n]\n    use `-a` to set kni action\n    use `-n` to show \n");
11 }
12 
get_action(const char * c)13 enum FF_KNICTL_CMD get_action(const char *c){
14     if (!c)
15         return FF_KNICTL_ACTION_MAX;
16     if (0 == strcasecmp(c, "alltokni")){
17         return FF_KNICTL_ACTION_ALL_TO_KNI;
18     } else  if (0 == strcasecmp(c, "alltoff")){
19         return FF_KNICTL_ACTION_ALL_TO_FF;
20     } else if (0 == strcasecmp(c, "default")){
21         return FF_KNICTL_ACTION_DEFAULT;
22     } else {
23         return FF_KNICTL_ACTION_MAX;
24     }
25 }
26 
get_action_str(enum FF_KNICTL_CMD cmd)27 const char * get_action_str(enum FF_KNICTL_CMD cmd){
28     switch (cmd)
29     {
30     case FF_KNICTL_ACTION_ALL_TO_KNI:
31         return "alltokni";
32         break;
33     case FF_KNICTL_ACTION_ALL_TO_FF:
34         return "alltoff";
35         break;
36     case FF_KNICTL_ACTION_DEFAULT:
37         return "default";
38         break;
39     default:
40         return "unknown";
41         break;
42     }
43     return "unknown";
44 }
45 
46 
knictl_status(struct ff_knictl_args * knictl)47 int knictl_status(struct ff_knictl_args *knictl){
48     int            ret;
49     struct ff_msg *msg, *retmsg = NULL;
50 
51     msg = ff_ipc_msg_alloc();
52     if (msg == NULL) {
53         errno = ENOMEM;
54         return -1;
55     }
56 
57     msg->msg_type = FF_KNICTL;
58     msg->knictl = *knictl;
59     ret = ff_ipc_send(msg);
60     if (ret < 0) {
61         errno = EPIPE;
62         ff_ipc_msg_free(msg);
63         return -1;
64     }
65 
66     do {
67         if (retmsg != NULL) {
68             ff_ipc_msg_free(retmsg);
69         }
70 
71         ret = ff_ipc_recv(&retmsg, msg->msg_type);
72         if (ret < 0) {
73             errno = EPIPE;
74             ff_ipc_msg_free(msg);
75             return -1;
76         }
77     } while (msg != retmsg);
78 
79     *knictl = retmsg->knictl;
80 
81     ff_ipc_msg_free(msg);
82 
83     return 0;
84 }
85 
main(int argc,char ** argv)86 int main(int argc, char **argv)
87 {
88     int ch, has_action = 0, i;
89     enum FF_KNICTL_CMD cmd;
90     struct ff_knictl_args knictl = {.kni_cmd = FF_KNICTL_CMD_GET};
91     struct ff_knictl_args pknictl[RTE_MAX_LCORE];
92     int proc_id = 0, max_proc_id = -1;
93 
94     ff_ipc_init();
95     while ((ch = getopt(argc, argv, "hp:P:a:n")) != -1) {
96         switch(ch) {
97         case 'p':
98             proc_id = atoi(optarg);
99             ff_set_proc_id(proc_id);
100             break;
101         case 'P':
102             max_proc_id = atoi(optarg);
103             if (max_proc_id < 0 || max_proc_id >= RTE_MAX_LCORE) {
104                 usage();
105                 ff_ipc_exit();
106                 return -1;
107             }
108             break;
109         case 'a':
110             if (has_action){
111                 usage();
112                 ff_ipc_exit();
113                 return -1;
114             }
115             has_action = 1;
116             cmd = knictl.kni_cmd = FF_KNICTL_CMD_SET;
117             knictl.kni_action = get_action(optarg);
118             if (knictl.kni_action < FF_KNICTL_ACTION_DEFAULT || knictl.kni_action >= FF_KNICTL_ACTION_MAX){
119                 usage();
120                 ff_ipc_exit();
121                 return -1;
122             }
123             break;
124         case 'n':
125             if (has_action){
126                 usage();
127                 ff_ipc_exit();
128                 return -1;
129             }
130             has_action = 1;
131             cmd = knictl.kni_cmd = FF_KNICTL_CMD_GET;
132             break;
133         case 'h':
134         default:
135             usage();
136             ff_ipc_exit();
137             return -1;
138         }
139     }
140     if (max_proc_id == -1){
141         printf("  using default proc id\n");
142         int ret = knictl_status(&knictl);
143         printf("  %s to %s knictl type: %s\n", ret ? "fail": "success", knictl.kni_cmd == FF_KNICTL_CMD_GET ? "get" : "set", get_action_str(knictl.kni_action));
144     }
145     else {
146         int proc_id = 0;
147         for (; proc_id < max_proc_id; proc_id++){
148             pknictl[proc_id] = knictl;
149             ff_set_proc_id(proc_id);
150             int ret = knictl_status(&pknictl[proc_id]);
151             printf("  %s to %s knictl type: %s, proc_id: %d\n", ret ? "fail": "success", pknictl[proc_id].kni_cmd == FF_KNICTL_CMD_GET ? "get" : "set", get_action_str(pknictl[proc_id].kni_action), proc_id);
152         }
153     }
154 
155     ff_ipc_exit();
156 }