2 // Copyright (c) 2010-2020 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.
21 #include "task_init.h"
22 #include "task_base.h"
25 #include "prox_port_cfg.h"
32 #include "prox_cksum.h"
33 #include "prox_compat.h"
36 struct task_base base;
37 struct rte_mempool *igmp_pool;
38 uint32_t runtime_flags;
39 uint32_t igmp_address;
40 uint8_t src_dst_mac[12];
43 uint64_t last_echo_req_rcvd_tsc;
44 uint64_t last_echo_rep_rcvd_tsc;
49 #define NB_IGMP_MBUF 1024
50 #define IGMP_MBUF_SIZE 2048
51 #define NB_CACHE_IGMP_MBUF 256
53 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
55 prox_rte_ether_hdr *hdr;
56 prox_rte_ether_addr mac;
58 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))) {
59 /* Source and Destination mac hardcoded */
60 hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
61 rte_memcpy(hdr, task->src_dst_mac, sizeof(task->src_dst_mac));
63 hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
64 if (likely((task->runtime_flags & TASK_ARG_SRC_MAC_SET) == 0)) {
65 /* dst mac will be used as src mac */
66 prox_rte_ether_addr_copy(&hdr->d_addr, &mac);
69 if (unlikely(task->runtime_flags & TASK_ARG_DST_MAC_SET))
70 prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[0], &hdr->d_addr);
72 prox_rte_ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
74 if (unlikely(task->runtime_flags & TASK_ARG_SRC_MAC_SET)) {
75 prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
77 prox_rte_ether_addr_copy(&mac, &hdr->s_addr);
81 static inline void build_mcast_mac(uint32_t ip, prox_rte_ether_addr *dst_mac)
83 // MAC address is 01:00:5e followed by 23 LSB of IP address
84 uint64_t mac = 0x0000005e0001L | ((ip & 0xFFFF7F00L) << 16);
85 memcpy(dst_mac, &mac, sizeof(prox_rte_ether_addr));
88 static inline void build_icmp_reply_message(struct task_base *tbase, struct rte_mbuf *mbuf)
90 struct task_swap *task = (struct task_swap *)tbase;
91 prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
92 prox_rte_ether_addr dst_mac;
93 prox_rte_ether_addr_copy(&hdr->s_addr, &dst_mac);
94 prox_rte_ether_addr_copy(&hdr->d_addr, &hdr->s_addr);
95 prox_rte_ether_addr_copy(&dst_mac, &hdr->d_addr);
96 prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
97 ip_hdr->dst_addr = ip_hdr->src_addr;
98 ip_hdr->src_addr = task->local_ipv4;
99 prox_rte_icmp_hdr *picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
100 picmp->icmp_type = PROX_RTE_IP_ICMP_ECHO_REPLY;
103 static inline void build_igmp_message(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t ip, uint8_t igmp_message)
105 struct task_swap *task = (struct task_swap *)tbase;
106 prox_rte_ether_hdr *hdr = rte_pktmbuf_mtod(mbuf, prox_rte_ether_hdr *);
107 prox_rte_ether_addr dst_mac;
108 build_mcast_mac(ip, &dst_mac);
110 rte_pktmbuf_pkt_len(mbuf) = 46;
111 rte_pktmbuf_data_len(mbuf) = 46;
114 prox_rte_ether_addr_copy(&dst_mac, &hdr->d_addr);
115 prox_rte_ether_addr_copy((prox_rte_ether_addr *)&task->src_dst_mac[6], &hdr->s_addr);
116 hdr->ether_type = ETYPE_IPv4;
118 prox_rte_ipv4_hdr *ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
119 ip_hdr->version_ihl = 0x45; /**< version and header length */
120 ip_hdr->type_of_service = 0; /**< type of service */
121 ip_hdr->total_length = rte_cpu_to_be_16(32); /**< length of packet */
122 ip_hdr->packet_id = 0; /**< packet ID */
123 ip_hdr->fragment_offset = 0; /**< fragmentation offset */
124 ip_hdr->time_to_live = 1; /**< time to live */
125 ip_hdr->next_proto_id = IPPROTO_IGMP; /**< protocol ID */
126 ip_hdr->hdr_checksum = 0; /**< header checksum */
127 ip_hdr->src_addr = task->local_ipv4; /**< source address */
128 ip_hdr->dst_addr = ip; /**< destination address */
129 struct igmpv2_hdr *pigmp = (struct igmpv2_hdr *)(ip_hdr + 1);
130 pigmp->type = igmp_message;
131 pigmp->max_resp_time = 0;
133 pigmp->group_address = ip;
134 prox_ip_udp_cksum(mbuf, ip_hdr, sizeof(prox_rte_ether_hdr), sizeof(prox_rte_ipv4_hdr), task->offload_crc);
137 static void stop_swap(struct task_base *tbase)
139 struct task_swap *task = (struct task_swap *)tbase;
140 if (task->igmp_pool) {
141 rte_mempool_free(task->igmp_pool);
142 task->igmp_pool = NULL;
146 static void handle_ipv6(struct task_swap *task, struct rte_mbuf *mbufs, prox_rte_ipv6_hdr *ipv6_hdr, uint8_t *out)
148 __m128i ip = _mm_loadu_si128((__m128i*)&(ipv6_hdr->src_addr));
150 uint16_t payload_len;
151 prox_rte_udp_hdr *udp_hdr;
153 rte_mov16((uint8_t *)&(ipv6_hdr->src_addr), (uint8_t *)&(ipv6_hdr->dst_addr)); // Copy dst into src
154 rte_mov16((uint8_t *)&(ipv6_hdr->dst_addr), (uint8_t *)&ip); // Copy src into dst
155 switch(ipv6_hdr->proto) {
158 payload_len = ipv6_hdr->payload_len;
159 udp_hdr = (prox_rte_udp_hdr *)(ipv6_hdr + 1);
160 if (unlikely(udp_hdr->dgram_len < payload_len)) {
161 plog_warn("Unexpected L4 len (%u) versus L3 payload len (%u) in IPv6 packet\n", udp_hdr->dgram_len, payload_len);
165 port = udp_hdr->dst_port;
166 udp_hdr->dst_port = udp_hdr->src_port;
167 udp_hdr->src_port = port;
168 write_src_and_dst_mac(task, mbufs);
172 plog_warn("Unsupported next hop %u in IPv6 packet\n", ipv6_hdr->proto);
178 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
180 struct task_swap *task = (struct task_swap *)tbase;
181 prox_rte_ether_hdr *hdr;
182 prox_rte_ether_addr mac;
183 prox_rte_ipv4_hdr *ip_hdr;
184 prox_rte_udp_hdr *udp_hdr;
185 prox_rte_ipv6_hdr *ipv6_hdr;
186 struct gre_hdr *pgre;
187 prox_rte_ipv4_hdr *inner_ip_hdr;
190 uint8_t out[64] = {0};
191 struct mpls_hdr *mpls;
192 uint32_t mpls_len = 0;
193 struct qinq_hdr *qinq;
194 prox_rte_vlan_hdr *vlan;
196 struct igmpv2_hdr *pigmp;
197 prox_rte_icmp_hdr *picmp;
200 for (j = 0; j < n_pkts; ++j) {
203 for (j = 0; j < n_pkts; ++j) {
204 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
207 // TODO 1: check packet is long enough for Ethernet + IP + UDP = 42 bytes
208 for (uint16_t j = 0; j < n_pkts; ++j) {
209 hdr = rte_pktmbuf_mtod(mbufs[j], prox_rte_ether_hdr *);
210 switch (hdr->ether_type) {
212 mpls = (struct mpls_hdr *)(hdr + 1);
213 while (!(mpls->bytes & 0x00010000)) {
214 // TODO: verify pcket length
216 mpls_len += sizeof(struct mpls_hdr);
218 mpls_len += sizeof(struct mpls_hdr);
219 ip_hdr = (prox_rte_ipv4_hdr *)(mpls + 1);
220 if (unlikely((ip_hdr->version_ihl >> 4) == 6)) {
221 ipv6_hdr = (prox_rte_ipv6_hdr *)(ip_hdr);
222 handle_ipv6(task, mbufs[j], ipv6_hdr, &out[j]);
227 qinq = (struct qinq_hdr *)hdr;
228 if (qinq->cvlan.eth_proto != ETYPE_VLAN) {
229 plog_warn("Unexpected proto in QinQ = %#04x\n", qinq->cvlan.eth_proto);
230 out[j] = OUT_DISCARD;
233 if (qinq->ether_type == ETYPE_IPv4) {
234 ip_hdr = (prox_rte_ipv4_hdr *)(qinq + 1);
235 } else if (qinq->ether_type == ETYPE_IPv6) {
236 ipv6_hdr = (prox_rte_ipv6_hdr *)(qinq + 1);
237 handle_ipv6(task, mbufs[j], ipv6_hdr, &out[j]);
240 plog_warn("Unsupported packet type\n");
241 out[j] = OUT_DISCARD;
246 vlan = (prox_rte_vlan_hdr *)(hdr + 1);
247 if (vlan->eth_proto == ETYPE_IPv4) {
248 ip_hdr = (prox_rte_ipv4_hdr *)(vlan + 1);
249 } else if (vlan->eth_proto == ETYPE_IPv6) {
250 ipv6_hdr = (prox_rte_ipv6_hdr *)(vlan + 1);
251 handle_ipv6(task, mbufs[j], ipv6_hdr, &out[j]);
253 } else if (vlan->eth_proto == ETYPE_VLAN) {
254 vlan = (prox_rte_vlan_hdr *)(vlan + 1);
255 if (vlan->eth_proto == ETYPE_IPv4) {
256 ip_hdr = (prox_rte_ipv4_hdr *)(vlan + 1);
258 else if (vlan->eth_proto == ETYPE_IPv6) {
259 ipv6_hdr = (prox_rte_ipv6_hdr *)(vlan + 1);
260 handle_ipv6(task, mbufs[j], ipv6_hdr, &out[j]);
264 plog_warn("Unsupported packet type\n");
265 out[j] = OUT_DISCARD;
269 plog_warn("Unsupported packet type\n");
270 out[j] = OUT_DISCARD;
275 ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
278 ipv6_hdr = (prox_rte_ipv6_hdr *)(hdr + 1);
279 handle_ipv6(task, mbufs[j], ipv6_hdr, &out[j]);
282 out[j] = OUT_DISCARD;
285 plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
286 out[j] = OUT_DISCARD;
289 // TODO 2 : check packet is long enough for Ethernet + IP + UDP + extra header (VLAN, MPLS, ...)
292 ip = ip_hdr->dst_addr;
293 if (unlikely((ip_hdr->version_ihl >> 4) != 4)) {
294 out[j] = OUT_DISCARD;
298 switch (ip_hdr->next_proto_id) {
300 ip_hdr->dst_addr = ip_hdr->src_addr;
301 ip_hdr->src_addr = ip;
303 pgre = (struct gre_hdr *)(ip_hdr + 1);
304 inner_ip_hdr = ((prox_rte_ipv4_hdr *)(pgre + 1));
305 ip = inner_ip_hdr->dst_addr;
306 inner_ip_hdr->dst_addr = inner_ip_hdr->src_addr;
307 inner_ip_hdr->src_addr = ip;
309 udp_hdr = (prox_rte_udp_hdr *)(inner_ip_hdr + 1);
310 // TODO 3.1 : verify proto is UPD or TCP
311 port = udp_hdr->dst_port;
312 udp_hdr->dst_port = udp_hdr->src_port;
313 udp_hdr->src_port = port;
314 write_src_and_dst_mac(task, mbufs[j]);
318 if (unlikely(task->igmp_address && PROX_RTE_IS_IPV4_MCAST(rte_be_to_cpu_32(ip)))) {
319 out[j] = OUT_DISCARD;
322 udp_hdr = (prox_rte_udp_hdr *)(ip_hdr + 1);
323 ip_hdr->dst_addr = ip_hdr->src_addr;
324 ip_hdr->src_addr = ip;
326 port = udp_hdr->dst_port;
327 udp_hdr->dst_port = udp_hdr->src_port;
328 udp_hdr->src_port = port;
329 write_src_and_dst_mac(task, mbufs[j]);
332 picmp = (prox_rte_icmp_hdr *)(ip_hdr + 1);
333 type = picmp->icmp_type;
334 if (type == PROX_RTE_IP_ICMP_ECHO_REQUEST) {
335 if (ip_hdr->dst_addr == task->local_ipv4) {
337 if (rte_rdtsc() - task->last_echo_req_rcvd_tsc > rte_get_tsc_hz()) {
338 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)));
339 task->n_echo_req = 0;
340 task->last_echo_req_rcvd_tsc = rte_rdtsc();
342 build_icmp_reply_message(tbase, mbufs[j]);
344 out[j] = OUT_DISCARD;
347 } else if (type == PROX_RTE_IP_ICMP_ECHO_REPLY) {
348 if (ip_hdr->dst_addr == task->local_ipv4) {
350 if (rte_rdtsc() - task->last_echo_rep_rcvd_tsc > rte_get_tsc_hz()) {
351 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)));
352 task->n_echo_rep = 0;
353 task->last_echo_rep_rcvd_tsc = rte_rdtsc();
356 out[j] = OUT_DISCARD;
360 out[j] = OUT_DISCARD;
365 pigmp = (struct igmpv2_hdr *)(ip_hdr + 1);
366 // TODO: check packet len
368 if (type == IGMP_MEMBERSHIP_QUERY) {
369 if (task->igmp_address) {
370 // We have an address registered
371 if ((task->igmp_address == pigmp->group_address) || (pigmp->group_address == 0)) {
372 // We get a request for the registered address, or to 0.0.0.0
373 build_igmp_message(tbase, mbufs[j], task->igmp_address, IGMP_MEMBERSHIP_REPORT); // replace Membership query packet with a response
375 // Discard as either we are not registered or this is a query for a different group
376 out[j] = OUT_DISCARD;
380 // Discard as either we are not registered
381 out[j] = OUT_DISCARD;
385 // Do not forward other IGMP packets back
386 out[j] = OUT_DISCARD;
391 plog_warn("Unsupported IP protocol 0x%x\n", ip_hdr->next_proto_id);
392 out[j] = OUT_DISCARD;
396 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
399 void igmp_join_group(struct task_base *tbase, uint32_t igmp_address)
401 struct task_swap *task = (struct task_swap *)tbase;
402 struct rte_mbuf *igmp_mbuf;
403 uint8_t out[64] = {0};
406 task->igmp_address = igmp_address;
407 ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
409 plog_err("Unable to allocate igmp mbuf\n");
412 build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_MEMBERSHIP_REPORT);
413 task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
416 void igmp_leave_group(struct task_base *tbase)
418 struct task_swap *task = (struct task_swap *)tbase;
419 struct rte_mbuf *igmp_mbuf;
420 uint8_t out[64] = {0};
423 task->igmp_address = 0;
424 ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
426 plog_err("Unable to allocate igmp mbuf\n");
429 build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_LEAVE_GROUP);
430 task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
433 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
435 struct task_swap *task = (struct task_swap *)tbase;
436 prox_rte_ether_addr *src_addr, *dst_addr;
439 * The destination MAC of the outgoing packet is based on the config file:
440 * - 'dst mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as dst mac
441 * - 'dst mac=packet' => the src mac of the incoming packet is used as dst mac
442 * - (default - no 'dst mac') => the src mac from the incoming packet is used as dst mac
444 * The source MAC of the outgoing packet is based on the config file:
445 * - 'src mac=xx:xx:xx:xx:xx:xx' => the pre-configured mac will be used as src mac
446 * - 'src mac=packet' => the dst mac of the incoming packet is used as src mac
447 * - 'src mac=hw' => the mac address of the tx port is used as src mac
448 * An error is returned if there are no physical tx ports
449 * - (default - no 'src mac') => if there is physical tx port, the mac of that port is used as src mac
450 * - (default - no 'src mac') if there are no physical tx ports the dst mac of the incoming packet
453 if (targ->flags & TASK_ARG_DST_MAC_SET) {
454 dst_addr = &targ->edaddr;
455 memcpy(&task->src_dst_mac[0], dst_addr, sizeof(*src_addr));
458 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");
459 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");
461 if (targ->flags & TASK_ARG_SRC_MAC_SET) {
462 src_addr = &targ->esaddr;
463 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
464 plog_info("\t\tCore %d: src mac set from config file\n", targ->lconf->id);
466 if (targ->flags & TASK_ARG_HW_SRC_MAC)
467 PROX_PANIC(targ->nb_txports == 0, "src mac set to hw but no tx port\n");
468 if (targ->nb_txports) {
469 src_addr = &prox_port_cfg[task->base.tx_params_hw.tx_port_queue[0].port].eth_addr;
470 memcpy(&task->src_dst_mac[6], src_addr, sizeof(*dst_addr));
471 targ->flags |= TASK_ARG_SRC_MAC_SET;
472 plog_info("\t\tCore %d: src mac set from port\n", targ->lconf->id);
475 task->runtime_flags = targ->flags;
476 task->igmp_address = rte_cpu_to_be_32(targ->igmp_address);
477 if (task->igmp_pool == NULL) {
478 static char name[] = "igmp0_pool";
480 struct rte_mempool *ret = rte_mempool_create(name, NB_IGMP_MBUF, IGMP_MBUF_SIZE, NB_CACHE_IGMP_MBUF,
481 sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL, rte_pktmbuf_init, 0,
483 PROX_PANIC(ret == NULL, "Failed to allocate IGMP memory pool on socket %u with %u elements\n",
484 rte_socket_id(), NB_IGMP_MBUF);
485 plog_info("\t\tMempool %p (%s) size = %u * %u cache %u, socket %d\n", ret, name, NB_IGMP_MBUF,
486 IGMP_MBUF_SIZE, NB_CACHE_IGMP_MBUF, rte_socket_id());
487 task->igmp_pool = ret;
489 task->local_ipv4 = rte_cpu_to_be_32(targ->local_ipv4);
491 struct prox_port_cfg *port = find_reachable_port(targ);
493 task->offload_crc = port->requested_tx_offload & (DEV_TX_OFFLOAD_IPV4_CKSUM | DEV_TX_OFFLOAD_UDP_CKSUM);
497 static struct task_init task_init_swap = {
499 .init = init_task_swap,
500 .handle = handle_swap_bulk,
502 .size = sizeof(struct task_swap),
503 .stop_last = stop_swap
506 __attribute__((constructor)) static void reg_task_swap(void)
508 reg_task(&task_init_swap);