Imported Upstream version 16.04
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_common_be.h
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 #ifndef __INCLUDE_PIPELINE_COMMON_BE_H__
35 #define __INCLUDE_PIPELINE_COMMON_BE_H__
36
37 #include <rte_common.h>
38 #include <rte_ring.h>
39 #include <rte_pipeline.h>
40
41 #include "pipeline_be.h"
42
43 struct pipeline;
44
45 enum pipeline_msg_req_type {
46         PIPELINE_MSG_REQ_PING = 0,
47         PIPELINE_MSG_REQ_STATS_PORT_IN,
48         PIPELINE_MSG_REQ_STATS_PORT_OUT,
49         PIPELINE_MSG_REQ_STATS_TABLE,
50         PIPELINE_MSG_REQ_PORT_IN_ENABLE,
51         PIPELINE_MSG_REQ_PORT_IN_DISABLE,
52         PIPELINE_MSG_REQ_CUSTOM,
53         PIPELINE_MSG_REQS
54 };
55
56 typedef void *(*pipeline_msg_req_handler)(struct pipeline *p, void *msg);
57
58 struct pipeline {
59         struct rte_pipeline *p;
60         uint32_t port_in_id[PIPELINE_MAX_PORT_IN];
61         uint32_t port_out_id[PIPELINE_MAX_PORT_OUT];
62         uint32_t table_id[PIPELINE_MAX_TABLES];
63         struct rte_ring *msgq_in[PIPELINE_MAX_MSGQ_IN];
64         struct rte_ring *msgq_out[PIPELINE_MAX_MSGQ_OUT];
65
66         uint32_t n_ports_in;
67         uint32_t n_ports_out;
68         uint32_t n_tables;
69         uint32_t n_msgq;
70
71         pipeline_msg_req_handler handlers[PIPELINE_MSG_REQS];
72         char name[PIPELINE_NAME_SIZE];
73         uint32_t log_level;
74 };
75
76 enum pipeline_log_level {
77         PIPELINE_LOG_LEVEL_HIGH = 1,
78         PIPELINE_LOG_LEVEL_LOW,
79         PIPELINE_LOG_LEVELS
80 };
81
82 #define PLOG(p, level, fmt, ...)                                        \
83 do {                                                                    \
84         if (p->log_level >= PIPELINE_LOG_LEVEL_ ## level)               \
85                 fprintf(stdout, "[%s] " fmt "\n", p->name, ## __VA_ARGS__);\
86 } while (0)
87
88 static inline void *
89 pipeline_msg_recv(struct pipeline *p,
90         uint32_t msgq_id)
91 {
92         struct rte_ring *r = p->msgq_in[msgq_id];
93         void *msg;
94         int status = rte_ring_sc_dequeue(r, &msg);
95
96         if (status != 0)
97                 return NULL;
98
99         return msg;
100 }
101
102 static inline void
103 pipeline_msg_send(struct pipeline *p,
104         uint32_t msgq_id,
105         void *msg)
106 {
107         struct rte_ring *r = p->msgq_out[msgq_id];
108         int status;
109
110         do {
111                 status = rte_ring_sp_enqueue(r, msg);
112         } while (status == -ENOBUFS);
113 }
114
115 struct pipeline_msg_req {
116         enum pipeline_msg_req_type type;
117 };
118
119 struct pipeline_stats_msg_req {
120         enum pipeline_msg_req_type type;
121         uint32_t id;
122 };
123
124 struct pipeline_port_in_msg_req {
125         enum pipeline_msg_req_type type;
126         uint32_t port_id;
127 };
128
129 struct pipeline_custom_msg_req {
130         enum pipeline_msg_req_type type;
131         uint32_t subtype;
132 };
133
134 struct pipeline_msg_rsp {
135         int status;
136 };
137
138 struct pipeline_stats_port_in_msg_rsp {
139         int status;
140         struct rte_pipeline_port_in_stats stats;
141 };
142
143 struct pipeline_stats_port_out_msg_rsp {
144         int status;
145         struct rte_pipeline_port_out_stats stats;
146 };
147
148 struct pipeline_stats_table_msg_rsp {
149         int status;
150         struct rte_pipeline_table_stats stats;
151 };
152
153 void *pipeline_msg_req_ping_handler(struct pipeline *p, void *msg);
154 void *pipeline_msg_req_stats_port_in_handler(struct pipeline *p, void *msg);
155 void *pipeline_msg_req_stats_port_out_handler(struct pipeline *p, void *msg);
156 void *pipeline_msg_req_stats_table_handler(struct pipeline *p, void *msg);
157 void *pipeline_msg_req_port_in_enable_handler(struct pipeline *p, void *msg);
158 void *pipeline_msg_req_port_in_disable_handler(struct pipeline *p, void *msg);
159 void *pipeline_msg_req_invalid_handler(struct pipeline *p, void *msg);
160
161 int pipeline_msg_req_handle(struct pipeline *p);
162
163 #endif