Add heartbeat support (stop all cores in case of TCP socket issues)
[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(struct ipv4_hdr *buf)
25 {
26         const uint16_t size = sizeof(struct 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(struct 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(struct 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, struct 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 & DEV_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                 struct udp_hdr *udp = (struct udp_hdr *)(((uint8_t*)pip) + l3_len);
99 #ifndef SOFT_CRC
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);
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                 struct tcp_hdr *tcp = (struct tcp_hdr *)(((uint8_t*)pip) + l3_len);
108 #ifndef SOFT_CRC
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;
112                 } else
113 #endif
114                 prox_tcp_cksum_sw(tcp, l4_len, pip->src_addr, pip->dst_addr);
115         }
116 }
117
118 static inline uint16_t checksum_byte_seq(uint16_t *buf, uint16_t len)
119 {
120         uint32_t csum = 0;
121
122         while (len > 1) {
123                 csum += *buf;
124                 while (csum >> 16) {
125                         csum &= 0xffff;
126                         csum +=1;
127                 }
128                 buf++;
129                 len -= 2;
130         }
131
132         if (len) {
133                 csum += *(uint8_t*)buf;
134                 while (csum >> 16) {
135                         csum &= 0xffff;
136                         csum +=1;
137                 }
138         }
139         return ~csum;
140 }
141
142 inline void prox_udp_cksum_sw(struct udp_hdr *udp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
143 {
144         prox_write_udp_pseudo_hdr(udp, len, src_ip_addr, dst_ip_addr);
145         uint16_t csum = checksum_byte_seq((uint16_t *)udp, len);
146         udp->dgram_cksum = csum;
147 }
148
149 inline void prox_tcp_cksum_sw(struct tcp_hdr *tcp, uint16_t len, uint32_t src_ip_addr, uint32_t dst_ip_addr)
150 {
151         prox_write_tcp_pseudo_hdr(tcp, len, src_ip_addr, dst_ip_addr);
152
153         uint16_t csum = checksum_byte_seq((uint16_t *)tcp, len);
154         tcp->cksum = csum;
155 }