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 * The destination MAC of the outgoing packet is based on the config file:
207 * - 'dst mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as dst mac
208 * - 'dst mac=packet' => the src mac of the incoming packet is used as dst mac
209 * - (default - no 'dst mac') => the src mac from the incoming packet is used as dst mac
211 * The source MAC of the outgoing packet is based on the config file:
212 * - 'src mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as src mac
213 * - 'src mac=packet' => the dst mac of the incoming packet is used as src mac
214 * - 'src mac=hw' => the mac address of the tx port is used as src mac
215 * An error is returned if there are no physical tx ports
216 * - (default - no 'src mac') => if there is physical tx port, the mac of that port is used as src mac
217 * - (default - no 'src mac') if there are no physical tx ports the dst mac of the incoming packet
220 if (targ->flags & TASK_ARG_DST_MAC_SET) {
221 dst_addr = &targ->edaddr;
222 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
225 PROX_PANIC(targ->flags & TASK_ARG_DO_NOT_SET_SRC_MAC, "src mac must be set in swap mode, by definition => src mac=no is not supported\n");
226 PROX_PANIC(targ->flags & TASK_ARG_DO_NOT_SET_DST_MAC, "dst mac must be set in swap mode, by definition => dst mac=no is not supported\n");
228 if (targ->flags & TASK_ARG_SRC_MAC_SET) {
229 src_addr = &targ->esaddr;
230 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
231 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
233 if (targ->flags & TASK_ARG_HW_SRC_MAC)
234 PROX_PANIC(targ->nb_txports == 0, "src mac set to hw but no tx port\n");
235 if (targ->nb_txports) {
236 src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
237 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
238 targ->flags |= TASK_ARG_SRC_MAC_SET;
239 plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
242 task->runtime_flags = targ->flags;
245 static struct task_init task_init_swap = {
247 .init = init_task_swap,
248 .handle = handle_swap_bulk,
249 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
250 .size = sizeof(struct task_swap),
251 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
254 static struct task_init task_init_swap_arp = {
256 .sub_mode_str = "l3",
257 .init = init_task_swap,
258 .handle = handle_swap_bulk,
259 .flag_features = TASK_FEATURE_TXQ_FLAGS_NOOFFLOADS|TASK_FEATURE_TXQ_FLAGS_NOMULTSEGS,
260 .size = sizeof(struct task_swap),
261 .mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
264 __attribute__((constructor)) static void reg_task_swap(void)
266 reg_task(&task_init_swap);
267 reg_task(&task_init_swap_arp);