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.
20 #include "task_init.h"
21 #include "task_base.h"
25 #include "handle_swap.h"
26 #include "prox_port_cfg.h"
33 struct task_base base;
34 uint8_t src_dst_mac[12];
35 uint32_t runtime_flags;
38 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
40 struct ether_hdr *hdr;
41 struct ether_addr mac;
43 if (unlikely((task->runtime_flags & (TASK_ARG_DST_MAC_SET|TASK_ARG_SRC_MAC_SET)) == (TASK_ARG_DST_MAC_SET|TASK_ARG_SRC_MAC_SET))) {
44 /* Source and Destination mac hardcoded */
45 hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
46 rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
48 hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
49 if (likely((task->runtime_flags & TASK_ARG_SRC_MAC_SET) == 0)) {
50 /* dst mac will be used as src mac */
51 ether_addr_copy(&hdr->d_addr, &mac);
54 if (unlikely(task->runtime_flags & TASK_ARG_DST_MAC_SET))
55 ether_addr_copy((struct ether_addr *)&task->src_dst_mac[0], &hdr->d_addr);
57 ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
59 if (unlikely(task->runtime_flags & TASK_ARG_SRC_MAC_SET)) {
60 ether_addr_copy((struct ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
62 ether_addr_copy(&mac, &hdr->s_addr);
66 static inline int handle_arp_request(struct task_swap *task, struct ether_hdr_arp *hdr_arp, struct ether_addr *s_addr, uint32_t ip)
68 if ((hdr_arp->arp.data.tpa == ip) || (ip == 0)) {
69 build_arp_reply(hdr_arp, s_addr);
71 } else if (task->runtime_flags & TASK_MULTIPLE_MAC) {
72 struct ether_addr tmp_s_addr;
73 create_mac(hdr_arp, &tmp_s_addr);
74 build_arp_reply(hdr_arp, &tmp_s_addr);
77 plogx_dbg("Received ARP on unexpected IP %x, expecting %x\n", rte_be_to_cpu_32(hdr_arp->arp.data.tpa), rte_be_to_cpu_32(ip));
83 * swap mode does not send arp requests, so does not expect arp replies
84 * Need to understand later whether we must send arp requests
86 static inline int handle_arp_replies(struct task_swap *task, struct ether_hdr_arp *hdr_arp)
91 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
93 struct task_swap *task = (struct task_swap *)tbase;
94 struct ether_hdr *hdr;
95 struct ether_addr mac;
96 struct ipv4_hdr *ip_hdr;
97 struct udp_hdr *udp_hdr;
100 uint8_t out[64] = {0};
101 struct mpls_hdr *mpls;
102 uint32_t mpls_len = 0;
103 struct qinq_hdr *qinq;
104 struct vlan_hdr *vlan;
105 struct ether_hdr_arp *hdr_arp;
108 for (j = 0; j < n_pkts; ++j) {
111 for (j = 0; j < n_pkts; ++j) {
112 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
115 for (uint16_t j = 0; j < n_pkts; ++j) {
116 hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr *);
117 switch (hdr->ether_type) {
119 mpls = (struct mpls_hdr *)(hdr + 1);
120 while (!(mpls->bytes & 0x00010000)) {
122 mpls_len += sizeof(struct mpls_hdr);
124 mpls_len += sizeof(struct mpls_hdr);
125 ip_hdr = (struct ipv4_hdr *)(mpls + 1);
128 qinq = (struct qinq_hdr *)hdr;
129 if (qinq->cvlan.eth_proto != ETYPE_VLAN) {
130 plog_warn("Unexpected proto in QinQ = %#04x\n", qinq->cvlan.eth_proto);
131 out[j] = OUT_DISCARD;
134 ip_hdr = (struct ipv4_hdr *)(qinq + 1);
137 vlan = (struct vlan_hdr *)(hdr + 1);
138 if (vlan->eth_proto == ETYPE_IPv4) {
139 ip_hdr = (struct ipv4_hdr *)(vlan + 1);
140 } else if (vlan->eth_proto == ETYPE_VLAN) {
141 vlan = (struct vlan_hdr *)(vlan + 1);
142 if (vlan->eth_proto == ETYPE_IPv4) {
143 ip_hdr = (struct ipv4_hdr *)(vlan + 1);
145 else if (vlan->eth_proto == ETYPE_IPv6) {
146 plog_warn("Unsupported IPv6\n");
147 out[j] = OUT_DISCARD;
151 plog_warn("Unsupported packet type\n");
152 out[j] = OUT_DISCARD;
156 plog_warn("Unsupported packet type\n");
157 out[j] = OUT_DISCARD;
162 ip_hdr = (struct ipv4_hdr *)(hdr + 1);
165 plog_warn("Unsupported IPv6\n");
166 out[j] = OUT_DISCARD;
169 out[j] = OUT_DISCARD;
172 plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
173 out[j] = OUT_DISCARD;
176 udp_hdr = (struct udp_hdr *)(ip_hdr + 1);
177 ip = ip_hdr->dst_addr;
178 ip_hdr->dst_addr = ip_hdr->src_addr;
179 ip_hdr->src_addr = ip;
180 if (ip_hdr->next_proto_id == IPPROTO_GRE) {
181 struct gre_hdr *pgre = (struct gre_hdr *)(ip_hdr + 1);
182 struct ipv4_hdr *inner_ip_hdr = ((struct ipv4_hdr *)(pgre + 1));
183 ip = inner_ip_hdr->dst_addr;
184 inner_ip_hdr->dst_addr = inner_ip_hdr->src_addr;
185 inner_ip_hdr->src_addr = ip;
186 udp_hdr = (struct udp_hdr *)(inner_ip_hdr + 1);
187 port = udp_hdr->dst_port;
188 udp_hdr->dst_port = udp_hdr->src_port;
189 udp_hdr->src_port = port;
191 port = udp_hdr->dst_port;
192 udp_hdr->dst_port = udp_hdr->src_port;
193 udp_hdr->src_port = port;
195 write_src_and_dst_mac(task, mbufs[j]);
197 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
200 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
202 struct task_swap *task = (struct task_swap *)tbase;
203 struct ether_addr *src_addr, *dst_addr;
206 * Destination MAC can come from
207 * - pre-configured mac in case 'dst mac=xx:xx:xx:xx:xx:xx' in config file
208 * - src mac from the packet in case 'dst mac=packet' in config file
209 * - not written in case 'dst mac=no' in config file
210 * - (default - no 'dst mac') src mac from the packet
211 * Source MAC can come from
212 * - pre-configured mac in case 'src mac=xx:xx:xx:xx:xx:xx' in config file
213 * - dst mac from the packet in case 'src mac=packet' in config file
214 * - not written in case 'src mac=no' in config file
215 * - (default - no 'src mac') if (tx_port) port mac
216 * - (default - no 'src mac') if (no tx_port) dst mac from the packet
219 if (targ->flags & TASK_ARG_DST_MAC_SET) {
220 dst_addr = &targ->edaddr;
221 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
224 if (targ->flags & TASK_ARG_SRC_MAC_SET) {
225 src_addr = &targ->esaddr;
226 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
227 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
228 } else if (targ->nb_txports) {
229 src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
230 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
231 if (targ->flags & TASK_ARG_HW_SRC_MAC){
232 targ->flags |= TASK_ARG_SRC_MAC_SET;
233 plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
236 task->runtime_flags = targ->flags;
239 static struct task_init task_init_swap = {
241 .init = init_task_swap,
242 .handle = handle_swap_bulk,
243 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
244 .size = sizeof(struct task_swap),
245 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
248 static struct task_init task_init_swap_arp = {
250 .sub_mode_str = "l3",
251 .init = init_task_swap,
252 .handle = handle_swap_bulk,
253 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
254 .size = sizeof(struct task_swap),
255 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
258 __attribute__((constructor)) static void reg_task_swap(void)
260 reg_task(&task_init_swap);
261 reg_task(&task_init_swap_arp);