/* * Copyright (c) 2016 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** @file udp state machine, etc. */ #include #include #include #include udp_connection_t * udp_connection_alloc (u32 thread_index) { udp_main_t *um = &udp_main; udp_connection_t *uc; u32 will_expand = 0; pool_get_aligned_will_expand (um->connections[thread_index], will_expand, CLIB_CACHE_LINE_BYTES); if (PREDICT_FALSE (will_expand)) { clib_spinlock_lock_if_init (&udp_main.peekers_write_locks [thread_index]); pool_get_aligned (udp_main.connections[thread_index], uc, CLIB_CACHE_LINE_BYTES); clib_spinlock_unlock_if_init (&udp_main.peekers_write_locks [thread_index]); } else { pool_get_aligned (um->connections[thread_index], uc, CLIB_CACHE_LINE_BYTES); } memset (uc, 0, sizeof (*uc)); uc->c_c_index = uc - um->connections[thread_index]; uc->c_thread_index = thread_index; uc->c_proto = TRANSPORT_PROTO_UDP; return uc; } void udp_connection_free (udp_connection_t * uc) { pool_put (udp_main.connections[uc->c_thread_index], uc); if (CLIB_DEBUG) memset (uc, 0xFA, sizeof (*uc)); } u32 udp_session_bind (u32 session_index, transport_endpoint_t * lcl) { udp_main_t *um = vnet_get_udp_main (); vlib_main_t *vm = vlib_get_main (); udp_connection_t *listener; u32 node_index; void *iface_ip; udp_dst_port_info_t *pi; pi = udp_get_dst_port_info (um, lcl->port, lcl->is_ip4); if (pi) return -1; pool_get (um->listener_pool, listener); memset (listener, 0, sizeof (udp_connection_t)); listener->c_lcl_port = lcl->port; listener->c_c_index = listener - um->listener_pool; /* If we are provided a sw_if_index, bind using one of its ips */ if (ip_is_zero (&lcl->ip, 1) && lcl->sw_if_index != ENDPOINT_INVALID_INDEX) { if ((iface_ip = ip_interface_get_first_ip (lcl->sw_if_index, lcl->is_ip4))) ip_set (&lcl->ip, iface_ip, lcl->is_ip4); } ip_copy (&listener->c_lcl_ip, &lcl->ip, lcl->is_ip4); listener->c_is_ip4 = lcl->is_ip4; listener->c_proto = TRANSPORT_PROTO_UDP; listener->c_s_index = session_index; listener->c_fib_index = lcl->fib_index; node_index = lcl->is_ip4 ? udp4_input_node.index : udp6_input_node.index; udp_register_dst_port (vm, clib_net_to_host_u16 (lcl->port), node_index, 1 /* is_ipv4 */ ); return listener->c_c_index; } u32 udp_session_unbind (u32 listener_index) { vlib_main_t *vm = vlib_get_main (); udp_connection_t *listener; listener = udp_listener_get (listener_index); udp_unregister_dst_port (vm, clib_net_to_host_u16 (listener->c_lcl_port), listener->c_is_ip4); return 0; } transport_connection_t * udp_session_get_listener (u32 listener_index) { udp_connection_t *us; us = udp_listener_get (listener_index); return &us->connection; } u32 udp_push_header (transport_connection_t * tc, vlib_buffer_t * b) { udp_connection_t *uc; vlib_main_t *vm = vlib_get_main (); uc = udp_get_connection_from_transport (tc); vlib_buffer_push_udp (b, uc->c_lcl_port, uc->c_rmt_port, 1); if (tc->is_ip4) vlib_buffer_push_ip4 (vm, b, &uc->c_lcl_ip4, &uc->c_rmt_ip4, IP_PROTOCOL_UDP, 1); else { ip6_header_t *ih; ih = vlib_buffer_push_ip6 (vm, b, &uc->c_lcl_ip6, &uc->c_rmt_ip6, IP_PROTOCOL_UDP); vnet_buffer (b)->l3_hdr_offset = (u8 *) ih - b->data; } vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; return 0; } transport_connection_t * udp_session_get (u32 connection_index, u32 thread_index) { udp_connection_t *uc; uc = udp_connection_get (connection_index, thread_index); if (uc) return &uc->connection; return 0; } void udp_session_close (u32 connection_index, u32 thread_index) { vlib_main_t *vm = vlib_get_main (); udp_connection_t *uc; uc = udp_connection_get (connection_index, thread_index); if (uc) { udp_unregister_dst_port (vm, clib_net_to_host_u16 (uc->c_lcl_port), uc->c_is_ip4); stream_session_delete_notify (&uc->connection); udp_connection_free (uc); } } void udp_session_cleanup (u32 connection_index, u32 thread_index) { udp_connection_t *uc; uc = udp_connection_get (connection_index, thread_index); if (uc) udp_connection_free (uc); } u8 * format_udp_connection_id (u8 * s, va_list * args) { udp_connection_t *uc =