Fix tons of deprecation warnings
[samplevnf.git] / VNFs / DPPD-PROX / prox_cksum.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 "prox_cksum.h"
18 #include "prox_port_cfg.h"
19 #include <rte_byteorder.h>
20 #include "log.h"
21
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)
25 {
26         const uint16_t size = sizeof(prox_rte_ipv4_hdr);
27         uint32_t cksum = 0;
28         uint32_t nb_dwords;
29         uint32_t tail, mask;
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.
37         */
38         typedef uint32_t __attribute__((__may_alias__)) uint32_may_alias;
39         uint32_may_alias *pdwd = (uint32_may_alias *)buf;
40
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);
45                 ++pdwd;
46         }
47
48         /* deal with the odd byte length */
49         if (size & 0x03) {
50                 tail = *pdwd;
51                 /* calculate mask for valid parts */
52                 mask = 0xFFFFFFFF << ((size & 0x03) << 3);
53                 /* clear unused bits */
54                 tail &= ~mask;
55
56                 cksum += (tail >> 16) + (tail & 0xFFFF);
57         }
58
59         cksum = (cksum >> 16) + (cksum & 0xFFFF);
60         cksum = (cksum >> 16) + (cksum & 0xFFFF);
61
62         buf->hdr_checksum = ~((uint16_t)cksum);
63 }
64
65 static inline uint16_t calc_pseudo_checksum(uint8_t ipproto, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
66 {
67         uint32_t csum = 0;
68
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);
73         return csum;
74 }
75
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)
77 {
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
83            hardware. */
84         udp->dgram_cksum = calc_pseudo_checksum(IPPROTO_UDP, len, src_ip_addr, dst_ip_addr);
85 }
86
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)
88 {
89         tcp->cksum = calc_pseudo_checksum(IPPROTO_TCP, len, src_ip_addr, dst_ip_addr);
90 }
91
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)
93 {
94         prox_ip_cksum(mbuf, pip, l2_len, l3_len, cksum_offload & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM);
95
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);
99 #ifndef SOFT_CRC
100                 if (cksum_offload & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
101                         mbuf->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
102                         prox_write_udp_pseudo_hdr(udp, l4_len, pip->src_addr, pip->dst_addr);
103                 } else
104 #endif
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);
108 #ifndef SOFT_CRC
109                 if (cksum_offload & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
110                         prox_write_tcp_pseudo_hdr(tcp, l4_len, pip->src_addr, pip->dst_addr);
111                         mbuf->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
112                 } else
113 #endif
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);
119         }
120 }
121
122 static inline uint16_t checksum_byte_seq(uint16_t *buf, uint16_t len)
123 {
124         uint32_t csum = 0;
125
126         while (len > 1) {
127                 csum += *buf;
128                 while (csum >> 16) {
129                         csum &= 0xffff;
130                         csum +=1;
131                 }
132                 buf++;
133                 len -= 2;
134         }
135
136         if (len) {
137                 csum += *(uint8_t*)buf;
138                 while (csum >> 16) {
139                         csum &= 0xffff;
140                         csum +=1;
141                 }
142         }
143         return ~csum;
144 }
145
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)
147 {
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;
151 }
152
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)
154 {
155         prox_write_tcp_pseudo_hdr(tcp, len, src_ip_addr, dst_ip_addr);
156
157         uint16_t csum = checksum_byte_seq((uint16_t *)tcp, len);
158         tcp->cksum = csum;
159 }
160
161 inline void prox_igmp_cksum_sw(struct igmpv2_hdr *igmp, uint16_t len)
162 {
163         igmp->checksum = 0;
164         uint16_t csum = checksum_byte_seq((uint16_t *)igmp, len);
165         igmp->checksum = csum;
166 }