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 if (payload_family < N_AF)
147 vnet_calc_checksums_inline (vm, b0, payload_ip4, !payload_ip4);
148 vnet_calc_checksums_inline (vm, b1, payload_ip4, !payload_ip4);
151 vlib_buffer_advance (b0, -ec_len);
152 vlib_buffer_advance (b1, -ec_len);
154 if (encap_family == AF_IP4)
156 ip4_header_t *ip0, *ip1;
157 ip_csum_t sum0, sum1;
158 u16 old_l0 = 0, old_l1 = 0;
160 ip0 = vlib_buffer_get_current (b0);
161 ip1 = vlib_buffer_get_current (b1);
163 /* Apply the encap string */
164 clib_memcpy_fast (ip0, ec0, ec_len);
165 clib_memcpy_fast (ip1, ec1, ec_len);
167 /* fix the <bleep>ing outer-IP checksum */
168 sum0 = ip0->checksum;
169 sum1 = ip1->checksum;
171 /* old_l0 always 0, see the rewrite setup */
172 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
173 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
175 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
176 length /* changed member */ );
177 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
178 length /* changed member */ );
180 ip0->checksum = ip_csum_fold (sum0);
181 ip1->checksum = ip_csum_fold (sum1);
183 ip0->length = new_l0;
184 ip1->length = new_l1;
187 udp0 = (udp_header_t *) (ip0 + 1);
188 udp1 = (udp_header_t *) (ip1 + 1);
191 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
194 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
196 udp0->length = new_l0;
197 udp1->length = new_l1;
201 ip6_header_t *ip0, *ip1;
204 ip0 = vlib_buffer_get_current (b0);
205 ip1 = vlib_buffer_get_current (b1);
207 /* Apply the encap string. */
208 clib_memcpy_fast (ip0, ec0, ec_len);
209 clib_memcpy_fast (ip1, ec1, ec_len);
211 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
213 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
215 ip0->payload_length = new_l0;
216 ip1->payload_length = new_l1;
219 udp0 = (udp_header_t *) (ip0 + 1);
220 udp1 = (udp_header_t *) (ip1 + 1);
222 udp0->length = new_l0;
223 udp1->length = new_l1;
226 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
228 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
229 ASSERT (bogus0 == 0);
230 ASSERT (bogus1 == 0);
232 if (udp0->checksum == 0)
233 udp0->checksum = 0xffff;
234 if (udp1->checksum == 0)
235 udp1->checksum = 0xffff;
239 #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */
242 * fd.io coding-style-patch-verification: ON
245 * eval: (c-set-style "gnu")