Support 5 tuple load balancing for IP addresses other than 0.0.0.X
[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         uint32_t tmp_ip;
37         uint32_t ip;
38 };
39
40 static void task_update_config(struct task_swap *task)
41 {
42         if (unlikely(task->ip != task->tmp_ip))
43                 task->ip = task->tmp_ip;
44 }
45
46 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
47 {
48         struct ether_hdr *hdr;
49         struct ether_addr mac;
50
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));
55         } else {
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);
60                 }
61
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);
64                 else
65                         ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
66
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);
69                 } else {
70                         ether_addr_copy(&mac, &hdr->s_addr);
71                 }
72         }
73 }
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)
75 {
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);
80                 return 0;
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);
87                 return 0;
88         } else {
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));
90                 return OUT_DISCARD;
91         }
92 }
93
94 /*
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
97  */
98 static inline int handle_arp_replies(struct task_swap *task, struct ether_hdr_arp *hdr_arp)
99 {
100         return OUT_DISCARD;
101 }
102
103 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
104 {
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;
110         uint32_t ip;
111         uint16_t port;
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;
118         uint16_t j;
119
120         for (j = 0; j < n_pkts; ++j) {
121                 PREFETCH0(mbufs[j]);
122         }
123         for (j = 0; j < n_pkts; ++j) {
124                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
125         }
126
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) {
130                 case ETYPE_ARP:
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);
139                         } else {
140                                 plog_info("Received unexpected ARP operation %d\n", hdr_arp->arp.oper);
141                                 out[j] = OUT_DISCARD;
142                         }
143                         continue;
144                 case ETYPE_MPLSU:
145                         mpls = (struct mpls_hdr *)(hdr + 1);
146                         while (!(mpls->bytes & 0x00010000)) {
147                                 mpls++;
148                                 mpls_len += sizeof(struct mpls_hdr);
149                         }
150                         mpls_len += sizeof(struct mpls_hdr);
151                         ip_hdr = (struct ipv4_hdr *)(mpls + 1);
152                         break;
153                 case ETYPE_8021ad:
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;
158                                 continue;
159                         }
160                         ip_hdr = (struct ipv4_hdr *)(qinq + 1);
161                         break;
162                 case ETYPE_VLAN:
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);
170                                 }
171                                 else if (vlan->eth_proto == ETYPE_IPv6) {
172                                         plog_warn("Unsupported IPv6\n");
173                                         out[j] = OUT_DISCARD;
174                                         continue;
175                                 }
176                                 else {
177                                         plog_warn("Unsupported packet type\n");
178                                         out[j] = OUT_DISCARD;
179                                         continue;
180                                 }
181                         } else {
182                                 plog_warn("Unsupported packet type\n");
183                                 out[j] = OUT_DISCARD;
184                                 continue;
185                         }
186                         break;
187                 case ETYPE_IPv4:
188                         ip_hdr = (struct ipv4_hdr *)(hdr + 1);
189                         break;
190                 case ETYPE_IPv6:
191                         plog_warn("Unsupported IPv6\n");
192                         out[j] = OUT_DISCARD;
193                         continue;
194                 case ETYPE_LLDP:
195                         out[j] = OUT_DISCARD;
196                         continue;
197                 default:
198                         plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
199                         out[j] = OUT_DISCARD;
200                         continue;
201                 }
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;
216                 } else {
217                         port = udp_hdr->dst_port;
218                         udp_hdr->dst_port = udp_hdr->src_port;
219                         udp_hdr->src_port = port;
220                 }
221                 write_src_and_dst_mac(task, mbufs[j]);
222         }
223         task_update_config(task);
224         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
225 }
226
227 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
228 {
229         struct task_swap *task = (struct task_swap *)tbase;
230         struct ether_addr *src_addr, *dst_addr;
231
232         /*
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
244          */
245
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));
249         }
250
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);
261                 }
262         }
263         task->runtime_flags = targ->flags;
264         task->ip = rte_cpu_to_be_32(targ->local_ipv4);
265         task->tmp_ip = task->ip;
266 }
267
268 static struct task_init task_init_swap = {
269         .mode_str = "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,
275 };
276
277 static struct task_init task_init_swap_arp = {
278         .mode_str = "swap",
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,
285 };
286
287 __attribute__((constructor)) static void reg_task_swap(void)
288 {
289         reg_task(&task_init_swap);
290         reg_task(&task_init_swap_arp);
291 }