Merge "docs: Update install and release docs for DPDK migration support"
[samplevnf.git] / VNFs / DPPD-PROX / handle_mplstag.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 "defines.h"
18 #include "hash_entry_types.h"
19 #include "mpls.h"
20 #include "prefetch.h"
21 #include "task_base.h"
22 #include "tx_pkt.h"
23 #include "task_init.h"
24 #include "prox_port_cfg.h"
25 #include "prox_cksum.h"
26 #include "thread_generic.h"
27 #include "prefetch.h"
28 #include "prox_assert.h"
29 #include "etypes.h"
30 #include "log.h"
31 #include "mbuf_utils.h"
32
33 struct task_unmpls {
34         struct task_base base;
35         uint8_t n_tags;
36 };
37
38 static void init_task_unmpls(__attribute__((unused)) struct task_base *tbase,
39                              __attribute__((unused)) struct task_args *targ)
40 {
41 }
42
43 static inline uint8_t handle_unmpls(__attribute__((unused)) struct task_unmpls *task, struct rte_mbuf *mbuf)
44 {
45         struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
46         struct mpls_hdr *mpls = (struct mpls_hdr *)(peth + 1);
47         uint32_t mpls_len = sizeof(struct mpls_hdr);
48         while (!(mpls->bytes & 0x00010000)) {
49                 mpls++;
50                 mpls_len += sizeof(struct mpls_hdr);
51         }
52                 uint32_t tot_eth_addr_len = 2*sizeof(struct ether_addr);
53                 rte_memcpy(((uint8_t *)peth) + mpls_len, peth, tot_eth_addr_len);
54         struct ipv4_hdr *ip = (struct ipv4_hdr *)(mpls + 1);
55         switch (ip->version_ihl >> 4) {
56         case 4:
57                 peth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, mpls_len);
58                 peth->ether_type = ETYPE_IPv4;
59                 return 0;
60         case 6:
61                 peth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, mpls_len);
62                 peth->ether_type = ETYPE_IPv6;
63                 return 0;
64         default:
65                 plog_warn("Failed Decoding MPLS Packet - neither IPv4 nor IPv6: version %u\n", ip->version_ihl);
66                 return OUT_DISCARD;
67         }
68 }
69
70 static int handle_unmpls_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
71 {
72         struct task_unmpls *task = (struct task_unmpls *)tbase;
73         uint8_t out[MAX_PKT_BURST];
74         uint16_t j;
75         prefetch_first(mbufs, n_pkts);
76         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
77 #ifdef PROX_PREFETCH_OFFSET
78                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
79                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
80 #endif
81                 out[j] = handle_unmpls(task, mbufs[j]);
82         }
83 #ifdef PROX_PREFETCH_OFFSET
84         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
85         for (; j < n_pkts; ++j) {
86                 out[j] = handle_unmpls(task, mbufs[j]);
87         }
88 #endif
89         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
90 }
91
92 static struct task_init task_init_unmpls = {
93         .mode_str = "unmpls",
94         .init = init_task_unmpls,
95         .handle = handle_unmpls_bulk,
96         .thread_x = thread_generic,
97         .size = sizeof(struct task_unmpls)
98 };
99
100 struct task_tagmpls {
101         struct task_base base;
102         uint8_t n_tags;
103 };
104
105 static void init_task_tagmpls(__attribute__((unused)) struct task_base *tbase,
106                               __attribute__((unused)) struct task_args *targ)
107 {
108 }
109
110 static inline uint8_t handle_tagmpls(__attribute__((unused)) struct task_tagmpls *task, struct rte_mbuf *mbuf)
111 {
112         struct ether_hdr *peth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf, 4);
113         PROX_ASSERT(peth);
114         rte_prefetch0(peth);
115         uint32_t mpls = 0;
116
117         uint32_t tot_eth_addr_len = 2*sizeof(struct ether_addr);
118         rte_memcpy(peth, ((uint8_t *)peth) + sizeof(struct mpls_hdr), tot_eth_addr_len);
119         *((uint32_t *)(peth + 1)) = mpls | 0x00010000; // Set BoS to 1
120         peth->ether_type = ETYPE_MPLSU;
121         return 0;
122 }
123
124 static int handle_tagmpls_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
125 {
126         struct task_tagmpls *task = (struct task_tagmpls *)tbase;
127         uint8_t out[MAX_PKT_BURST];
128         uint16_t j;
129         prefetch_first(mbufs, n_pkts);
130         for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
131 #ifdef PROX_PREFETCH_OFFSET
132                 PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
133                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
134 #endif
135                 out[j] = handle_tagmpls(task, mbufs[j]);
136         }
137 #ifdef PROX_PREFETCH_OFFSET
138         PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
139         for (; j < n_pkts; ++j) {
140                 out[j] = handle_tagmpls(task, mbufs[j]);
141         }
142 #endif
143         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
144 }
145
146 static struct task_init task_init_tagmpls = {
147         .mode_str = "tagmpls",
148         .init = init_task_tagmpls,
149         .handle = handle_tagmpls_bulk,
150         .size = sizeof(struct task_tagmpls)
151 };
152
153 __attribute__((constructor)) static void reg_task_mplstag(void)
154 {
155         reg_task(&task_init_unmpls);
156         reg_task(&task_init_tagmpls);
157 }