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"
37 #if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
38 #define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
42 struct task_base base;
44 struct rte_meter_srtcm *sr_flows;
45 struct rte_meter_trtcm *tr_flows;
49 enum police_action police_act[3][3];
51 uint8_t runtime_flags;
54 typedef uint8_t (*hp) (struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user);
56 static uint8_t handle_police(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
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 out_color = rte_meter_srtcm_color_aware_check(&task->sr_flows[user], tsc, pkt_len, in_color);
63 return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
66 static uint8_t handle_police_tr(struct task_police *task, struct rte_mbuf *mbuf, uint64_t tsc, uint32_t user)
68 enum rte_meter_color in_color = e_RTE_METER_GREEN;
69 enum rte_meter_color out_color;
70 uint32_t pkt_len = rte_pktmbuf_pkt_len(mbuf) + task->overhead;
71 out_color = rte_meter_trtcm_color_aware_check(&task->tr_flows[user], tsc, pkt_len, in_color);
73 if (task->runtime_flags & TASK_MARK) {
74 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
75 uint32_t subport, pipe, traffic_class, queue;
76 enum rte_meter_color color;
78 rte_sched_port_pkt_read_tree_path(mbuf, &subport, &pipe, &traffic_class, &queue);
79 color = task->police_act[in_color][out_color];
81 rte_sched_port_pkt_write(mbuf, subport, pipe, traffic_class, queue, color);
83 struct rte_sched_port_hierarchy *sched =
84 (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
85 sched->color = task->police_act[in_color][out_color];
89 return task->police_act[in_color][out_color] == ACT_DROP? OUT_DISCARD : 0;
92 static inline int get_user(struct task_police *task, struct rte_mbuf *mbuf)
94 if (task->runtime_flags & TASK_CLASSIFY) {
95 struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbuf, struct qinq_hdr *);
96 return PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
99 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
103 rte_sched_port_pkt_read_tree_path(mbuf, &dummy, &pipe, &dummy, &dummy);
106 struct rte_sched_port_hierarchy *sched =
107 (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
112 #define PHASE1_DELAY PREFETCH_OFFSET
113 #define PHASE2_DELAY PREFETCH_OFFSET
114 #define PHASE3_DELAY PREFETCH_OFFSET
115 #define PHASE4_DELAY PREFETCH_OFFSET
117 static inline int handle_pb(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts, hp handle_police_func)
119 struct task_police *task = (struct task_police *)tbase;
121 uint64_t cur_tsc = rte_rdtsc();
123 uint8_t out[MAX_PKT_BURST];
125 for (j = 0; j < PHASE1_DELAY && j < n_pkts; ++j) {
129 for (j = 0; j < PHASE2_DELAY && j + PHASE1_DELAY < n_pkts; ++j) {
130 PREFETCH0(mbufs[j + PHASE1_DELAY]);
131 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void*));
134 for (j = 0; j < PHASE3_DELAY && j + PHASE2_DELAY + PHASE1_DELAY < n_pkts; ++j) {
135 PREFETCH0(mbufs[j + PHASE2_DELAY + PHASE1_DELAY]);
136 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE2_DELAY], void*));
137 cur_user = get_user(task, mbufs[j]);
139 PREFETCH0(&task->user_table[cur_user]);
142 /* At this point, the whole pipeline is running */
143 for (j = 0; j + PHASE3_DELAY + PHASE2_DELAY + PHASE1_DELAY < n_pkts; ++j) {
144 PREFETCH0(mbufs[j + PHASE3_DELAY + PHASE2_DELAY + PHASE1_DELAY]);
145 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE3_DELAY + PHASE2_DELAY], void*));
146 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
147 user[j + PHASE3_DELAY] = cur_user;
148 PREFETCH0(&task->user_table[cur_user]);
150 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
153 /* Last part of pipeline */
154 for (; j + PHASE3_DELAY + PHASE2_DELAY < n_pkts; ++j) {
155 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PHASE3_DELAY + PHASE2_DELAY], void*));
156 PREFETCH0(&task->user_table[j + PHASE3_DELAY]);
157 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
158 user[j + PHASE3_DELAY] = cur_user;
159 PREFETCH0(&task->user_table[cur_user]);
161 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
164 for (; j + PHASE3_DELAY < n_pkts; ++j) {
165 cur_user = get_user(task, mbufs[j + PHASE3_DELAY]);
166 user[j + PHASE3_DELAY] = cur_user;
167 PREFETCH0(&task->user_table[cur_user]);
169 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
172 for (; j < n_pkts; ++j) {
173 out[j] = handle_police_func(task, mbufs[j], cur_tsc, task->user_table[user[j]]);
176 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
179 static int handle_police_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
181 return handle_pb(tbase, mbuf, n_pkts, handle_police);
184 static int handle_police_tr_bulk(struct task_base *tbase, struct rte_mbuf **mbuf, uint16_t n_pkts)
186 return handle_pb(tbase, mbuf, n_pkts, handle_police_tr);
189 static void init_task_police(struct task_base *tbase, struct task_args *targ)
191 struct task_police *task = (struct task_police *)tbase;
192 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
194 task->overhead = targ->overhead;
195 task->runtime_flags = targ->runtime_flags;
197 task->user_table = prox_sh_find_socket(socket_id, "user_table");
198 if (!task->user_table) {
199 PROX_PANIC(!strcmp(targ->user_table, ""), "No user table defined\n");
200 int ret = lua_to_user_table(prox_lua(), GLOBAL, targ->user_table, socket_id, &task->user_table);
201 PROX_PANIC(ret, "Failed to create user table from config:\n%s\n", get_lua_to_errors());
202 prox_sh_add_socket(socket_id, "user_table", task->user_table);
205 if (strcmp(targ->task_init->sub_mode_str, "trtcm")) {
206 task->sr_flows = prox_zmalloc(targ->n_flows * sizeof(*task->sr_flows), socket_id);
207 PROX_PANIC(task->sr_flows == NULL, "Failed to allocate flow contexts\n");
208 PROX_PANIC(!targ->cir, "Commited information rate is set to 0\n");
209 PROX_PANIC(!targ->cbs, "Commited information bucket size is set to 0\n");
210 PROX_PANIC(!targ->ebs, "Execess information bucket size is set to 0\n");
212 struct rte_meter_srtcm_params params = {
218 for (uint32_t i = 0; i < targ->n_flows; ++i) {
219 rte_meter_srtcm_config(&task->sr_flows[i], ¶ms);
223 task->tr_flows = prox_zmalloc(targ->n_flows * sizeof(*task->tr_flows), socket_id);
224 PROX_PANIC(task->tr_flows == NULL, "Failed to allocate flow contexts\n");
225 PROX_PANIC(!targ->pir, "Peak information rate is set to 0\n");
226 PROX_PANIC(!targ->cir, "Commited information rate is set to 0\n");
227 PROX_PANIC(!targ->pbs, "Peak information bucket size is set to 0\n");
228 PROX_PANIC(!targ->cbs, "Commited information bucket size is set to 0\n");
230 struct rte_meter_trtcm_params params = {
237 for (uint32_t i = 0; i < targ->n_flows; ++i) {
238 rte_meter_trtcm_config(&task->tr_flows[i], ¶ms);
242 for (uint32_t i = 0; i < 3; ++i) {
243 for (uint32_t j = 0; j < 3; ++j) {
244 task->police_act[i][j] = targ->police_act[i][j];
249 static struct task_init task_init_police = {
250 .mode_str = "police",
251 .init = init_task_police,
252 .handle = handle_police_bulk,
253 .flag_features = TASK_FEATURE_CLASSIFY,
254 .size = sizeof(struct task_police)
257 static struct task_init task_init_police2 = {
258 .mode_str = "police",
259 .sub_mode_str = "trtcm",
260 .init = init_task_police,
261 .handle = handle_police_tr_bulk,
262 .flag_features = TASK_FEATURE_CLASSIFY,
263 .size = sizeof(struct task_police)
266 __attribute__((constructor)) static void reg_task_police(void)
268 reg_task(&task_init_police);
269 reg_task(&task_init_police2);