Some fixes after code review
[samplevnf.git] / common / VIL / pipeline_master / pipeline_master_be.c
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include <rte_common.h>
21 #include <rte_malloc.h>
22
23 #include <cmdline_parse.h>
24 #include <cmdline_parse_string.h>
25 #include <cmdline_socket.h>
26 #include <cmdline.h>
27
28 #include "app.h"
29 #include "pipeline_master_be.h"
30
31 struct cmdline *pipe_cl;
32
33 struct pipeline_master {
34         struct app_params *app;
35         struct cmdline *cl;
36         int script_file_done;
37 } __rte_cache_aligned;
38
39 static void*
40 pipeline_init(__rte_unused struct pipeline_params *params, void *arg)
41 {
42         struct app_params *app = (struct app_params *) arg;
43         struct pipeline_master *p;
44         uint32_t size;
45
46         /* Check input arguments */
47         if (app == NULL)
48                 return NULL;
49
50         /* Memory allocation */
51         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct pipeline_master));
52         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
53         if (p == NULL)
54                 return NULL;
55
56         /* Initialization */
57         p->app = app;
58
59         p->cl = cmdline_stdin_new(app->cmds, "pipeline> ");
60         if (p->cl == NULL) {
61                 rte_free(p);
62                 return NULL;
63         }
64         pipe_cl = p->cl;
65
66         p->script_file_done = 0;
67         if (app->script_file == NULL)
68                 p->script_file_done = 1;
69
70         return (void *) p;
71 }
72
73 static int
74 pipeline_free(void *pipeline)
75 {
76         struct pipeline_master *p = (struct pipeline_master *) pipeline;
77
78         if (p == NULL)
79                 return -EINVAL;
80
81         cmdline_stdin_exit(p->cl);
82         rte_free(p);
83
84         return 0;
85 }
86
87 static int
88 pipeline_run(void *pipeline)
89 {
90         struct pipeline_master *p = (struct pipeline_master *) pipeline;
91
92         int status;
93
94         if (p->script_file_done == 0) {
95                 struct app_params *app = p->app;
96                 int fd = open(app->script_file, O_RDONLY);
97
98                 if (fd < 0)
99                         printf("Cannot open CLI script file \"%s\"\n",
100                                 app->script_file);
101                 else {
102                         struct cmdline *file_cl;
103
104                         printf("Running CLI script file \"%s\" ...\n",
105                                 app->script_file);
106                         file_cl = cmdline_new(p->cl->ctx, "", fd, 1);
107                         cmdline_interact(file_cl);
108                         close(fd);
109                 }
110
111                 p->script_file_done = 1;
112         }
113
114         status = cmdline_poll(p->cl);
115         if (status < 0)
116                 rte_panic("CLI poll error (%" PRId32 ")\n", status);
117         else if (status == RDLINE_EXITED) {
118                 cmdline_stdin_exit(p->cl);
119                 rte_exit(0, "Bye!\n");
120         }
121
122         return 0;
123 }
124
125 static int
126 pipeline_timer(__rte_unused void *pipeline)
127 {
128         rte_timer_manage();
129         return 0;
130 }
131
132 struct pipeline_be_ops pipeline_master_be_ops = {
133                 .f_init = pipeline_init,
134                 .f_free = pipeline_free,
135                 .f_run = pipeline_run,
136                 .f_timer = pipeline_timer,
137                 .f_track = NULL,
138 };