Prepare for DPDK 19.08 support
[samplevnf.git] / VNFs / DPPD-PROX / handle_classify.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 <stdio.h>
19 #include <string.h>
20 #include <rte_version.h>
21
22 #include "prox_lua.h"
23 #include "prox_lua_types.h"
24
25 #include "lconf.h"
26 #include "task_base.h"
27 #include "task_init.h"
28 #include "defines.h"
29 #include "prefetch.h"
30 #include "qinq.h"
31 #include "prox_cfg.h"
32 #include "log.h"
33 #include "quit.h"
34 #include "prox_shared.h"
35 #include "handle_sched.h"
36 #include "prox_compat.h"
37
38 struct task_classify {
39         struct task_base    base;
40         uint16_t           *user_table;
41         uint8_t             *dscp;
42         struct rte_sched_port *sched_port;
43 };
44
45 static inline void handle_classify(struct task_classify *task, struct rte_mbuf *mbuf)
46 {
47         const struct qinq_hdr *pqinq = rte_pktmbuf_mtod(mbuf, const struct qinq_hdr *);
48
49         uint32_t qinq = PKT_TO_LUTQINQ(pqinq->svlan.vlan_tci, pqinq->cvlan.vlan_tci);
50
51         /* Traffic class can be set by ACL task. If this is the case,
52            don't overwrite it using dscp. Instead, use the
53            traffic class that had been set. */
54
55         uint32_t prev_tc;
56 #if RTE_VERSION >= RTE_VERSION_NUM(1,8,0,0)
57         uint32_t dummy;
58         prox_rte_sched_port_pkt_read_tree_path(task->sched_port, mbuf, &dummy, &dummy, &prev_tc, &dummy);
59 #else
60         struct rte_sched_port_hierarchy *sched = (struct rte_sched_port_hierarchy *) &mbuf->pkt.hash.sched;
61         prev_tc = sched->traffic_class;
62 #endif
63
64         const prox_rte_ipv4_hdr *ipv4_hdr = (const prox_rte_ipv4_hdr *)(pqinq + 1);
65         uint8_t dscp = task->dscp[ipv4_hdr->type_of_service >> 2];
66
67         uint8_t queue = dscp & 0x3;
68         uint8_t tc = prev_tc? prev_tc : dscp >> 2;
69
70         prox_rte_sched_port_pkt_write(task->sched_port, mbuf, 0, task->user_table[qinq], tc, queue, 0);
71 }
72
73 static int handle_classify_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
74 {
75         struct task_classify *task = (struct task_classify *)tbase;
76
77         uint16_t j;
78 #ifdef PROX_PREFETCH_OFFSET
79         for (j = 0; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
80                 prefetch_nta(mbufs[j]);
81         }
82         for (j = 1; j < PROX_PREFETCH_OFFSET && j < n_pkts; ++j) {
83                 prefetch_nta(rte_pktmbuf_mtod(mbufs[j - 1], void *));
84         }
85 #endif
86         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
87 #ifdef PROX_PREFETCH_OFFSET
88                 prefetch_nta(mbufs[j + PREFETCH_OFFSET]);
89                 prefetch_nta(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
90 #endif
91                 handle_classify(task, mbufs[j]);
92         }
93 #ifdef PROX_PREFETCH_OFFSET
94         prefetch_nta(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
95         for (; j < n_pkts; ++j) {
96                 handle_classify(task, mbufs[j]);
97         }
98 #endif
99
100         return task->base.tx_pkt(&task->base, mbufs, n_pkts, NULL);
101 }
102
103 static void init_task_classify(struct task_base *tbase, struct task_args *targ)
104 {
105         struct task_classify *task = (struct task_classify *)tbase;
106         const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);
107
108         task->user_table = prox_sh_find_socket(socket_id, "user_table");
109         if (!task->user_table) {
110                 PROX_PANIC(!strcmp(targ->user_table, ""), "No user table defined\n");
111                 int ret = lua_to_user_table(prox_lua(), GLOBAL, targ->user_table, socket_id, &task->user_table);
112                 PROX_PANIC(ret, "Failed to create user table from config:\n%s\n", get_lua_to_errors());
113                 prox_sh_add_socket(socket_id, "user_table", task->user_table);
114         }
115
116         PROX_PANIC(!strcmp(targ->dscp, ""), "DSCP table not specified\n");
117         task->dscp = prox_sh_find_socket(socket_id, targ->dscp);
118         if (!task->dscp) {
119                 int ret = lua_to_dscp(prox_lua(), GLOBAL, targ->dscp, socket_id, &task->dscp);
120                 PROX_PANIC(ret, "Failed to create dscp table from config\n");
121                 prox_sh_add_socket(socket_id, targ->dscp, task->dscp);
122         }
123         int rc = init_port_sched(&task->sched_port, targ);
124         PROX_PANIC(rc, "Did not find any QoS task to transmit to => undefined sched_port parameters\n");
125 }
126
127 static struct task_init task_init_classify = {
128         .mode_str = "classify",
129         .init = init_task_classify,
130         .handle = handle_classify_bulk,
131         .flag_features = TASK_FEATURE_NEVER_DISCARDS,
132         .size = sizeof(struct task_classify)
133 };
134
135 __attribute__((constructor)) static void reg_task_classify(void)
136 {
137         reg_task(&task_init_classify);
138 }