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.
19 #include <rte_sched.h>
22 #include "prox_lua_types.h"
26 #include "task_init.h"
28 #include "task_base.h"
31 #include "handle_qos.h"
36 #include "prox_shared.h"
39 struct task_base base;
40 struct rte_sched_port *sched_port;
43 uint32_t nb_buffered_pkts;
44 uint8_t runtime_flags;
47 uint32_t task_qos_n_pkts_buffered(struct task_base *tbase)
49 struct task_qos *task = (struct task_qos *)tbase;
51 return task->nb_buffered_pkts;
54 static inline int handle_qos_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
56 struct task_qos *task = (struct task_qos *)tbase;
60 if (task->runtime_flags & TASK_CLASSIFY) {
62 #ifdef PROX_PREFETCH_OFFSET
63 for (j = 0; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
64 prefetch_nta(mbufs[j]);
66 for (j = 1; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
67 prefetch_nta(rte_pktmbuf_mtod(mbufs[j - 1], void *));
72 for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
73 prefetch_nta(mbufs[j + PREFETCH_OFFSET]);
74 prefetch_nta(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
75 const struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbufs[j], const struct qinq_hdr *);
76 uint32_t qinq = PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
77 if (pqinq->ether_type == ETYPE_IPv4) {
78 const struct ipv4_hdr *ipv4_hdr = (const struct ipv4_hdr *)(pqinq + 1);
79 queue = task->dscp[ipv4_hdr->type_of_service >> 2] & 0x3;
80 tc = task->dscp[ipv4_hdr->type_of_service >> 2] >> 2;
82 // Keep queue and tc = 0 for other packet types like ARP
87 rte_sched_port_pkt_write(mbufs[j], 0, task->user_table[qinq], tc, queue, 0);
89 #ifdef PROX_PREFETCH_OFFSET
90 prefetch_nta(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
91 for (; j < n_pkts; ++j) {
92 const struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbufs[j], const struct qinq_hdr *);
93 uint32_t qinq = PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
94 if (pqinq->ether_type == ETYPE_IPv4) {
95 const struct ipv4_hdr *ipv4_hdr = (const struct ipv4_hdr *)(pqinq + 1);
96 queue = task->dscp[ipv4_hdr->type_of_service >> 2] & 0x3;
97 tc = task->dscp[ipv4_hdr->type_of_service >> 2] >> 2;
99 // Keep queue and tc = 0 for other packet types like ARP
104 rte_sched_port_pkt_write(mbufs[j], 0, task->user_table[qinq], tc, queue, 0);
108 int16_t ret = rte_sched_port_enqueue(task->sched_port, mbufs, n_pkts);
109 task->nb_buffered_pkts += ret;
110 TASK_STATS_ADD_DROP_DISCARD(&task->base.aux->stats, n_pkts - ret);
113 if (task->nb_buffered_pkts) {
114 n_pkts = rte_sched_port_dequeue(task->sched_port, mbufs, 32);
115 if (likely(n_pkts)) {
116 task->nb_buffered_pkts -= n_pkts;
117 ret = task->base.tx_pkt(&task->base, mbufs, n_pkts, NULL);
123 static void init_task_qos(struct task_base *tbase, struct task_args *targ)
125 struct task_qos *task = (struct task_qos *)tbase;
126 const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
129 snprintf(name, sizeof(name), "qos_sched_port_%u_%u", targ->lconf->id, 0);
131 targ->qos_conf.port_params.name = name;
132 targ->qos_conf.port_params.socket = socket_id;
133 task->sched_port = rte_sched_port_config(&targ->qos_conf.port_params);
135 PROX_PANIC(task->sched_port == NULL, "failed to create sched_port");
137 plog_info("number of pipes: %d\n\n", targ->qos_conf.port_params.n_pipes_per_subport);
138 int err = rte_sched_subport_config(task->sched_port, 0, targ->qos_conf.subport_params);
139 PROX_PANIC(err != 0, "Failed setting up sched_port subport, error: %d", err);
141 /* only single subport and single pipe profile is supported */
142 for (uint32_t pipe = 0; pipe < targ->qos_conf.port_params.n_pipes_per_subport; ++pipe) {
143 err = rte_sched_pipe_config(task->sched_port, 0 , pipe, 0);
144 PROX_PANIC(err != 0, "failed setting up sched port pipe, error: %d", err);
147 task->runtime_flags = targ->runtime_flags;
149 task->user_table = prox_sh_find_socket(socket_id, "user_table");
150 if (!task->user_table) {
151 PROX_PANIC(!strcmp(targ->user_table, ""), "No user table defined\n");
152 int ret = lua_to_user_table(prox_lua(), GLOBAL, targ->user_table, socket_id, &task->user_table);
153 PROX_PANIC(ret, "Failed to create user table from config:\n%s\n", get_lua_to_errors());
154 prox_sh_add_socket(socket_id, "user_table", task->user_table);
157 if (task->runtime_flags & TASK_CLASSIFY) {
158 PROX_PANIC(!strcmp(targ->dscp, ""), "DSCP table not specified\n");
159 task->dscp = prox_sh_find_socket(socket_id, targ->dscp);
161 int ret = lua_to_dscp(prox_lua(), GLOBAL, targ->dscp, socket_id, &task->dscp);
162 PROX_PANIC(ret, "Failed to create dscp table from config:\n%s\n", get_lua_to_errors());
163 prox_sh_add_socket(socket_id, targ->dscp, task->dscp);
168 static struct task_init task_init_qos = {
170 .init = init_task_qos,
171 .handle = handle_qos_bulk,
172 .flag_features = TASK_FEATURE_CLASSIFY | TASK_FEATURE_NEVER_DISCARDS | TASK_FEATURE_MULTI_RX | TASK_FEATURE_ZERO_RX,
173 .size = sizeof(struct task_qos)
176 __attribute__((constructor)) static void reg_task_qos(void)
178 reg_task(&task_init_qos);