Fix minor compilation issue on recent gcc compilers
[samplevnf.git] / VNFs / DPPD-PROX / handle_police.c
1 /*
2 // Copyright (c) 2010-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 #include <string.h>
18 #include <stdio.h>
19 #include <rte_mbuf.h>
20 #include <rte_cycles.h>
21 #include <rte_version.h>
22
23 #include "prox_lua.h"
24 #include "prox_lua_types.h"
25 #include "prox_malloc.h"
26 #include "task_base.h"
27 #include "task_init.h"
28 #include "lconf.h"
29 #include "prefetch.h"
30 #include "quit.h"
31 #include "log.h"
32 #include "defines.h"
33 #include "qinq.h"
34 #include "prox_cfg.h"
35 #include "prox_shared.h"
36 #include "prox_compat.h"
37
38 #if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
39 #define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
40 #endif
41
42 struct task_police {
43         struct task_base base;
44         union {
45                 struct rte_meter_srtcm *sr_flows;
46                 struct rte_meter_trtcm *tr_flows;
47         };
48         union {
49 #if RTE_VERSION >= RTE_VERSION_NUM(18,5,0,0)
50                 struct rte_meter_srtcm_profile sr_profile;
51                 struct rte_meter_trtcm_profile tr_profile;
52 #endif
53         };
54         uint16_t           *user_table;
55         enum police_action police_act[3][3];
56         uint16_t overhead;
57         uint8_t runtime_flags;
58         struct rte_sched_port *sched_port;
59 };
60
61 typedef uint8_t (*hp) (struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user);
62
63 static uint8_t handle_police(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
64 {
65         enum prox_rte_color in_color = RTE_COLOR_GREEN;
66         enum prox_rte_color out_color;
67         uint32_t pkt_len = rte_pktmbuf_pkt_len(mbuf) + task->overhead;
68
69 #if RTE_VERSION < RTE_VERSION_NUM(18,5,0,0)
70         out_color = rte_meter_srtcm_color_aware_check(&task->sr_flows[user], tsc, pkt_len, in_color);
71 #else
72         out_color = rte_meter_srtcm_color_aware_check(&task->sr_flows[user], &task->sr_profile, tsc, pkt_len, in_color);
73 #endif
74         return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
75 }
76
77 static uint8_t handle_police_tr(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
78 {
79         enum prox_rte_color in_color = RTE_COLOR_GREEN;
80         enum prox_rte_color out_color;
81         uint32_t pkt_len = rte_pktmbuf_pkt_len(mbuf) + task->overhead;
82 #if RTE_VERSION < RTE_VERSION_NUM(18,5,0,0)
83         out_color = rte_meter_trtcm_color_aware_check(&task->tr_flows[user], tsc, pkt_len, in_color);
84 #else
85         out_color = rte_meter_trtcm_color_aware_check(&task->tr_flows[user], &task->tr_profile, tsc, pkt_len, in_color);
86 #endif
87
88         if (task->runtime_flags  & TASK_MARK) {
89 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
90                 uint32_t subport, pipe, traffic_class, queue;
91                 enum prox_rte_color color;
92
93                 prox_rte_sched_port_pkt_read_tree_path(task->sched_port, mbuf, &subport, &pipe, &traffic_class, &queue);
94                 color = task->police_act[in_color][out_color];
95
96                 prox_rte_sched_port_pkt_write(task->sched_port, mbuf, subport, pipe, traffic_class, queue, color);
97 #else
98                 struct rte_sched_port_hierarchy *sched =
99                         (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
100                 sched->color = task->police_act[in_color][out_color];
101 #endif
102         }
103
104         return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
105 }
106
107 static inline int get_user(struct task_police *task, struct rte_mbuf *mbuf)
108 {
109         if (task->runtime_flags & TASK_CLASSIFY) {
110                 struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbuf, struct qinq_hdr *);
111                 return PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
112         }
113
114 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
115         uint32_t dummy;
116         uint32_t pipe;
117
118         prox_rte_sched_port_pkt_read_tree_path(task->sched_port, mbuf, &dummy, &pipe, &dummy, &dummy);
119         return pipe;
120 #else
121         struct rte_sched_port_hierarchy *sched =
122                 (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
123         return sched->pipe;
124 #endif
125 }
126
127 #define PHASE1_DELAY PREFETCH_OFFSET
128 #define PHASE2_DELAY PREFETCH_OFFSET
129 #define PHASE3_DELAY PREFETCH_OFFSET
130 #define PHASE4_DELAY PREFETCH_OFFSET
131
132 static inline int handle_pb(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts, hp handle_police_func)
133 {
134         struct task_police *task = (struct task_police *)tbase;
135         uint16_t j;
136         uint64_t cur_tsc = rte_rdtsc();
137         uint32_t user[64];
138         uint8_t  out[MAX_PKT_BURST];
139         uint32_t cur_user;
140         for (j = 0; j < PHASE1_DELAY && j < n_pkts; ++j) {
141                 PREFETCH0(mbufs[j]);
142         }
143
144         for (j = 0; j < PHASE2_DELAY && j + PHASE1_DELAY < n_pkts; ++j) {
145                 PREFETCH0(mbufs[j + PHASE1_DELAY]);
146                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void*));
147         }
148
149         for (j = 0; j < PHASE3_DELAY && j + PHASE2_DELAY + PHASE1_DELAY < n_pkts; ++j) {
150                 PREFETCH0(mbufs[j + PHASE2_DELAY + PHASE1_DELAY]);
151                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE2_DELAY], void*));
152                 cur_user = get_user(task, mbufs[j]);
153                 user[j] = cur_user;
154                 PREFETCH0(&task->user_table[cur_user]);
155         }
156
157         /* At this point, the whole pipeline is running */
158         for (j = 0; j + PHASE3_DELAY + PHASE2_DELAY + PHASE1_DELAY < n_pkts; ++j) {
159                 PREFETCH0(mbufs[j + PHASE3_DELAY + PHASE2_DELAY + PHASE1_DELAY]);
160                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE3_DELAY + PHASE2_DELAY], void*));
161                 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
162                 user[j + PHASE3_DELAY] = cur_user;
163                 PREFETCH0(&task->user_table[cur_user]);
164
165                 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
166         }
167
168         /* Last part of pipeline */
169         for (; j + PHASE3_DELAY + PHASE2_DELAY < n_pkts; ++j) {
170                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE3_DELAY + PHASE2_DELAY], void*));
171                 PREFETCH0(&task->user_table[j + PHASE3_DELAY]);
172                 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
173                 user[j + PHASE3_DELAY] = cur_user;
174                 PREFETCH0(&task->user_table[cur_user]);
175
176                 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
177         }
178
179         for (; j + PHASE3_DELAY < n_pkts; ++j) {
180                 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
181                 user[j + PHASE3_DELAY] = cur_user;
182                 PREFETCH0(&task->user_table[cur_user]);
183
184                 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
185         }
186
187         for (; j < n_pkts; ++j) {
188                 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
189         }
190
191         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
192 }
193
194 static int handle_police_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
195 {
196         return handle_pb(tbase, mbuf, n_pkts, handle_police);
197 }
198
199 static int handle_police_tr_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
200 {
201         return handle_pb(tbase, mbuf, n_pkts, handle_police_tr);
202 }
203
204 static void init_task_police(struct task_base *tbase, struct task_args *targ)
205 {
206         struct task_police *task = (struct task_police *)tbase;
207         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
208
209         task->overhead = targ->overhead;
210         task->runtime_flags = targ->runtime_flags;
211
212         task->user_table = prox_sh_find_socket(socket_id, "user_table");
213         if (!task->user_table) {
214                 PROX_PANIC(!strcmp(targ->user_table, ""), "No user table defined\n");
215                 int ret = lua_to_user_table(prox_lua(), GLOBAL, targ->user_table, socket_id, &task->user_table);
216                 PROX_PANIC(ret, "Failed to create user table from config:\n%s\n", get_lua_to_errors());
217                 prox_sh_add_socket(socket_id, "user_table", task->user_table);
218         }
219
220         task->sched_port = rte_sched_port_config(&targ->qos_conf.port_params);
221
222         if (strcmp(targ->task_init->sub_mode_str, "trtcm")) {
223                 task->sr_flows = prox_zmalloc(targ->n_flows * sizeof(*task->sr_flows), socket_id);
224                 PROX_PANIC(task->sr_flows == NULL, "Failed to allocate flow contexts\n");
225                 PROX_PANIC(!targ->cir, "Commited information rate is set to 0\n");
226                 PROX_PANIC(!targ->cbs, "Commited information bucket size is set to 0\n");
227                 PROX_PANIC(!targ->ebs, "Execess information bucket size is set to 0\n");
228
229                 struct rte_meter_srtcm_params params = {
230                         .cir = targ->cir,
231                         .cbs = targ->cbs,
232                         .ebs = targ->ebs,
233                 };
234 #if RTE_VERSION >= RTE_VERSION_NUM(18,5,0,0)
235                 PROX_PANIC(rte_meter_srtcm_profile_config(&task->sr_profile, &params) != 0, "Failed to rte_meter_srtcm_profile_config\n");
236                 for (uint32_t i = 0; i < targ->n_flows; ++i) {
237                         PROX_PANIC(rte_meter_srtcm_config(&task->sr_flows[i], &task->sr_profile) != 0, "Failed to rte_meter_srtcm_config");
238                 }
239 #else
240                 for (uint32_t i = 0; i < targ->n_flows; ++i) {
241                         rte_meter_srtcm_config(&task->sr_flows[i], &params);
242                 }
243 #endif
244         }
245         else {
246                 task->tr_flows = prox_zmalloc(targ->n_flows * sizeof(*task->tr_flows), socket_id);
247                 PROX_PANIC(task->tr_flows == NULL, "Failed to allocate flow contexts\n");
248                 PROX_PANIC(!targ->pir, "Peak information rate is set to 0\n");
249                 PROX_PANIC(!targ->cir, "Commited information rate is set to 0\n");
250                 PROX_PANIC(!targ->pbs, "Peak information bucket size is set to 0\n");
251                 PROX_PANIC(!targ->cbs, "Commited information bucket size is set to 0\n");
252
253                 struct rte_meter_trtcm_params params = {
254                         .pir = targ->pir,
255                         .pbs = targ->pbs,
256                         .cir = targ->cir,
257                         .cbs = targ->cbs,
258                 };
259 #if RTE_VERSION >= RTE_VERSION_NUM(18,5,0,0)
260                 PROX_PANIC(rte_meter_trtcm_profile_config(&task->tr_profile, &params) != 0, "Failed to rte_meter_srtcm_profile_config\n");
261                 for (uint32_t i = 0; i < targ->n_flows; ++i) {
262                         PROX_PANIC(rte_meter_trtcm_config(&task->tr_flows[i], &task->tr_profile) != 0, "Failed to rte_meter_trtcm_config\n");
263                 }
264 #else
265
266                 for (uint32_t i = 0; i < targ->n_flows; ++i) {
267                         rte_meter_trtcm_config(&task->tr_flows[i], &params);
268                 }
269 #endif
270         }
271
272         for (uint32_t i = 0; i < 3; ++i) {
273                 for (uint32_t j = 0; j < 3; ++j) {
274                         task->police_act[i][j] = targ->police_act[i][j];
275                 }
276         }
277 }
278
279 static struct task_init task_init_police = {
280         .mode = POLICE,
281         .mode_str = "police",
282         .init = init_task_police,
283         .handle = handle_police_bulk,
284         .flag_features = TASK_FEATURE_CLASSIFY,
285         .size = sizeof(struct task_police)
286 };
287
288 static struct task_init task_init_police2 = {
289         .mode = POLICE,
290         .mode_str = "police",
291         .sub_mode_str = "trtcm",
292         .init = init_task_police,
293         .handle = handle_police_tr_bulk,
294         .flag_features = TASK_FEATURE_CLASSIFY,
295         .size = sizeof(struct task_police)
296 };
297
298 __attribute__((constructor)) static void reg_task_police(void)
299 {
300         reg_task(&task_init_police);
301         reg_task(&task_init_police2);
302 }