Merge "lw_AFTR: IP Checksum required on generated packet."
[samplevnf.git] / VNFs / DPPD-PROX / handle_swap.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 <rte_mbuf.h>
18 #include <rte_udp.h>
19
20 #include "task_init.h"
21 #include "task_base.h"
22 #include "lconf.h"
23 #include "log.h"
24 #include "arp.h"
25 #include "handle_swap.h"
26 #include "prox_port_cfg.h"
27 #include "mpls.h"
28 #include "qinq.h"
29 #include "gre.h"
30 #include "prefetch.h"
31
32 struct task_swap {
33         struct task_base base;
34         uint8_t src_dst_mac[12];
35         uint32_t runtime_flags;
36 };
37
38 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
39 {
40         struct ether_hdr *hdr;
41         struct ether_addr mac;
42
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));
47         } else {
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);
52                 }
53
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);
56                 else
57                         ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
58
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);
61                 } else {
62                         ether_addr_copy(&mac, &hdr->s_addr);
63                 }
64         }
65 }
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)
67 {
68         if ((hdr_arp->arp.data.tpa == ip) || (ip == 0)) {
69                 build_arp_reply(hdr_arp, s_addr);
70                 return 0;
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);
75                 return 0;
76         } else {
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));
78                 return OUT_DISCARD;
79         }
80 }
81
82 /*
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
85  */
86 static inline int handle_arp_replies(struct task_swap *task, struct ether_hdr_arp *hdr_arp)
87 {
88         return OUT_DISCARD;
89 }
90
91 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
92 {
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;
98         uint32_t ip;
99         uint16_t port;
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;
106         uint16_t j;
107
108         for (j = 0; j < n_pkts; ++j) {
109                 PREFETCH0(mbufs[j]);
110         }
111         for (j = 0; j < n_pkts; ++j) {
112                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
113         }
114
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) {
118                 case ETYPE_MPLSU:
119                         mpls = (struct mpls_hdr *)(hdr + 1);
120                         while (!(mpls->bytes & 0x00010000)) {
121                                 mpls++;
122                                 mpls_len += sizeof(struct mpls_hdr);
123                         }
124                         mpls_len += sizeof(struct mpls_hdr);
125                         ip_hdr = (struct ipv4_hdr *)(mpls + 1);
126                         break;
127                 case ETYPE_8021ad:
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;
132                                 continue;
133                         }
134                         ip_hdr = (struct ipv4_hdr *)(qinq + 1);
135                         break;
136                 case ETYPE_VLAN:
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);
144                                 }
145                                 else if (vlan->eth_proto == ETYPE_IPv6) {
146                                         plog_warn("Unsupported IPv6\n");
147                                         out[j] = OUT_DISCARD;
148                                         continue;
149                                 }
150                                 else {
151                                         plog_warn("Unsupported packet type\n");
152                                         out[j] = OUT_DISCARD;
153                                         continue;
154                                 }
155                         } else {
156                                 plog_warn("Unsupported packet type\n");
157                                 out[j] = OUT_DISCARD;
158                                 continue;
159                         }
160                         break;
161                 case ETYPE_IPv4:
162                         ip_hdr = (struct ipv4_hdr *)(hdr + 1);
163                         break;
164                 case ETYPE_IPv6:
165                         plog_warn("Unsupported IPv6\n");
166                         out[j] = OUT_DISCARD;
167                         continue;
168                 case ETYPE_LLDP:
169                         out[j] = OUT_DISCARD;
170                         continue;
171                 default:
172                         plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
173                         out[j] = OUT_DISCARD;
174                         continue;
175                 }
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;
190                 } else {
191                         port = udp_hdr->dst_port;
192                         udp_hdr->dst_port = udp_hdr->src_port;
193                         udp_hdr->src_port = port;
194                 }
195                 write_src_and_dst_mac(task, mbufs[j]);
196         }
197         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
198 }
199
200 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
201 {
202         struct task_swap *task = (struct task_swap *)tbase;
203         struct ether_addr *src_addr, *dst_addr;
204
205         /*
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
217          */
218
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));
222         }
223
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);
234                 }
235         }
236         task->runtime_flags = targ->flags;
237 }
238
239 static struct task_init task_init_swap = {
240         .mode_str = "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,
246 };
247
248 static struct task_init task_init_swap_arp = {
249         .mode_str = "swap",
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,
256 };
257
258 __attribute__((constructor)) static void reg_task_swap(void)
259 {
260         reg_task(&task_init_swap);
261         reg_task(&task_init_swap_arp);
262 }