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