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,
103 vnet_calc_checksums_inline (vm, b0, is_ip4, !is_ip4);
105 vlib_buffer_advance (b0, -ec_len);
111 ip0 = vlib_buffer_get_current (b0);
113 /* Apply the encap string. */
114 clib_memcpy_fast (ip0, ec0, ec_len);
115 ip_udp_fixup_one (vm, b0, 1);
121 ip0 = vlib_buffer_get_current (b0);
123 /* Apply the encap string. */
124 clib_memcpy_fast (ip0, ec0, ec_len);
125 ip_udp_fixup_one (vm, b0, 0);
130 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
131 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
134 udp_header_t *udp0, *udp1;
136 ASSERT (_vec_len (ec0) == _vec_len (ec1));
138 vnet_calc_checksums_inline (vm, b0, is_v4, !is_v4);
139 vnet_calc_checksums_inline (vm, b1, is_v4, !is_v4);
141 vlib_buffer_advance (b0, -ec_len);
142 vlib_buffer_advance (b1, -ec_len);
146 ip4_header_t *ip0, *ip1;
147 ip_csum_t sum0, sum1;
148 u16 old_l0 = 0, old_l1 = 0;
150 ip0 = vlib_buffer_get_current (b0);
151 ip1 = vlib_buffer_get_current (b1);
153 /* Apply the encap string */
154 clib_memcpy_fast (ip0, ec0, ec_len);
155 clib_memcpy_fast (ip1, ec1, ec_len);
157 /* fix the <bleep>ing outer-IP checksum */
158 sum0 = ip0->checksum;
159 sum1 = ip1->checksum;
161 /* old_l0 always 0, see the rewrite setup */
162 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
163 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
165 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
166 length /* changed member */ );
167 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
168 length /* changed member */ );
170 ip0->checksum = ip_csum_fold (sum0);
171 ip1->checksum = ip_csum_fold (sum1);
173 ip0->length = new_l0;
174 ip1->length = new_l1;
177 udp0 = (udp_header_t *) (ip0 + 1);
178 udp1 = (udp_header_t *) (ip1 + 1);
181 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
184 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
186 udp0->length = new_l0;
187 udp1->length = new_l1;
191 ip6_header_t *ip0, *ip1;
194 ip0 = vlib_buffer_get_current (b0);
195 ip1 = vlib_buffer_get_current (b1);
197 /* Apply the encap string. */
198 clib_memcpy_fast (ip0, ec0, ec_len);
199 clib_memcpy_fast (ip1, ec1, ec_len);
201 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
203 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
205 ip0->payload_length = new_l0;
206 ip1->payload_length = new_l1;
209 udp0 = (udp_header_t *) (ip0 + 1);
210 udp1 = (udp_header_t *) (ip1 + 1);
212 udp0->length = new_l0;
213 udp1->length = new_l1;
216 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
218 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
219 ASSERT (bogus0 == 0);
220 ASSERT (bogus1 == 0);
222 if (udp0->checksum == 0)
223 udp0->checksum = 0xffff;
224 if (udp1->checksum == 0)
225 udp1->checksum = 0xffff;
229 #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */
232 * fd.io coding-style-patch-verification: ON
235 * eval: (c-set-style "gnu")