Added support for netlink
[samplevnf.git] / VNFs / DPPD-PROX / handle_swap.c
1 /*
2 // Copyright (c) 2010-2020 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 "prox_port_cfg.h"
25 #include "mpls.h"
26 #include "qinq.h"
27 #include "gre.h"
28 #include "prefetch.h"
29 #include "defines.h"
30 #include "igmp.h"
31 #include "prox_cksum.h"
32 #include "prox_compat.h"
33
34 struct task_swap {
35         struct task_base base;
36         struct rte_mempool *igmp_pool;
37         uint32_t runtime_flags;
38         uint32_t igmp_address;
39         uint8_t src_dst_mac[12];
40         uint32_t local_ipv4;
41         int offload_crc;
42         uint64_t last_echo_req_rcvd_tsc;
43         uint64_t last_echo_rep_rcvd_tsc;
44         uint32_t n_echo_req;
45         uint32_t n_echo_rep;
46 };
47
48 #define NB_IGMP_MBUF            1024
49 #define IGMP_MBUF_SIZE          2048
50 #define NB_CACHE_IGMP_MBUF      256
51
52 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
53 {
54         prox_rte_ether_hdr *hdr;
55         prox_rte_ether_addr mac;
56
57         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))) {
58                 /* Source and Destination mac hardcoded */
59                 hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
60                 rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
61         } else {
62                 hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
63                 if (likely((task->runtime_flags & TASK_ARG_SRC_MAC_SET) == 0)) {
64                         /* dst mac will be used as src mac */
65                         prox_rte_ether_addr_copy(&hdr->d_addr, &mac);
66                 }
67
68                 if (unlikely(task->runtime_flags & TASK_ARG_DST_MAC_SET))
69                         prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[0], &hdr->d_addr);
70                 else
71                         prox_rte_ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
72
73                 if (unlikely(task->runtime_flags & TASK_ARG_SRC_MAC_SET)) {
74                         prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
75                 } else {
76                         prox_rte_ether_addr_copy(&mac, &hdr->s_addr);
77                 }
78         }
79 }
80 static inline void build_mcast_mac(uint32_t ip, prox_rte_ether_addr *dst_mac)
81 {
82         // MAC address is 01:00:5e followed by 23 LSB of IP address
83         uint64_t mac = 0x0000005e0001L | ((ip & 0xFFFF7F00L) << 16);
84         memcpy(dst_mac, &mac, sizeof(prox_rte_ether_addr));
85 }
86
87 static inline void build_icmp_reply_message(struct task_base *tbase, struct rte_mbuf *mbuf)
88 {
89         struct task_swap *task = (struct task_swap *)tbase;
90         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
91         prox_rte_ether_addr dst_mac;
92         prox_rte_ether_addr_copy(&hdr->s_addr, &dst_mac);
93         prox_rte_ether_addr_copy(&hdr->d_addr, &hdr->s_addr);
94         prox_rte_ether_addr_copy(&dst_mac, &hdr->d_addr);
95         prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
96         ip_hdr->dst_addr = ip_hdr->src_addr;
97         ip_hdr->src_addr = task->local_ipv4;
98         prox_rte_icmp_hdr *picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
99         picmp->icmp_type = PROX_RTE_IP_ICMP_ECHO_REPLY;
100 }
101
102 static inline void build_igmp_message(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t ip, uint8_t igmp_message)
103 {
104         struct task_swap *task = (struct task_swap *)tbase;
105         prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
106         prox_rte_ether_addr dst_mac;
107         build_mcast_mac(ip, &dst_mac);
108
109         rte_pktmbuf_pkt_len(mbuf) = 46;
110         rte_pktmbuf_data_len(mbuf) = 46;
111         init_mbuf_seg(mbuf);
112
113         prox_rte_ether_addr_copy(&dst_mac, &hdr->d_addr);
114         prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
115         hdr->ether_type = ETYPE_IPv4;
116
117         prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
118         ip_hdr->version_ihl = 0x45;             /**< version and header length */
119         ip_hdr->type_of_service = 0;    /**< type of service */
120         ip_hdr->total_length = rte_cpu_to_be_16(32);            /**< length of packet */
121         ip_hdr->packet_id = 0;          /**< packet ID */
122         ip_hdr->fragment_offset = 0;    /**< fragmentation offset */
123         ip_hdr->time_to_live = 1;               /**< time to live */
124         ip_hdr->next_proto_id = IPPROTO_IGMP;           /**< protocol ID */
125         ip_hdr->hdr_checksum = 0;               /**< header checksum */
126         ip_hdr->src_addr = task->local_ipv4;            /**< source address */
127         ip_hdr->dst_addr = ip;  /**< destination address */
128         struct igmpv2_hdr *pigmp = (struct igmpv2_hdr *)(ip_hdr + 1);
129         pigmp->type = igmp_message;
130         pigmp->max_resp_time = 0;
131         pigmp->checksum = 0;
132         pigmp->group_address = ip;
133         prox_ip_udp_cksum(mbuf, ip_hdr, sizeof(prox_rte_ether_hdr), sizeof(prox_rte_ipv4_hdr), task->offload_crc);
134 }
135
136 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
137 {
138         struct task_swap *task = (struct task_swap *)tbase;
139         prox_rte_ether_hdr *hdr;
140         prox_rte_ether_addr mac;
141         prox_rte_ipv4_hdr *ip_hdr;
142         prox_rte_udp_hdr *udp_hdr;
143         struct gre_hdr *pgre;
144         prox_rte_ipv4_hdr *inner_ip_hdr;
145         uint32_t ip;
146         uint16_t port;
147         uint8_t out[64] = {0};
148         struct mpls_hdr *mpls;
149         uint32_t mpls_len = 0;
150         struct qinq_hdr *qinq;
151         prox_rte_vlan_hdr *vlan;
152         uint16_t j;
153         struct igmpv2_hdr *pigmp;
154         prox_rte_icmp_hdr *picmp;
155         uint8_t type;
156
157         for (j = 0; j < n_pkts; ++j) {
158                 PREFETCH0(mbufs[j]);
159         }
160         for (j = 0; j < n_pkts; ++j) {
161                 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
162         }
163
164         // TODO 1: check packet is long enough for Ethernet + IP + UDP = 42 bytes
165         for (uint16_t j = 0; j < n_pkts; ++j) {
166                 hdr = rte_pktmbuf_mtod(mbufs[j], prox_rte_ether_hdr *);
167                 switch (hdr->ether_type) {
168                 case ETYPE_MPLSU:
169                         mpls = (struct mpls_hdr *)(hdr + 1);
170                         while (!(mpls->bytes & 0x00010000)) {
171                                 // TODO: verify pcket length
172                                 mpls++;
173                                 mpls_len += sizeof(struct mpls_hdr);
174                         }
175                         mpls_len += sizeof(struct mpls_hdr);
176                         ip_hdr = (prox_rte_ipv4_hdr *)(mpls + 1);
177                         break;
178                 case ETYPE_8021ad:
179                         qinq = (struct qinq_hdr *)hdr;
180                         if (qinq->cvlan.eth_proto != ETYPE_VLAN) {
181                                 plog_warn("Unexpected proto in QinQ = %#04x\n", qinq->cvlan.eth_proto);
182                                 out[j] = OUT_DISCARD;
183                                 continue;
184                         }
185                         ip_hdr = (prox_rte_ipv4_hdr *)(qinq + 1);
186                         break;
187                 case ETYPE_VLAN:
188                         vlan = (prox_rte_vlan_hdr *)(hdr + 1);
189                         if (vlan->eth_proto == ETYPE_IPv4) {
190                                 ip_hdr = (prox_rte_ipv4_hdr *)(vlan + 1);
191                         } else if (vlan->eth_proto == ETYPE_VLAN) {
192                                 vlan = (prox_rte_vlan_hdr *)(vlan + 1);
193                                 if (vlan->eth_proto == ETYPE_IPv4) {
194                                         ip_hdr = (prox_rte_ipv4_hdr *)(vlan + 1);
195                                 }
196                                 else if (vlan->eth_proto == ETYPE_IPv6) {
197                                         plog_warn("Unsupported IPv6\n");
198                                         out[j] = OUT_DISCARD;
199                                         continue;
200                                 }
201                                 else {
202                                         plog_warn("Unsupported packet type\n");
203                                         out[j] = OUT_DISCARD;
204                                         continue;
205                                 }
206                         } else {
207                                 plog_warn("Unsupported packet type\n");
208                                 out[j] = OUT_DISCARD;
209                                 continue;
210                         }
211                         break;
212                 case ETYPE_IPv4:
213                         ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
214                         break;
215                 case ETYPE_IPv6:
216                         plog_warn("Unsupported IPv6\n");
217                         out[j] = OUT_DISCARD;
218                         continue;
219                 case ETYPE_LLDP:
220                         out[j] = OUT_DISCARD;
221                         continue;
222                 default:
223                         plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
224                         out[j] = OUT_DISCARD;
225                         continue;
226                 }
227                 // TODO 2 : check packet is long enough for Ethernet + IP + UDP + extra header (VLAN, MPLS, ...)
228                 ip = ip_hdr->dst_addr;
229
230                 switch (ip_hdr->next_proto_id) {
231                 case IPPROTO_GRE:
232                         ip_hdr->dst_addr = ip_hdr->src_addr;
233                         ip_hdr->src_addr = ip;
234
235                         pgre = (struct gre_hdr *)(ip_hdr + 1);
236                         inner_ip_hdr = ((prox_rte_ipv4_hdr *)(pgre + 1));
237                         ip = inner_ip_hdr->dst_addr;
238                         inner_ip_hdr->dst_addr = inner_ip_hdr->src_addr;
239                         inner_ip_hdr->src_addr = ip;
240
241                         udp_hdr = (prox_rte_udp_hdr *)(inner_ip_hdr + 1);
242                         // TODO 3.1 : verify proto is UPD or TCP
243                         port = udp_hdr->dst_port;
244                         udp_hdr->dst_port = udp_hdr->src_port;
245                         udp_hdr->src_port = port;
246                         write_src_and_dst_mac(task, mbufs[j]);
247                         break;
248                 case IPPROTO_UDP:
249                 case IPPROTO_TCP:
250                         if (task->igmp_address && PROX_RTE_IS_IPV4_MCAST(rte_be_to_cpu_32(ip))) {
251                                 out[j] = OUT_DISCARD;
252                                 continue;
253                         }
254                         udp_hdr = (prox_rte_udp_hdr *)(ip_hdr + 1);
255                         ip_hdr->dst_addr = ip_hdr->src_addr;
256                         ip_hdr->src_addr = ip;
257
258                         port = udp_hdr->dst_port;
259                         udp_hdr->dst_port = udp_hdr->src_port;
260                         udp_hdr->src_port = port;
261                         write_src_and_dst_mac(task, mbufs[j]);
262                         break;
263                 case IPPROTO_ICMP:
264                         picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
265                         type = picmp->icmp_type;
266                         if (type == PROX_RTE_IP_ICMP_ECHO_REQUEST) {
267                                 if (ip_hdr->dst_addr == task->local_ipv4) {
268                                         task->n_echo_req++;
269                                         if (rte_rdtsc() - task->last_echo_req_rcvd_tsc > rte_get_tsc_hz()) {
270                                                 plog_info("Received %u Echo Request on IP "IPv4_BYTES_FMT" (last received from IP "IPv4_BYTES_FMT")\n", task->n_echo_req, IPv4_BYTES(((uint8_t*)&ip_hdr->dst_addr)), IPv4_BYTES(((uint8_t*)&ip_hdr->src_addr)));
271                                                 task->n_echo_req = 0;
272                                                 task->last_echo_req_rcvd_tsc = rte_rdtsc();
273                                         }
274                                         build_icmp_reply_message(tbase, mbufs[j]);
275                                 } else {
276                                         out[j] = OUT_DISCARD;
277                                         continue;
278                                 }
279                         } else if (type == PROX_RTE_IP_ICMP_ECHO_REPLY) {
280                                 if (ip_hdr->dst_addr == task->local_ipv4) {
281                                         task->n_echo_rep++;
282                                         if (rte_rdtsc() - task->last_echo_rep_rcvd_tsc > rte_get_tsc_hz()) {
283                                                 plog_info("Received %u Echo Reply on IP "IPv4_BYTES_FMT" (last received from IP "IPv4_BYTES_FMT")\n", task->n_echo_rep, IPv4_BYTES(((uint8_t*)&ip_hdr->dst_addr)), IPv4_BYTES(((uint8_t*)&ip_hdr->src_addr)));
284                                                 task->n_echo_rep = 0;
285                                                 task->last_echo_rep_rcvd_tsc = rte_rdtsc();
286                                         }
287                                 } else {
288                                         out[j] = OUT_DISCARD;
289                                         continue;
290                                 }
291                         } else {
292                                 out[j] = OUT_DISCARD;
293                                 continue;
294                         }
295                         break;
296                 case IPPROTO_IGMP:
297                         pigmp = (struct igmpv2_hdr *)(ip_hdr + 1);
298                         // TODO: check packet len
299                         type = pigmp->type;
300                         if (type == IGMP_MEMBERSHIP_QUERY) {
301                                 if (task->igmp_address) {
302                                         // We have an address registered
303                                         if ((task->igmp_address == pigmp->group_address) || (pigmp->group_address == 0)) {
304                                                 // We get a request for the registered address, or to 0.0.0.0
305                                                 build_igmp_message(tbase, mbufs[j], task->igmp_address, IGMP_MEMBERSHIP_REPORT);        // replace Membership query packet with a response
306                                         } else {
307                                                 // Discard as either we are not registered or this is a query for a different group
308                                                 out[j] = OUT_DISCARD;
309                                                 continue;
310                                         }
311                                 } else {
312                                         // Discard as either we are not registered
313                                         out[j] = OUT_DISCARD;
314                                         continue;
315                                 }
316                         } else {
317                                 // Do not forward other IGMP packets back
318                                 out[j] = OUT_DISCARD;
319                                 continue;
320                         }
321                         break;
322                 default:
323                         plog_warn("Unsupported IP protocol 0x%x\n", ip_hdr->next_proto_id);
324                         out[j] = OUT_DISCARD;
325                         continue;
326                 }
327         }
328         return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
329 }
330
331 void igmp_join_group(struct task_base *tbase, uint32_t igmp_address)
332 {
333         struct task_swap *task = (struct task_swap *)tbase;
334         struct rte_mbuf *igmp_mbuf;
335         uint8_t out[64] = {0};
336         int ret;
337
338         task->igmp_address = igmp_address;
339         ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
340         if (ret != 0) {
341                 plog_err("Unable to allocate igmp mbuf\n");
342                 return;
343         }
344         build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_MEMBERSHIP_REPORT);
345         task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
346 }
347
348 void igmp_leave_group(struct task_base *tbase)
349 {
350         struct task_swap *task = (struct task_swap *)tbase;
351         struct rte_mbuf *igmp_mbuf;
352         uint8_t out[64] = {0};
353         int ret;
354
355         task->igmp_address = 0;
356         ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
357         if (ret != 0) {
358                 plog_err("Unable to allocate igmp mbuf\n");
359                 return;
360         }
361         build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_LEAVE_GROUP);
362         task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
363 }
364
365 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
366 {
367         struct task_swap *task = (struct task_swap *)tbase;
368         prox_rte_ether_addr *src_addr, *dst_addr;
369
370         /*
371          * The destination MAC of the outgoing packet is based on the config file:
372          *    - 'dst mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as dst mac
373          *    - 'dst mac=packet'            => the src mac of the incoming packet is used as dst mac
374          *    - (default - no 'dst mac')    => the src mac from the incoming packet is used as dst mac
375          *
376          * The source MAC of the outgoing packet is based on the config file:
377          *    - 'src mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as src mac
378          *    - 'src mac=packet'            => the dst mac of the incoming packet is used as src mac
379          *    - 'src mac=hw'                => the mac address of the tx port is used as src mac
380          *                                     An error is returned if there are no physical tx ports
381          *    - (default - no 'src mac')    => if there is physical tx port, the mac of that port is used as src mac
382          *    - (default - no 'src mac')       if there are no physical tx ports the dst mac of the incoming packet
383          */
384
385         if (targ->flags & TASK_ARG_DST_MAC_SET) {
386                 dst_addr = &targ->edaddr;
387                 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
388         }
389
390         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");
391         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");
392
393         if (targ->flags & TASK_ARG_SRC_MAC_SET) {
394                 src_addr =  &targ->esaddr;
395                 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
396                 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
397         } else {
398                 if (targ->flags & TASK_ARG_HW_SRC_MAC)
399                         PROX_PANIC(targ->nb_txports == 0, "src mac set to hw but no tx port\n");
400                 if (targ->nb_txports) {
401                         src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
402                         memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
403                         targ->flags |= TASK_ARG_SRC_MAC_SET;
404                         plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
405                 }
406         }
407         task->runtime_flags = targ->flags;
408         task->igmp_address =  rte_cpu_to_be_32(targ->igmp_address);
409         if (task->igmp_pool == NULL) {
410                 static char name[] = "igmp0_pool";
411                 name[4]++;
412                 struct rte_mempool *ret = rte_mempool_create(name, NB_IGMP_MBUF, IGMP_MBUF_SIZE, NB_CACHE_IGMP_MBUF,
413                         sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, 0,
414                         rte_socket_id(), 0);
415                 PROX_PANIC(ret == NULL, "Failed to allocate IGMP memory pool on socket %u with %u elements\n",
416                         rte_socket_id(), NB_IGMP_MBUF);
417                 plog_info("\t\tMempool %p (%s) size = %u * %u cache %u, socket %d\n", ret, name, NB_IGMP_MBUF,
418                         IGMP_MBUF_SIZE, NB_CACHE_IGMP_MBUF, rte_socket_id());
419                 task->igmp_pool = ret;
420         }
421         task->local_ipv4 = rte_cpu_to_be_32(targ->local_ipv4);
422
423         struct prox_port_cfg *port = find_reachable_port(targ);
424         if (port) {
425                 task->offload_crc = port->requested_tx_offload & (DEV_TX_OFFLOAD_IPV4_CKSUM | DEV_TX_OFFLOAD_UDP_CKSUM);
426         }
427 }
428
429 static struct task_init task_init_swap = {
430         .mode_str = "swap",
431         .init = init_task_swap,
432         .handle = handle_swap_bulk,
433         .flag_features = 0,
434         .size = sizeof(struct task_swap),
435 };
436
437 __attribute__((constructor)) static void reg_task_swap(void)
438 {
439         reg_task(&task_init_swap);
440 }