2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <rte_cycles.h>
21 #include <rte_version.h>
24 #include "prox_lua_types.h"
25 #include "prox_malloc.h"
26 #include "task_base.h"
27 #include "task_init.h"
35 #include "prox_shared.h"
36 #include "prox_compat.h"
38 #if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
39 #define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
43 struct task_base base;
45 struct rte_meter_srtcm *sr_flows;
46 struct rte_meter_trtcm *tr_flows;
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;
55 enum police_action police_act[3][3];
57 uint8_t runtime_flags;
58 struct rte_sched_port *sched_port;
61 typedef uint8_t (*hp) (struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user);
63 static uint8_t handle_police(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
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;
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);
72 out_color = rte_meter_srtcm_color_aware_check(&task->sr_flows[user], &task->sr_profile, tsc, pkt_len, in_color);
74 return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
77 static uint8_t handle_police_tr(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
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);
85 out_color = rte_meter_trtcm_color_aware_check(&task->tr_flows[user], &task->tr_profile, tsc, pkt_len, in_color);
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;
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];
96 prox_rte_sched_port_pkt_write(task->sched_port, mbuf, subport, pipe, traffic_class, queue, color);
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];
104 return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
107 static inline int get_user(struct task_police *task, struct rte_mbuf *mbuf)
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);
114 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
118 prox_rte_sched_port_pkt_read_tree_path(task->sched_port, mbuf, &dummy, &pipe, &dummy, &dummy);
121 struct rte_sched_port_hierarchy *sched =
122 (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
127 #define PHASE1_DELAY PREFETCH_OFFSET
128 #define PHASE2_DELAY PREFETCH_OFFSET
129 #define PHASE3_DELAY PREFETCH_OFFSET
130 #define PHASE4_DELAY PREFETCH_OFFSET
132 static inline int handle_pb(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts, hp handle_police_func)
134 struct task_police *task = (struct task_police *)tbase;
136 uint64_t cur_tsc = rte_rdtsc();
138 uint8_t out[MAX_PKT_BURST];
140 for (j = 0; j < PHASE1_DELAY && j < n_pkts; ++j) {
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*));
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]);
154 PREFETCH0(&task->user_table[cur_user]);
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]);
165 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
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]);
176 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
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]);
184 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
187 for (; j < n_pkts; ++j) {
188 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
191 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
194 static int handle_police_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
196 return handle_pb(tbase, mbuf, n_pkts, handle_police);
199 static int handle_police_tr_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
201 return handle_pb(tbase, mbuf, n_pkts, handle_police_tr);
204 static void init_task_police(struct task_base *tbase, struct task_args *targ)
206 struct task_police *task = (struct task_police *)tbase;
207 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
209 task->overhead = targ->overhead;
210 task->runtime_flags = targ->runtime_flags;
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);
220 task->sched_port = rte_sched_port_config(&targ->qos_conf.port_params);
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");
229 struct rte_meter_srtcm_params params = {
234 #if RTE_VERSION >= RTE_VERSION_NUM(18,5,0,0)
235 PROX_PANIC(rte_meter_srtcm_profile_config(&task->sr_profile, ¶ms) != 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");
240 for (uint32_t i = 0; i < targ->n_flows; ++i) {
241 rte_meter_srtcm_config(&task->sr_flows[i], ¶ms);
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");
253 struct rte_meter_trtcm_params params = {
259 #if RTE_VERSION >= RTE_VERSION_NUM(18,5,0,0)
260 PROX_PANIC(rte_meter_trtcm_profile_config(&task->tr_profile, ¶ms) != 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");
266 for (uint32_t i = 0; i < targ->n_flows; ++i) {
267 rte_meter_trtcm_config(&task->tr_flows[i], ¶ms);
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];
279 static struct task_init task_init_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)
288 static struct task_init task_init_police2 = {
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)
298 __attribute__((constructor)) static void reg_task_police(void)
300 reg_task(&task_init_police);
301 reg_task(&task_init_police2);