2 // Copyright (c) 2010-2017 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.
17 #include "prox_cksum.h"
18 #include "prox_port_cfg.h"
19 #include <rte_byteorder.h>
22 /* compute IP 16 bit checksum */
23 /* The hdr_checksum field must be set to 0 by the caller. */
24 inline void prox_ip_cksum_sw(prox_rte_ipv4_hdr *buf)
26 const uint16_t size = sizeof(prox_rte_ipv4_hdr);
30 /* Defining pdwd as (uint32_t *) causes some optimization issues (gcc -O2).
31 In prox_ip_cksum(), hdr_checksum is set to 0, as expected by the code below,
32 but when *pdwd is plain uint32_t, GCC does not see the pointer aliasing on
33 the IPv4 header, optimizes this hdr_checksum initialization away, and hence
34 breaks the expectations of the checksum computation loop below.
35 The following typedef tells GCC that the IPv4 header may be aliased by
36 pdwd, which prevents GCC from removing the hdr_checksum = 0 assignment.
38 typedef uint32_t __attribute__((__may_alias__)) uint32_may_alias;
39 uint32_may_alias *pdwd = (uint32_may_alias *)buf;
41 /* compute 16 bit checksum using hi and low parts of 32 bit integers */
42 for (nb_dwords = (size >> 2); nb_dwords > 0; --nb_dwords) {
43 cksum += (*pdwd >> 16);
44 cksum += (*pdwd & 0xFFFF);
48 /* deal with the odd byte length */
51 /* calculate mask for valid parts */
52 mask = 0xFFFFFFFF << ((size & 0x03) << 3);
53 /* clear unused bits */
56 cksum += (tail >> 16) + (tail & 0xFFFF);
59 cksum = (cksum >> 16) + (cksum & 0xFFFF);
60 cksum = (cksum >> 16) + (cksum & 0xFFFF);
62 buf->hdr_checksum = ~((uint16_t)cksum);
65 static inline uint16_t calc_pseudo_checksum(uint8_t ipproto, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
69 csum += (src_ip_addr >> 16) + (src_ip_addr & 0xFFFF);
70 csum += (dst_ip_addr >> 16) + (dst_ip_addr & 0xFFFF);
71 csum += rte_bswap16(ipproto) + rte_bswap16(len);
72 csum = (csum >> 16) + (csum & 0xFFFF);
76 static inline void prox_write_udp_pseudo_hdr(prox_rte_udp_hdr *udp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
78 /* Note that the csum is not complemented, while the pseaudo
79 header checksum is calculated as "... the 16-bit one's
80 complement of the one's complement sum of a pseudo header
81 of information ...", the psuedoheader forms as a basis for
82 the actual checksum calculated later either in software or
84 udp->dgram_cksum = calc_pseudo_checksum(IPPROTO_UDP, len, src_ip_addr, dst_ip_addr);
87 static inline void prox_write_tcp_pseudo_hdr(prox_rte_tcp_hdr *tcp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
89 tcp->cksum = calc_pseudo_checksum(IPPROTO_TCP, len, src_ip_addr, dst_ip_addr);
92 inline void prox_ip_udp_cksum(struct rte_mbuf *mbuf, prox_rte_ipv4_hdr *pip, uint16_t l2_len, uint16_t l3_len, int cksum_offload)
94 prox_ip_cksum(mbuf, pip, l2_len, l3_len, cksum_offload & DEV_TX_OFFLOAD_IPV4_CKSUM);
96 uint32_t l4_len = rte_bswap16(pip->total_length) - l3_len;
97 if (pip->next_proto_id == IPPROTO_UDP) {
98 prox_rte_udp_hdr *udp = (prox_rte_udp_hdr *)(((uint8_t*)pip) + l3_len);
100 if (cksum_offload & DEV_TX_OFFLOAD_UDP_CKSUM) {
101 mbuf->ol_flags |= PKT_TX_UDP_CKSUM;
102 prox_write_udp_pseudo_hdr(udp, l4_len, pip->src_addr, pip->dst_addr);
105 prox_udp_cksum_sw(udp, l4_len, pip->src_addr, pip->dst_addr);
106 } else if (pip->next_proto_id == IPPROTO_TCP) {
107 prox_rte_tcp_hdr *tcp = (prox_rte_tcp_hdr *)(((uint8_t*)pip) + l3_len);
109 if (cksum_offload & DEV_TX_OFFLOAD_TCP_CKSUM) {
110 prox_write_tcp_pseudo_hdr(tcp, l4_len, pip->src_addr, pip->dst_addr);
111 mbuf->ol_flags |= PKT_TX_UDP_CKSUM;
114 prox_tcp_cksum_sw(tcp, l4_len, pip->src_addr, pip->dst_addr);
115 } else if (pip->next_proto_id == IPPROTO_IGMP) {
116 struct igmpv2_hdr *igmp = (struct igmpv2_hdr *)(((uint8_t*)pip) + l3_len);
117 // Not sure NIC can offload IGMP checkum => do it in software
118 prox_igmp_cksum_sw(igmp, l4_len);
122 static inline uint16_t checksum_byte_seq(uint16_t *buf, uint16_t len)
137 csum += *(uint8_t*)buf;
146 inline void prox_udp_cksum_sw(prox_rte_udp_hdr *udp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
148 prox_write_udp_pseudo_hdr(udp, len, src_ip_addr, dst_ip_addr);
149 uint16_t csum = checksum_byte_seq((uint16_t *)udp, len);
150 udp->dgram_cksum = csum;
153 inline void prox_tcp_cksum_sw(prox_rte_tcp_hdr *tcp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
155 prox_write_tcp_pseudo_hdr(tcp, len, src_ip_addr, dst_ip_addr);
157 uint16_t csum = checksum_byte_seq((uint16_t *)tcp, len);
161 inline void prox_igmp_cksum_sw(struct igmpv2_hdr *igmp, uint16_t len)
164 uint16_t csum = checksum_byte_seq((uint16_t *)igmp, len);
165 igmp->checksum = csum;