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.
18 #include "hash_entry_types.h"
21 #include "task_base.h"
23 #include "task_init.h"
24 #include "prox_port_cfg.h"
25 #include "prox_cksum.h"
26 #include "thread_generic.h"
28 #include "prox_assert.h"
31 #include "mbuf_utils.h"
34 struct task_base base;
38 static void init_task_unmpls(__attribute__((unused)) struct task_base *tbase,
39 __attribute__((unused)) struct task_args *targ)
43 static inline uint8_t handle_unmpls(__attribute__((unused)) struct task_unmpls *task, struct rte_mbuf *mbuf)
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)) {
50 mpls_len += sizeof(struct mpls_hdr);
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) {
57 peth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, mpls_len);
58 peth->ether_type = ETYPE_IPv4;
61 peth = (struct ether_hdr *)rte_pktmbuf_adj(mbuf, mpls_len);
62 peth->ether_type = ETYPE_IPv6;
65 plog_warn("Failed Decoding MPLS Packet - neither IPv4 nor IPv6: version %u\n", ip->version_ihl);
70 static int handle_unmpls_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
72 struct task_unmpls *task = (struct task_unmpls *)tbase;
73 uint8_t out[MAX_PKT_BURST];
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 *));
81 out[j] = handle_unmpls(task, mbufs[j]);
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]);
89 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
92 static struct task_init task_init_unmpls = {
94 .init = init_task_unmpls,
95 .handle = handle_unmpls_bulk,
96 .thread_x = thread_generic,
97 .size = sizeof(struct task_unmpls)
100 struct task_tagmpls {
101 struct task_base base;
105 static void init_task_tagmpls(__attribute__((unused)) struct task_base *tbase,
106 __attribute__((unused)) struct task_args *targ)
110 static inline uint8_t handle_tagmpls(__attribute__((unused)) struct task_tagmpls *task, struct rte_mbuf *mbuf)
112 struct ether_hdr *peth = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf, 4);
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;
124 static int handle_tagmpls_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
126 struct task_tagmpls *task = (struct task_tagmpls *)tbase;
127 uint8_t out[MAX_PKT_BURST];
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 *));
135 out[j] = handle_tagmpls(task, mbufs[j]);
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]);
143 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
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)
153 __attribute__((constructor)) static void reg_task_mplstag(void)
155 reg_task(&task_init_unmpls);
156 reg_task(&task_init_tagmpls);