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.
20 #include "task_init.h"
21 #include "task_base.h"
24 #include "prox_port_cfg.h"
31 #include "prox_cksum.h"
32 #include "prox_compat.h"
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];
42 uint64_t last_echo_req_rcvd_tsc;
43 uint64_t last_echo_rep_rcvd_tsc;
48 #define NB_IGMP_MBUF 1024
49 #define IGMP_MBUF_SIZE 2048
50 #define NB_CACHE_IGMP_MBUF 256
52 static void write_src_and_dst_mac(struct task_swap *task, struct rte_mbuf *mbuf)
54 prox_rte_ether_hdr *hdr;
55 prox_rte_ether_addr mac;
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));
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);
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);
71 prox_rte_ether_addr_copy(&hdr->s_addr, &hdr->d_addr);
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);
76 prox_rte_ether_addr_copy(&mac, &hdr->s_addr);
80 static inline void build_mcast_mac(uint32_t ip, prox_rte_ether_addr *dst_mac)
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));
87 static inline void build_icmp_reply_message(struct task_base *tbase, struct rte_mbuf *mbuf)
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;
102 static inline void build_igmp_message(struct task_base *tbase, struct rte_mbuf *mbuf, uint32_t ip, uint8_t igmp_message)
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);
109 rte_pktmbuf_pkt_len(mbuf) = 46;
110 rte_pktmbuf_data_len(mbuf) = 46;
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;
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;
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);
136 static int handle_swap_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
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;
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;
153 struct igmpv2_hdr *pigmp;
154 prox_rte_icmp_hdr *picmp;
157 for (j = 0; j < n_pkts; ++j) {
160 for (j = 0; j < n_pkts; ++j) {
161 PREFETCH0(rte_pktmbuf_mtod(mbufs[j], void *));
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) {
169 mpls = (struct mpls_hdr *)(hdr + 1);
170 while (!(mpls->bytes & 0x00010000)) {
171 // TODO: verify pcket length
173 mpls_len += sizeof(struct mpls_hdr);
175 mpls_len += sizeof(struct mpls_hdr);
176 ip_hdr = (prox_rte_ipv4_hdr *)(mpls + 1);
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;
185 ip_hdr = (prox_rte_ipv4_hdr *)(qinq + 1);
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);
196 else if (vlan->eth_proto == ETYPE_IPv6) {
197 plog_warn("Unsupported IPv6\n");
198 out[j] = OUT_DISCARD;
202 plog_warn("Unsupported packet type\n");
203 out[j] = OUT_DISCARD;
207 plog_warn("Unsupported packet type\n");
208 out[j] = OUT_DISCARD;
213 ip_hdr = (prox_rte_ipv4_hdr *)(hdr + 1);
216 plog_warn("Unsupported IPv6\n");
217 out[j] = OUT_DISCARD;
220 out[j] = OUT_DISCARD;
223 plog_warn("Unsupported ether_type 0x%x\n", hdr->ether_type);
224 out[j] = OUT_DISCARD;
227 // TODO 2 : check packet is long enough for Ethernet + IP + UDP + extra header (VLAN, MPLS, ...)
228 ip = ip_hdr->dst_addr;
230 switch (ip_hdr->next_proto_id) {
232 ip_hdr->dst_addr = ip_hdr->src_addr;
233 ip_hdr->src_addr = ip;
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;
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]);
250 if (task->igmp_address && PROX_RTE_IS_IPV4_MCAST(rte_be_to_cpu_32(ip))) {
251 out[j] = OUT_DISCARD;
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;
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]);
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) {
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();
274 build_icmp_reply_message(tbase, mbufs[j]);
276 out[j] = OUT_DISCARD;
279 } else if (type == PROX_RTE_IP_ICMP_ECHO_REPLY) {
280 if (ip_hdr->dst_addr == task->local_ipv4) {
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();
288 out[j] = OUT_DISCARD;
292 out[j] = OUT_DISCARD;
297 pigmp = (struct igmpv2_hdr *)(ip_hdr + 1);
298 // TODO: check packet len
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
307 // Discard as either we are not registered or this is a query for a different group
308 out[j] = OUT_DISCARD;
312 // Discard as either we are not registered
313 out[j] = OUT_DISCARD;
317 // Do not forward other IGMP packets back
318 out[j] = OUT_DISCARD;
323 plog_warn("Unsupported IP protocol 0x%x\n", ip_hdr->next_proto_id);
324 out[j] = OUT_DISCARD;
328 return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
331 void igmp_join_group(struct task_base *tbase, uint32_t igmp_address)
333 struct task_swap *task = (struct task_swap *)tbase;
334 struct rte_mbuf *igmp_mbuf;
335 uint8_t out[64] = {0};
338 task->igmp_address = igmp_address;
339 ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
341 plog_err("Unable to allocate igmp mbuf\n");
344 build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_MEMBERSHIP_REPORT);
345 task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
348 void igmp_leave_group(struct task_base *tbase)
350 struct task_swap *task = (struct task_swap *)tbase;
351 struct rte_mbuf *igmp_mbuf;
352 uint8_t out[64] = {0};
355 task->igmp_address = 0;
356 ret = rte_mempool_get(task->igmp_pool, (void **)&igmp_mbuf);
358 plog_err("Unable to allocate igmp mbuf\n");
361 build_igmp_message(tbase, igmp_mbuf, task->igmp_address, IGMP_LEAVE_GROUP);
362 task->base.tx_pkt(&task->base, &igmp_mbuf, 1, out);
365 static void init_task_swap(struct task_base *tbase, struct task_args *targ)
367 struct task_swap *task = (struct task_swap *)tbase;
368 prox_rte_ether_addr *src_addr, *dst_addr;
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
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
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));
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");
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);
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);
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";
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,
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;
421 task->local_ipv4 = rte_cpu_to_be_32(targ->local_ipv4);
423 struct prox_port_cfg *port = find_reachable_port(targ);
425 task->offload_crc = port->requested_tx_offload & (DEV_TX_OFFLOAD_IPV4_CKSUM | DEV_TX_OFFLOAD_UDP_CKSUM);
429 static struct task_init task_init_swap = {
431 .init = init_task_swap,
432 .handle = handle_swap_bulk,
434 .size = sizeof(struct task_swap),
437 __attribute__((constructor)) static void reg_task_swap(void)
439 reg_task(&task_init_swap);