2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef SRC_VNET_UDP_UDP_INLINES_H_
17 #define SRC_VNET_UDP_UDP_INLINES_H_
19 #include <vnet/vnet.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/ip/ip6.h>
22 #include <vnet/udp/udp_packet.h>
23 #include <vnet/interface_output.h>
26 vlib_buffer_push_udp (vlib_buffer_t * b, u16 sp, u16 dp, u8 offload_csum)
29 u16 udp_len = sizeof (udp_header_t) + b->current_length;
30 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
31 udp_len += b->total_length_not_including_first_buffer;
33 uh = vlib_buffer_push_uninit (b, sizeof (udp_header_t));
37 uh->length = clib_host_to_net_u16 (udp_len);
39 vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
40 vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data;
41 b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
46 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
57 ip0 = vlib_buffer_get_current (b0);
59 /* fix the <bleep>ing outer-IP checksum */
61 /* old_l0 always 0, see the rewrite setup */
62 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
64 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
65 length /* changed member */ );
66 ip0->checksum = ip_csum_fold (sum0);
70 udp0 = (udp_header_t *) (ip0 + 1);
71 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
73 udp0->length = new_l0;
80 ip0 = vlib_buffer_get_current (b0);
82 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
84 ip0->payload_length = new_l0;
87 udp0 = (udp_header_t *) (ip0 + 1);
88 udp0->length = new_l0;
91 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
94 if (udp0->checksum == 0)
95 udp0->checksum = 0xffff;
100 ip_udp_encap_one (vlib_main_t *vm, vlib_buffer_t *b0, u8 *ec0, word ec_len,
101 ip_address_family_t encap_family,
102 ip_address_family_t payload_family)
105 if (payload_family < N_AF)
107 vnet_calc_checksums_inline (vm, b0, payload_family == AF_IP4,
108 payload_family == AF_IP6);
111 vlib_buffer_advance (b0, -ec_len);
113 if (encap_family == AF_IP4)
117 ip0 = vlib_buffer_get_current (b0);
119 /* Apply the encap string. */
120 clib_memcpy_fast (ip0, ec0, ec_len);
121 ip_udp_fixup_one (vm, b0, 1);
127 ip0 = vlib_buffer_get_current (b0);
129 /* Apply the encap string. */
130 clib_memcpy_fast (ip0, ec0, ec_len);
131 ip_udp_fixup_one (vm, b0, 0);
136 ip_udp_encap_two (vlib_main_t *vm, vlib_buffer_t *b0, vlib_buffer_t *b1,
137 u8 *ec0, u8 *ec1, word ec_len,
138 ip_address_family_t encap_family,
139 ip_address_family_t payload_family)
142 udp_header_t *udp0, *udp1;
143 int payload_ip4 = (payload_family == AF_IP4);
145 ASSERT (_vec_len (ec0) == _vec_len (ec1));
147 if (payload_family < N_AF)
149 vnet_calc_checksums_inline (vm, b0, payload_ip4, !payload_ip4);
150 vnet_calc_checksums_inline (vm, b1, payload_ip4, !payload_ip4);
153 vlib_buffer_advance (b0, -ec_len);
154 vlib_buffer_advance (b1, -ec_len);
156 if (encap_family == AF_IP4)
158 ip4_header_t *ip0, *ip1;
159 ip_csum_t sum0, sum1;
160 u16 old_l0 = 0, old_l1 = 0;
162 ip0 = vlib_buffer_get_current (b0);
163 ip1 = vlib_buffer_get_current (b1);
165 /* Apply the encap string */
166 clib_memcpy_fast (ip0, ec0, ec_len);
167 clib_memcpy_fast (ip1, ec1, ec_len);
169 /* fix the <bleep>ing outer-IP checksum */
170 sum0 = ip0->checksum;
171 sum1 = ip1->checksum;
173 /* old_l0 always 0, see the rewrite setup */
174 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
175 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
177 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
178 length /* changed member */ );
179 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
180 length /* changed member */ );
182 ip0->checksum = ip_csum_fold (sum0);
183 ip1->checksum = ip_csum_fold (sum1);
185 ip0->length = new_l0;
186 ip1->length = new_l1;
189 udp0 = (udp_header_t *) (ip0 + 1);
190 udp1 = (udp_header_t *) (ip1 + 1);
193 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
196 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
198 udp0->length = new_l0;
199 udp1->length = new_l1;
203 ip6_header_t *ip0, *ip1;
206 ip0 = vlib_buffer_get_current (b0);
207 ip1 = vlib_buffer_get_current (b1);
209 /* Apply the encap string. */
210 clib_memcpy_fast (ip0, ec0, ec_len);
211 clib_memcpy_fast (ip1, ec1, ec_len);
213 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
215 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
217 ip0->payload_length = new_l0;
218 ip1->payload_length = new_l1;
221 udp0 = (udp_header_t *) (ip0 + 1);
222 udp1 = (udp_header_t *) (ip1 + 1);
224 udp0->length = new_l0;
225 udp1->length = new_l1;
228 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
230 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
231 ASSERT (bogus0 == 0);
232 ASSERT (bogus1 == 0);
234 if (udp0->checksum == 0)
235 udp0->checksum = 0xffff;
236 if (udp1->checksum == 0)
237 udp1->checksum = 0xffff;
241 #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */
244 * fd.io coding-style-patch-verification: ON
247 * eval: (c-set-style "gnu")