Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "ipip.h" |
| 17 | #include <vppinfra/error.h> |
| 18 | #include <vnet/vnet.h> |
| 19 | |
| 20 | static clib_error_t *create_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 21 | unformat_input_t *input, |
| 22 | vlib_cli_command_t *cmd) { |
| 23 | unformat_input_t _line_input, *line_input = &_line_input; |
| 24 | ip46_address_t src = ip46_address_initializer, dst = ip46_address_initializer; |
| 25 | u32 instance = ~0; |
| 26 | u32 fib_index = 0; |
| 27 | int rv; |
| 28 | u32 num_m_args = 0; |
| 29 | u32 sw_if_index; |
| 30 | clib_error_t *error = NULL; |
| 31 | bool ip4_set = false, ip6_set = false; |
| 32 | |
| 33 | /* Get a line of input. */ |
| 34 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 35 | return 0; |
| 36 | |
| 37 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 38 | if (unformat(line_input, "instance %d", &instance)) |
| 39 | ; |
| 40 | else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) { |
| 41 | num_m_args++; |
| 42 | ip4_set = true; |
| 43 | } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) { |
| 44 | num_m_args++; |
| 45 | ip4_set = true; |
| 46 | } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) { |
| 47 | num_m_args++; |
| 48 | ip6_set = true; |
| 49 | } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) { |
| 50 | num_m_args++; |
| 51 | ip6_set = true; |
| 52 | } else if (unformat(line_input, "outer-fib-id %d", &fib_index)) |
| 53 | ; |
| 54 | else { |
| 55 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 56 | line_input); |
| 57 | goto done; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (num_m_args < 2) { |
| 62 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 63 | goto done; |
| 64 | } |
| 65 | if (ip4_set && ip6_set) { |
| 66 | error = clib_error_return(0, "source and destination must be of same address family"); |
| 67 | goto done; |
| 68 | } |
| 69 | |
| 70 | rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4, |
| 71 | instance, |
| 72 | &src, |
| 73 | &dst, |
| 74 | fib_index, |
Ole Troan | d57f636 | 2018-05-24 13:21:43 +0200 | [diff] [blame] | 75 | 0, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 76 | &sw_if_index); |
| 77 | |
| 78 | switch (rv) { |
| 79 | case 0: |
| 80 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), |
| 81 | sw_if_index); |
| 82 | break; |
| 83 | case VNET_API_ERROR_IF_ALREADY_EXISTS: |
| 84 | error = clib_error_return(0, "IPIP tunnel already exists..."); |
| 85 | goto done; |
| 86 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 87 | error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index); |
| 88 | goto done; |
| 89 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 90 | error = clib_error_return(0, "IPIP tunnel doesn't exist"); |
| 91 | goto done; |
| 92 | case VNET_API_ERROR_INSTANCE_IN_USE: |
| 93 | error = clib_error_return(0, "Instance is in use"); |
| 94 | goto done; |
| 95 | default: |
| 96 | error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv); |
| 97 | goto done; |
| 98 | } |
| 99 | |
| 100 | done: |
| 101 | unformat_free(line_input); |
| 102 | |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 107 | unformat_input_t *input, |
| 108 | vlib_cli_command_t *cmd) { |
| 109 | unformat_input_t _line_input, *line_input = &_line_input; |
| 110 | int rv; |
| 111 | u32 num_m_args = 0; |
| 112 | u32 sw_if_index = ~0; |
| 113 | clib_error_t *error = NULL; |
| 114 | |
| 115 | /* Get a line of input. */ |
| 116 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 117 | return 0; |
| 118 | |
| 119 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 120 | if (unformat(line_input, "sw_if_index %d", &sw_if_index)) |
| 121 | num_m_args++; |
| 122 | else { |
| 123 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 124 | line_input); |
| 125 | goto done; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (num_m_args < 1) { |
| 130 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 131 | goto done; |
| 132 | } |
| 133 | |
| 134 | rv = ipip_del_tunnel(sw_if_index); |
| 135 | printf("RV %d\n", rv); |
| 136 | |
| 137 | done: |
| 138 | unformat_free(line_input); |
| 139 | |
| 140 | return error; |
| 141 | } |
| 142 | |
| 143 | /* *INDENT-OFF* */ |
| 144 | VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = { |
| 145 | .path = "create ipip tunnel", |
| 146 | .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] " |
| 147 | "[outer-fib-id <fib>]", |
| 148 | .function = create_ipip_tunnel_command_fn, |
| 149 | }; |
| 150 | VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = { |
| 151 | .path = "delete ipip tunnel", |
| 152 | .short_help = "delete ipip tunnel sw_if_index <sw_if_index ", |
| 153 | .function = delete_ipip_tunnel_command_fn, |
| 154 | }; |
| 155 | /* *INDENT-ON* */ |
| 156 | |
| 157 | static u8 *format_ipip_tunnel(u8 *s, va_list *args) { |
| 158 | ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *); |
| 159 | |
| 160 | ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6; |
| 161 | switch (t->mode) { |
| 162 | case IPIP_MODE_6RD: |
| 163 | s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d fib-idx %d sw-if-idx %d ", |
| 164 | t->dev_instance, |
| 165 | format_ip46_address, &t->tunnel_src, type, |
| 166 | format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len, |
| 167 | t->fib_index, t->sw_if_index); |
| 168 | break; |
| 169 | case IPIP_MODE_P2P: |
| 170 | default: |
| 171 | s = format(s, "[%d] instance %d src %U dst %U fib-idx %d sw-if-idx %d ", |
| 172 | t->dev_instance, t->user_instance, |
| 173 | format_ip46_address, &t->tunnel_src, type, |
| 174 | format_ip46_address, &t->tunnel_dst, type, |
| 175 | t->fib_index, t->sw_if_index); |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | return s; |
| 180 | } |
| 181 | |
| 182 | static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 183 | unformat_input_t *input, |
| 184 | vlib_cli_command_t *cmd) { |
| 185 | ipip_main_t *gm = &ipip_main; |
| 186 | ipip_tunnel_t *t; |
| 187 | u32 ti = ~0; |
| 188 | |
| 189 | if (pool_elts(gm->tunnels) == 0) |
| 190 | vlib_cli_output(vm, "No IPIP tunnels configured..."); |
| 191 | |
| 192 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) { |
| 193 | if (unformat(input, "%d", &ti)) |
| 194 | ; |
| 195 | else |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | if (ti == ~0) { |
| 200 | /* *INDENT-OFF* */ |
| 201 | pool_foreach(t, gm->tunnels, |
| 202 | ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); })); |
| 203 | /* *INDENT-ON* */ |
| 204 | } else { |
| 205 | t = pool_elt_at_index(gm->tunnels, ti); |
| 206 | if (t) |
| 207 | vlib_cli_output(vm, "%U", format_ipip_tunnel, t); |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* *INDENT-OFF* */ |
| 213 | VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = { |
| 214 | .path = "show ipip tunnel", |
| 215 | .function = show_ipip_tunnel_command_fn, |
| 216 | }; |
| 217 | /* *INDENT-ON* */ |
| 218 | |
| 219 | static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm, |
| 220 | unformat_input_t *input, |
| 221 | vlib_cli_command_t *cmd) { |
| 222 | unformat_input_t _line_input, *line_input = &_line_input; |
| 223 | ip4_address_t ip4_prefix; |
| 224 | ip6_address_t ip6_prefix; |
| 225 | ip4_address_t ip4_src; |
| 226 | u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index; |
| 227 | u32 num_m_args = 0; |
| 228 | /* Optional arguments */ |
| 229 | u32 fib_index = 0; |
| 230 | clib_error_t *error = 0; |
| 231 | bool security_check = false; |
| 232 | |
| 233 | /* Get a line of input. */ |
| 234 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 235 | return 0; |
| 236 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 237 | if (unformat(line_input, "security-check")) |
| 238 | security_check = true; |
| 239 | else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, |
| 240 | &ip6_prefix, &ip6_prefix_len)) |
| 241 | num_m_args++; |
| 242 | else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, |
| 243 | &ip4_prefix, &ip4_prefix_len)) |
| 244 | num_m_args++; |
| 245 | else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src)) |
| 246 | num_m_args++; |
| 247 | else if (unformat(line_input, "fib-id %d", &fib_index)) |
| 248 | ; |
| 249 | else { |
| 250 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 251 | line_input); |
| 252 | goto done; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (num_m_args < 3) { |
| 257 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 258 | goto done; |
| 259 | } |
| 260 | int rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix, |
| 261 | ip4_prefix_len, &ip4_src, security_check, |
| 262 | fib_index, &sixrd_tunnel_index); |
| 263 | if (rv) |
| 264 | error = clib_error_return(0, "adding tunnel failed %d", rv); |
| 265 | |
| 266 | done: |
| 267 | unformat_free(line_input); |
| 268 | |
| 269 | return error; |
| 270 | } |
| 271 | |
| 272 | static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm, |
| 273 | unformat_input_t *input, |
| 274 | vlib_cli_command_t *cmd) { |
| 275 | unformat_input_t _line_input, *line_input = &_line_input; |
| 276 | u32 num_m_args = 0; |
| 277 | /* Optional arguments */ |
| 278 | clib_error_t *error = 0; |
| 279 | u32 sw_if_index = ~0; |
| 280 | |
| 281 | /* Get a line of input. */ |
| 282 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 283 | return 0; |
| 284 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 285 | if (unformat(line_input, "sw_if_index %d", &sw_if_index)) |
| 286 | num_m_args++; |
| 287 | else { |
| 288 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 289 | line_input); |
| 290 | goto done; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (num_m_args < 1) { |
| 295 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 296 | goto done; |
| 297 | } |
| 298 | int rv = sixrd_del_tunnel(sw_if_index); |
| 299 | printf("RV %d\n", rv); |
| 300 | |
| 301 | done: |
| 302 | unformat_free(line_input); |
| 303 | |
| 304 | return error; |
| 305 | } |
| 306 | |
| 307 | /* *INDENT-OFF* */ |
| 308 | VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = { |
| 309 | .path = "create 6rd tunnel", |
| 310 | .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> " |
| 311 | "ip4-src <ip4-addr> [del]", |
| 312 | .function = create_sixrd_tunnel_command_fn, |
| 313 | }; |
| 314 | VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = { |
| 315 | .path = "delete 6rd tunnel", |
| 316 | .short_help = "delete 6rd tunnel sw_if_index <sw_if_index", |
| 317 | .function = delete_sixrd_tunnel_command_fn, |
| 318 | }; |
| 319 | /* *INDENT-ON* */ |