Imported Upstream version 16.04
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_common_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 <rte_common.h>
35 #include <rte_ring.h>
36 #include <rte_malloc.h>
37
38 #include "pipeline_common_be.h"
39
40 void *
41 pipeline_msg_req_ping_handler(__rte_unused struct pipeline *p,
42         void *msg)
43 {
44         struct pipeline_msg_rsp *rsp = msg;
45
46         rsp->status = 0; /* OK */
47
48         return rsp;
49 }
50
51 void *
52 pipeline_msg_req_stats_port_in_handler(struct pipeline *p,
53         void *msg)
54 {
55         struct pipeline_stats_msg_req *req = msg;
56         struct pipeline_stats_port_in_msg_rsp *rsp = msg;
57         uint32_t port_id;
58
59         /* Check request */
60         if (req->id >= p->n_ports_in) {
61                 rsp->status = -1;
62                 return rsp;
63         }
64         port_id = p->port_in_id[req->id];
65
66         /* Process request */
67         rsp->status = rte_pipeline_port_in_stats_read(p->p,
68                 port_id,
69                 &rsp->stats,
70                 1);
71
72         return rsp;
73 }
74
75 void *
76 pipeline_msg_req_stats_port_out_handler(struct pipeline *p,
77         void *msg)
78 {
79         struct pipeline_stats_msg_req *req = msg;
80         struct pipeline_stats_port_out_msg_rsp *rsp = msg;
81         uint32_t port_id;
82
83         /* Check request */
84         if (req->id >= p->n_ports_out) {
85                 rsp->status = -1;
86                 return rsp;
87         }
88         port_id = p->port_out_id[req->id];
89
90         /* Process request */
91         rsp->status = rte_pipeline_port_out_stats_read(p->p,
92                 port_id,
93                 &rsp->stats,
94                 1);
95
96         return rsp;
97 }
98
99 void *
100 pipeline_msg_req_stats_table_handler(struct pipeline *p,
101         void *msg)
102 {
103         struct pipeline_stats_msg_req *req = msg;
104         struct pipeline_stats_table_msg_rsp *rsp = msg;
105         uint32_t table_id;
106
107         /* Check request */
108         if (req->id >= p->n_tables) {
109                 rsp->status = -1;
110                 return rsp;
111         }
112         table_id = p->table_id[req->id];
113
114         /* Process request */
115         rsp->status = rte_pipeline_table_stats_read(p->p,
116                 table_id,
117                 &rsp->stats,
118                 1);
119
120         return rsp;
121 }
122
123 void *
124 pipeline_msg_req_port_in_enable_handler(struct pipeline *p,
125         void *msg)
126 {
127         struct pipeline_port_in_msg_req *req = msg;
128         struct pipeline_msg_rsp *rsp = msg;
129         uint32_t port_id;
130
131         /* Check request */
132         if (req->port_id >= p->n_ports_in) {
133                 rsp->status = -1;
134                 return rsp;
135         }
136         port_id = p->port_in_id[req->port_id];
137
138         /* Process request */
139         rsp->status = rte_pipeline_port_in_enable(p->p,
140                 port_id);
141
142         return rsp;
143 }
144
145 void *
146 pipeline_msg_req_port_in_disable_handler(struct pipeline *p,
147         void *msg)
148 {
149         struct pipeline_port_in_msg_req *req = msg;
150         struct pipeline_msg_rsp *rsp = msg;
151         uint32_t port_id;
152
153         /* Check request */
154         if (req->port_id >= p->n_ports_in) {
155                 rsp->status = -1;
156                 return rsp;
157         }
158         port_id = p->port_in_id[req->port_id];
159
160         /* Process request */
161         rsp->status = rte_pipeline_port_in_disable(p->p,
162                 port_id);
163
164         return rsp;
165 }
166
167 void *
168 pipeline_msg_req_invalid_handler(__rte_unused struct pipeline *p,
169         void *msg)
170 {
171         struct pipeline_msg_rsp *rsp = msg;
172
173         rsp->status = -1; /* Error */
174
175         return rsp;
176 }
177
178 int
179 pipeline_msg_req_handle(struct pipeline *p)
180 {
181         uint32_t msgq_id;
182
183         for (msgq_id = 0; msgq_id < p->n_msgq; msgq_id++) {
184                 for ( ; ; ) {
185                         struct pipeline_msg_req *req;
186                         pipeline_msg_req_handler f_handle;
187
188                         req = pipeline_msg_recv(p, msgq_id);
189                         if (req == NULL)
190                                 break;
191
192                         f_handle = (req->type < PIPELINE_MSG_REQS) ?
193                                 p->handlers[req->type] :
194                                 pipeline_msg_req_invalid_handler;
195
196                         if (f_handle == NULL)
197                                 f_handle = pipeline_msg_req_invalid_handler;
198
199                         pipeline_msg_send(p,
200                                 msgq_id,
201                                 f_handle(p, (void *) req));
202                 }
203         }
204
205         return 0;
206 }