Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 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/udp/udp_encap.h> |
| 17 | #include <vnet/fib/fib_entry.h> |
Neale Ranns | 1f50bf8 | 2019-07-16 15:28:52 +0000 | [diff] [blame] | 18 | #include <vnet/fib/fib_entry_track.h> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 19 | #include <vnet/fib/fib_table.h> |
| 20 | #include <vnet/dpo/drop_dpo.h> |
| 21 | |
| 22 | /** |
| 23 | * Registered DPO types for the IP header encapsulated, v4 or v6. |
| 24 | */ |
| 25 | dpo_type_t udp_encap_dpo_types[FIB_PROTOCOL_MAX]; |
| 26 | |
| 27 | /** |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 28 | * Pool of encaps |
| 29 | */ |
| 30 | udp_encap_t *udp_encap_pool; |
| 31 | |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Stats for each UDP encap object |
| 34 | */ |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 35 | vlib_combined_counter_main_t udp_encap_counters = { |
| 36 | /** |
| 37 | * The counter collection's name. |
| 38 | */ |
| 39 | .name = "udp-encap", |
| 40 | /** |
| 41 | * Name in stat segment directory |
| 42 | */ |
| 43 | .stat_segment_name = "/net/udp-encap", |
| 44 | }; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 45 | |
| 46 | static void |
| 47 | udp_encap_restack (udp_encap_t * ue) |
| 48 | { |
| 49 | dpo_stack (udp_encap_dpo_types[ue->ue_ip_proto], |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 50 | fib_proto_to_dpo (ue->ue_ip_proto), &ue->ue_dpo, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 51 | fib_entry_contribute_ip_forwarding (ue->ue_fib_entry_index)); |
| 52 | } |
| 53 | |
| 54 | index_t |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 55 | udp_encap_add_and_lock (fib_protocol_t proto, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 56 | index_t fib_index, |
| 57 | const ip46_address_t * src_ip, |
| 58 | const ip46_address_t * dst_ip, |
| 59 | u16 src_port, |
| 60 | u16 dst_port, udp_encap_fixup_flags_t flags) |
| 61 | { |
| 62 | udp_encap_t *ue; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 63 | u8 pfx_len = 0; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 64 | index_t uei; |
| 65 | |
Arthur de Kerhor | 7aeb647 | 2021-06-01 11:27:48 +0200 | [diff] [blame] | 66 | pool_get_aligned_zero (udp_encap_pool, ue, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 67 | uei = ue - udp_encap_pool; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 68 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 69 | vlib_validate_combined_counter (&(udp_encap_counters), uei); |
| 70 | vlib_zero_combined_counter (&(udp_encap_counters), uei); |
| 71 | |
| 72 | fib_node_init (&ue->ue_fib_node, FIB_NODE_TYPE_UDP_ENCAP); |
| 73 | fib_node_lock (&ue->ue_fib_node); |
| 74 | ue->ue_fib_index = fib_index; |
| 75 | ue->ue_flags = flags; |
| 76 | ue->ue_ip_proto = proto; |
| 77 | |
| 78 | switch (proto) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 79 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 80 | case FIB_PROTOCOL_IP4: |
| 81 | pfx_len = 32; |
| 82 | ue->ue_hdrs.ip4.ue_ip4.ip_version_and_header_length = 0x45; |
| 83 | ue->ue_hdrs.ip4.ue_ip4.ttl = 254; |
| 84 | ue->ue_hdrs.ip4.ue_ip4.protocol = IP_PROTOCOL_UDP; |
| 85 | ue->ue_hdrs.ip4.ue_ip4.src_address.as_u32 = src_ip->ip4.as_u32; |
| 86 | ue->ue_hdrs.ip4.ue_ip4.dst_address.as_u32 = dst_ip->ip4.as_u32; |
| 87 | ue->ue_hdrs.ip4.ue_ip4.checksum = |
| 88 | ip4_header_checksum (&ue->ue_hdrs.ip4.ue_ip4); |
| 89 | ue->ue_hdrs.ip4.ue_udp.src_port = clib_host_to_net_u16 (src_port); |
| 90 | ue->ue_hdrs.ip4.ue_udp.dst_port = clib_host_to_net_u16 (dst_port); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 91 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 92 | break; |
| 93 | case FIB_PROTOCOL_IP6: |
| 94 | pfx_len = 128; |
| 95 | ue->ue_hdrs.ip6.ue_ip6.ip_version_traffic_class_and_flow_label = |
| 96 | clib_host_to_net_u32 (6 << 28); |
| 97 | ue->ue_hdrs.ip6.ue_ip6.hop_limit = 255; |
| 98 | ue->ue_hdrs.ip6.ue_ip6.protocol = IP_PROTOCOL_UDP; |
| 99 | ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[0] = src_ip->ip6.as_u64[0]; |
| 100 | ue->ue_hdrs.ip6.ue_ip6.src_address.as_u64[1] = src_ip->ip6.as_u64[1]; |
| 101 | ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[0] = dst_ip->ip6.as_u64[0]; |
| 102 | ue->ue_hdrs.ip6.ue_ip6.dst_address.as_u64[1] = dst_ip->ip6.as_u64[1]; |
| 103 | ue->ue_hdrs.ip6.ue_udp.src_port = clib_host_to_net_u16 (src_port); |
| 104 | ue->ue_hdrs.ip6.ue_udp.dst_port = clib_host_to_net_u16 (dst_port); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 105 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | ASSERT (0); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 109 | } |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * track the destination address |
| 113 | */ |
| 114 | fib_prefix_t dst_pfx = { |
| 115 | .fp_proto = proto, |
| 116 | .fp_len = pfx_len, |
| 117 | .fp_addr = *dst_ip, |
| 118 | }; |
| 119 | |
Neale Ranns | 1f50bf8 | 2019-07-16 15:28:52 +0000 | [diff] [blame] | 120 | ue->ue_fib_entry_index = fib_entry_track (fib_index, |
| 121 | &dst_pfx, |
| 122 | FIB_NODE_TYPE_UDP_ENCAP, |
| 123 | uei, &ue->ue_fib_sibling); |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 124 | udp_encap_restack (ue); |
| 125 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 126 | return (uei); |
| 127 | } |
| 128 | |
| 129 | void |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 130 | udp_encap_contribute_forwarding (index_t uei, dpo_proto_t proto, |
| 131 | dpo_id_t * dpo) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 132 | { |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 133 | if (INDEX_INVALID == uei) |
| 134 | { |
| 135 | dpo_copy (dpo, drop_dpo_get (proto)); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | udp_encap_t *ue; |
| 140 | |
| 141 | ue = udp_encap_get (uei); |
| 142 | |
| 143 | dpo_set (dpo, udp_encap_dpo_types[ue->ue_ip_proto], proto, uei); |
| 144 | } |
| 145 | } |
| 146 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 147 | void |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 148 | udp_encap_lock (index_t uei) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 149 | { |
| 150 | udp_encap_t *ue; |
| 151 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 152 | ue = udp_encap_get (uei); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 153 | |
| 154 | if (NULL != ue) |
| 155 | { |
| 156 | fib_node_lock (&ue->ue_fib_node); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 161 | udp_encap_unlock (index_t uei) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 162 | { |
| 163 | udp_encap_t *ue; |
| 164 | |
| 165 | if (INDEX_INVALID == uei) |
| 166 | { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | ue = udp_encap_get (uei); |
| 171 | |
| 172 | if (NULL != ue) |
| 173 | { |
| 174 | fib_node_unlock (&ue->ue_fib_node); |
| 175 | } |
| 176 | } |
| 177 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 178 | static void |
| 179 | udp_encap_dpo_lock (dpo_id_t * dpo) |
| 180 | { |
| 181 | udp_encap_t *ue; |
| 182 | |
| 183 | ue = udp_encap_get (dpo->dpoi_index); |
| 184 | |
| 185 | fib_node_lock (&ue->ue_fib_node); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | udp_encap_dpo_unlock (dpo_id_t * dpo) |
| 190 | { |
| 191 | udp_encap_t *ue; |
| 192 | |
| 193 | ue = udp_encap_get (dpo->dpoi_index); |
| 194 | |
| 195 | fib_node_unlock (&ue->ue_fib_node); |
| 196 | } |
| 197 | |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 198 | u8 * |
| 199 | format_udp_encap_fixup_flags (u8 *s, va_list *args) |
| 200 | { |
| 201 | udp_encap_fixup_flags_t flags = va_arg (*args, udp_encap_fixup_flags_t); |
| 202 | |
| 203 | if (flags == UDP_ENCAP_FIXUP_NONE) |
| 204 | return format (s, "none"); |
| 205 | |
| 206 | if (flags & UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY) |
| 207 | s = format (s, "%s", "src-port-is-entropy"); |
| 208 | |
| 209 | return (s); |
| 210 | } |
| 211 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 212 | static u8 * |
| 213 | format_udp_encap_i (u8 * s, va_list * args) |
| 214 | { |
| 215 | index_t uei = va_arg (*args, index_t); |
| 216 | u32 indent = va_arg (*args, u32); |
| 217 | u32 details = va_arg (*args, u32); |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 218 | vlib_counter_t to; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 219 | udp_encap_t *ue; |
| 220 | |
| 221 | ue = udp_encap_get (uei); |
| 222 | |
| 223 | // FIXME |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 224 | s = format (s, "udp-encap:[%d]: ip-fib-index:%d ", uei, ue->ue_fib_index); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 225 | if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto) |
| 226 | { |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 227 | s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d, dst:%d] flags:%U", |
| 228 | format_ip4_address, &ue->ue_hdrs.ip4.ue_ip4.src_address, |
| 229 | format_ip4_address, &ue->ue_hdrs.ip4.ue_ip4.dst_address, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 230 | clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.src_port), |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 231 | clib_net_to_host_u16 (ue->ue_hdrs.ip4.ue_udp.dst_port), |
| 232 | format_udp_encap_fixup_flags, ue->ue_flags); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 236 | s = format (s, "ip:[src:%U, dst:%U] udp:[src:%d dst:%d] flags:%U", |
| 237 | format_ip6_address, &ue->ue_hdrs.ip6.ue_ip6.src_address, |
| 238 | format_ip6_address, &ue->ue_hdrs.ip6.ue_ip6.dst_address, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 239 | clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.src_port), |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 240 | clib_net_to_host_u16 (ue->ue_hdrs.ip6.ue_udp.dst_port), |
| 241 | format_udp_encap_fixup_flags, ue->ue_flags); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 242 | } |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 243 | vlib_get_combined_counter (&(udp_encap_counters), uei, &to); |
| 244 | s = format (s, " to:[%Ld:%Ld]]", to.packets, to.bytes); |
| 245 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 246 | if (details) |
| 247 | { |
| 248 | s = format (s, " locks:%d", ue->ue_fib_node.fn_locks); |
| 249 | s = format (s, "\n%UStacked on:", format_white_space, indent + 1); |
| 250 | s = format (s, "\n%U%U", |
| 251 | format_white_space, indent + 2, |
| 252 | format_dpo_id, &ue->ue_dpo, indent + 3); |
| 253 | } |
| 254 | return (s); |
| 255 | } |
| 256 | |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 257 | void |
| 258 | udp_encap_get_stats (index_t uei, u64 * packets, u64 * bytes) |
| 259 | { |
| 260 | vlib_counter_t to; |
| 261 | |
| 262 | vlib_get_combined_counter (&(udp_encap_counters), uei, &to); |
| 263 | |
| 264 | *packets = to.packets; |
| 265 | *bytes = to.bytes; |
| 266 | } |
| 267 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 268 | static u8 * |
| 269 | format_udp_encap_dpo (u8 * s, va_list * args) |
| 270 | { |
| 271 | index_t uei = va_arg (*args, index_t); |
| 272 | u32 indent = va_arg (*args, u32); |
| 273 | |
| 274 | return (format (s, "%U", format_udp_encap_i, uei, indent, 1)); |
| 275 | } |
| 276 | |
| 277 | u8 * |
| 278 | format_udp_encap (u8 * s, va_list * args) |
| 279 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 280 | index_t uei = va_arg (*args, u32); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 281 | u32 details = va_arg (*args, u32); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 282 | |
| 283 | return (format (s, "%U", format_udp_encap_i, uei, 0, details)); |
| 284 | } |
| 285 | |
| 286 | static udp_encap_t * |
| 287 | udp_encap_from_fib_node (fib_node_t * node) |
| 288 | { |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 289 | ASSERT (FIB_NODE_TYPE_UDP_ENCAP == node->fn_type); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 290 | return ((udp_encap_t *) (((char *) node) - |
| 291 | STRUCT_OFFSET_OF (udp_encap_t, ue_fib_node))); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Function definition to backwalk a FIB node |
| 296 | */ |
| 297 | static fib_node_back_walk_rc_t |
| 298 | udp_encap_fib_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx) |
| 299 | { |
| 300 | udp_encap_restack (udp_encap_from_fib_node (node)); |
| 301 | |
| 302 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Function definition to get a FIB node from its index |
| 307 | */ |
| 308 | static fib_node_t * |
| 309 | udp_encap_fib_node_get (fib_node_index_t index) |
| 310 | { |
| 311 | udp_encap_t *ue; |
| 312 | |
| 313 | ue = pool_elt_at_index (udp_encap_pool, index); |
| 314 | |
| 315 | return (&ue->ue_fib_node); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Function definition to inform the FIB node that its last lock has gone. |
| 320 | */ |
| 321 | static void |
| 322 | udp_encap_fib_last_lock_gone (fib_node_t * node) |
| 323 | { |
| 324 | udp_encap_t *ue; |
| 325 | |
| 326 | ue = udp_encap_from_fib_node (node); |
| 327 | |
| 328 | /** |
| 329 | * reset the stacked DPO to unlock it |
| 330 | */ |
| 331 | dpo_reset (&ue->ue_dpo); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 332 | |
Neale Ranns | 1f50bf8 | 2019-07-16 15:28:52 +0000 | [diff] [blame] | 333 | fib_entry_untrack (ue->ue_fib_entry_index, ue->ue_fib_sibling); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 334 | |
| 335 | pool_put (udp_encap_pool, ue); |
| 336 | } |
| 337 | |
| 338 | const static char *const udp4_encap_ip4_nodes[] = { |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 339 | "udp4o4-encap", |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 340 | NULL, |
| 341 | }; |
| 342 | |
| 343 | const static char *const udp4_encap_ip6_nodes[] = { |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 344 | "udp6o4-encap", |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 345 | NULL, |
| 346 | }; |
| 347 | |
| 348 | const static char *const udp4_encap_mpls_nodes[] = { |
| 349 | "udp4-encap", |
| 350 | NULL, |
| 351 | }; |
| 352 | |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 353 | const static char *const udp4_encap_bier_nodes[] = { |
| 354 | "udp4-encap", |
| 355 | NULL, |
| 356 | }; |
| 357 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 358 | const static char *const udp6_encap_ip4_nodes[] = { |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 359 | "udp4o6-encap", |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 360 | NULL, |
| 361 | }; |
| 362 | |
| 363 | const static char *const udp6_encap_ip6_nodes[] = { |
Mauro Sardara | 9539647 | 2022-03-22 17:53:46 +0000 | [diff] [blame] | 364 | "udp6o6-encap", |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 365 | NULL, |
| 366 | }; |
| 367 | |
| 368 | const static char *const udp6_encap_mpls_nodes[] = { |
| 369 | "udp6-encap", |
| 370 | NULL, |
| 371 | }; |
| 372 | |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 373 | const static char *const udp6_encap_bier_nodes[] = { |
| 374 | "udp6-encap", |
| 375 | NULL, |
| 376 | }; |
| 377 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 378 | const static char *const *const udp4_encap_nodes[DPO_PROTO_NUM] = { |
| 379 | [DPO_PROTO_IP4] = udp4_encap_ip4_nodes, |
| 380 | [DPO_PROTO_IP6] = udp4_encap_ip6_nodes, |
| 381 | [DPO_PROTO_MPLS] = udp4_encap_mpls_nodes, |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 382 | [DPO_PROTO_BIER] = udp4_encap_bier_nodes, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | const static char *const *const udp6_encap_nodes[DPO_PROTO_NUM] = { |
| 386 | [DPO_PROTO_IP4] = udp6_encap_ip4_nodes, |
| 387 | [DPO_PROTO_IP6] = udp6_encap_ip6_nodes, |
| 388 | [DPO_PROTO_MPLS] = udp6_encap_mpls_nodes, |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 389 | [DPO_PROTO_BIER] = udp6_encap_bier_nodes, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | /* |
| 393 | * Virtual function table registered by UDP encaps |
| 394 | * for participation in the FIB object graph. |
| 395 | */ |
| 396 | const static fib_node_vft_t udp_encap_fib_vft = { |
| 397 | .fnv_get = udp_encap_fib_node_get, |
| 398 | .fnv_last_lock = udp_encap_fib_last_lock_gone, |
| 399 | .fnv_back_walk = udp_encap_fib_back_walk, |
| 400 | }; |
| 401 | |
| 402 | const static dpo_vft_t udp_encap_dpo_vft = { |
| 403 | .dv_lock = udp_encap_dpo_lock, |
| 404 | .dv_unlock = udp_encap_dpo_unlock, |
| 405 | .dv_format = format_udp_encap_dpo, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | clib_error_t * |
| 409 | udp_encap_init (vlib_main_t * vm) |
| 410 | { |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 411 | fib_node_register_type (FIB_NODE_TYPE_UDP_ENCAP, &udp_encap_fib_vft); |
| 412 | |
| 413 | udp_encap_dpo_types[FIB_PROTOCOL_IP4] = |
| 414 | dpo_register_new_type (&udp_encap_dpo_vft, udp4_encap_nodes); |
| 415 | udp_encap_dpo_types[FIB_PROTOCOL_IP6] = |
| 416 | dpo_register_new_type (&udp_encap_dpo_vft, udp6_encap_nodes); |
| 417 | |
| 418 | return (NULL); |
| 419 | } |
| 420 | |
| 421 | VLIB_INIT_FUNCTION (udp_encap_init); |
| 422 | |
| 423 | clib_error_t * |
| 424 | udp_encap_cli (vlib_main_t * vm, |
| 425 | unformat_input_t * main_input, vlib_cli_command_t * cmd) |
| 426 | { |
| 427 | unformat_input_t _line_input, *line_input = &_line_input; |
| 428 | clib_error_t *error = NULL; |
| 429 | ip46_address_t src_ip, dst_ip; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 430 | u32 table_id, src_port, dst_port; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 431 | udp_encap_fixup_flags_t flags; |
| 432 | fib_protocol_t fproto; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 433 | index_t uei; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 434 | u8 is_del; |
| 435 | |
| 436 | is_del = 0; |
| 437 | table_id = 0; |
| 438 | flags = UDP_ENCAP_FIXUP_NONE; |
| 439 | fproto = FIB_PROTOCOL_MAX; |
| 440 | dst_port = 0; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 441 | uei = ~0; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 442 | |
| 443 | /* Get a line of input. */ |
| 444 | if (!unformat_user (main_input, unformat_line_input, line_input)) |
| 445 | return 0; |
| 446 | |
| 447 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 448 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 449 | if (unformat (line_input, "index %d", &uei)) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 450 | ; |
| 451 | else if (unformat (line_input, "add")) |
| 452 | is_del = 0; |
| 453 | else if (unformat (line_input, "del")) |
| 454 | is_del = 1; |
| 455 | else if (unformat (line_input, "%U %U", |
| 456 | unformat_ip4_address, |
| 457 | &src_ip.ip4, unformat_ip4_address, &dst_ip.ip4)) |
| 458 | fproto = FIB_PROTOCOL_IP4; |
| 459 | else if (unformat (line_input, "%U %U", |
| 460 | unformat_ip6_address, |
| 461 | &src_ip.ip6, unformat_ip6_address, &dst_ip.ip6)) |
| 462 | fproto = FIB_PROTOCOL_IP6; |
| 463 | else if (unformat (line_input, "%d %d", &src_port, &dst_port)) |
| 464 | ; |
| 465 | else if (unformat (line_input, "%d", &dst_port)) |
| 466 | ; |
| 467 | else if (unformat (line_input, "table-id %d", &table_id)) |
| 468 | ; |
| 469 | else if (unformat (line_input, "src-port-is-entropy")) |
| 470 | flags |= UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY; |
| 471 | else |
| 472 | { |
| 473 | error = unformat_parse_error (line_input); |
| 474 | goto done; |
| 475 | } |
| 476 | } |
| 477 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 478 | if (!is_del && fproto != FIB_PROTOCOL_MAX) |
| 479 | { |
| 480 | u32 fib_index; |
| 481 | index_t uei; |
| 482 | |
| 483 | fib_index = fib_table_find (fproto, table_id); |
| 484 | |
| 485 | if (~0 == fib_index) |
| 486 | { |
| 487 | error = clib_error_return (0, "Nonexistent table id %d", table_id); |
| 488 | goto done; |
| 489 | } |
| 490 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 491 | uei = udp_encap_add_and_lock (fproto, fib_index, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 492 | &src_ip, &dst_ip, |
| 493 | src_port, dst_port, flags); |
| 494 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 495 | vlib_cli_output (vm, "udp-encap: %d\n", uei); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 496 | } |
| 497 | else if (is_del) |
| 498 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 499 | if (INDEX_INVALID == uei) |
| 500 | { |
| 501 | error = clib_error_return (0, "specify udp-encap object index"); |
| 502 | goto done; |
| 503 | } |
| 504 | udp_encap_unlock (uei); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 505 | } |
| 506 | else |
| 507 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 508 | error = clib_error_return (0, "specify some IP addresses"); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | done: |
| 512 | unformat_free (line_input); |
| 513 | return error; |
| 514 | } |
| 515 | |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 516 | void |
| 517 | udp_encap_walk (udp_encap_walk_cb_t cb, void *ctx) |
| 518 | { |
| 519 | index_t uei; |
| 520 | |
| 521 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 522 | pool_foreach_index (uei, udp_encap_pool) |
| 523 | { |
Neale Ranns | 586479a | 2018-06-07 02:08:07 -0700 | [diff] [blame] | 524 | if (WALK_STOP == cb(uei, ctx)) |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 525 | break; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 526 | } |
Neale Ranns | 43b1f44 | 2018-03-20 01:47:35 -0700 | [diff] [blame] | 527 | /* *INDENT-ON* */ |
| 528 | } |
| 529 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 530 | clib_error_t * |
| 531 | udp_encap_show (vlib_main_t * vm, |
| 532 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 533 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 534 | index_t uei; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 535 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 536 | uei = INDEX_INVALID; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 537 | |
| 538 | /* Get a line of input. */ |
| 539 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 540 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 541 | if (unformat (input, "%d", &uei)) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 542 | ; |
Swarup Nayak | 529a425 | 2017-12-21 11:35:33 +0530 | [diff] [blame] | 543 | else |
| 544 | return clib_error_return (0, "unknown input `%U'", |
| 545 | format_unformat_error, input); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 548 | if (INDEX_INVALID == uei) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 549 | { |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 550 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 551 | pool_foreach_index (uei, udp_encap_pool) |
| 552 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 553 | vlib_cli_output(vm, "%U", format_udp_encap, uei, 0); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 554 | } |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 555 | /* *INDENT-ON* */ |
| 556 | } |
| 557 | else |
| 558 | { |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 559 | vlib_cli_output (vm, "%U", format_udp_encap, uei, 1); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | /* *INDENT-OFF* */ |
| 566 | VLIB_CLI_COMMAND (udp_encap_add_command, static) = { |
| 567 | .path = "udp encap", |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 568 | .short_help = "udp encap [add|del] <id ID> <src-ip> <dst-ip> [<src-port>] " |
| 569 | "<dst-port> [src-port-is-entropy] [table-id <table>]", |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 570 | .function = udp_encap_cli, |
| 571 | .is_mp_safe = 1, |
| 572 | }; |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 573 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 574 | VLIB_CLI_COMMAND (udp_encap_show_command, static) = { |
| 575 | .path = "show udp encap", |
| 576 | .short_help = "show udp encap [ID]", |
| 577 | .function = udp_encap_show, |
| 578 | .is_mp_safe = 1, |
| 579 | }; |
| 580 | /* *INDENT-ON* */ |
| 581 | |
| 582 | /* |
| 583 | * fd.io coding-style-patch-verification: ON |
| 584 | * |
| 585 | * Local Variables: |
| 586 | * eval: (c-set-style "gnu") |
| 587 | * End: |
| 588 | */ |