Adding centos.json to be used with packer to generate a VM with PROX
[samplevnf.git] / VNFs / DPPD-PROX / handle_qos.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 <rte_ip.h>
18 #include <rte_mbuf.h>
19 #include <rte_sched.h>
20
21 #include "prox_lua.h"
22 #include "prox_lua_types.h"
23
24 #include "etypes.h"
25 #include "stats.h"
26 #include "task_init.h"
27 #include "lconf.h"
28 #include "task_base.h"
29 #include "defines.h"
30 #include "prefetch.h"
31 #include "handle_qos.h"
32 #include "log.h"
33 #include "quit.h"
34 #include "qinq.h"
35 #include "prox_cfg.h"
36 #include "prox_shared.h"
37
38 struct task_qos {
39         struct task_base base;
40         struct rte_sched_port *sched_port;
41         uint16_t *user_table;
42         uint8_t  *dscp;
43         uint32_t nb_buffered_pkts;
44         uint8_t runtime_flags;
45 };
46
47 uint32_t task_qos_n_pkts_buffered(struct task_base *tbase)
48 {
49         struct task_qos *task = (struct task_qos *)tbase;
50
51         return task->nb_buffered_pkts;
52 }
53
54 static inline int handle_qos_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
55 {
56         struct task_qos *task = (struct task_qos *)tbase;
57         int ret = 0;
58
59         if (n_pkts) {
60                 if (task->runtime_flags & TASK_CLASSIFY) {
61                         uint16_t j;
62 #ifdef PROX_PREFETCH_OFFSET
63                         for (j = 0; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
64                                 prefetch_nta(mbufs[j]);
65                         }
66                         for (j = 1; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
67                                 prefetch_nta(rte_pktmbuf_mtod(mbufs[j - 1], void *));
68                         }
69 #endif
70                         uint8_t queue = 0;
71                         uint8_t tc = 0;
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;
81                                 } else {
82                                         // Keep queue and tc = 0 for other packet types like ARP
83                                         queue = 0;
84                                         tc = 0;
85                                 }
86
87                                 rte_sched_port_pkt_write(mbufs[j], 0, task->user_table[qinq], tc, queue, 0);
88                         }
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;
98                                 } else {
99                                         // Keep queue and tc = 0 for other packet types like ARP
100                                         queue = 0;
101                                         tc = 0;
102                                 }
103
104                                 rte_sched_port_pkt_write(mbufs[j], 0, task->user_table[qinq], tc, queue, 0);
105                         }
106 #endif
107                 }
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);
111         }
112
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);
118                 }
119         }
120         return ret;
121 }
122
123 static void init_task_qos(struct task_base *tbase, struct task_args *targ)
124 {
125         struct task_qos *task = (struct task_qos *)tbase;
126         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
127         char name[64];
128
129         snprintf(name, sizeof(name), "qos_sched_port_%u_%u", targ->lconf->id, 0);
130
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);
134
135         PROX_PANIC(task->sched_port == NULL, "failed to create sched_port");
136
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);
140
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);
145         }
146
147         task->runtime_flags = targ->runtime_flags;
148
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);
155         }
156
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);
160                 if (!task->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);
164                 }
165         }
166 }
167
168 static struct task_init task_init_qos = {
169         .mode_str = "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)
174 };
175
176 __attribute__((constructor)) static void reg_task_qos(void)
177 {
178         reg_task(&task_init_qos);
179 }