Neale Ranns | 9220775 | 2019-06-03 13:21:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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/ip/ip.h> |
| 17 | #include <vnet/ip/ip_punt_drop.h> |
| 18 | #include <vnet/policer/policer.h> |
| 19 | #include <vnet/policer/police_inlines.h> |
| 20 | #include <vnet/fib/fib_path_list.h> |
| 21 | |
| 22 | ip_punt_redirect_cfg_t ip_punt_redirect_cfg; |
| 23 | |
| 24 | u8 * |
| 25 | format_ip_punt_redirect_trace (u8 * s, va_list * args) |
| 26 | { |
| 27 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 28 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 29 | ip_punt_redirect_trace_t *t = va_arg (*args, ip_punt_redirect_trace_t *); |
| 30 | |
| 31 | if (INDEX_INVALID == t->rrxi) |
Neale Ranns | 03c254e | 2020-03-17 14:25:10 +0000 | [diff] [blame] | 32 | s = format (s, "ignore"); |
Neale Ranns | 9220775 | 2019-06-03 13:21:40 +0000 | [diff] [blame] | 33 | else |
| 34 | s = format (s, "via redirect:%d", t->rrxi); |
| 35 | |
| 36 | return s; |
| 37 | } |
| 38 | |
| 39 | static void |
| 40 | ip_punt_redirect_stack (ip_punt_redirect_rx_t * ipr) |
| 41 | { |
| 42 | dpo_id_t dpo = DPO_INVALID; |
| 43 | vlib_node_t *pnode; |
| 44 | |
| 45 | fib_path_list_contribute_forwarding (ipr->pl, |
| 46 | ipr->payload_type, |
| 47 | FIB_PATH_LIST_FWD_FLAG_COLLAPSE, &dpo); |
| 48 | |
| 49 | if (FIB_PROTOCOL_IP4 == ipr->fproto) |
| 50 | pnode = |
| 51 | vlib_get_node_by_name (vlib_get_main (), (u8 *) "ip4-punt-redirect"); |
| 52 | else |
| 53 | pnode = |
| 54 | vlib_get_node_by_name (vlib_get_main (), (u8 *) "ip6-punt-redirect"); |
| 55 | |
| 56 | dpo_stack_from_node (pnode->index, &ipr->dpo, &dpo); |
| 57 | dpo_reset (&dpo); |
| 58 | } |
| 59 | |
| 60 | index_t |
| 61 | ip_punt_redirect_find (fib_protocol_t fproto, u32 rx_sw_if_index) |
| 62 | { |
| 63 | index_t *rxs; |
| 64 | |
| 65 | rxs = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto]; |
| 66 | |
| 67 | if (vec_len (rxs) <= rx_sw_if_index) |
| 68 | return (INDEX_INVALID); |
| 69 | |
| 70 | return rxs[rx_sw_if_index]; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | ip_punt_redirect_add (fib_protocol_t fproto, |
| 75 | u32 rx_sw_if_index, |
| 76 | fib_forward_chain_type_t ct, fib_route_path_t * rpaths) |
| 77 | { |
| 78 | ip_punt_redirect_rx_t *ipr; |
| 79 | index_t ipri; |
| 80 | |
| 81 | if (~0 == rx_sw_if_index) |
| 82 | rx_sw_if_index = 0; |
| 83 | |
| 84 | vec_validate_init_empty (ip_punt_redirect_cfg.redirect_by_rx_sw_if_index |
| 85 | [fproto], rx_sw_if_index, INDEX_INVALID); |
| 86 | |
| 87 | pool_get (ip_punt_redirect_cfg.pool, ipr); |
| 88 | ipri = ipr - ip_punt_redirect_cfg.pool; |
| 89 | |
| 90 | ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto][rx_sw_if_index] = |
| 91 | ipri; |
| 92 | |
| 93 | fib_node_init (&ipr->node, FIB_NODE_TYPE_IP_PUNT_REDIRECT); |
| 94 | ipr->fproto = fproto; |
| 95 | ipr->payload_type = ct; |
| 96 | |
| 97 | ipr->pl = fib_path_list_create (FIB_PATH_LIST_FLAG_NO_URPF, rpaths); |
| 98 | |
| 99 | ipr->sibling = fib_path_list_child_add (ipr->pl, |
| 100 | FIB_NODE_TYPE_IP_PUNT_REDIRECT, |
| 101 | ipri); |
| 102 | |
| 103 | ip_punt_redirect_stack (ipr); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | ip_punt_redirect_del (fib_protocol_t fproto, u32 rx_sw_if_index) |
| 108 | { |
| 109 | ip_punt_redirect_rx_t *ipr; |
| 110 | index_t *rxs; |
| 111 | |
| 112 | if (~0 == rx_sw_if_index) |
| 113 | rx_sw_if_index = 0; |
| 114 | |
| 115 | rxs = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto]; |
| 116 | |
| 117 | if ((vec_len (rxs) <= rx_sw_if_index) || |
| 118 | (INDEX_INVALID == rxs[rx_sw_if_index])) |
| 119 | return; |
| 120 | |
| 121 | ipr = ip_punt_redirect_get (rxs[rx_sw_if_index]); |
| 122 | |
| 123 | fib_path_list_child_remove (ipr->pl, ipr->sibling); |
| 124 | dpo_reset (&ipr->dpo); |
| 125 | pool_put (ip_punt_redirect_cfg.pool, ipr); |
| 126 | |
| 127 | rxs[rx_sw_if_index] = INDEX_INVALID; |
| 128 | } |
| 129 | |
| 130 | u8 * |
| 131 | format_ip_punt_redirect (u8 * s, va_list * args) |
| 132 | { |
| 133 | fib_protocol_t fproto = va_arg (*args, int); |
| 134 | ip_punt_redirect_rx_t *rx; |
| 135 | index_t *rxs; |
| 136 | u32 rx_sw_if_index; |
| 137 | vnet_main_t *vnm = vnet_get_main (); |
| 138 | |
| 139 | rxs = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto]; |
| 140 | |
| 141 | vec_foreach_index (rx_sw_if_index, rxs) |
| 142 | { |
| 143 | if (INDEX_INVALID == rxs[rx_sw_if_index]) |
| 144 | continue; |
| 145 | |
| 146 | rx = ip_punt_redirect_get (rxs[rx_sw_if_index]); |
| 147 | |
| 148 | s = format (s, " rx %U via:\n", |
| 149 | format_vnet_sw_interface_name, vnm, |
| 150 | vnet_get_sw_interface (vnm, rx_sw_if_index)); |
| 151 | s = format (s, " %U", format_fib_path_list, rx->pl, 2); |
| 152 | s = format (s, " forwarding\n", format_dpo_id, &rx->dpo, 0); |
| 153 | s = format (s, " %U\n", format_dpo_id, &rx->dpo, 0); |
| 154 | } |
| 155 | |
| 156 | return (s); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | ip_punt_redirect_walk (fib_protocol_t fproto, |
| 161 | ip_punt_redirect_walk_cb_t cb, void *ctx) |
| 162 | { |
| 163 | ip_punt_redirect_rx_t *rx; |
| 164 | u32 ii, rx_sw_if_index; |
| 165 | index_t *rxs; |
| 166 | |
| 167 | rxs = ip_punt_redirect_cfg.redirect_by_rx_sw_if_index[fproto]; |
| 168 | |
| 169 | vec_foreach_index (ii, rxs) |
| 170 | { |
| 171 | if (INDEX_INVALID == rxs[ii]) |
| 172 | continue; |
| 173 | |
| 174 | rx = ip_punt_redirect_get (rxs[ii]); |
| 175 | |
| 176 | rx_sw_if_index = (ii == 0 ? ~0 : ii); |
| 177 | cb (rx_sw_if_index, rx, ctx); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static fib_node_t * |
| 182 | ip_punt_redirect_get_node (fib_node_index_t index) |
| 183 | { |
| 184 | ip_punt_redirect_rx_t *ipr = ip_punt_redirect_get (index); |
| 185 | return (&(ipr->node)); |
| 186 | } |
| 187 | |
| 188 | static ip_punt_redirect_rx_t * |
| 189 | ip_punt_redirect_get_from_node (fib_node_t * node) |
| 190 | { |
| 191 | return ((ip_punt_redirect_rx_t *) (((char *) node) - |
| 192 | STRUCT_OFFSET_OF (ip_punt_redirect_rx_t, |
| 193 | node))); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | ip_punt_redirect_last_lock_gone (fib_node_t * node) |
| 198 | { |
| 199 | /* |
| 200 | * the lifetime of the entry is managed by the table. |
| 201 | */ |
| 202 | ASSERT (0); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * A back walk has reached this BIER entry |
| 207 | */ |
| 208 | static fib_node_back_walk_rc_t |
| 209 | ip_punt_redirect_back_walk_notify (fib_node_t * node, |
| 210 | fib_node_back_walk_ctx_t * ctx) |
| 211 | { |
| 212 | /* |
| 213 | * re-populate the ECMP tables with new choices |
| 214 | */ |
| 215 | ip_punt_redirect_rx_t *ipr = ip_punt_redirect_get_from_node (node); |
| 216 | |
| 217 | ip_punt_redirect_stack (ipr); |
| 218 | |
| 219 | /* |
| 220 | * no need to propagate further up the graph, since there's nothing there |
| 221 | */ |
| 222 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * The BIER fmask's graph node virtual function table |
| 227 | */ |
| 228 | static const fib_node_vft_t ip_punt_redirect_vft = { |
| 229 | .fnv_get = ip_punt_redirect_get_node, |
| 230 | .fnv_last_lock = ip_punt_redirect_last_lock_gone, |
| 231 | .fnv_back_walk = ip_punt_redirect_back_walk_notify, |
| 232 | }; |
| 233 | |
| 234 | static clib_error_t * |
| 235 | ip_punt_drop_init (vlib_main_t * vm) |
| 236 | { |
| 237 | fib_node_register_type (FIB_NODE_TYPE_IP_PUNT_REDIRECT, |
| 238 | &ip_punt_redirect_vft); |
| 239 | |
| 240 | return (NULL); |
| 241 | } |
| 242 | |
| 243 | VLIB_INIT_FUNCTION (ip_punt_drop_init); |
| 244 | |
| 245 | /* |
| 246 | * fd.io coding-style-patch-verification: ON |
| 247 | * |
| 248 | * Local Variables: |
| 249 | * eval: (c-set-style "gnu") |
| 250 | * End: |
| 251 | */ |