common: Adding common library for sample vnf
[samplevnf.git] / common / VIL / pipeline_common / pipeline_common_be.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef __INCLUDE_PIPELINE_COMMON_BE_H__
18 #define __INCLUDE_PIPELINE_COMMON_BE_H__
19
20 #include <rte_common.h>
21 #include <rte_ring.h>
22 #include <rte_pipeline.h>
23
24 #include "pipeline_be.h"
25
26 struct pipeline;
27
28 enum pipeline_msg_req_type {
29         PIPELINE_MSG_REQ_PING = 0,
30         PIPELINE_MSG_REQ_STATS_PORT_IN,
31         PIPELINE_MSG_REQ_STATS_PORT_OUT,
32         PIPELINE_MSG_REQ_STATS_TABLE,
33         PIPELINE_MSG_REQ_PORT_IN_ENABLE,
34         PIPELINE_MSG_REQ_PORT_IN_DISABLE,
35         PIPELINE_MSG_REQ_CUSTOM,
36         PIPELINE_MSG_REQS
37 };
38
39 typedef void *(*pipeline_msg_req_handler)(struct pipeline *p, void *msg);
40
41 struct pipeline {
42         struct rte_pipeline *p;
43         uint32_t port_in_id[PIPELINE_MAX_PORT_IN];
44         uint32_t port_out_id[PIPELINE_MAX_PORT_OUT];
45         uint32_t table_id[PIPELINE_MAX_TABLES];
46         struct rte_ring *msgq_in[PIPELINE_MAX_MSGQ_IN];
47         struct rte_ring *msgq_out[PIPELINE_MAX_MSGQ_OUT];
48
49         uint32_t n_ports_in;
50         uint32_t n_ports_out;
51         uint32_t n_tables;
52         uint32_t n_msgq;
53
54         pipeline_msg_req_handler handlers[PIPELINE_MSG_REQS];
55         char name[PIPELINE_NAME_SIZE];
56         uint32_t log_level;
57 };
58
59 enum pipeline_log_level {
60         PIPELINE_LOG_LEVEL_HIGH = 1,
61         PIPELINE_LOG_LEVEL_LOW,
62         PIPELINE_LOG_LEVELS
63 };
64
65 #define PLOG(p, level, fmt, ...)                                        \
66 do {                                                                    \
67         if (p->log_level >= PIPELINE_LOG_LEVEL_ ## level)               \
68                 fprintf(stdout, "[%s] " fmt "\n", p->name, ## __VA_ARGS__);\
69 } while (0)
70
71 static inline void *
72 pipeline_msg_recv(struct pipeline *p,
73         uint32_t msgq_id)
74 {
75         struct rte_ring *r = p->msgq_in[msgq_id];
76         void *msg;
77         int status = rte_ring_sc_dequeue(r, &msg);
78
79         if (status != 0)
80                 return NULL;
81
82         return msg;
83 }
84
85 static inline void
86 pipeline_msg_send(struct pipeline *p,
87         uint32_t msgq_id,
88         void *msg)
89 {
90         struct rte_ring *r = p->msgq_out[msgq_id];
91         int status;
92
93         do {
94                 status = rte_ring_sp_enqueue(r, msg);
95         } while (status == -ENOBUFS);
96 }
97
98 struct pipeline_msg_req {
99         enum pipeline_msg_req_type type;
100 };
101
102 struct pipeline_stats_msg_req {
103         enum pipeline_msg_req_type type;
104         uint32_t id;
105 };
106
107 struct pipeline_port_in_msg_req {
108         enum pipeline_msg_req_type type;
109         uint32_t port_id;
110 };
111
112 struct pipeline_custom_msg_req {
113         enum pipeline_msg_req_type type;
114         uint32_t subtype;
115 };
116
117 struct pipeline_msg_rsp {
118         int status;
119 };
120
121 struct pipeline_stats_port_in_msg_rsp {
122         int status;
123         struct rte_pipeline_port_in_stats stats;
124 };
125
126 struct pipeline_stats_port_out_msg_rsp {
127         int status;
128         struct rte_pipeline_port_out_stats stats;
129 };
130
131 struct pipeline_stats_table_msg_rsp {
132         int status;
133         struct rte_pipeline_table_stats stats;
134 };
135
136 void *pipeline_msg_req_ping_handler(struct pipeline *p, void *msg);
137 void *pipeline_msg_req_stats_port_in_handler(struct pipeline *p, void *msg);
138 void *pipeline_msg_req_stats_port_out_handler(struct pipeline *p, void *msg);
139 void *pipeline_msg_req_stats_table_handler(struct pipeline *p, void *msg);
140 void *pipeline_msg_req_port_in_enable_handler(struct pipeline *p, void *msg);
141 void *pipeline_msg_req_port_in_disable_handler(struct pipeline *p, void *msg);
142 void *pipeline_msg_req_invalid_handler(struct pipeline *p, void *msg);
143
144 int pipeline_msg_req_handle(struct pipeline *p);
145
146 #endif