425e2334bf216eca23c43cba087bc366069c1c2b
[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 pipeline_master {
32         struct app_params *app;
33         struct cmdline *cl;
34         int script_file_done;
35 } __rte_cache_aligned;
36
37 static void*
38 pipeline_init(__rte_unused struct pipeline_params *params, void *arg)
39 {
40         struct app_params *app = (struct app_params *) arg;
41         struct pipeline_master *p;
42         uint32_t size;
43
44         /* Check input arguments */
45         if (app == NULL)
46                 return NULL;
47
48         /* Memory allocation */
49         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct pipeline_master));
50         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
51         if (p == NULL)
52                 return NULL;
53
54         /* Initialization */
55         p->app = app;
56
57         p->cl = cmdline_stdin_new(app->cmds, "pipeline> ");
58         if (p->cl == NULL) {
59                 rte_free(p);
60                 return NULL;
61         }
62
63         p->script_file_done = 0;
64         if (app->script_file == NULL)
65                 p->script_file_done = 1;
66
67         return (void *) p;
68 }
69
70 static int
71 pipeline_free(void *pipeline)
72 {
73         struct pipeline_master *p = (struct pipeline_master *) pipeline;
74
75         if (p == NULL)
76                 return -EINVAL;
77
78         cmdline_stdin_exit(p->cl);
79         rte_free(p);
80
81         return 0;
82 }
83
84 static int
85 pipeline_run(void *pipeline)
86 {
87         struct pipeline_master *p = (struct pipeline_master *) pipeline;
88         int status;
89
90         if (p->script_file_done == 0) {
91                 struct app_params *app = p->app;
92                 int fd = open(app->script_file, O_RDONLY);
93
94                 if (fd < 0)
95                         printf("Cannot open CLI script file \"%s\"\n",
96                                 app->script_file);
97                 else {
98                         struct cmdline *file_cl;
99
100                         printf("Running CLI script file \"%s\" ...\n",
101                                 app->script_file);
102                         file_cl = cmdline_new(p->cl->ctx, "", fd, 1);
103                         cmdline_interact(file_cl);
104                         close(fd);
105                 }
106
107                 p->script_file_done = 1;
108         }
109
110         status = cmdline_poll(p->cl);
111         if (status < 0)
112                 rte_panic("CLI poll error (%" PRId32 ")\n", status);
113         else if (status == RDLINE_EXITED) {
114                 cmdline_stdin_exit(p->cl);
115                 rte_exit(0, "Bye!\n");
116         }
117
118         return 0;
119 }
120
121 static int
122 pipeline_timer(__rte_unused void *pipeline)
123 {
124         rte_timer_manage();
125         return 0;
126 }
127
128 struct pipeline_be_ops pipeline_master_be_ops = {
129                 .f_init = pipeline_init,
130                 .f_free = pipeline_free,
131                 .f_run = pipeline_run,
132                 .f_timer = pipeline_timer,
133                 .f_track = NULL,
134 };