2 * Copyright (c) 2017 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.
15 #ifndef __included_udp_h__
16 #define __included_udp_h__
18 #include <vnet/vnet.h>
19 #include <vnet/udp/udp_packet.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip/ip4.h>
22 #include <vnet/ip/ip4_packet.h>
23 #include <vnet/pg/pg.h>
24 #include <vnet/ip/format.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/session/transport.h>
31 transport_connection_t connection; /** must be first */
33 /** ersatz MTU to limit fifo pushes to test data size */
37 typedef struct _udp_uri_main
39 /* Per-worker thread udp connection pools */
40 udp_connection_t **udp_sessions;
41 udp_connection_t *udp_listeners;
44 vlib_main_t *vlib_main;
45 vnet_main_t *vnet_main;
50 extern udp_uri_main_t udp_uri_main;
51 extern vlib_node_registration_t udp4_uri_input_node;
53 always_inline udp_uri_main_t *
59 always_inline udp_connection_t *
60 udp_connection_get (u32 conn_index, u32 thread_index)
62 return pool_elt_at_index (udp_uri_main.udp_sessions[thread_index],
66 always_inline udp_connection_t *
67 udp_listener_get (u32 conn_index)
69 return pool_elt_at_index (udp_uri_main.udp_listeners, conn_index);
74 #define udp_error(n,s) UDP_ERROR_##n,
75 #include <vnet/udp/udp_error.def>
80 #define foreach_udp4_dst_port \
81 _ (67, dhcp_to_server) \
82 _ (68, dhcp_to_client) \
95 #define foreach_udp6_dst_port \
96 _ (547, dhcpv6_to_server) \
97 _ (546, dhcpv6_to_client) \
100 _ (4341, lisp_gpe6) \
102 _ (4790, vxlan6_gpe) \
107 #define _(n,f) UDP_DST_PORT_##f = n,
108 foreach_udp4_dst_port foreach_udp6_dst_port
114 #define _(n,f) UDP6_DST_PORT_##f = n,
115 foreach_udp6_dst_port
121 /* Name (a c string). */
124 /* GRE protocol type in host byte order. */
125 udp_dst_port_t dst_port;
127 /* Node which handles this type. */
130 /* Next index for this type. */
132 } udp_dst_port_info_t;
137 UDP_IP4, /* the code is full of is_ip4... */
143 udp_dst_port_info_t *dst_port_infos[N_UDP_AF];
145 /* Hash tables mapping name/protocol to protocol info index. */
146 uword *dst_port_info_by_name[N_UDP_AF];
147 uword *dst_port_info_by_dst_port[N_UDP_AF];
149 /* Sparse vector mapping udp dst_port in network byte order
151 u16 *next_by_dst_port4;
152 u16 *next_by_dst_port6;
157 vlib_main_t *vlib_main;
160 always_inline udp_dst_port_info_t *
161 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
163 uword *p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
164 return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
167 format_function_t format_udp_header;
168 format_function_t format_udp_rx_trace;
170 unformat_function_t unformat_udp_header;
172 void udp_register_dst_port (vlib_main_t * vm,
173 udp_dst_port_t dst_port,
174 u32 node_index, u8 is_ip4);
177 udp_unregister_dst_port (vlib_main_t * vm,
178 udp_dst_port_t dst_port, u8 is_ip4);
180 void udp_punt_unknown (vlib_main_t * vm, u8 is_ip4, u8 is_add);
183 ip_udp_fixup_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 is_ip4)
194 ip0 = vlib_buffer_get_current (b0);
196 /* fix the <bleep>ing outer-IP checksum */
197 sum0 = ip0->checksum;
198 /* old_l0 always 0, see the rewrite setup */
199 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
201 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
202 length /* changed member */ );
203 ip0->checksum = ip_csum_fold (sum0);
204 ip0->length = new_l0;
207 udp0 = (udp_header_t *) (ip0 + 1);
208 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
210 udp0->length = new_l0;
217 ip0 = vlib_buffer_get_current (b0);
219 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
221 ip0->payload_length = new_l0;
224 udp0 = (udp_header_t *) (ip0 + 1);
225 udp0->length = new_l0;
228 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
229 ASSERT (bogus0 == 0);
231 if (udp0->checksum == 0)
232 udp0->checksum = 0xffff;
237 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
240 vlib_buffer_advance (b0, -ec_len);
246 ip0 = vlib_buffer_get_current (b0);
248 /* Apply the encap string. */
249 clib_memcpy (ip0, ec0, ec_len);
250 ip_udp_fixup_one (vm, b0, 1);
256 ip0 = vlib_buffer_get_current (b0);
258 /* Apply the encap string. */
259 clib_memcpy (ip0, ec0, ec_len);
260 ip_udp_fixup_one (vm, b0, 0);
265 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
266 u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
269 udp_header_t *udp0, *udp1;
271 ASSERT (_vec_len (ec0) == _vec_len (ec1));
273 vlib_buffer_advance (b0, -ec_len);
274 vlib_buffer_advance (b1, -ec_len);
278 ip4_header_t *ip0, *ip1;
279 ip_csum_t sum0, sum1;
280 u16 old_l0 = 0, old_l1 = 0;
282 ip0 = vlib_buffer_get_current (b0);
283 ip1 = vlib_buffer_get_current (b1);
285 /* Apply the encap string */
286 clib_memcpy (ip0, ec0, ec_len);
287 clib_memcpy (ip1, ec1, ec_len);
289 /* fix the <bleep>ing outer-IP checksum */
290 sum0 = ip0->checksum;
291 sum1 = ip1->checksum;
293 /* old_l0 always 0, see the rewrite setup */
294 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
295 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
297 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
298 length /* changed member */ );
299 sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t,
300 length /* changed member */ );
302 ip0->checksum = ip_csum_fold (sum0);
303 ip1->checksum = ip_csum_fold (sum1);
305 ip0->length = new_l0;
306 ip1->length = new_l1;
309 udp0 = (udp_header_t *) (ip0 + 1);
310 udp1 = (udp_header_t *) (ip1 + 1);
313 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
316 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1) -
318 udp0->length = new_l0;
319 udp1->length = new_l1;
323 ip6_header_t *ip0, *ip1;
326 ip0 = vlib_buffer_get_current (b0);
327 ip1 = vlib_buffer_get_current (b1);
329 /* Apply the encap string. */
330 clib_memcpy (ip0, ec0, ec_len);
331 clib_memcpy (ip1, ec1, ec_len);
333 new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
335 new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
337 ip0->payload_length = new_l0;
338 ip1->payload_length = new_l1;
341 udp0 = (udp_header_t *) (ip0 + 1);
342 udp1 = (udp_header_t *) (ip1 + 1);
344 udp0->length = new_l0;
345 udp1->length = new_l1;
348 ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
350 ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
351 ASSERT (bogus0 == 0);
352 ASSERT (bogus1 == 0);
354 if (udp0->checksum == 0)
355 udp0->checksum = 0xffff;
356 if (udp1->checksum == 0)
357 udp1->checksum = 0xffff;
362 * fd.io coding-style-patch-verification: ON
365 * eval: (c-set-style "gnu")
369 #endif /* __included_udp_h__ */