Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | |
| 19 | #include <vnet/udp/udp_encap.h> |
| 20 | #include <vnet/fib/fib_table.h> |
| 21 | |
| 22 | #include <vnet/vnet_msg_enum.h> |
| 23 | |
| 24 | #define vl_typedefs /* define message structures */ |
| 25 | #include <vnet/vnet_all_api_h.h> |
| 26 | #undef vl_typedefs |
| 27 | |
| 28 | #define vl_endianfun /* define message structures */ |
| 29 | #include <vnet/vnet_all_api_h.h> |
| 30 | #undef vl_endianfun |
| 31 | |
| 32 | /* instantiate all the print functions we know about */ |
| 33 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 34 | #define vl_printfun |
| 35 | #include <vnet/vnet_all_api_h.h> |
| 36 | #undef vl_printfun |
| 37 | |
| 38 | #include <vlibapi/api_helper_macros.h> |
| 39 | |
| 40 | |
| 41 | #define foreach_udp_api_msg \ |
| 42 | _(UDP_ENCAP_ADD_DEL, udp_encap_add_del) \ |
| 43 | _(UDP_ENCAP_DUMP, udp_encap_dump) |
| 44 | |
| 45 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 46 | send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg, |
| 47 | u32 context) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 48 | { |
| 49 | vl_api_udp_encap_details_t *mp; |
| 50 | fib_table_t *fib_table; |
| 51 | |
| 52 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 53 | memset (mp, 0, sizeof (*mp)); |
| 54 | mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS); |
| 55 | mp->context = context; |
| 56 | |
| 57 | mp->is_ip6 = (ue->ue_ip_proto == FIB_PROTOCOL_IP6); |
| 58 | |
| 59 | if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto) |
| 60 | { |
| 61 | clib_memcpy (mp->src_ip, &ue->ue_hdrs.ip4.ue_ip4.src_address, 4); |
| 62 | clib_memcpy (mp->dst_ip, &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4); |
| 63 | mp->src_port = htons (ue->ue_hdrs.ip4.ue_udp.src_port); |
| 64 | mp->dst_port = htons (ue->ue_hdrs.ip4.ue_udp.dst_port); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | clib_memcpy (mp->src_ip, &ue->ue_hdrs.ip6.ue_ip6.src_address, 16); |
| 69 | clib_memcpy (mp->dst_ip, &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16); |
| 70 | mp->src_port = htons (ue->ue_hdrs.ip6.ue_udp.src_port); |
| 71 | mp->dst_port = htons (ue->ue_hdrs.ip6.ue_udp.dst_port); |
| 72 | } |
| 73 | |
| 74 | fib_table = fib_table_get (ue->ue_fib_index, ue->ue_ip_proto); |
| 75 | mp->table_id = htonl (fib_table->ft_table_id); |
| 76 | mp->id = htonl (ue->ue_id); |
| 77 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 78 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void |
| 82 | vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t * mp, |
| 83 | vlib_main_t * vm) |
| 84 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 85 | vl_api_registration_t *reg; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 86 | udp_encap_t *ue; |
| 87 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 88 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 89 | if (!reg) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 90 | return; |
| 91 | |
| 92 | /* *INDENT-OFF* */ |
| 93 | pool_foreach(ue, udp_encap_pool, |
| 94 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 95 | send_udp_encap_details(ue, reg, mp->context); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 96 | })); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 97 | /* *INDENT-ON* */ |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void |
| 101 | vl_api_udp_encap_add_del_t_handler (vl_api_udp_encap_add_del_t * mp, |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 102 | vlib_main_t * vm) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 103 | { |
| 104 | vl_api_udp_encap_add_del_reply_t *rmp; |
| 105 | ip46_address_t src_ip, dst_ip; |
| 106 | u32 fib_index, table_id, ue_id; |
| 107 | fib_protocol_t fproto; |
| 108 | int rv = 0; |
| 109 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 110 | ue_id = ntohl (mp->id); |
| 111 | table_id = ntohl (mp->table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 112 | fproto = (mp->is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4); |
| 113 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 114 | fib_index = fib_table_find (fproto, table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 115 | |
| 116 | if (~0 == fib_index) |
| 117 | { |
| 118 | rv = VNET_API_ERROR_NO_SUCH_TABLE; |
| 119 | goto done; |
| 120 | } |
| 121 | |
| 122 | if (FIB_PROTOCOL_IP4 == fproto) |
| 123 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 124 | clib_memcpy (&src_ip.ip4, mp->src_ip, 4); |
| 125 | clib_memcpy (&dst_ip.ip4, mp->dst_ip, 4); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 129 | clib_memcpy (&src_ip.ip6, mp->src_ip, 16); |
| 130 | clib_memcpy (&dst_ip.ip6, mp->dst_ip, 16); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (mp->is_add) |
| 134 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 135 | udp_encap_add_and_lock (ue_id, fproto, fib_index, |
| 136 | &src_ip, &dst_ip, |
| 137 | ntohs (mp->src_port), |
| 138 | ntohs (mp->dst_port), UDP_ENCAP_FIXUP_NONE); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 142 | udp_encap_unlock (ue_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 145 | done: |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 146 | REPLY_MACRO (VL_API_UDP_ENCAP_ADD_DEL_REPLY); |
| 147 | } |
| 148 | |
| 149 | #define vl_msg_name_crc_list |
| 150 | #include <vnet/udp/udp.api.h> |
| 151 | #undef vl_msg_name_crc_list |
| 152 | |
| 153 | static void |
| 154 | setup_message_id_table (api_main_t * am) |
| 155 | { |
| 156 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 157 | foreach_vl_msg_name_crc_udp; |
| 158 | #undef _ |
| 159 | } |
| 160 | |
| 161 | static clib_error_t * |
| 162 | udp_api_hookup (vlib_main_t * vm) |
| 163 | { |
| 164 | api_main_t *am = &api_main; |
| 165 | |
| 166 | #define _(N,n) \ |
| 167 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 168 | vl_api_##n##_t_handler, \ |
| 169 | vl_noop_handler, \ |
| 170 | vl_api_##n##_t_endian, \ |
| 171 | vl_api_##n##_t_print, \ |
| 172 | sizeof(vl_api_##n##_t), 1); |
| 173 | foreach_udp_api_msg; |
| 174 | #undef _ |
| 175 | |
| 176 | /* |
| 177 | * Set up the (msg_name, crc, message-id) table |
| 178 | */ |
| 179 | setup_message_id_table (am); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | VLIB_API_INIT_FUNCTION (udp_api_hookup); |
| 185 | |
| 186 | /* |
| 187 | * fd.io coding-style-patch-verification: ON |
| 188 | * |
| 189 | * Local Variables: |
| 190 | * eval: (c-set-style "gnu") |
| 191 | * End: |
| 192 | */ |