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