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