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_version.h>
23 #include "prox_lua_types.h"
26 #include "task_base.h"
27 #include "task_init.h"
34 #include "prox_shared.h"
36 struct task_classify {
37 struct task_base base;
42 static inline void handle_classify(struct task_classify *task, struct rte_mbuf *mbuf)
44 const struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbuf, const struct qinq_hdr *);
46 uint32_t qinq = PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
48 /* Traffic class can be set by ACL task. If this is the case,
49 don't overwrite it using dscp. Instead, use the
50 traffic class that had been set. */
53 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
55 rte_sched_port_pkt_read_tree_path(mbuf, &dummy, &dummy, &prev_tc, &dummy);
57 struct rte_sched_port_hierarchy *sched = (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
58 prev_tc = sched->traffic_class;
61 const struct ipv4_hdr *ipv4_hdr = (const struct ipv4_hdr *)(pqinq + 1);
62 uint8_t dscp = task->dscp[ipv4_hdr->type_of_service >> 2];
64 uint8_t queue = dscp & 0x3;
65 uint8_t tc = prev_tc? prev_tc : dscp >> 2;
67 rte_sched_port_pkt_write(mbuf, 0, task->user_table[qinq], tc, queue, 0);
70 static int handle_classify_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
72 struct task_classify *task = (struct task_classify *)tbase;
75 #ifdef PROX_PREFETCH_OFFSET
76 for (j = 0; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
77 prefetch_nta(mbufs[j]);
79 for (j = 1; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
80 prefetch_nta(rte_pktmbuf_mtod(mbufs[j - 1], void *));
83 for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
84 #ifdef PROX_PREFETCH_OFFSET
85 prefetch_nta(mbufs[j + PREFETCH_OFFSET]);
86 prefetch_nta(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
88 handle_classify(task, mbufs[j]);
90 #ifdef PROX_PREFETCH_OFFSET
91 prefetch_nta(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
92 for (; j < n_pkts; ++j) {
93 handle_classify(task, mbufs[j]);
97 return task->base.tx_pkt(&task->base, mbufs, n_pkts, NULL);
100 static void init_task_classify(struct task_base *tbase, struct task_args *targ)
102 struct task_classify *task = (struct task_classify *)tbase;
103 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
105 task->user_table = prox_sh_find_socket(socket_id, "user_table");
106 if (!task->user_table) {
107 PROX_PANIC(!strcmp(targ->user_table, ""), "No user table defined\n");
108 int ret = lua_to_user_table(prox_lua(), GLOBAL, targ->user_table, socket_id, &task->user_table);
109 PROX_PANIC(ret, "Failed to create user table from config:\n%s\n", get_lua_to_errors());
110 prox_sh_add_socket(socket_id, "user_table", task->user_table);
113 PROX_PANIC(!strcmp(targ->dscp, ""), "DSCP table not specified\n");
114 task->dscp = prox_sh_find_socket(socket_id, targ->dscp);
116 int ret = lua_to_dscp(prox_lua(), GLOBAL, targ->dscp, socket_id, &task->dscp);
117 PROX_PANIC(ret, "Failed to create dscp table from config\n");
118 prox_sh_add_socket(socket_id, targ->dscp, task->dscp);
122 static struct task_init task_init_classify = {
123 .mode_str = "classify",
124 .init = init_task_classify,
125 .handle = handle_classify_bulk,
126 .flag_features = TASK_FEATURE_NEVER_DISCARDS,
127 .size = sizeof(struct task_classify)
130 __attribute__((constructor)) static void reg_task_classify(void)
132 reg_task(&task_init_classify);