Imported Upstream version 16.07-rc1
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_master_be.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #include <rte_common.h>
38 #include <rte_malloc.h>
39
40 #include <cmdline_parse.h>
41 #include <cmdline_parse_string.h>
42 #include <cmdline_socket.h>
43 #include <cmdline.h>
44
45 #include "app.h"
46 #include "pipeline_master_be.h"
47
48 struct pipeline_master {
49         struct app_params *app;
50         struct cmdline *cl;
51         int post_init_done;
52         int script_file_done;
53 } __rte_cache_aligned;
54
55 static void*
56 pipeline_init(__rte_unused struct pipeline_params *params, void *arg)
57 {
58         struct app_params *app = (struct app_params *) arg;
59         struct pipeline_master *p;
60         uint32_t size;
61
62         /* Check input arguments */
63         if (app == NULL)
64                 return NULL;
65
66         /* Memory allocation */
67         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct pipeline_master));
68         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
69         if (p == NULL)
70                 return NULL;
71
72         /* Initialization */
73         p->app = app;
74
75         p->cl = cmdline_stdin_new(app->cmds, "pipeline> ");
76         if (p->cl == NULL) {
77                 rte_free(p);
78                 return NULL;
79         }
80
81         p->post_init_done = 0;
82         p->script_file_done = 0;
83         if (app->script_file == NULL)
84                 p->script_file_done = 1;
85
86         return (void *) p;
87 }
88
89 static int
90 pipeline_free(void *pipeline)
91 {
92         struct pipeline_master *p = (struct pipeline_master *) pipeline;
93
94         if (p == NULL)
95                 return -EINVAL;
96
97         cmdline_stdin_exit(p->cl);
98         rte_free(p);
99
100         return 0;
101 }
102
103 static int
104 pipeline_run(void *pipeline)
105 {
106         struct pipeline_master *p = (struct pipeline_master *) pipeline;
107         struct app_params *app = p->app;
108         int status;
109 #ifdef RTE_LIBRTE_KNI
110         uint32_t i;
111 #endif /* RTE_LIBRTE_KNI */
112
113         /* Application post-init phase */
114         if (p->post_init_done == 0) {
115                 app_post_init(app);
116
117                 p->post_init_done = 1;
118         }
119
120         /* Run startup script file */
121         if (p->script_file_done == 0) {
122                 struct app_params *app = p->app;
123                 int fd = open(app->script_file, O_RDONLY);
124
125                 if (fd < 0)
126                         printf("Cannot open CLI script file \"%s\"\n",
127                                 app->script_file);
128                 else {
129                         struct cmdline *file_cl;
130
131                         printf("Running CLI script file \"%s\" ...\n",
132                                 app->script_file);
133                         file_cl = cmdline_new(p->cl->ctx, "", fd, 1);
134                         cmdline_interact(file_cl);
135                         close(fd);
136                 }
137
138                 p->script_file_done = 1;
139         }
140
141         /* Command Line Interface (CLI) */
142         status = cmdline_poll(p->cl);
143         if (status < 0)
144                 rte_panic("CLI poll error (%" PRId32 ")\n", status);
145         else if (status == RDLINE_EXITED) {
146                 cmdline_stdin_exit(p->cl);
147                 rte_exit(0, "Bye!\n");
148         }
149
150 #ifdef RTE_LIBRTE_KNI
151         /* Handle KNI requests from Linux kernel */
152         for (i = 0; i < app->n_pktq_kni; i++)
153                 rte_kni_handle_request(app->kni[i]);
154 #endif /* RTE_LIBRTE_KNI */
155
156         return 0;
157 }
158
159 static int
160 pipeline_timer(__rte_unused void *pipeline)
161 {
162         return 0;
163 }
164
165 struct pipeline_be_ops pipeline_master_be_ops = {
166                 .f_init = pipeline_init,
167                 .f_free = pipeline_free,
168                 .f_run = pipeline_run,
169                 .f_timer = pipeline_timer,
170 };