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>
25 vlib_buffer_push_udp (vlib_buffer_t * b, u16 sp, u16 dp, u8 offload_csum)
28 u16 udp_len = sizeof (udp_header_t) + b->current_length;
29 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID))
30 udp_len += b->total_length_not_including_first_buffer;
32 uh = vlib_buffer_push_uninit (b, sizeof (udp_header_t));
36 uh->length = clib_host_to_net_u16 (udp_len);
38 vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
39 vnet_buffer (b)->l4_hdr_offset = (u8 *) uh - b->data;
40 b->flags |= VNET_BUFFER_F_L4_HDR_OFFSET_VALID;
45 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
56 ip0 = vlib_buffer_get_current (b0);
58 /* fix the <bleep>ing outer-IP checksum */
60 /* old_l0 always 0, see the rewrite setup */
61 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
63 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
64 length /* changed member */ );
65 ip0->checksum = ip_csum_fold (sum0);
69 udp0 = (udp_header_t *) (ip0 + 1);
70 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
72 udp0->length = new_l0;
79 ip0 = vlib_buffer_get_current (b0);
81 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
83 ip0->payload_length = new_l0;
86 udp0 = (udp_header_t *) (ip0 + 1);
87 udp0->length = new_l0;
90 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
93 if (udp0->checksum == 0)
94 udp0->checksum = 0xffff;
99 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
102 vlib_buffer_advance (b0, -ec_len);
108 ip0 = vlib_buffer_get_current (b0);
110 /* Apply the encap string. */
111 clib_memcpy_fast (ip0, ec0, ec_len);
112 ip_udp_fixup_one (vm, b0, 1);
118 ip0 = vlib_buffer_get_current (b0);
120 /* Apply the encap string. */
121 clib_memcpy_fast (ip0, ec0, ec_len);
122 ip_udp_fixup_one (vm, b0, 0);
127 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
128 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
131 udp_header_t *udp0, *udp1;
133 ASSERT (_vec_len (ec0) == _vec_len (ec1));
135 vlib_buffer_advance (b0, -ec_len);
136 vlib_buffer_advance (b1, -ec_len);
140 ip4_header_t *ip0, *ip1;
141 ip_csum_t sum0, sum1;
142 u16 old_l0 = 0, old_l1 = 0;
144 ip0 = vlib_buffer_get_current (b0);
145 ip1 = vlib_buffer_get_current (b1);
147 /* Apply the encap string */
148 clib_memcpy_fast (ip0, ec0, ec_len);
149 clib_memcpy_fast (ip1, ec1, ec_len);
151 /* fix the <bleep>ing outer-IP checksum */
152 sum0 = ip0->checksum;
153 sum1 = ip1->checksum;
155 /* old_l0 always 0, see the rewrite setup */
156 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
157 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
159 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
160 length /* changed member */ );
161 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
162 length /* changed member */ );
164 ip0->checksum = ip_csum_fold (sum0);
165 ip1->checksum = ip_csum_fold (sum1);
167 ip0->length = new_l0;
168 ip1->length = new_l1;
171 udp0 = (udp_header_t *) (ip0 + 1);
172 udp1 = (udp_header_t *) (ip1 + 1);
175 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
178 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
180 udp0->length = new_l0;
181 udp1->length = new_l1;
185 ip6_header_t *ip0, *ip1;
188 ip0 = vlib_buffer_get_current (b0);
189 ip1 = vlib_buffer_get_current (b1);
191 /* Apply the encap string. */
192 clib_memcpy_fast (ip0, ec0, ec_len);
193 clib_memcpy_fast (ip1, ec1, ec_len);
195 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
197 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
199 ip0->payload_length = new_l0;
200 ip1->payload_length = new_l1;
203 udp0 = (udp_header_t *) (ip0 + 1);
204 udp1 = (udp_header_t *) (ip1 + 1);
206 udp0->length = new_l0;
207 udp1->length = new_l1;
210 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
212 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
213 ASSERT (bogus0 == 0);
214 ASSERT (bogus1 == 0);
216 if (udp0->checksum == 0)
217 udp0->checksum = 0xffff;
218 if (udp1->checksum == 0)
219 udp1->checksum = 0xffff;
223 #endif /* SRC_VNET_UDP_UDP_INLINES_H_ */
226 * fd.io coding-style-patch-verification: ON
229 * eval: (c-set-style "gnu")