Merge "Add l3 support for tasks without physical tx ports"
[samplevnf.git] / VNFs / DPPD-PROX / handle_qinq_encap4.h
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 #ifndef _HANDLE_QINQ_ENCAP4_H_
18 #define _HANDLE_QINQ_ENCAP4_H_
19
20 #include <rte_ip.h>
21 #include <rte_ether.h>
22
23 #include "log.h"
24 #include "prox_assert.h"
25 #include "etypes.h"
26 #include "mpls.h"
27 #include "task_init.h"
28
29 struct task_qinq_encap4 {
30         struct task_base base;
31         struct rte_table_hash  *cpe_table;
32         uint16_t         qinq_tag;
33         uint64_t         src_mac[PROX_MAX_PORTS];
34         int              offload_crc;
35         uint8_t          runtime_flags;
36         uint8_t          *dscp;
37         uint64_t         keys[64];
38         struct rte_mbuf* fake_packets[64];
39         uint64_t         cpe_timeout;
40 #ifdef ENABLE_EXTRA_USER_STATISTICS
41         uint32_t        *stats_per_user;
42         uint32_t        n_users;
43 #endif
44 };
45
46 struct qinq_gre_entry {
47         uint16_t svlan;
48         uint16_t cvlan;
49         uint32_t gre_id;
50         uint32_t user;
51         uint32_t rss; // RSS based on Toeplitz_hash(svlan and cvlan)
52 };
53
54 struct qinq_gre_map {
55         uint32_t              count;
56         struct qinq_gre_entry entries[0];
57 };
58
59 struct qinq_gre_map *get_qinq_gre_map(struct task_args *targ);
60
61 struct task_args;
62 struct prox_shared;
63
64 void init_qinq_gre_table(struct task_args *targ, struct qinq_gre_map *qinq_gre_map);
65 void init_qinq_gre_hash(struct task_args *targ, struct qinq_gre_map *qinq_gre_map);
66 void init_cpe4_table(struct task_args *targ);
67 void init_cpe4_hash(struct task_args *targ);
68
69 static inline uint8_t mpls_untag(struct rte_mbuf *mbuf)
70 {
71         struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
72         const uint16_t eth_type = peth->ether_type;
73
74         if (eth_type == ETYPE_MPLSU) {
75                 struct ether_hdr *pneweth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, 4);
76                 const struct mpls_hdr *mpls = (const struct mpls_hdr *)(peth + 1);
77
78                 if (mpls->bos == 0) {
79                         // Double MPLS tag
80                         pneweth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, 4);
81                         PROX_ASSERT(pneweth);
82                 }
83
84                 const struct ipv4_hdr *pip = (const struct ipv4_hdr *)(pneweth + 1);
85                 if ((pip->version_ihl >> 4) == 4) {
86                         pneweth->ether_type = ETYPE_IPv4;
87                         return 1;
88                 }
89                 else if ((pip->version_ihl >> 4) == 6) {
90                         pneweth->ether_type = ETYPE_IPv6;
91                         return 1;
92                 }
93
94                 plog_info("Error removing MPLS: unexpected IP version: %d\n", pip->version_ihl >> 4);
95                 return 0;
96         }
97         if (eth_type != ETYPE_LLDP) {
98                 plog_info("Error Removing MPLS: ether_type = %#06x\n", eth_type);
99         }
100         return 0;
101 }
102
103 #endif /* _HANDLE_QINQ_ENCAP4_H_ */