Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 16 | #include <vnet/teib/teib.h> |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 17 | |
| 18 | static clib_error_t * |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 19 | teib_add (vlib_main_t * vm, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 20 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 21 | { |
| 22 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 23 | ip_address_t peer = ip_address_initializer; |
| 24 | ip_address_t nh = ip_address_initializer; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 25 | u32 sw_if_index, nh_table_id; |
| 26 | clib_error_t *error = NULL; |
| 27 | int rv; |
| 28 | |
| 29 | sw_if_index = ~0; |
| 30 | nh_table_id = 0; |
| 31 | |
| 32 | /* Get a line of input. */ |
| 33 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 34 | return 0; |
| 35 | |
| 36 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 37 | { |
| 38 | if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 39 | vnet_get_main (), &sw_if_index)) |
| 40 | ; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 41 | else if (unformat (line_input, "peer %U", unformat_ip_address, &peer)) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 42 | ; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 43 | else if (unformat (line_input, "nh-table-id %d", &nh_table_id)) |
| 44 | ; |
Neale Ranns | bd8e43d | 2021-03-15 14:42:30 +0000 | [diff] [blame] | 45 | else if (unformat (line_input, "nh %U", unformat_ip_address, &nh)) |
| 46 | ; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 47 | else |
| 48 | { |
| 49 | error = clib_error_return (0, "unknown input `%U'", |
| 50 | format_unformat_error, line_input); |
| 51 | goto done; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (~0 == sw_if_index) |
| 56 | { |
| 57 | error = clib_error_return (0, "interface required'", |
| 58 | format_unformat_error, line_input); |
| 59 | goto done; |
| 60 | } |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 61 | if (ip_address_is_zero (&peer)) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 62 | { |
| 63 | error = clib_error_return (0, "peer required'", |
| 64 | format_unformat_error, line_input); |
| 65 | goto done; |
| 66 | } |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 67 | if (ip_address_is_zero (&nh)) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 68 | { |
| 69 | error = clib_error_return (0, "next-hop required'", |
| 70 | format_unformat_error, line_input); |
| 71 | goto done; |
| 72 | } |
| 73 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 74 | rv = teib_entry_add (sw_if_index, &peer, nh_table_id, &nh); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 75 | |
| 76 | if (rv) |
| 77 | { |
Paul Vinciguerra | 641c6be | 2020-12-26 23:49:51 +0000 | [diff] [blame] | 78 | error = clib_error_return_code (NULL, rv, 0, "TEIB error", |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 79 | format_unformat_error, line_input); |
| 80 | } |
| 81 | |
| 82 | done: |
| 83 | unformat_free (line_input); |
| 84 | |
| 85 | return error; |
| 86 | } |
| 87 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 88 | VLIB_CLI_COMMAND (teib_create_command, static) = { |
| 89 | .path = "create teib", |
| 90 | .short_help = "create teib <interface> peer <addr> nh <addr> [nh-table-id <ID>]", |
| 91 | .function = teib_add, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 92 | }; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 93 | |
| 94 | static clib_error_t * |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 95 | teib_del (vlib_main_t * vm, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 96 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 97 | { |
| 98 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 99 | ip_address_t peer = ip_address_initializer; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 100 | clib_error_t *error = NULL; |
| 101 | u32 sw_if_index; |
| 102 | int rv; |
| 103 | |
| 104 | sw_if_index = ~0; |
| 105 | |
| 106 | /* Get a line of input. */ |
| 107 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 108 | return 0; |
| 109 | |
| 110 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 111 | { |
| 112 | if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 113 | vnet_get_main (), &sw_if_index)) |
| 114 | ; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 115 | else if (unformat (line_input, "peer %U", unformat_ip_address, &peer)) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 116 | ; |
| 117 | else |
| 118 | { |
| 119 | error = clib_error_return (0, "unknown input `%U'", |
| 120 | format_unformat_error, line_input); |
| 121 | goto done; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (~0 == sw_if_index) |
| 126 | { |
| 127 | error = clib_error_return (0, "interface required'", |
| 128 | format_unformat_error, line_input); |
| 129 | } |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 130 | if (ip_address_is_zero (&peer)) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 131 | { |
| 132 | error = clib_error_return (0, "peer required'", |
| 133 | format_unformat_error, line_input); |
| 134 | goto done; |
| 135 | } |
| 136 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 137 | rv = teib_entry_del (sw_if_index, &peer); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 138 | |
| 139 | if (rv) |
| 140 | { |
Paul Vinciguerra | 641c6be | 2020-12-26 23:49:51 +0000 | [diff] [blame] | 141 | error = clib_error_return_code (NULL, rv, 0, "TEIB error", |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 142 | format_unformat_error, line_input); |
| 143 | } |
| 144 | |
| 145 | done: |
| 146 | unformat_free (line_input); |
| 147 | |
| 148 | return error; |
| 149 | } |
| 150 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 151 | VLIB_CLI_COMMAND (teib_delete_command, static) = { |
| 152 | .path = "delete teib", |
| 153 | .short_help = "delete teib <interface> peer <addr>", |
| 154 | .function = teib_del, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 155 | }; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 156 | |
| 157 | static walk_rc_t |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 158 | teib_show_one (index_t nei, void *ctx) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 159 | { |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 160 | vlib_cli_output (ctx, "%U", format_teib_entry, nei); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 161 | |
| 162 | return (WALK_CONTINUE); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static clib_error_t * |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 167 | teib_show (vlib_main_t * vm, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 168 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 169 | { |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 170 | teib_walk (teib_show_one, vm); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 171 | return (NULL); |
| 172 | } |
| 173 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 174 | VLIB_CLI_COMMAND (teib_show_command, static) = { |
| 175 | .path = "show teib", |
| 176 | .short_help = "show teib", |
| 177 | .function = teib_show, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 178 | }; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * fd.io coding-style-patch-verification: ON |
| 182 | * |
| 183 | * Local Variables: |
| 184 | * eval: (c-set-style "gnu") |
| 185 | * End: |
| 186 | */ |