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;
40 static void task_update_config(struct task_swap *task)
42 if (unlikely(task->ip != task->tmp_ip))
43 task->ip = task->tmp_ip;
46 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
48 struct ether_hdr *hdr;
49 struct ether_addr mac;
51 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))) {
52 /* Source and Destination mac hardcoded */
53 hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
54 rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
56 hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
57 if (likely((task->runtime_flags & TASK_ARG_SRC_MAC_SET) == 0)) {
58 /* dst mac will be used as src mac */
59 ether_addr_copy(&hdr->d_addr, &mac);
62 if (unlikely(task->runtime_flags & TASK_ARG_DST_MAC_SET))
63 ether_addr_copy((struct ether_addr *)&task->src_dst_mac[0], &hdr->d_addr);
65 ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
67 if (unlikely(task->runtime_flags & TASK_ARG_SRC_MAC_SET)) {
68 ether_addr_copy((struct ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
70 ether_addr_copy(&mac, &hdr->s_addr);
74 static inline int handle_arp_request(struct task_swap *task, struct ether_hdr_arp *hdr_arp, struct ether_addr *s_addr, uint32_t ip)
76 if ((hdr_arp->arp.data.tpa == ip) || (ip == 0)) {
77 prepare_arp_reply(hdr_arp, s_addr);
78 memcpy(hdr_arp->ether_hdr.d_addr.addr_bytes, hdr_arp->ether_hdr.s_addr.addr_bytes, 6);
79 memcpy(hdr_arp->ether_hdr.s_addr.addr_bytes, s_addr, 6);
81 } else if (task->runtime_flags & TASK_MULTIPLE_MAC) {
82 struct ether_addr tmp_s_addr;
83 create_mac(hdr_arp, &tmp_s_addr);
84 prepare_arp_reply(hdr_arp, &tmp_s_addr);
85 memcpy(hdr_arp->ether_hdr.d_addr.addr_bytes, hdr_arp->ether_hdr.s_addr.addr_bytes, 6);
86 memcpy(hdr_arp->ether_hdr.s_addr.addr_bytes, &tmp_s_addr, 6);
89 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));
95 * swap mode does not send arp requests, so does not expect arp replies
96 * Need to understand later whether we must send arp requests
98 static inline int handle_arp_replies(struct task_swap *task, struct ether_hdr_arp *hdr_arp)
103 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
105 struct task_swap *task = (struct task_swap *)tbase;
106 struct ether_hdr *hdr;
107 struct ether_addr mac;
108 struct ipv4_hdr *ip_hdr;
109 struct udp_hdr *udp_hdr;
112 uint8_t out[64] = {0};
113 struct mpls_hdr *mpls;
114 uint32_t mpls_len = 0;
115 struct qinq_hdr *qinq;
116 struct vlan_hdr *vlan;
117 struct ether_hdr_arp *hdr_arp;
120 for (j = 0; j < n_pkts; ++j) {
123 for (j = 0; j < n_pkts; ++j) {
124 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
127 for (uint16_t j = 0; j < n_pkts; ++j) {
128 hdr = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr *);
129 switch (hdr->ether_type) {
131 hdr_arp = rte_pktmbuf_mtod(mbufs[j], struct ether_hdr_arp *);
132 if (arp_is_gratuitous(hdr_arp)) {
133 plog_info("Received gratuitous packet \n");
134 out[j] = OUT_DISCARD;
135 } else if (hdr_arp->arp.oper == ARP_REQUEST) {
136 out[j] = handle_arp_request(task, hdr_arp, (struct ether_addr *)&task->src_dst_mac[6], task->ip);
137 } else if (hdr_arp->arp.oper == ARP_REPLY) {
138 out[j] = handle_arp_replies(task, hdr_arp);
140 plog_info("Received unexpected ARP operation %d\n", hdr_arp->arp.oper);
141 out[j] = OUT_DISCARD;
145 mpls = (struct mpls_hdr *)(hdr + 1);
146 while (!(mpls->bytes & 0x00010000)) {
148 mpls_len += sizeof(struct mpls_hdr);
150 mpls_len += sizeof(struct mpls_hdr);
151 ip_hdr = (struct ipv4_hdr *)(mpls + 1);
154 qinq = (struct qinq_hdr *)hdr;
155 if (qinq->cvlan.eth_proto != ETYPE_VLAN) {
156 plog_warn("Unexpected proto in QinQ = %#04x\n", qinq->cvlan.eth_proto);
157 out[j] = OUT_DISCARD;
160 ip_hdr = (struct ipv4_hdr *)(qinq + 1);
163 vlan = (struct vlan_hdr *)(hdr + 1);
164 if (vlan->eth_proto == ETYPE_IPv4) {
165 ip_hdr = (struct ipv4_hdr *)(vlan + 1);
166 } else if (vlan->eth_proto == ETYPE_VLAN) {
167 vlan = (struct vlan_hdr *)(vlan + 1);
168 if (vlan->eth_proto == ETYPE_IPv4) {
169 ip_hdr = (struct ipv4_hdr *)(vlan + 1);
171 else if (vlan->eth_proto == ETYPE_IPv6) {
172 plog_warn("Unsupported IPv6\n");
173 out[j] = OUT_DISCARD;
177 plog_warn("Unsupported packet type\n");
178 out[j] = OUT_DISCARD;
182 plog_warn("Unsupported packet type\n");
183 out[j] = OUT_DISCARD;
188 ip_hdr = (struct ipv4_hdr *)(hdr + 1);
191 plog_warn("Unsupported IPv6\n");
192 out[j] = OUT_DISCARD;
195 out[j] = OUT_DISCARD;
198 plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
199 out[j] = OUT_DISCARD;
202 udp_hdr = (struct udp_hdr *)(ip_hdr + 1);
203 ip = ip_hdr->dst_addr;
204 ip_hdr->dst_addr = ip_hdr->src_addr;
205 ip_hdr->src_addr = ip;
206 if (ip_hdr->next_proto_id == IPPROTO_GRE) {
207 struct gre_hdr *pgre = (struct gre_hdr *)(ip_hdr + 1);
208 struct ipv4_hdr *inner_ip_hdr = ((struct ipv4_hdr *)(pgre + 1));
209 ip = inner_ip_hdr->dst_addr;
210 inner_ip_hdr->dst_addr = inner_ip_hdr->src_addr;
211 inner_ip_hdr->src_addr = ip;
212 udp_hdr = (struct udp_hdr *)(inner_ip_hdr + 1);
213 port = udp_hdr->dst_port;
214 udp_hdr->dst_port = udp_hdr->src_port;
215 udp_hdr->src_port = port;
217 port = udp_hdr->dst_port;
218 udp_hdr->dst_port = udp_hdr->src_port;
219 udp_hdr->src_port = port;
221 write_src_and_dst_mac(task, mbufs[j]);
223 task_update_config(task);
224 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
227 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
229 struct task_swap *task = (struct task_swap *)tbase;
230 struct ether_addr *src_addr, *dst_addr;
233 * Destination MAC can come from
234 * - pre-configured mac in case 'dst mac=xx:xx:xx:xx:xx:xx' in config file
235 * - src mac from the packet in case 'dst mac=packet' in config file
236 * - not written in case 'dst mac=no' in config file
237 * - (default - no 'dst mac') src mac from the packet
238 * Source MAC can come from
239 * - pre-configured mac in case 'src mac=xx:xx:xx:xx:xx:xx' in config file
240 * - dst mac from the packet in case 'src mac=packet' in config file
241 * - not written in case 'src mac=no' in config file
242 * - (default - no 'src mac') if (tx_port) port mac
243 * - (default - no 'src mac') if (no tx_port) dst mac from the packet
246 if (targ->flags & TASK_ARG_DST_MAC_SET) {
247 dst_addr = &targ->edaddr;
248 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
251 if (targ->flags & TASK_ARG_SRC_MAC_SET) {
252 src_addr = &targ->esaddr;
253 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
254 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
255 } else if (targ->nb_txports) {
256 src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
257 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
258 if (targ->flags & TASK_ARG_HW_SRC_MAC){
259 targ->flags |= TASK_ARG_SRC_MAC_SET;
260 plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
263 task->runtime_flags = targ->flags;
264 task->ip = rte_cpu_to_be_32(targ->local_ipv4);
265 task->tmp_ip = task->ip;
268 static struct task_init task_init_swap = {
270 .init = init_task_swap,
271 .handle = handle_swap_bulk,
272 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
273 .size = sizeof(struct task_swap),
274 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
277 static struct task_init task_init_swap_arp = {
279 .sub_mode_str = "l3",
280 .init = init_task_swap,
281 .handle = handle_swap_bulk,
282 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
283 .size = sizeof(struct task_swap),
284 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
287 __attribute__((constructor)) static void reg_task_swap(void)
289 reg_task(&task_init_swap);
290 reg_task(&task_init_swap_arp);