New upstream version 18.02
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_master_be.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <unistd.h>
7
8 #include <rte_common.h>
9 #include <rte_malloc.h>
10
11 #include <cmdline_parse.h>
12 #include <cmdline_parse_string.h>
13 #include <cmdline_socket.h>
14 #include <cmdline.h>
15
16 #include "app.h"
17 #include "pipeline_master_be.h"
18
19 struct pipeline_master {
20         struct app_params *app;
21         struct cmdline *cl;
22         int post_init_done;
23         int script_file_done;
24 } __rte_cache_aligned;
25
26 static void*
27 pipeline_init(__rte_unused struct pipeline_params *params, void *arg)
28 {
29         struct app_params *app = (struct app_params *) arg;
30         struct pipeline_master *p;
31         uint32_t size;
32
33         /* Check input arguments */
34         if (app == NULL)
35                 return NULL;
36
37         /* Memory allocation */
38         size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct pipeline_master));
39         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
40         if (p == NULL)
41                 return NULL;
42
43         /* Initialization */
44         p->app = app;
45
46         p->cl = cmdline_stdin_new(app->cmds, "pipeline> ");
47         if (p->cl == NULL) {
48                 rte_free(p);
49                 return NULL;
50         }
51
52         p->post_init_done = 0;
53         p->script_file_done = 0;
54         if (app->script_file == NULL)
55                 p->script_file_done = 1;
56
57         return (void *) p;
58 }
59
60 static int
61 pipeline_free(void *pipeline)
62 {
63         struct pipeline_master *p = (struct pipeline_master *) pipeline;
64
65         if (p == NULL)
66                 return -EINVAL;
67
68         cmdline_stdin_exit(p->cl);
69         rte_free(p);
70
71         return 0;
72 }
73
74 static int
75 pipeline_run(void *pipeline)
76 {
77         struct pipeline_master *p = (struct pipeline_master *) pipeline;
78         struct app_params *app = p->app;
79         int status;
80 #ifdef RTE_LIBRTE_KNI
81         uint32_t i;
82 #endif /* RTE_LIBRTE_KNI */
83
84         /* Application post-init phase */
85         if (p->post_init_done == 0) {
86                 app_post_init(app);
87
88                 p->post_init_done = 1;
89         }
90
91         /* Run startup script file */
92         if (p->script_file_done == 0) {
93                 struct app_params *app = p->app;
94                 int fd = open(app->script_file, O_RDONLY);
95
96                 if (fd < 0)
97                         printf("Cannot open CLI script file \"%s\"\n",
98                                 app->script_file);
99                 else {
100                         struct cmdline *file_cl;
101
102                         printf("Running CLI script file \"%s\" ...\n",
103                                 app->script_file);
104                         file_cl = cmdline_new(p->cl->ctx, "", fd, 1);
105                         cmdline_interact(file_cl);
106                         close(fd);
107                 }
108
109                 p->script_file_done = 1;
110         }
111
112         /* Command Line Interface (CLI) */
113         status = cmdline_poll(p->cl);
114         if (status < 0)
115                 rte_panic("CLI poll error (%" PRId32 ")\n", status);
116         else if (status == RDLINE_EXITED) {
117                 cmdline_stdin_exit(p->cl);
118                 rte_exit(0, "Bye!\n");
119         }
120
121 #ifdef RTE_LIBRTE_KNI
122         /* Handle KNI requests from Linux kernel */
123         for (i = 0; i < app->n_pktq_kni; i++)
124                 rte_kni_handle_request(app->kni[i]);
125 #endif /* RTE_LIBRTE_KNI */
126
127         return 0;
128 }
129
130 static int
131 pipeline_timer(__rte_unused void *pipeline)
132 {
133         return 0;
134 }
135
136 struct pipeline_be_ops pipeline_master_be_ops = {
137                 .f_init = pipeline_init,
138                 .f_free = pipeline_free,
139                 .f_run = pipeline_run,
140                 .f_timer = pipeline_timer,
141 };