Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [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 | * ip/ip4_forward.c: IP v4 forwarding |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/vnet.h> |
| 41 | #include <vnet/ip/ip.h> |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 42 | #include <vnet/ip/ip_frag.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 43 | #include <vnet/ethernet/ethernet.h> /* for ethernet_header_t */ |
| 44 | #include <vnet/ethernet/arp_packet.h> /* for ethernet_arp_header_t */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #include <vnet/ppp/ppp.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 46 | #include <vnet/srp/srp.h> /* for srp_hw_interface_class */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 47 | #include <vnet/api_errno.h> /* for API error numbers */ |
| 48 | #include <vnet/fib/fib_table.h> /* for FIB table and entry creation */ |
| 49 | #include <vnet/fib/fib_entry.h> /* for FIB table and entry creation */ |
| 50 | #include <vnet/fib/fib_urpf_list.h> /* for FIB uRPF check */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 51 | #include <vnet/fib/ip4_fib.h> |
| 52 | #include <vnet/dpo/load_balance.h> |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 53 | #include <vnet/dpo/load_balance_map.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 54 | #include <vnet/dpo/classify_dpo.h> |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 55 | #include <vnet/mfib/mfib_table.h> /* for mFIB table and entry creation */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 57 | #include <vnet/ip/ip4_forward.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 59 | /** @brief IPv4 lookup node. |
Dave Barach | 9770e20 | 2016-07-06 10:29:27 -0400 | [diff] [blame] | 60 | @node ip4-lookup |
| 61 | |
| 62 | This is the main IPv4 lookup dispatch node. |
| 63 | |
| 64 | @param vm vlib_main_t corresponding to the current thread |
| 65 | @param node vlib_node_runtime_t |
| 66 | @param frame vlib_frame_t whose contents should be dispatched |
| 67 | |
| 68 | @par Graph mechanics: buffer metadata, next index usage |
| 69 | |
| 70 | @em Uses: |
| 71 | - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code> |
| 72 | - Indicates the @c sw_if_index value of the interface that the |
| 73 | packet was received on. |
| 74 | - <code>vnet_buffer(b)->sw_if_index[VLIB_TX]</code> |
| 75 | - When the value is @c ~0 then the node performs a longest prefix |
| 76 | match (LPM) for the packet destination address in the FIB attached |
| 77 | to the receive interface. |
| 78 | - Otherwise perform LPM for the packet destination address in the |
| 79 | indicated FIB. In this case <code>[VLIB_TX]</code> is a FIB index |
| 80 | value (0, 1, ...) and not a VRF id. |
| 81 | |
| 82 | @em Sets: |
| 83 | - <code>vnet_buffer(b)->ip.adj_index[VLIB_TX]</code> |
| 84 | - The lookup result adjacency index. |
| 85 | |
| 86 | <em>Next Index:</em> |
| 87 | - Dispatches the packet to the node index found in |
| 88 | ip_adjacency_t @c adj->lookup_next_index |
| 89 | (where @c adj is the lookup result adjacency). |
| 90 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | static uword |
| 92 | ip4_lookup (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 93 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | { |
Damjan Marion | aca64c9 | 2016-04-13 09:48:56 +0200 | [diff] [blame] | 95 | return ip4_lookup_inline (vm, node, frame, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 96 | /* lookup_for_responses_to_locally_received_packets */ |
| 97 | 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | } |
| 100 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 101 | static u8 *format_ip4_lookup_trace (u8 * s, va_list * args); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 102 | |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 103 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 104 | VLIB_REGISTER_NODE (ip4_lookup_node) = |
| 105 | { |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 106 | .function = ip4_lookup, |
| 107 | .name = "ip4-lookup", |
| 108 | .vector_size = sizeof (u32), |
| 109 | .format_trace = format_ip4_lookup_trace, |
| 110 | .n_next_nodes = IP_LOOKUP_N_NEXT, |
| 111 | .next_nodes = IP4_LOOKUP_NEXT_NODES, |
| 112 | }; |
| 113 | /* *INDENT-ON* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 114 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 115 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_lookup_node, ip4_lookup); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 116 | |
| 117 | always_inline uword |
| 118 | ip4_load_balance (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters; |
| 122 | u32 n_left_from, n_left_to_next, *from, *to_next; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 123 | ip_lookup_next_t next; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 124 | u32 thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 126 | from = vlib_frame_vector_args (frame); |
| 127 | n_left_from = frame->n_vectors; |
| 128 | next = node->cached_next_index; |
| 129 | |
| 130 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 131 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 132 | |
| 133 | while (n_left_from > 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 135 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 136 | |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 137 | |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 138 | while (n_left_from >= 4 && n_left_to_next >= 2) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 139 | { |
| 140 | ip_lookup_next_t next0, next1; |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 141 | const load_balance_t *lb0, *lb1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 142 | vlib_buffer_t *p0, *p1; |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 143 | u32 pi0, lbi0, hc0, pi1, lbi1, hc1; |
| 144 | const ip4_header_t *ip0, *ip1; |
| 145 | const dpo_id_t *dpo0, *dpo1; |
| 146 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 147 | /* Prefetch next iteration. */ |
| 148 | { |
| 149 | vlib_buffer_t *p2, *p3; |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 150 | |
| 151 | p2 = vlib_get_buffer (vm, from[2]); |
| 152 | p3 = vlib_get_buffer (vm, from[3]); |
| 153 | |
| 154 | vlib_prefetch_buffer_header (p2, STORE); |
| 155 | vlib_prefetch_buffer_header (p3, STORE); |
| 156 | |
| 157 | CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE); |
| 158 | CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | } |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 160 | |
| 161 | pi0 = to_next[0] = from[0]; |
| 162 | pi1 = to_next[1] = from[1]; |
| 163 | |
| 164 | from += 2; |
| 165 | n_left_from -= 2; |
| 166 | to_next += 2; |
| 167 | n_left_to_next -= 2; |
| 168 | |
| 169 | p0 = vlib_get_buffer (vm, pi0); |
| 170 | p1 = vlib_get_buffer (vm, pi1); |
| 171 | |
| 172 | ip0 = vlib_buffer_get_current (p0); |
| 173 | ip1 = vlib_buffer_get_current (p1); |
| 174 | lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 175 | lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX]; |
| 176 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 177 | lb0 = load_balance_get (lbi0); |
| 178 | lb1 = load_balance_get (lbi1); |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 179 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | /* |
| 181 | * this node is for via FIBs we can re-use the hash value from the |
| 182 | * to node if present. |
| 183 | * We don't want to use the same hash value at each level in the recursion |
| 184 | * graph as that would lead to polarisation |
| 185 | */ |
AkshayaNadahalli | 153b871 | 2017-03-06 18:22:29 +0000 | [diff] [blame] | 186 | hc0 = hc1 = 0; |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 187 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 188 | if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) |
| 189 | { |
| 190 | if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash)) |
| 191 | { |
| 192 | hc0 = vnet_buffer (p0)->ip.flow_hash = |
| 193 | vnet_buffer (p0)->ip.flow_hash >> 1; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | hc0 = vnet_buffer (p0)->ip.flow_hash = |
AkshayaNadahalli | 153b871 | 2017-03-06 18:22:29 +0000 | [diff] [blame] | 198 | ip4_compute_flow_hash (ip0, lb0->lb_hash_config); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 199 | } |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 200 | dpo0 = load_balance_get_fwd_bucket |
| 201 | (lb0, (hc0 & (lb0->lb_n_buckets_minus_1))); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | dpo0 = load_balance_get_bucket_i (lb0, 0); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 206 | } |
| 207 | if (PREDICT_FALSE (lb1->lb_n_buckets > 1)) |
| 208 | { |
| 209 | if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash)) |
| 210 | { |
| 211 | hc1 = vnet_buffer (p1)->ip.flow_hash = |
| 212 | vnet_buffer (p1)->ip.flow_hash >> 1; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | hc1 = vnet_buffer (p1)->ip.flow_hash = |
AkshayaNadahalli | 153b871 | 2017-03-06 18:22:29 +0000 | [diff] [blame] | 217 | ip4_compute_flow_hash (ip1, lb1->lb_hash_config); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 218 | } |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 219 | dpo1 = load_balance_get_fwd_bucket |
| 220 | (lb1, (hc1 & (lb1->lb_n_buckets_minus_1))); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 221 | } |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 222 | else |
| 223 | { |
| 224 | dpo1 = load_balance_get_bucket_i (lb1, 0); |
| 225 | } |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 226 | |
| 227 | next0 = dpo0->dpoi_next_node; |
| 228 | next1 = dpo1->dpoi_next_node; |
| 229 | |
| 230 | vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; |
| 231 | vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index; |
| 232 | |
| 233 | vlib_increment_combined_counter |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 234 | (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0)); |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 235 | vlib_increment_combined_counter |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 236 | (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1)); |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 237 | |
| 238 | vlib_validate_buffer_enqueue_x2 (vm, node, next, |
| 239 | to_next, n_left_to_next, |
| 240 | pi0, pi1, next0, next1); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 241 | } |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 242 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 243 | while (n_left_from > 0 && n_left_to_next > 0) |
| 244 | { |
| 245 | ip_lookup_next_t next0; |
| 246 | const load_balance_t *lb0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 247 | vlib_buffer_t *p0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 248 | u32 pi0, lbi0, hc0; |
| 249 | const ip4_header_t *ip0; |
| 250 | const dpo_id_t *dpo0; |
| 251 | |
| 252 | pi0 = from[0]; |
| 253 | to_next[0] = pi0; |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 254 | from += 1; |
| 255 | to_next += 1; |
| 256 | n_left_to_next -= 1; |
| 257 | n_left_from -= 1; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 258 | |
| 259 | p0 = vlib_get_buffer (vm, pi0); |
| 260 | |
| 261 | ip0 = vlib_buffer_get_current (p0); |
| 262 | lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 263 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 264 | lb0 = load_balance_get (lbi0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 265 | |
AkshayaNadahalli | 153b871 | 2017-03-06 18:22:29 +0000 | [diff] [blame] | 266 | hc0 = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 267 | if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) |
| 268 | { |
| 269 | if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash)) |
| 270 | { |
| 271 | hc0 = vnet_buffer (p0)->ip.flow_hash = |
| 272 | vnet_buffer (p0)->ip.flow_hash >> 1; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | hc0 = vnet_buffer (p0)->ip.flow_hash = |
AkshayaNadahalli | 153b871 | 2017-03-06 18:22:29 +0000 | [diff] [blame] | 277 | ip4_compute_flow_hash (ip0, lb0->lb_hash_config); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 278 | } |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 279 | dpo0 = load_balance_get_fwd_bucket |
| 280 | (lb0, (hc0 & (lb0->lb_n_buckets_minus_1))); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 281 | } |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 282 | else |
| 283 | { |
| 284 | dpo0 = load_balance_get_bucket_i (lb0, 0); |
| 285 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 286 | |
| 287 | next0 = dpo0->dpoi_next_node; |
| 288 | vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; |
| 289 | |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 290 | vlib_increment_combined_counter |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 291 | (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 292 | |
Neale Ranns | 2be95c1 | 2016-11-19 13:50:04 +0000 | [diff] [blame] | 293 | vlib_validate_buffer_enqueue_x1 (vm, node, next, |
| 294 | to_next, n_left_to_next, |
| 295 | pi0, next0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 301 | return frame->n_vectors; |
| 302 | } |
| 303 | |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 304 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 305 | VLIB_REGISTER_NODE (ip4_load_balance_node) = |
| 306 | { |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 307 | .function = ip4_load_balance, |
| 308 | .name = "ip4-load-balance", |
| 309 | .vector_size = sizeof (u32), |
| 310 | .sibling_of = "ip4-lookup", |
| 311 | .format_trace = |
| 312 | format_ip4_lookup_trace, |
| 313 | }; |
| 314 | /* *INDENT-ON* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 315 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 316 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_load_balance_node, ip4_load_balance); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 317 | |
| 318 | /* get first interface address */ |
| 319 | ip4_address_t * |
| 320 | ip4_interface_first_address (ip4_main_t * im, u32 sw_if_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 321 | ip_interface_address_t ** result_ia) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 322 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 323 | ip_lookup_main_t *lm = &im->lookup_main; |
| 324 | ip_interface_address_t *ia = 0; |
| 325 | ip4_address_t *result = 0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 326 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 327 | /* *INDENT-OFF* */ |
| 328 | foreach_ip_interface_address |
| 329 | (lm, ia, sw_if_index, |
| 330 | 1 /* honor unnumbered */ , |
| 331 | ({ |
| 332 | ip4_address_t * a = |
| 333 | ip_interface_address_get_address (lm, ia); |
| 334 | result = a; |
| 335 | break; |
| 336 | })); |
| 337 | /* *INDENT-OFF* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 338 | if (result_ia) |
| 339 | *result_ia = result ? ia : 0; |
| 340 | return result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void |
| 344 | ip4_add_interface_routes (u32 sw_if_index, |
| 345 | ip4_main_t * im, u32 fib_index, |
| 346 | ip_interface_address_t * a) |
| 347 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 348 | ip_lookup_main_t *lm = &im->lookup_main; |
| 349 | ip4_address_t *address = ip_interface_address_get_address (lm, a); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 350 | fib_prefix_t pfx = { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 351 | .fp_len = a->address_length, |
| 352 | .fp_proto = FIB_PROTOCOL_IP4, |
| 353 | .fp_addr.ip4 = *address, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 354 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 355 | |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 356 | if (pfx.fp_len <= 30) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 357 | { |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 358 | /* a /30 or shorter - add a glean for the network address */ |
Neale Ranns | 7a27274 | 2017-05-30 02:08:14 -0700 | [diff] [blame] | 359 | fib_table_entry_update_one_path (fib_index, &pfx, |
| 360 | FIB_SOURCE_INTERFACE, |
| 361 | (FIB_ENTRY_FLAG_CONNECTED | |
| 362 | FIB_ENTRY_FLAG_ATTACHED), |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 363 | DPO_PROTO_IP4, |
Neale Ranns | 7a27274 | 2017-05-30 02:08:14 -0700 | [diff] [blame] | 364 | /* No next-hop address */ |
| 365 | NULL, |
| 366 | sw_if_index, |
| 367 | // invalid FIB index |
| 368 | ~0, |
| 369 | 1, |
| 370 | // no out-label stack |
| 371 | NULL, |
| 372 | FIB_ROUTE_PATH_FLAG_NONE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 373 | |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 374 | /* Add the two broadcast addresses as drop */ |
| 375 | fib_prefix_t net_pfx = { |
| 376 | .fp_len = 32, |
| 377 | .fp_proto = FIB_PROTOCOL_IP4, |
| 378 | .fp_addr.ip4.as_u32 = address->as_u32 & im->fib_masks[pfx.fp_len], |
| 379 | }; |
| 380 | if (net_pfx.fp_addr.ip4.as_u32 != pfx.fp_addr.ip4.as_u32) |
| 381 | fib_table_entry_special_add(fib_index, |
| 382 | &net_pfx, |
| 383 | FIB_SOURCE_INTERFACE, |
| 384 | (FIB_ENTRY_FLAG_DROP | |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 385 | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT)); |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 386 | net_pfx.fp_addr.ip4.as_u32 |= ~im->fib_masks[pfx.fp_len]; |
| 387 | if (net_pfx.fp_addr.ip4.as_u32 != pfx.fp_addr.ip4.as_u32) |
| 388 | fib_table_entry_special_add(fib_index, |
| 389 | &net_pfx, |
| 390 | FIB_SOURCE_INTERFACE, |
| 391 | (FIB_ENTRY_FLAG_DROP | |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 392 | FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT)); |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 393 | } |
| 394 | else if (pfx.fp_len == 31) |
| 395 | { |
| 396 | u32 mask = clib_host_to_net_u32(1); |
| 397 | fib_prefix_t net_pfx = pfx; |
| 398 | |
| 399 | net_pfx.fp_len = 32; |
| 400 | net_pfx.fp_addr.ip4.as_u32 ^= mask; |
| 401 | |
| 402 | /* a /31 - add the other end as an attached host */ |
| 403 | fib_table_entry_update_one_path (fib_index, &net_pfx, |
| 404 | FIB_SOURCE_INTERFACE, |
| 405 | (FIB_ENTRY_FLAG_ATTACHED), |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 406 | DPO_PROTO_IP4, |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 407 | &net_pfx.fp_addr, |
| 408 | sw_if_index, |
| 409 | // invalid FIB index |
| 410 | ~0, |
| 411 | 1, |
| 412 | NULL, |
| 413 | FIB_ROUTE_PATH_FLAG_NONE); |
| 414 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 415 | pfx.fp_len = 32; |
| 416 | |
| 417 | if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 418 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 419 | u32 classify_table_index = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 420 | lm->classify_table_index_by_sw_if_index[sw_if_index]; |
| 421 | if (classify_table_index != (u32) ~ 0) |
| 422 | { |
| 423 | dpo_id_t dpo = DPO_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 424 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 425 | dpo_set (&dpo, |
| 426 | DPO_CLASSIFY, |
| 427 | DPO_PROTO_IP4, |
| 428 | classify_dpo_create (DPO_PROTO_IP4, classify_table_index)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 429 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 430 | fib_table_entry_special_dpo_add (fib_index, |
| 431 | &pfx, |
| 432 | FIB_SOURCE_CLASSIFY, |
| 433 | FIB_ENTRY_FLAG_NONE, &dpo); |
| 434 | dpo_reset (&dpo); |
| 435 | } |
| 436 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 437 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 438 | fib_table_entry_update_one_path (fib_index, &pfx, |
| 439 | FIB_SOURCE_INTERFACE, |
| 440 | (FIB_ENTRY_FLAG_CONNECTED | |
| 441 | FIB_ENTRY_FLAG_LOCAL), |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 442 | DPO_PROTO_IP4, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 443 | &pfx.fp_addr, |
| 444 | sw_if_index, |
| 445 | // invalid FIB index |
| 446 | ~0, |
| 447 | 1, NULL, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 448 | FIB_ROUTE_PATH_FLAG_NONE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static void |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 452 | ip4_del_interface_routes (ip4_main_t * im, |
| 453 | u32 fib_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 454 | ip4_address_t * address, u32 address_length) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 456 | fib_prefix_t pfx = { |
| 457 | .fp_len = address_length, |
| 458 | .fp_proto = FIB_PROTOCOL_IP4, |
| 459 | .fp_addr.ip4 = *address, |
| 460 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 462 | if (pfx.fp_len <= 30) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 463 | { |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 464 | fib_prefix_t net_pfx = { |
| 465 | .fp_len = 32, |
| 466 | .fp_proto = FIB_PROTOCOL_IP4, |
| 467 | .fp_addr.ip4.as_u32 = address->as_u32 & im->fib_masks[pfx.fp_len], |
| 468 | }; |
| 469 | if (net_pfx.fp_addr.ip4.as_u32 != pfx.fp_addr.ip4.as_u32) |
| 470 | fib_table_entry_special_remove(fib_index, |
| 471 | &net_pfx, |
| 472 | FIB_SOURCE_INTERFACE); |
| 473 | net_pfx.fp_addr.ip4.as_u32 |= ~im->fib_masks[pfx.fp_len]; |
| 474 | if (net_pfx.fp_addr.ip4.as_u32 != pfx.fp_addr.ip4.as_u32) |
| 475 | fib_table_entry_special_remove(fib_index, |
| 476 | &net_pfx, |
| 477 | FIB_SOURCE_INTERFACE); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 478 | fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 479 | } |
Neale Ranns | 9a69a60 | 2017-03-26 10:56:33 -0700 | [diff] [blame] | 480 | else if (pfx.fp_len == 31) |
| 481 | { |
| 482 | u32 mask = clib_host_to_net_u32(1); |
| 483 | fib_prefix_t net_pfx = pfx; |
| 484 | |
| 485 | net_pfx.fp_len = 32; |
| 486 | net_pfx.fp_addr.ip4.as_u32 ^= mask; |
| 487 | |
| 488 | fib_table_entry_delete (fib_index, &net_pfx, FIB_SOURCE_INTERFACE); |
| 489 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 491 | pfx.fp_len = 32; |
| 492 | fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 495 | void |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 496 | ip4_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 497 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 498 | ip4_main_t *im = &ip4_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 500 | vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0); |
| 501 | |
| 502 | /* |
| 503 | * enable/disable only on the 1<->0 transition |
| 504 | */ |
| 505 | if (is_enable) |
| 506 | { |
| 507 | if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index]) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 508 | return; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 509 | } |
| 510 | else |
| 511 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 512 | ASSERT (im->ip_enabled_by_sw_if_index[sw_if_index] > 0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 513 | if (0 != --im->ip_enabled_by_sw_if_index[sw_if_index]) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 514 | return; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 515 | } |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 516 | vnet_feature_enable_disable ("ip4-unicast", "ip4-not-enabled", sw_if_index, |
Damjan Marion | 4d48993 | 2016-12-09 03:21:27 -0800 | [diff] [blame] | 517 | !is_enable, 0, 0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 518 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 519 | |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 520 | vnet_feature_enable_disable ("ip4-multicast", "ip4-not-enabled", |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 521 | sw_if_index, !is_enable, 0, 0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 522 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | static clib_error_t * |
| 525 | ip4_add_del_interface_address_internal (vlib_main_t * vm, |
| 526 | u32 sw_if_index, |
| 527 | ip4_address_t * address, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 528 | u32 address_length, u32 is_del) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 530 | vnet_main_t *vnm = vnet_get_main (); |
| 531 | ip4_main_t *im = &ip4_main; |
| 532 | ip_lookup_main_t *lm = &im->lookup_main; |
| 533 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | u32 if_address_index, elts_before; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 535 | ip4_address_fib_t ip4_af, *addr_fib = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Pavel Kotucek | 5780898 | 2017-08-02 08:20:19 +0200 | [diff] [blame] | 537 | /* local0 interface doesn't support IP addressing */ |
| 538 | if (sw_if_index == 0) |
| 539 | { |
| 540 | return |
| 541 | clib_error_create ("local0 interface doesn't support IP addressing"); |
| 542 | } |
| 543 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | vec_validate (im->fib_index_by_sw_if_index, sw_if_index); |
| 545 | ip4_addr_fib_init (&ip4_af, address, |
| 546 | vec_elt (im->fib_index_by_sw_if_index, sw_if_index)); |
| 547 | vec_add1 (addr_fib, ip4_af); |
| 548 | |
Neale Ranns | 744902e | 2017-08-14 10:35:44 -0700 | [diff] [blame] | 549 | /* |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 550 | * there is no support for adj-fib handling in the presence of overlapping |
| 551 | * subnets on interfaces. Easy fix - disallow overlapping subnets, like |
| 552 | * most routers do. |
| 553 | */ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 554 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 555 | if (!is_del) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 557 | /* When adding an address check that it does not conflict |
Neale Ranns | 744902e | 2017-08-14 10:35:44 -0700 | [diff] [blame] | 558 | with an existing address on any interface in this table. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 559 | ip_interface_address_t *ia; |
Neale Ranns | 744902e | 2017-08-14 10:35:44 -0700 | [diff] [blame] | 560 | vnet_sw_interface_t *sif; |
| 561 | |
| 562 | pool_foreach(sif, vnm->interface_main.sw_interfaces, |
| 563 | ({ |
| 564 | if (im->fib_index_by_sw_if_index[sw_if_index] == |
| 565 | im->fib_index_by_sw_if_index[sif->sw_if_index]) |
| 566 | { |
| 567 | foreach_ip_interface_address |
| 568 | (&im->lookup_main, ia, sif->sw_if_index, |
| 569 | 0 /* honor unnumbered */ , |
| 570 | ({ |
| 571 | ip4_address_t * x = |
| 572 | ip_interface_address_get_address |
| 573 | (&im->lookup_main, ia); |
| 574 | if (ip4_destination_matches_route |
| 575 | (im, address, x, ia->address_length) || |
| 576 | ip4_destination_matches_route (im, |
| 577 | x, |
| 578 | address, |
| 579 | address_length)) |
| 580 | { |
| 581 | vnm->api_errno = VNET_API_ERROR_DUPLICATE_IF_ADDRESS; |
| 582 | |
| 583 | return |
| 584 | clib_error_create |
| 585 | ("failed to add %U which conflicts with %U for interface %U", |
| 586 | format_ip4_address_and_length, address, |
| 587 | address_length, |
| 588 | format_ip4_address_and_length, x, |
| 589 | ia->address_length, |
| 590 | format_vnet_sw_if_index_name, vnm, |
| 591 | sif->sw_if_index); |
| 592 | } |
| 593 | })); |
| 594 | } |
| 595 | })); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 597 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | elts_before = pool_elts (lm->if_address_pool); |
| 600 | |
| 601 | error = ip_interface_address_add_del |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 602 | (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | if (error) |
| 604 | goto done; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 605 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 606 | ip4_sw_interface_enable_disable (sw_if_index, !is_del); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 607 | |
| 608 | if (is_del) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 609 | ip4_del_interface_routes (im, ip4_af.fib_index, address, address_length); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 610 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 611 | ip4_add_interface_routes (sw_if_index, |
| 612 | im, ip4_af.fib_index, |
| 613 | pool_elt_at_index |
| 614 | (lm->if_address_pool, if_address_index)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | |
| 616 | /* If pool did not grow/shrink: add duplicate address. */ |
| 617 | if (elts_before != pool_elts (lm->if_address_pool)) |
| 618 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 619 | ip4_add_del_interface_address_callback_t *cb; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 620 | vec_foreach (cb, im->add_del_interface_address_callbacks) |
| 621 | cb->function (im, cb->function_opaque, sw_if_index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 622 | address, address_length, if_address_index, is_del); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 625 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | vec_free (addr_fib); |
| 627 | return error; |
| 628 | } |
| 629 | |
| 630 | clib_error_t * |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 631 | ip4_add_del_interface_address (vlib_main_t * vm, |
| 632 | u32 sw_if_index, |
| 633 | ip4_address_t * address, |
| 634 | u32 address_length, u32 is_del) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | { |
| 636 | return ip4_add_del_interface_address_internal |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 637 | (vm, sw_if_index, address, address_length, is_del); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 640 | /* Built-in ip4 unicast rx feature path definition */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 641 | /* *INDENT-OFF* */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 642 | VNET_FEATURE_ARC_INIT (ip4_unicast, static) = |
| 643 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 644 | .arc_name = "ip4-unicast", |
Damjan Marion | 892e076 | 2016-12-09 18:52:05 +0100 | [diff] [blame] | 645 | .start_nodes = VNET_FEATURES ("ip4-input", "ip4-input-no-checksum"), |
| 646 | .arc_index_ptr = &ip4_main.lookup_main.ucast_feature_arc_index, |
| 647 | }; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 648 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 649 | VNET_FEATURE_INIT (ip4_flow_classify, static) = |
| 650 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 651 | .arc_name = "ip4-unicast", |
Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 652 | .node_name = "ip4-flow-classify", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 653 | .runs_before = VNET_FEATURES ("ip4-inacl"), |
Juraj Sloboda | 506b245 | 2016-08-07 23:45:24 -0700 | [diff] [blame] | 654 | }; |
| 655 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 656 | VNET_FEATURE_INIT (ip4_inacl, static) = |
| 657 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 658 | .arc_name = "ip4-unicast", |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 659 | .node_name = "ip4-inacl", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 660 | .runs_before = VNET_FEATURES ("ip4-source-check-via-rx"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 661 | }; |
| 662 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 663 | VNET_FEATURE_INIT (ip4_source_check_1, static) = |
| 664 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 665 | .arc_name = "ip4-unicast", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 666 | .node_name = "ip4-source-check-via-rx", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 667 | .runs_before = VNET_FEATURES ("ip4-source-check-via-any"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 668 | }; |
| 669 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 670 | VNET_FEATURE_INIT (ip4_source_check_2, static) = |
| 671 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 672 | .arc_name = "ip4-unicast", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 673 | .node_name = "ip4-source-check-via-any", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 674 | .runs_before = VNET_FEATURES ("ip4-policer-classify"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 675 | }; |
| 676 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 677 | VNET_FEATURE_INIT (ip4_source_and_port_range_check_rx, static) = |
| 678 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 679 | .arc_name = "ip4-unicast", |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 680 | .node_name = "ip4-source-and-port-range-check-rx", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 681 | .runs_before = VNET_FEATURES ("ip4-policer-classify"), |
Dave Barach | 6f9bca2 | 2016-04-30 10:25:32 -0400 | [diff] [blame] | 682 | }; |
| 683 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 684 | VNET_FEATURE_INIT (ip4_policer_classify, static) = |
| 685 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 686 | .arc_name = "ip4-unicast", |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 687 | .node_name = "ip4-policer-classify", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 688 | .runs_before = VNET_FEATURES ("ipsec-input-ip4"), |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 691 | VNET_FEATURE_INIT (ip4_ipsec, static) = |
| 692 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 693 | .arc_name = "ip4-unicast", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 694 | .node_name = "ipsec-input-ip4", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 695 | .runs_before = VNET_FEATURES ("vpath-input-ip4"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 696 | }; |
| 697 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 698 | VNET_FEATURE_INIT (ip4_vpath, static) = |
| 699 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 700 | .arc_name = "ip4-unicast", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 701 | .node_name = "vpath-input-ip4", |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 702 | .runs_before = VNET_FEATURES ("ip4-vxlan-bypass"), |
| 703 | }; |
| 704 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 705 | VNET_FEATURE_INIT (ip4_vxlan_bypass, static) = |
| 706 | { |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 707 | .arc_name = "ip4-unicast", |
| 708 | .node_name = "ip4-vxlan-bypass", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 709 | .runs_before = VNET_FEATURES ("ip4-lookup"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 710 | }; |
| 711 | |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 712 | VNET_FEATURE_INIT (ip4_not_enabled, static) = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 713 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 714 | .arc_name = "ip4-unicast", |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 715 | .node_name = "ip4-not-enabled", |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 716 | .runs_before = VNET_FEATURES ("ip4-lookup"), |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 717 | }; |
| 718 | |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 719 | VNET_FEATURE_INIT (ip4_lookup, static) = |
| 720 | { |
| 721 | .arc_name = "ip4-unicast", |
| 722 | .node_name = "ip4-lookup", |
| 723 | .runs_before = 0, /* not before any other features */ |
| 724 | }; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 725 | |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 726 | /* Built-in ip4 multicast rx feature path definition */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 727 | VNET_FEATURE_ARC_INIT (ip4_multicast, static) = |
| 728 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 729 | .arc_name = "ip4-multicast", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 730 | .start_nodes = VNET_FEATURES ("ip4-input", "ip4-input-no-checksum"), |
| 731 | .arc_index_ptr = &ip4_main.lookup_main.mcast_feature_arc_index, |
| 732 | }; |
| 733 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 734 | VNET_FEATURE_INIT (ip4_vpath_mc, static) = |
| 735 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 736 | .arc_name = "ip4-multicast", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 737 | .node_name = "vpath-input-ip4", |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 738 | .runs_before = VNET_FEATURES ("ip4-mfib-forward-lookup"), |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 739 | }; |
| 740 | |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 741 | VNET_FEATURE_INIT (ip4_mc_not_enabled, static) = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 742 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 743 | .arc_name = "ip4-multicast", |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 744 | .node_name = "ip4-not-enabled", |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 745 | .runs_before = VNET_FEATURES ("ip4-mfib-forward-lookup"), |
| 746 | }; |
| 747 | |
| 748 | VNET_FEATURE_INIT (ip4_lookup_mc, static) = |
| 749 | { |
| 750 | .arc_name = "ip4-multicast", |
| 751 | .node_name = "ip4-mfib-forward-lookup", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 752 | .runs_before = 0, /* last feature */ |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 753 | }; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 754 | |
| 755 | /* Source and port-range check ip4 tx feature path definition */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 756 | VNET_FEATURE_ARC_INIT (ip4_output, static) = |
| 757 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 758 | .arc_name = "ip4-output", |
Neale Ranns | f068c3e | 2018-01-03 04:18:48 -0800 | [diff] [blame] | 759 | .start_nodes = VNET_FEATURES ("ip4-rewrite", "ip4-midchain", "ip4-dvr-dpo"), |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 760 | .arc_index_ptr = &ip4_main.lookup_main.output_feature_arc_index, |
| 761 | }; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 762 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 763 | VNET_FEATURE_INIT (ip4_source_and_port_range_check_tx, static) = |
| 764 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 765 | .arc_name = "ip4-output", |
| 766 | .node_name = "ip4-source-and-port-range-check-tx", |
Andrew Yourtchenko | 815d7d5 | 2018-02-07 11:37:02 +0100 | [diff] [blame] | 767 | .runs_before = VNET_FEATURES ("ip4-outacl"), |
| 768 | }; |
| 769 | |
| 770 | VNET_FEATURE_INIT (ip4_outacl, static) = |
| 771 | { |
| 772 | .arc_name = "ip4-output", |
| 773 | .node_name = "ip4-outacl", |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 774 | .runs_before = VNET_FEATURES ("ipsec-output-ip4"), |
| 775 | }; |
| 776 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 777 | VNET_FEATURE_INIT (ip4_ipsec_output, static) = |
| 778 | { |
Matus Fabian | 08a6f01 | 2016-11-15 06:08:51 -0800 | [diff] [blame] | 779 | .arc_name = "ip4-output", |
| 780 | .node_name = "ipsec-output-ip4", |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 781 | .runs_before = VNET_FEATURES ("interface-output"), |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | /* Built-in ip4 tx feature path definition */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 785 | VNET_FEATURE_INIT (ip4_interface_output, static) = |
| 786 | { |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 787 | .arc_name = "ip4-output", |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 788 | .node_name = "interface-output", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 789 | .runs_before = 0, /* not before any other features */ |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 790 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 791 | /* *INDENT-ON* */ |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 792 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | static clib_error_t * |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 794 | ip4_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 796 | ip4_main_t *im = &ip4_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 798 | /* Fill in lookup tables with default table (0). */ |
| 799 | vec_validate (im->fib_index_by_sw_if_index, sw_if_index); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 800 | vec_validate (im->mfib_index_by_sw_if_index, sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 801 | |
Pavel Kotucek | 9f5a2b6 | 2017-06-14 13:56:55 +0200 | [diff] [blame] | 802 | if (!is_add) |
| 803 | { |
| 804 | ip4_main_t *im4 = &ip4_main; |
| 805 | ip_lookup_main_t *lm4 = &im4->lookup_main; |
| 806 | ip_interface_address_t *ia = 0; |
| 807 | ip4_address_t *address; |
| 808 | vlib_main_t *vm = vlib_get_main (); |
| 809 | |
Neale Ranns | 2ae2bc5 | 2018-03-16 03:22:39 -0700 | [diff] [blame] | 810 | vnet_sw_interface_update_unnumbered (sw_if_index, ~0, 0); |
Pavel Kotucek | 9f5a2b6 | 2017-06-14 13:56:55 +0200 | [diff] [blame] | 811 | /* *INDENT-OFF* */ |
Neale Ranns | 2ae2bc5 | 2018-03-16 03:22:39 -0700 | [diff] [blame] | 812 | foreach_ip_interface_address (lm4, ia, sw_if_index, 0, |
Pavel Kotucek | 9f5a2b6 | 2017-06-14 13:56:55 +0200 | [diff] [blame] | 813 | ({ |
| 814 | address = ip_interface_address_get_address (lm4, ia); |
| 815 | ip4_add_del_interface_address(vm, sw_if_index, address, ia->address_length, 1); |
| 816 | })); |
| 817 | /* *INDENT-ON* */ |
| 818 | } |
| 819 | |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 820 | vnet_feature_enable_disable ("ip4-unicast", "ip4-not-enabled", sw_if_index, |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 821 | is_add, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | |
Neale Ranns | 8269d3d | 2018-01-30 09:02:20 -0800 | [diff] [blame] | 823 | vnet_feature_enable_disable ("ip4-multicast", "ip4-not-enabled", |
| 824 | sw_if_index, is_add, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 826 | return /* no error */ 0; |
| 827 | } |
| 828 | |
| 829 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip4_sw_interface_add_del); |
| 830 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | /* Global IP4 main. */ |
| 832 | ip4_main_t ip4_main; |
| 833 | |
| 834 | clib_error_t * |
| 835 | ip4_lookup_init (vlib_main_t * vm) |
| 836 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 837 | ip4_main_t *im = &ip4_main; |
| 838 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | uword i; |
| 840 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 841 | if ((error = vlib_call_init_function (vm, vnet_feature_init))) |
| 842 | return error; |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 843 | if ((error = vlib_call_init_function (vm, ip4_mtrie_module_init))) |
| 844 | return (error); |
| 845 | if ((error = vlib_call_init_function (vm, fib_module_init))) |
| 846 | return error; |
| 847 | if ((error = vlib_call_init_function (vm, mfib_module_init))) |
| 848 | return error; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 849 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 850 | for (i = 0; i < ARRAY_LEN (im->fib_masks); i++) |
| 851 | { |
| 852 | u32 m; |
| 853 | |
| 854 | if (i < 32) |
| 855 | m = pow2_mask (i) << (32 - i); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 856 | else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | m = ~0; |
| 858 | im->fib_masks[i] = clib_host_to_net_u32 (m); |
| 859 | } |
| 860 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 861 | ip_lookup_init (&im->lookup_main, /* is_ip6 */ 0); |
| 862 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 863 | /* Create FIB with index 0 and table id of 0. */ |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 864 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, 0, |
| 865 | FIB_SOURCE_DEFAULT_ROUTE); |
| 866 | mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, 0, |
| 867 | MFIB_SOURCE_DEFAULT_ROUTE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 868 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 869 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 870 | pg_node_t *pn; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 871 | pn = pg_get_node (ip4_lookup_node.index); |
| 872 | pn->unformat_edit = unformat_pg_ip4_header; |
| 873 | } |
| 874 | |
| 875 | { |
| 876 | ethernet_arp_header_t h; |
| 877 | |
| 878 | memset (&h, 0, sizeof (h)); |
| 879 | |
| 880 | /* Set target ethernet address to all zeros. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 881 | memset (h.ip4_over_ethernet[1].ethernet, 0, |
| 882 | sizeof (h.ip4_over_ethernet[1].ethernet)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | |
| 884 | #define _16(f,v) h.f = clib_host_to_net_u16 (v); |
| 885 | #define _8(f,v) h.f = v; |
| 886 | _16 (l2_type, ETHERNET_ARP_HARDWARE_TYPE_ethernet); |
| 887 | _16 (l3_type, ETHERNET_TYPE_IP4); |
| 888 | _8 (n_l2_address_bytes, 6); |
| 889 | _8 (n_l3_address_bytes, 4); |
| 890 | _16 (opcode, ETHERNET_ARP_OPCODE_request); |
| 891 | #undef _16 |
| 892 | #undef _8 |
| 893 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 894 | vlib_packet_template_init (vm, &im->ip4_arp_request_packet_template, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 895 | /* data */ &h, |
| 896 | sizeof (h), |
| 897 | /* alloc chunk size */ 8, |
| 898 | "ip4 arp"); |
| 899 | } |
| 900 | |
Dave Barach | 203c632 | 2016-06-26 10:29:03 -0400 | [diff] [blame] | 901 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | VLIB_INIT_FUNCTION (ip4_lookup_init); |
| 905 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 906 | typedef struct |
| 907 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 908 | /* Adjacency taken. */ |
Vengada Govindan | f154448 | 2016-09-28 02:45:57 -0700 | [diff] [blame] | 909 | u32 dpo_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 910 | u32 flow_hash; |
| 911 | u32 fib_index; |
| 912 | |
| 913 | /* Packet data, possibly *after* rewrite. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 914 | u8 packet_data[64 - 1 * sizeof (u32)]; |
| 915 | } |
| 916 | ip4_forward_next_trace_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 918 | u8 * |
| 919 | format_ip4_forward_next_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 920 | { |
| 921 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 922 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 923 | ip4_forward_next_trace_t *t = va_arg (*args, ip4_forward_next_trace_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 924 | u32 indent = format_get_indent (s); |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 925 | s = format (s, "%U%U", |
John Lo | ac8146c | 2016-09-27 17:44:02 -0400 | [diff] [blame] | 926 | format_white_space, indent, |
| 927 | format_ip4_header, t->packet_data, sizeof (t->packet_data)); |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 928 | return s; |
| 929 | } |
| 930 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 931 | static u8 * |
| 932 | format_ip4_lookup_trace (u8 * s, va_list * args) |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 933 | { |
| 934 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 935 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 936 | ip4_forward_next_trace_t *t = va_arg (*args, ip4_forward_next_trace_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 937 | u32 indent = format_get_indent (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 938 | |
John Lo | ac8146c | 2016-09-27 17:44:02 -0400 | [diff] [blame] | 939 | s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 940 | t->fib_index, t->dpo_index, t->flow_hash); |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 941 | s = format (s, "\n%U%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 942 | format_white_space, indent, |
| 943 | format_ip4_header, t->packet_data, sizeof (t->packet_data)); |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 944 | return s; |
| 945 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 947 | static u8 * |
| 948 | format_ip4_rewrite_trace (u8 * s, va_list * args) |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 949 | { |
| 950 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 951 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 952 | ip4_forward_next_trace_t *t = va_arg (*args, ip4_forward_next_trace_t *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 953 | u32 indent = format_get_indent (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | |
Vengada Govindan | f154448 | 2016-09-28 02:45:57 -0700 | [diff] [blame] | 955 | s = format (s, "tx_sw_if_index %d dpo-idx %d : %U flow hash: 0x%08x", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 956 | t->fib_index, t->dpo_index, format_ip_adjacency, |
| 957 | t->dpo_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash); |
Pierre Pfister | a38c3df | 2016-06-13 10:28:09 +0100 | [diff] [blame] | 958 | s = format (s, "\n%U%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 959 | format_white_space, indent, |
| 960 | format_ip_adjacency_packet_data, |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 961 | t->dpo_index, t->packet_data, sizeof (t->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | return s; |
| 963 | } |
| 964 | |
| 965 | /* Common trace function for all ip4-forward next nodes. */ |
| 966 | void |
| 967 | ip4_forward_next_trace (vlib_main_t * vm, |
| 968 | vlib_node_runtime_t * node, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 969 | vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 970 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 971 | u32 *from, n_left; |
| 972 | ip4_main_t *im = &ip4_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | |
| 974 | n_left = frame->n_vectors; |
| 975 | from = vlib_frame_vector_args (frame); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 976 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 977 | while (n_left >= 4) |
| 978 | { |
| 979 | u32 bi0, bi1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 980 | vlib_buffer_t *b0, *b1; |
| 981 | ip4_forward_next_trace_t *t0, *t1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 982 | |
| 983 | /* Prefetch next iteration. */ |
| 984 | vlib_prefetch_buffer_with_index (vm, from[2], LOAD); |
| 985 | vlib_prefetch_buffer_with_index (vm, from[3], LOAD); |
| 986 | |
| 987 | bi0 = from[0]; |
| 988 | bi1 = from[1]; |
| 989 | |
| 990 | b0 = vlib_get_buffer (vm, bi0); |
| 991 | b1 = vlib_get_buffer (vm, bi1); |
| 992 | |
| 993 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 994 | { |
| 995 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
Vengada Govindan | f154448 | 2016-09-28 02:45:57 -0700 | [diff] [blame] | 996 | t0->dpo_index = vnet_buffer (b0)->ip.adj_index[which_adj_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 997 | t0->flow_hash = vnet_buffer (b0)->ip.flow_hash; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 998 | t0->fib_index = |
| 999 | (vnet_buffer (b0)->sw_if_index[VLIB_TX] != |
| 1000 | (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] : |
| 1001 | vec_elt (im->fib_index_by_sw_if_index, |
| 1002 | vnet_buffer (b0)->sw_if_index[VLIB_RX]); |
Pierre Pfister | 0febaf1 | 2016-06-08 12:23:21 +0100 | [diff] [blame] | 1003 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 1004 | clib_memcpy (t0->packet_data, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1005 | vlib_buffer_get_current (b0), |
| 1006 | sizeof (t0->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1007 | } |
| 1008 | if (b1->flags & VLIB_BUFFER_IS_TRACED) |
| 1009 | { |
| 1010 | t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0])); |
Vengada Govindan | f154448 | 2016-09-28 02:45:57 -0700 | [diff] [blame] | 1011 | t1->dpo_index = vnet_buffer (b1)->ip.adj_index[which_adj_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1012 | t1->flow_hash = vnet_buffer (b1)->ip.flow_hash; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1013 | t1->fib_index = |
| 1014 | (vnet_buffer (b1)->sw_if_index[VLIB_TX] != |
| 1015 | (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] : |
| 1016 | vec_elt (im->fib_index_by_sw_if_index, |
| 1017 | vnet_buffer (b1)->sw_if_index[VLIB_RX]); |
| 1018 | clib_memcpy (t1->packet_data, vlib_buffer_get_current (b1), |
| 1019 | sizeof (t1->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1020 | } |
| 1021 | from += 2; |
| 1022 | n_left -= 2; |
| 1023 | } |
| 1024 | |
| 1025 | while (n_left >= 1) |
| 1026 | { |
| 1027 | u32 bi0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1028 | vlib_buffer_t *b0; |
| 1029 | ip4_forward_next_trace_t *t0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1030 | |
| 1031 | bi0 = from[0]; |
| 1032 | |
| 1033 | b0 = vlib_get_buffer (vm, bi0); |
| 1034 | |
| 1035 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 1036 | { |
| 1037 | t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0])); |
Vengada Govindan | f154448 | 2016-09-28 02:45:57 -0700 | [diff] [blame] | 1038 | t0->dpo_index = vnet_buffer (b0)->ip.adj_index[which_adj_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1039 | t0->flow_hash = vnet_buffer (b0)->ip.flow_hash; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1040 | t0->fib_index = |
| 1041 | (vnet_buffer (b0)->sw_if_index[VLIB_TX] != |
| 1042 | (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] : |
| 1043 | vec_elt (im->fib_index_by_sw_if_index, |
| 1044 | vnet_buffer (b0)->sw_if_index[VLIB_RX]); |
| 1045 | clib_memcpy (t0->packet_data, vlib_buffer_get_current (b0), |
| 1046 | sizeof (t0->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | } |
| 1048 | from += 1; |
| 1049 | n_left -= 1; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1053 | /* Compute TCP/UDP/ICMP4 checksum in software. */ |
| 1054 | u16 |
| 1055 | ip4_tcp_udp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, |
| 1056 | ip4_header_t * ip0) |
| 1057 | { |
| 1058 | ip_csum_t sum0; |
| 1059 | u32 ip_header_length, payload_length_host_byte_order; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1060 | u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1061 | u16 sum16; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1062 | void *data_this_buffer; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 1063 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1064 | /* Initialize checksum with ip header. */ |
| 1065 | ip_header_length = ip4_header_bytes (ip0); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1066 | payload_length_host_byte_order = |
| 1067 | clib_net_to_host_u16 (ip0->length) - ip_header_length; |
| 1068 | sum0 = |
| 1069 | clib_host_to_net_u32 (payload_length_host_byte_order + |
| 1070 | (ip0->protocol << 16)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (BITS (uword) == 32) |
| 1073 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1074 | sum0 = |
| 1075 | ip_csum_with_carry (sum0, |
| 1076 | clib_mem_unaligned (&ip0->src_address, u32)); |
| 1077 | sum0 = |
| 1078 | ip_csum_with_carry (sum0, |
| 1079 | clib_mem_unaligned (&ip0->dst_address, u32)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1080 | } |
| 1081 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1082 | sum0 = |
| 1083 | ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->src_address, u64)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1084 | |
| 1085 | n_bytes_left = n_this_buffer = payload_length_host_byte_order; |
| 1086 | data_this_buffer = (void *) ip0 + ip_header_length; |
Neale Ranns | d91c1db | 2017-07-31 02:30:50 -0700 | [diff] [blame] | 1087 | n_ip_bytes_this_buffer = |
| 1088 | p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1089 | if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer) |
| 1090 | { |
| 1091 | n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ? |
Neale Ranns | d91c1db | 2017-07-31 02:30:50 -0700 | [diff] [blame] | 1092 | n_ip_bytes_this_buffer - ip_header_length : 0; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 1093 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | while (1) |
| 1095 | { |
| 1096 | sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer); |
| 1097 | n_bytes_left -= n_this_buffer; |
| 1098 | if (n_bytes_left == 0) |
| 1099 | break; |
| 1100 | |
| 1101 | ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 1102 | p0 = vlib_get_buffer (vm, p0->next_buffer); |
| 1103 | data_this_buffer = vlib_buffer_get_current (p0); |
| 1104 | n_this_buffer = p0->current_length; |
| 1105 | } |
| 1106 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1107 | sum16 = ~ip_csum_fold (sum0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1108 | |
| 1109 | return sum16; |
| 1110 | } |
| 1111 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1112 | u32 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1113 | ip4_tcp_udp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0) |
| 1114 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1115 | ip4_header_t *ip0 = vlib_buffer_get_current (p0); |
| 1116 | udp_header_t *udp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1117 | u16 sum16; |
| 1118 | |
| 1119 | ASSERT (ip0->protocol == IP_PROTOCOL_TCP |
| 1120 | || ip0->protocol == IP_PROTOCOL_UDP); |
| 1121 | |
| 1122 | udp0 = (void *) (ip0 + 1); |
| 1123 | if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0) |
| 1124 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1125 | p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED |
| 1126 | | VNET_BUFFER_F_L4_CHECKSUM_CORRECT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1127 | return p0->flags; |
| 1128 | } |
| 1129 | |
| 1130 | sum16 = ip4_tcp_udp_compute_checksum (vm, p0, ip0); |
| 1131 | |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1132 | p0->flags |= (VNET_BUFFER_F_L4_CHECKSUM_COMPUTED |
| 1133 | | ((sum16 == 0) << VNET_BUFFER_F_LOG2_L4_CHECKSUM_CORRECT)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1134 | |
| 1135 | return p0->flags; |
| 1136 | } |
| 1137 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1138 | /* *INDENT-OFF* */ |
| 1139 | VNET_FEATURE_ARC_INIT (ip4_local) = |
| 1140 | { |
| 1141 | .arc_name = "ip4-local", |
| 1142 | .start_nodes = VNET_FEATURES ("ip4-local"), |
| 1143 | }; |
| 1144 | /* *INDENT-ON* */ |
| 1145 | |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1146 | static inline void |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1147 | ip4_local_l4_csum_validate (vlib_main_t * vm, vlib_buffer_t * p, |
| 1148 | ip4_header_t * ip, u8 is_udp, u8 * error, |
| 1149 | u8 * good_tcp_udp) |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1150 | { |
| 1151 | u32 flags0; |
| 1152 | flags0 = ip4_tcp_udp_validate_checksum (vm, p); |
| 1153 | *good_tcp_udp = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
| 1154 | if (is_udp) |
| 1155 | { |
| 1156 | udp_header_t *udp; |
| 1157 | u32 ip_len, udp_len; |
| 1158 | i32 len_diff; |
| 1159 | udp = ip4_next_header (ip); |
| 1160 | /* Verify UDP length. */ |
| 1161 | ip_len = clib_net_to_host_u16 (ip->length); |
| 1162 | udp_len = clib_net_to_host_u16 (udp->length); |
| 1163 | |
| 1164 | len_diff = ip_len - udp_len; |
| 1165 | *good_tcp_udp &= len_diff >= 0; |
| 1166 | *error = len_diff < 0 ? IP4_ERROR_UDP_LENGTH : *error; |
| 1167 | } |
| 1168 | } |
| 1169 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1170 | #define ip4_local_csum_is_offloaded(_b) \ |
| 1171 | _b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM \ |
| 1172 | || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM |
| 1173 | |
| 1174 | #define ip4_local_need_csum_check(is_tcp_udp, _b) \ |
| 1175 | (is_tcp_udp && !(_b->flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED \ |
| 1176 | || ip4_local_csum_is_offloaded (_b))) |
| 1177 | |
| 1178 | #define ip4_local_csum_is_valid(_b) \ |
| 1179 | (_b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT \ |
| 1180 | || (ip4_local_csum_is_offloaded (_b))) != 0 |
| 1181 | |
| 1182 | static inline void |
| 1183 | ip4_local_check_l4_csum (vlib_main_t * vm, vlib_buffer_t * b, |
| 1184 | ip4_header_t * ih, u8 * error) |
| 1185 | { |
| 1186 | u8 is_udp, is_tcp_udp, good_tcp_udp; |
| 1187 | |
| 1188 | is_udp = ih->protocol == IP_PROTOCOL_UDP; |
| 1189 | is_tcp_udp = is_udp || ih->protocol == IP_PROTOCOL_TCP; |
| 1190 | |
| 1191 | if (PREDICT_FALSE (ip4_local_need_csum_check (is_tcp_udp, b))) |
| 1192 | ip4_local_l4_csum_validate (vm, b, ih, is_udp, error, &good_tcp_udp); |
| 1193 | else |
| 1194 | good_tcp_udp = ip4_local_csum_is_valid (b); |
| 1195 | |
| 1196 | ASSERT (IP4_ERROR_TCP_CHECKSUM + 1 == IP4_ERROR_UDP_CHECKSUM); |
| 1197 | *error = (is_tcp_udp && !good_tcp_udp |
| 1198 | ? IP4_ERROR_TCP_CHECKSUM + is_udp : *error); |
| 1199 | } |
| 1200 | |
| 1201 | static inline void |
| 1202 | ip4_local_check_l4_csum_x2 (vlib_main_t * vm, vlib_buffer_t ** b, |
| 1203 | ip4_header_t ** ih, u8 * error) |
| 1204 | { |
| 1205 | u8 is_udp[2], is_tcp_udp[2], good_tcp_udp[2]; |
| 1206 | |
| 1207 | is_udp[0] = ih[0]->protocol == IP_PROTOCOL_UDP; |
| 1208 | is_udp[1] = ih[1]->protocol == IP_PROTOCOL_UDP; |
| 1209 | |
| 1210 | is_tcp_udp[0] = is_udp[0] || ih[0]->protocol == IP_PROTOCOL_TCP; |
| 1211 | is_tcp_udp[1] = is_udp[1] || ih[1]->protocol == IP_PROTOCOL_TCP; |
| 1212 | |
| 1213 | good_tcp_udp[0] = ip4_local_csum_is_valid (b[0]); |
| 1214 | good_tcp_udp[1] = ip4_local_csum_is_valid (b[1]); |
| 1215 | |
| 1216 | if (PREDICT_FALSE (ip4_local_need_csum_check (is_tcp_udp[0], b[0]) |
| 1217 | || ip4_local_need_csum_check (is_tcp_udp[1], b[1]))) |
| 1218 | { |
| 1219 | if (is_tcp_udp[0]) |
| 1220 | ip4_local_l4_csum_validate (vm, b[0], ih[0], is_udp[0], &error[0], |
| 1221 | &good_tcp_udp[0]); |
| 1222 | if (is_tcp_udp[1]) |
| 1223 | ip4_local_l4_csum_validate (vm, b[1], ih[1], is_udp[1], &error[1], |
| 1224 | &good_tcp_udp[1]); |
| 1225 | } |
| 1226 | |
| 1227 | error[0] = (is_tcp_udp[0] && !good_tcp_udp[0] ? |
| 1228 | IP4_ERROR_TCP_CHECKSUM + is_udp[0] : error[0]); |
| 1229 | error[1] = (is_tcp_udp[1] && !good_tcp_udp[1] ? |
| 1230 | IP4_ERROR_TCP_CHECKSUM + is_udp[1] : error[1]); |
| 1231 | } |
| 1232 | |
| 1233 | static inline void |
| 1234 | ip4_local_set_next_and_error (vlib_node_runtime_t * error_node, |
| 1235 | vlib_buffer_t * b, u16 * next, u8 error, |
| 1236 | u8 head_of_feature_arc) |
| 1237 | { |
| 1238 | u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index; |
| 1239 | u32 next_index; |
| 1240 | |
| 1241 | *next = error != IP4_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : *next; |
| 1242 | b->error = error ? error_node->errors[error] : 0; |
| 1243 | if (head_of_feature_arc) |
| 1244 | { |
| 1245 | next_index = *next; |
| 1246 | if (PREDICT_TRUE (error == (u8) IP4_ERROR_UNKNOWN_PROTOCOL)) |
| 1247 | { |
| 1248 | vnet_feature_arc_start (arc_index, |
| 1249 | vnet_buffer (b)->sw_if_index[VLIB_RX], |
| 1250 | &next_index, b); |
| 1251 | *next = next_index; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | typedef struct |
| 1257 | { |
| 1258 | ip4_address_t src; |
| 1259 | u32 lbi; |
| 1260 | u8 error; |
| 1261 | } ip4_local_last_check_t; |
| 1262 | |
| 1263 | static inline void |
| 1264 | ip4_local_check_src (vlib_buffer_t * b, ip4_header_t * ip0, |
| 1265 | ip4_local_last_check_t * last_check, u8 * error0) |
| 1266 | { |
| 1267 | ip4_fib_mtrie_leaf_t leaf0; |
| 1268 | ip4_fib_mtrie_t *mtrie0; |
| 1269 | const dpo_id_t *dpo0; |
| 1270 | load_balance_t *lb0; |
| 1271 | u32 lbi0; |
| 1272 | |
| 1273 | vnet_buffer (b)->ip.fib_index = |
| 1274 | vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0 ? |
| 1275 | vnet_buffer (b)->sw_if_index[VLIB_TX] : vnet_buffer (b)->ip.fib_index; |
| 1276 | |
| 1277 | if (PREDICT_FALSE (last_check->src.as_u32 != ip0->src_address.as_u32)) |
| 1278 | { |
| 1279 | mtrie0 = &ip4_fib_get (vnet_buffer (b)->ip.fib_index)->mtrie; |
| 1280 | leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); |
| 1281 | leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2); |
| 1282 | leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3); |
| 1283 | lbi0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); |
| 1284 | |
| 1285 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = lbi0; |
| 1286 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = lbi0; |
| 1287 | |
| 1288 | lb0 = load_balance_get (lbi0); |
| 1289 | dpo0 = load_balance_get_bucket_i (lb0, 0); |
| 1290 | |
| 1291 | /* |
| 1292 | * Must have a route to source otherwise we drop the packet. |
| 1293 | * ip4 broadcasts are accepted, e.g. to make dhcp client work |
| 1294 | * |
| 1295 | * The checks are: |
| 1296 | * - the source is a recieve => it's from us => bogus, do this |
| 1297 | * first since it sets a different error code. |
| 1298 | * - uRPF check for any route to source - accept if passes. |
| 1299 | * - allow packets destined to the broadcast address from unknown sources |
| 1300 | */ |
| 1301 | |
| 1302 | *error0 = ((*error0 == IP4_ERROR_UNKNOWN_PROTOCOL |
| 1303 | && dpo0->dpoi_type == DPO_RECEIVE) ? |
| 1304 | IP4_ERROR_SPOOFED_LOCAL_PACKETS : *error0); |
| 1305 | *error0 = ((*error0 == IP4_ERROR_UNKNOWN_PROTOCOL |
| 1306 | && !fib_urpf_check_size (lb0->lb_urpf) |
| 1307 | && ip0->dst_address.as_u32 != 0xFFFFFFFF) ? |
| 1308 | IP4_ERROR_SRC_LOOKUP_MISS : *error0); |
| 1309 | |
| 1310 | last_check->src.as_u32 = ip0->src_address.as_u32; |
| 1311 | last_check->lbi = lbi0; |
| 1312 | last_check->error = *error0; |
| 1313 | } |
| 1314 | else |
| 1315 | { |
| 1316 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = last_check->lbi; |
| 1317 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = last_check->lbi; |
| 1318 | *error0 = last_check->error; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | static inline void |
| 1323 | ip4_local_check_src_x2 (vlib_buffer_t ** b, ip4_header_t ** ip, |
| 1324 | ip4_local_last_check_t * last_check, u8 * error) |
| 1325 | { |
| 1326 | ip4_fib_mtrie_leaf_t leaf[2]; |
| 1327 | ip4_fib_mtrie_t *mtrie[2]; |
| 1328 | const dpo_id_t *dpo[2]; |
| 1329 | load_balance_t *lb[2]; |
| 1330 | u32 not_last_hit = 0; |
| 1331 | u32 lbi[2]; |
| 1332 | |
| 1333 | not_last_hit |= ip[0]->src_address.as_u32 ^ last_check->src.as_u32; |
| 1334 | not_last_hit |= ip[1]->src_address.as_u32 ^ last_check->src.as_u32; |
| 1335 | |
| 1336 | vnet_buffer (b[0])->ip.fib_index = |
| 1337 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ? |
| 1338 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] : |
| 1339 | vnet_buffer (b[0])->ip.fib_index; |
| 1340 | |
| 1341 | vnet_buffer (b[1])->ip.fib_index = |
| 1342 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ? |
| 1343 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] : |
| 1344 | vnet_buffer (b[1])->ip.fib_index; |
| 1345 | |
| 1346 | if (PREDICT_FALSE (not_last_hit)) |
| 1347 | { |
| 1348 | mtrie[0] = &ip4_fib_get (vnet_buffer (b[0])->ip.fib_index)->mtrie; |
| 1349 | mtrie[1] = &ip4_fib_get (vnet_buffer (b[1])->ip.fib_index)->mtrie; |
| 1350 | |
| 1351 | leaf[0] = ip4_fib_mtrie_lookup_step_one (mtrie[0], &ip[0]->src_address); |
| 1352 | leaf[1] = ip4_fib_mtrie_lookup_step_one (mtrie[1], &ip[1]->src_address); |
| 1353 | |
| 1354 | leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0], |
| 1355 | &ip[0]->src_address, 2); |
| 1356 | leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1], |
| 1357 | &ip[1]->src_address, 2); |
| 1358 | |
| 1359 | leaf[0] = ip4_fib_mtrie_lookup_step (mtrie[0], leaf[0], |
| 1360 | &ip[0]->src_address, 3); |
| 1361 | leaf[1] = ip4_fib_mtrie_lookup_step (mtrie[1], leaf[1], |
| 1362 | &ip[1]->src_address, 3); |
| 1363 | |
| 1364 | lbi[0] = ip4_fib_mtrie_leaf_get_adj_index (leaf[0]); |
| 1365 | lbi[1] = ip4_fib_mtrie_leaf_get_adj_index (leaf[1]); |
| 1366 | |
| 1367 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = lbi[0]; |
| 1368 | vnet_buffer (b[0])->ip.adj_index[VLIB_RX] = lbi[0]; |
| 1369 | |
| 1370 | vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = lbi[1]; |
| 1371 | vnet_buffer (b[1])->ip.adj_index[VLIB_RX] = lbi[1]; |
| 1372 | |
| 1373 | lb[0] = load_balance_get (lbi[0]); |
| 1374 | lb[1] = load_balance_get (lbi[1]); |
| 1375 | |
| 1376 | dpo[0] = load_balance_get_bucket_i (lb[0], 0); |
| 1377 | dpo[1] = load_balance_get_bucket_i (lb[1], 0); |
| 1378 | |
| 1379 | error[0] = ((error[0] == IP4_ERROR_UNKNOWN_PROTOCOL && |
| 1380 | dpo[0]->dpoi_type == DPO_RECEIVE) ? |
| 1381 | IP4_ERROR_SPOOFED_LOCAL_PACKETS : error[0]); |
| 1382 | error[0] = ((error[0] == IP4_ERROR_UNKNOWN_PROTOCOL && |
| 1383 | !fib_urpf_check_size (lb[0]->lb_urpf) && |
| 1384 | ip[0]->dst_address.as_u32 != 0xFFFFFFFF) |
| 1385 | ? IP4_ERROR_SRC_LOOKUP_MISS : error[0]); |
| 1386 | |
| 1387 | error[1] = ((error[1] == IP4_ERROR_UNKNOWN_PROTOCOL && |
| 1388 | dpo[1]->dpoi_type == DPO_RECEIVE) ? |
| 1389 | IP4_ERROR_SPOOFED_LOCAL_PACKETS : error[1]); |
| 1390 | error[1] = ((error[1] == IP4_ERROR_UNKNOWN_PROTOCOL && |
| 1391 | !fib_urpf_check_size (lb[1]->lb_urpf) && |
| 1392 | ip[1]->dst_address.as_u32 != 0xFFFFFFFF) |
| 1393 | ? IP4_ERROR_SRC_LOOKUP_MISS : error[1]); |
| 1394 | |
| 1395 | last_check->src.as_u32 = ip[1]->src_address.as_u32; |
| 1396 | last_check->lbi = lbi[1]; |
| 1397 | last_check->error = error[1]; |
| 1398 | } |
| 1399 | else |
| 1400 | { |
| 1401 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = last_check->lbi; |
| 1402 | vnet_buffer (b[0])->ip.adj_index[VLIB_RX] = last_check->lbi; |
| 1403 | |
| 1404 | vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = last_check->lbi; |
| 1405 | vnet_buffer (b[1])->ip.adj_index[VLIB_RX] = last_check->lbi; |
| 1406 | |
| 1407 | error[0] = last_check->error; |
| 1408 | error[1] = last_check->error; |
| 1409 | } |
| 1410 | } |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1411 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1412 | static inline uword |
| 1413 | ip4_local_inline (vlib_main_t * vm, |
| 1414 | vlib_node_runtime_t * node, |
| 1415 | vlib_frame_t * frame, int head_of_feature_arc) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1416 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1417 | ip4_main_t *im = &ip4_main; |
| 1418 | ip_lookup_main_t *lm = &im->lookup_main; |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1419 | u32 *from, n_left_from; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1420 | vlib_node_runtime_t *error_node = |
| 1421 | vlib_node_get_runtime (vm, ip4_input_node.index); |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1422 | u16 nexts[VLIB_FRAME_SIZE], *next; |
| 1423 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 1424 | ip4_header_t *ip[2]; |
| 1425 | u8 error[2]; |
| 1426 | |
| 1427 | ip4_local_last_check_t last_check = { |
| 1428 | .src = {.as_u32 = 0}, |
| 1429 | .lbi = ~0, |
| 1430 | .error = IP4_ERROR_UNKNOWN_PROTOCOL |
| 1431 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1432 | |
| 1433 | from = vlib_frame_vector_args (frame); |
| 1434 | n_left_from = frame->n_vectors; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 1435 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1436 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 1437 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
| 1438 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1439 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 1440 | b = bufs; |
| 1441 | next = nexts; |
| 1442 | |
| 1443 | while (n_left_from >= 6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1444 | { |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1445 | u32 is_nat, not_batch = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1446 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1447 | /* Prefetch next iteration. */ |
| 1448 | { |
| 1449 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 1450 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 1451 | |
| 1452 | CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 1453 | CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 1454 | } |
| 1455 | |
| 1456 | error[0] = error[1] = IP4_ERROR_UNKNOWN_PROTOCOL; |
| 1457 | |
| 1458 | ip[0] = vlib_buffer_get_current (b[0]); |
| 1459 | ip[1] = vlib_buffer_get_current (b[1]); |
| 1460 | |
| 1461 | vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data; |
| 1462 | vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data; |
| 1463 | |
| 1464 | is_nat = b[0]->flags & VNET_BUFFER_F_IS_NATED; |
| 1465 | not_batch |= is_nat ^ (b[1]->flags & VNET_BUFFER_F_IS_NATED); |
| 1466 | |
| 1467 | if (head_of_feature_arc == 0 || (is_nat && not_batch == 0)) |
| 1468 | goto skip_checks; |
| 1469 | |
| 1470 | if (PREDICT_TRUE (not_batch == 0)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1471 | { |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1472 | ip4_local_check_l4_csum_x2 (vm, b, ip, error); |
| 1473 | ip4_local_check_src_x2 (b, ip, &last_check, error); |
| 1474 | } |
| 1475 | else |
| 1476 | { |
| 1477 | if (!(b[0]->flags & VNET_BUFFER_F_IS_NATED)) |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1478 | { |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1479 | ip4_local_check_l4_csum (vm, b[0], ip[0], &error[0]); |
| 1480 | ip4_local_check_src (b[0], ip[0], &last_check, &error[0]); |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1481 | } |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1482 | if (!(b[1]->flags & VNET_BUFFER_F_IS_NATED)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1483 | { |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1484 | ip4_local_check_l4_csum (vm, b[1], ip[1], &error[1]); |
| 1485 | ip4_local_check_src (b[1], ip[1], &last_check, &error[1]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1489 | skip_checks: |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1490 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1491 | next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol]; |
| 1492 | next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol]; |
| 1493 | ip4_local_set_next_and_error (error_node, b[0], &next[0], error[0], |
| 1494 | head_of_feature_arc); |
| 1495 | ip4_local_set_next_and_error (error_node, b[1], &next[1], error[1], |
| 1496 | head_of_feature_arc); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 1497 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1498 | b += 2; |
| 1499 | next += 2; |
| 1500 | n_left_from -= 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Florin Coras | 1b25552 | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 1503 | while (n_left_from > 0) |
| 1504 | { |
| 1505 | error[0] = IP4_ERROR_UNKNOWN_PROTOCOL; |
| 1506 | |
| 1507 | ip[0] = vlib_buffer_get_current (b[0]); |
| 1508 | vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data; |
| 1509 | |
| 1510 | if (head_of_feature_arc == 0 || (b[0]->flags & VNET_BUFFER_F_IS_NATED)) |
| 1511 | goto skip_check; |
| 1512 | |
| 1513 | ip4_local_check_l4_csum (vm, b[0], ip[0], &error[0]); |
| 1514 | ip4_local_check_src (b[0], ip[0], &last_check, &error[0]); |
| 1515 | |
| 1516 | skip_check: |
| 1517 | |
| 1518 | next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol]; |
| 1519 | ip4_local_set_next_and_error (error_node, b[0], &next[0], error[0], |
| 1520 | head_of_feature_arc); |
| 1521 | |
| 1522 | b += 1; |
| 1523 | next += 1; |
| 1524 | n_left_from -= 1; |
| 1525 | } |
| 1526 | |
| 1527 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1528 | return frame->n_vectors; |
| 1529 | } |
| 1530 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1531 | static uword |
| 1532 | ip4_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 1533 | { |
| 1534 | return ip4_local_inline (vm, node, frame, 1 /* head of feature arc */ ); |
| 1535 | } |
| 1536 | |
| 1537 | /* *INDENT-OFF* */ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1538 | VLIB_REGISTER_NODE (ip4_local_node) = |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1539 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1540 | .function = ip4_local, |
| 1541 | .name = "ip4-local", |
| 1542 | .vector_size = sizeof (u32), |
| 1543 | .format_trace = format_ip4_forward_next_trace, |
| 1544 | .n_next_nodes = IP_LOCAL_N_NEXT, |
| 1545 | .next_nodes = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1546 | { |
Neale Ranns | d91c1db | 2017-07-31 02:30:50 -0700 | [diff] [blame] | 1547 | [IP_LOCAL_NEXT_DROP] = "ip4-drop", |
| 1548 | [IP_LOCAL_NEXT_PUNT] = "ip4-punt", |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1549 | [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip4-udp-lookup", |
Florin Coras | 20a14b9 | 2017-08-15 22:47:22 -0700 | [diff] [blame] | 1550 | [IP_LOCAL_NEXT_ICMP] = "ip4-icmp-input", |
| 1551 | }, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1552 | }; |
| 1553 | /* *INDENT-ON* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1554 | |
| 1555 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_node, ip4_local); |
| 1556 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1557 | static uword |
| 1558 | ip4_local_end_of_arc (vlib_main_t * vm, |
| 1559 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 1560 | { |
| 1561 | return ip4_local_inline (vm, node, frame, 0 /* head of feature arc */ ); |
| 1562 | } |
| 1563 | |
| 1564 | /* *INDENT-OFF* */ |
| 1565 | VLIB_REGISTER_NODE (ip4_local_end_of_arc_node,static) = { |
| 1566 | .function = ip4_local_end_of_arc, |
| 1567 | .name = "ip4-local-end-of-arc", |
| 1568 | .vector_size = sizeof (u32), |
| 1569 | |
| 1570 | .format_trace = format_ip4_forward_next_trace, |
| 1571 | .sibling_of = "ip4-local", |
| 1572 | }; |
| 1573 | |
| 1574 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_local_end_of_arc_node, ip4_local_end_of_arc) |
| 1575 | |
| 1576 | VNET_FEATURE_INIT (ip4_local_end_of_arc, static) = { |
| 1577 | .arc_name = "ip4-local", |
| 1578 | .node_name = "ip4-local-end-of-arc", |
| 1579 | .runs_before = 0, /* not before any other features */ |
| 1580 | }; |
| 1581 | /* *INDENT-ON* */ |
| 1582 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1583 | void |
| 1584 | ip4_register_protocol (u32 protocol, u32 node_index) |
| 1585 | { |
| 1586 | vlib_main_t *vm = vlib_get_main (); |
| 1587 | ip4_main_t *im = &ip4_main; |
| 1588 | ip_lookup_main_t *lm = &im->lookup_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1589 | |
| 1590 | ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol)); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1591 | lm->local_next_by_ip_protocol[protocol] = |
| 1592 | vlib_node_add_next (vm, ip4_local_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static clib_error_t * |
| 1596 | show_ip_local_command_fn (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1597 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1598 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1599 | ip4_main_t *im = &ip4_main; |
| 1600 | ip_lookup_main_t *lm = &im->lookup_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1601 | int i; |
| 1602 | |
| 1603 | vlib_cli_output (vm, "Protocols handled by ip4_local"); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1604 | for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1605 | { |
| 1606 | if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT) |
Pierre Pfister | 1bfd372 | 2017-09-18 11:40:32 +0200 | [diff] [blame] | 1607 | { |
| 1608 | u32 node_index = vlib_get_node (vm, |
| 1609 | ip4_local_node.index)-> |
| 1610 | next_nodes[lm->local_next_by_ip_protocol[i]]; |
| 1611 | vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm, |
| 1612 | node_index); |
| 1613 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1614 | } |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 1620 | /*? |
| 1621 | * Display the set of protocols handled by the local IPv4 stack. |
| 1622 | * |
| 1623 | * @cliexpar |
| 1624 | * Example of how to display local protocol table: |
| 1625 | * @cliexstart{show ip local} |
| 1626 | * Protocols handled by ip4_local |
| 1627 | * 1 |
| 1628 | * 17 |
| 1629 | * 47 |
| 1630 | * @cliexend |
| 1631 | ?*/ |
| 1632 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1633 | VLIB_CLI_COMMAND (show_ip_local, static) = |
| 1634 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1635 | .path = "show ip local", |
| 1636 | .function = show_ip_local_command_fn, |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 1637 | .short_help = "show ip local", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1638 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 1639 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1640 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1641 | always_inline uword |
| 1642 | ip4_arp_inline (vlib_main_t * vm, |
| 1643 | vlib_node_runtime_t * node, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1644 | vlib_frame_t * frame, int is_glean) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1645 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1646 | vnet_main_t *vnm = vnet_get_main (); |
| 1647 | ip4_main_t *im = &ip4_main; |
| 1648 | ip_lookup_main_t *lm = &im->lookup_main; |
| 1649 | u32 *from, *to_next_drop; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1650 | uword n_left_from, n_left_to_next_drop, next_index; |
| 1651 | static f64 time_last_seed_change = -1e100; |
| 1652 | static u32 hash_seeds[3]; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 1653 | static uword hash_bitmap[256 / BITS (uword)]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1654 | f64 time_now; |
| 1655 | |
| 1656 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 1657 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
| 1658 | |
| 1659 | time_now = vlib_time_now (vm); |
| 1660 | if (time_now - time_last_seed_change > 1e-3) |
| 1661 | { |
| 1662 | uword i; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1663 | u32 *r = clib_random_buffer_get_data (&vm->random_buffer, |
| 1664 | sizeof (hash_seeds)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1665 | for (i = 0; i < ARRAY_LEN (hash_seeds); i++) |
| 1666 | hash_seeds[i] = r[i]; |
| 1667 | |
| 1668 | /* Mark all hash keys as been no-seen before. */ |
| 1669 | for (i = 0; i < ARRAY_LEN (hash_bitmap); i++) |
| 1670 | hash_bitmap[i] = 0; |
| 1671 | |
| 1672 | time_last_seed_change = time_now; |
| 1673 | } |
| 1674 | |
| 1675 | from = vlib_frame_vector_args (frame); |
| 1676 | n_left_from = frame->n_vectors; |
| 1677 | next_index = node->cached_next_index; |
| 1678 | if (next_index == IP4_ARP_NEXT_DROP) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1679 | next_index = IP4_ARP_N_NEXT; /* point to first interface */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1680 | |
| 1681 | while (n_left_from > 0) |
| 1682 | { |
| 1683 | vlib_get_next_frame (vm, node, IP4_ARP_NEXT_DROP, |
| 1684 | to_next_drop, n_left_to_next_drop); |
| 1685 | |
| 1686 | while (n_left_from > 0 && n_left_to_next_drop > 0) |
| 1687 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1688 | u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1689 | ip_adjacency_t *adj0; |
| 1690 | vlib_buffer_t *p0; |
| 1691 | ip4_header_t *ip0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1692 | uword bm0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1693 | |
| 1694 | pi0 = from[0]; |
| 1695 | |
| 1696 | p0 = vlib_get_buffer (vm, pi0); |
| 1697 | |
| 1698 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 1699 | adj0 = adj_get (adj_index0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1700 | ip0 = vlib_buffer_get_current (p0); |
| 1701 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1702 | a0 = hash_seeds[0]; |
| 1703 | b0 = hash_seeds[1]; |
| 1704 | c0 = hash_seeds[2]; |
| 1705 | |
| 1706 | sw_if_index0 = adj0->rewrite_header.sw_if_index; |
| 1707 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 1708 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1709 | if (is_glean) |
| 1710 | { |
Neale Ranns | 948e00f | 2016-10-20 13:39:34 +0100 | [diff] [blame] | 1711 | /* |
| 1712 | * this is the Glean case, so we are ARPing for the |
| 1713 | * packet's destination |
| 1714 | */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1715 | a0 ^= ip0->dst_address.data_u32; |
| 1716 | } |
| 1717 | else |
| 1718 | { |
| 1719 | a0 ^= adj0->sub_type.nbr.next_hop.ip4.data_u32; |
| 1720 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1721 | b0 ^= sw_if_index0; |
| 1722 | |
Florin Coras | 2d3dbc4 | 2017-09-08 16:22:38 -0400 | [diff] [blame] | 1723 | hash_v3_mix32 (a0, b0, c0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1724 | hash_v3_finalize32 (a0, b0, c0); |
| 1725 | |
| 1726 | c0 &= BITS (hash_bitmap) - 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1727 | m0 = (uword) 1 << (c0 % BITS (uword)); |
Florin Coras | 2d3dbc4 | 2017-09-08 16:22:38 -0400 | [diff] [blame] | 1728 | c0 = c0 / BITS (uword); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1729 | |
| 1730 | bm0 = hash_bitmap[c0]; |
| 1731 | drop0 = (bm0 & m0) != 0; |
| 1732 | |
| 1733 | /* Mark it as seen. */ |
| 1734 | hash_bitmap[c0] = bm0 | m0; |
| 1735 | |
| 1736 | from += 1; |
| 1737 | n_left_from -= 1; |
| 1738 | to_next_drop[0] = pi0; |
| 1739 | to_next_drop += 1; |
| 1740 | n_left_to_next_drop -= 1; |
| 1741 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1742 | p0->error = |
| 1743 | node->errors[drop0 ? IP4_ARP_ERROR_DROP : |
| 1744 | IP4_ARP_ERROR_REQUEST_SENT]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1745 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 1746 | /* |
| 1747 | * the adj has been updated to a rewrite but the node the DPO that got |
| 1748 | * us here hasn't - yet. no big deal. we'll drop while we wait. |
| 1749 | */ |
| 1750 | if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index) |
| 1751 | continue; |
| 1752 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1753 | if (drop0) |
| 1754 | continue; |
| 1755 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1756 | /* |
| 1757 | * Can happen if the control-plane is programming tables |
| 1758 | * with traffic flowing; at least that's today's lame excuse. |
| 1759 | */ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1760 | if ((is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_GLEAN) |
| 1761 | || (!is_glean && adj0->lookup_next_index != IP_LOOKUP_NEXT_ARP)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1762 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1763 | p0->error = node->errors[IP4_ARP_ERROR_NON_ARP_ADJ]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1764 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1765 | else |
| 1766 | /* Send ARP request. */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1767 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1768 | u32 bi0 = 0; |
| 1769 | vlib_buffer_t *b0; |
| 1770 | ethernet_arp_header_t *h0; |
| 1771 | vnet_hw_interface_t *hw_if0; |
| 1772 | |
| 1773 | h0 = |
| 1774 | vlib_packet_template_get_packet (vm, |
| 1775 | &im->ip4_arp_request_packet_template, |
| 1776 | &bi0); |
| 1777 | |
Florin Coras | d172a62 | 2017-10-14 11:02:37 -0700 | [diff] [blame] | 1778 | /* Seems we're out of buffers */ |
| 1779 | if (PREDICT_FALSE (!h0)) |
| 1780 | continue; |
| 1781 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1782 | /* Add rewrite/encap string for ARP packet. */ |
| 1783 | vnet_rewrite_one_header (adj0[0], h0, |
| 1784 | sizeof (ethernet_header_t)); |
| 1785 | |
| 1786 | hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); |
| 1787 | |
| 1788 | /* Src ethernet address in ARP header. */ |
| 1789 | clib_memcpy (h0->ip4_over_ethernet[0].ethernet, |
| 1790 | hw_if0->hw_address, |
| 1791 | sizeof (h0->ip4_over_ethernet[0].ethernet)); |
| 1792 | |
| 1793 | if (is_glean) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1794 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1795 | /* The interface's source address is stashed in the Glean Adj */ |
| 1796 | h0->ip4_over_ethernet[0].ip4 = |
| 1797 | adj0->sub_type.glean.receive_addr.ip4; |
| 1798 | |
| 1799 | /* Copy in destination address we are requesting. This is the |
| 1800 | * glean case, so it's the packet's destination.*/ |
| 1801 | h0->ip4_over_ethernet[1].ip4.data_u32 = |
| 1802 | ip0->dst_address.data_u32; |
| 1803 | } |
| 1804 | else |
| 1805 | { |
| 1806 | /* Src IP address in ARP header. */ |
| 1807 | if (ip4_src_address_for_packet (lm, sw_if_index0, |
| 1808 | &h0-> |
| 1809 | ip4_over_ethernet[0].ip4)) |
| 1810 | { |
| 1811 | /* No source address available */ |
| 1812 | p0->error = |
| 1813 | node->errors[IP4_ARP_ERROR_NO_SOURCE_ADDRESS]; |
| 1814 | vlib_buffer_free (vm, &bi0, 1); |
| 1815 | continue; |
| 1816 | } |
| 1817 | |
| 1818 | /* Copy in destination address we are requesting from the |
| 1819 | incomplete adj */ |
| 1820 | h0->ip4_over_ethernet[1].ip4.data_u32 = |
| 1821 | adj0->sub_type.nbr.next_hop.ip4.as_u32; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1824 | vlib_buffer_copy_trace_flag (vm, p0, bi0); |
| 1825 | b0 = vlib_get_buffer (vm, bi0); |
Florin Coras | 2f9b0c0 | 2017-09-11 20:54:15 -0400 | [diff] [blame] | 1826 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1827 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0; |
| 1828 | |
| 1829 | vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes); |
| 1830 | |
| 1831 | vlib_set_next_frame_buffer (vm, node, |
| 1832 | adj0->rewrite_header.next_index, |
| 1833 | bi0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1834 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | vlib_put_next_frame (vm, node, IP4_ARP_NEXT_DROP, n_left_to_next_drop); |
| 1838 | } |
| 1839 | |
| 1840 | return frame->n_vectors; |
| 1841 | } |
| 1842 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1843 | static uword |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1844 | ip4_arp (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1845 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1846 | return (ip4_arp_inline (vm, node, frame, 0)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | static uword |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1850 | ip4_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1851 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1852 | return (ip4_arp_inline (vm, node, frame, 1)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1855 | static char *ip4_arp_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1856 | [IP4_ARP_ERROR_DROP] = "address overflow drops", |
| 1857 | [IP4_ARP_ERROR_REQUEST_SENT] = "ARP requests sent", |
| 1858 | [IP4_ARP_ERROR_NON_ARP_ADJ] = "ARPs to non-ARP adjacencies", |
| 1859 | [IP4_ARP_ERROR_REPLICATE_DROP] = "ARP replication completed", |
| 1860 | [IP4_ARP_ERROR_REPLICATE_FAIL] = "ARP replication failed", |
Pierre Pfister | d076f19 | 2016-06-22 12:58:30 +0100 | [diff] [blame] | 1861 | [IP4_ARP_ERROR_NO_SOURCE_ADDRESS] = "no source address for ARP request", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1862 | }; |
| 1863 | |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 1864 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1865 | VLIB_REGISTER_NODE (ip4_arp_node) = |
| 1866 | { |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 1867 | .function = ip4_arp, |
| 1868 | .name = "ip4-arp", |
| 1869 | .vector_size = sizeof (u32), |
| 1870 | .format_trace = format_ip4_forward_next_trace, |
| 1871 | .n_errors = ARRAY_LEN (ip4_arp_error_strings), |
| 1872 | .error_strings = ip4_arp_error_strings, |
| 1873 | .n_next_nodes = IP4_ARP_N_NEXT, |
| 1874 | .next_nodes = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1875 | { |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 1876 | [IP4_ARP_NEXT_DROP] = "error-drop", |
| 1877 | }, |
| 1878 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1879 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1880 | VLIB_REGISTER_NODE (ip4_glean_node) = |
| 1881 | { |
Neale Ranns | f868632 | 2017-11-29 02:39:53 -0800 | [diff] [blame] | 1882 | .function = ip4_glean, |
| 1883 | .name = "ip4-glean", |
| 1884 | .vector_size = sizeof (u32), |
| 1885 | .format_trace = format_ip4_forward_next_trace, |
| 1886 | .n_errors = ARRAY_LEN (ip4_arp_error_strings), |
| 1887 | .error_strings = ip4_arp_error_strings, |
| 1888 | .n_next_nodes = IP4_ARP_N_NEXT, |
| 1889 | .next_nodes = { |
| 1890 | [IP4_ARP_NEXT_DROP] = "error-drop", |
| 1891 | }, |
| 1892 | }; |
| 1893 | /* *INDENT-ON* */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1894 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1895 | #define foreach_notrace_ip4_arp_error \ |
| 1896 | _(DROP) \ |
| 1897 | _(REQUEST_SENT) \ |
| 1898 | _(REPLICATE_DROP) \ |
| 1899 | _(REPLICATE_FAIL) |
| 1900 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1901 | clib_error_t * |
| 1902 | arp_notrace_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1903 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1904 | vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, ip4_arp_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1905 | |
| 1906 | /* don't trace ARP request packets */ |
| 1907 | #define _(a) \ |
| 1908 | vnet_pcap_drop_trace_filter_add_del \ |
| 1909 | (rt->errors[IP4_ARP_ERROR_##a], \ |
| 1910 | 1 /* is_add */); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1911 | foreach_notrace_ip4_arp_error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1912 | #undef _ |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1916 | VLIB_INIT_FUNCTION (arp_notrace_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1917 | |
| 1918 | |
| 1919 | /* Send an ARP request to see if given destination is reachable on given interface. */ |
| 1920 | clib_error_t * |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame^] | 1921 | ip4_probe_neighbor (vlib_main_t * vm, ip4_address_t * dst, u32 sw_if_index, |
| 1922 | u8 refresh) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1923 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1924 | vnet_main_t *vnm = vnet_get_main (); |
| 1925 | ip4_main_t *im = &ip4_main; |
| 1926 | ethernet_arp_header_t *h; |
| 1927 | ip4_address_t *src; |
| 1928 | ip_interface_address_t *ia; |
| 1929 | ip_adjacency_t *adj; |
| 1930 | vnet_hw_interface_t *hi; |
| 1931 | vnet_sw_interface_t *si; |
| 1932 | vlib_buffer_t *b; |
Neale Ranns | 7a27274 | 2017-05-30 02:08:14 -0700 | [diff] [blame] | 1933 | adj_index_t ai; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1934 | u32 bi = 0; |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame^] | 1935 | u8 unicast_rewrite = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1936 | |
| 1937 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 1938 | |
| 1939 | if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) |
| 1940 | { |
| 1941 | return clib_error_return (0, "%U: interface %U down", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1942 | format_ip4_address, dst, |
| 1943 | format_vnet_sw_if_index_name, vnm, |
| 1944 | sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1947 | src = |
| 1948 | ip4_interface_address_matching_destination (im, dst, sw_if_index, &ia); |
| 1949 | if (!src) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1950 | { |
| 1951 | vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 1952 | return clib_error_return |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1953 | (0, |
| 1954 | "no matching interface address for destination %U (interface %U)", |
| 1955 | format_ip4_address, dst, format_vnet_sw_if_index_name, vnm, |
| 1956 | sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
Neale Ranns | 7a27274 | 2017-05-30 02:08:14 -0700 | [diff] [blame] | 1959 | h = vlib_packet_template_get_packet (vm, |
| 1960 | &im->ip4_arp_request_packet_template, |
| 1961 | &bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1962 | |
| 1963 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
Pavel Kotucek | 5780898 | 2017-08-02 08:20:19 +0200 | [diff] [blame] | 1964 | if (PREDICT_FALSE (!hi->hw_address)) |
| 1965 | { |
| 1966 | return clib_error_return (0, "%U: interface %U do not support ip probe", |
| 1967 | format_ip4_address, dst, |
| 1968 | format_vnet_sw_if_index_name, vnm, |
| 1969 | sw_if_index); |
| 1970 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1971 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1972 | clib_memcpy (h->ip4_over_ethernet[0].ethernet, hi->hw_address, |
| 1973 | sizeof (h->ip4_over_ethernet[0].ethernet)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1974 | |
| 1975 | h->ip4_over_ethernet[0].ip4 = src[0]; |
| 1976 | h->ip4_over_ethernet[1].ip4 = dst[0]; |
| 1977 | |
| 1978 | b = vlib_get_buffer (vm, bi); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 1979 | vnet_buffer (b)->sw_if_index[VLIB_RX] = |
| 1980 | vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1981 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1982 | ip46_address_t nh = { |
| 1983 | .ip4 = *dst, |
| 1984 | }; |
| 1985 | |
| 1986 | ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, |
| 1987 | VNET_LINK_IP4, &nh, sw_if_index); |
| 1988 | adj = adj_get (ai); |
| 1989 | |
| 1990 | /* Peer has been previously resolved, retrieve glean adj instead */ |
| 1991 | if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE) |
| 1992 | { |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame^] | 1993 | if (refresh) |
| 1994 | unicast_rewrite = 1; |
| 1995 | else |
| 1996 | { |
| 1997 | adj_unlock (ai); |
| 1998 | ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP4, |
| 1999 | VNET_LINK_IP4, sw_if_index, &nh); |
| 2000 | adj = adj_get (ai); |
| 2001 | } |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2004 | /* Add encapsulation string for software interface (e.g. ethernet header). */ |
| 2005 | vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t)); |
John Lo | 8637634 | 2018-06-11 20:14:49 -0400 | [diff] [blame^] | 2006 | if (unicast_rewrite) |
| 2007 | { |
| 2008 | u16 *etype = vlib_buffer_get_current (b) - 2; |
| 2009 | etype[0] = clib_host_to_net_u16 (ETHERNET_TYPE_ARP); |
| 2010 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2011 | vlib_buffer_advance (b, -adj->rewrite_header.data_bytes); |
| 2012 | |
| 2013 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2014 | vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index); |
| 2015 | u32 *to_next = vlib_frame_vector_args (f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2016 | to_next[0] = bi; |
| 2017 | f->n_vectors = 1; |
| 2018 | vlib_put_frame_to_node (vm, hi->output_node_index, f); |
| 2019 | } |
| 2020 | |
Neale Ranns | 7a27274 | 2017-05-30 02:08:14 -0700 | [diff] [blame] | 2021 | adj_unlock (ai); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2022 | return /* no error */ 0; |
| 2023 | } |
| 2024 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2025 | typedef enum |
| 2026 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2027 | IP4_REWRITE_NEXT_DROP, |
Chris Luke | 816f3e1 | 2016-06-14 16:24:47 -0400 | [diff] [blame] | 2028 | IP4_REWRITE_NEXT_ICMP_ERROR, |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 2029 | IP4_REWRITE_NEXT_FRAGMENT, |
| 2030 | IP4_REWRITE_N_NEXT /* Last */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2031 | } ip4_rewrite_next_t; |
| 2032 | |
Neale Ranns | 889fe94 | 2017-06-01 05:43:19 -0400 | [diff] [blame] | 2033 | /** |
| 2034 | * This bits of an IPv4 address to mask to construct a multicast |
| 2035 | * MAC address |
| 2036 | */ |
| 2037 | #if CLIB_ARCH_IS_BIG_ENDIAN |
| 2038 | #define IP4_MCAST_ADDR_MASK 0x007fffff |
| 2039 | #else |
| 2040 | #define IP4_MCAST_ADDR_MASK 0xffff7f00 |
| 2041 | #endif |
| 2042 | |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 2043 | always_inline void |
| 2044 | ip4_mtu_check (vlib_buffer_t * b, u16 packet_len, |
| 2045 | u16 adj_packet_bytes, bool df, u32 * next, u32 * error) |
| 2046 | { |
| 2047 | if (packet_len > adj_packet_bytes) |
| 2048 | { |
| 2049 | *error = IP4_ERROR_MTU_EXCEEDED; |
| 2050 | if (df) |
| 2051 | { |
| 2052 | icmp4_error_set_vnet_buffer |
| 2053 | (b, ICMP4_destination_unreachable, |
| 2054 | ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set, |
| 2055 | adj_packet_bytes); |
| 2056 | *next = IP4_REWRITE_NEXT_ICMP_ERROR; |
| 2057 | } |
| 2058 | else |
| 2059 | { |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 2060 | /* IP fragmentation */ |
| 2061 | ip_frag_set_vnet_buffer (b, 0, adj_packet_bytes, |
| 2062 | IP4_FRAG_NEXT_IP4_LOOKUP, 0); |
| 2063 | *next = IP4_REWRITE_NEXT_FRAGMENT; |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2068 | always_inline uword |
| 2069 | ip4_rewrite_inline (vlib_main_t * vm, |
| 2070 | vlib_node_runtime_t * node, |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2071 | vlib_frame_t * frame, |
| 2072 | int do_counters, int is_midchain, int is_mcast) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2073 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2074 | ip_lookup_main_t *lm = &ip4_main.lookup_main; |
| 2075 | u32 *from = vlib_frame_vector_args (frame); |
| 2076 | u32 n_left_from, n_left_to_next, *to_next, next_index; |
| 2077 | vlib_node_runtime_t *error_node = |
| 2078 | vlib_node_get_runtime (vm, ip4_input_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2079 | |
| 2080 | n_left_from = frame->n_vectors; |
| 2081 | next_index = node->cached_next_index; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2082 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2083 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2084 | while (n_left_from > 0) |
| 2085 | { |
| 2086 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 2087 | |
| 2088 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 2089 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2090 | ip_adjacency_t *adj0, *adj1; |
| 2091 | vlib_buffer_t *p0, *p1; |
| 2092 | ip4_header_t *ip0, *ip1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2093 | u32 pi0, rw_len0, next0, error0, checksum0, adj_index0; |
| 2094 | u32 pi1, rw_len1, next1, error1, checksum1, adj_index1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2095 | u32 tx_sw_if_index0, tx_sw_if_index1; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2096 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2097 | /* Prefetch next iteration. */ |
| 2098 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2099 | vlib_buffer_t *p2, *p3; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2100 | |
| 2101 | p2 = vlib_get_buffer (vm, from[2]); |
| 2102 | p3 = vlib_get_buffer (vm, from[3]); |
| 2103 | |
| 2104 | vlib_prefetch_buffer_header (p2, STORE); |
| 2105 | vlib_prefetch_buffer_header (p3, STORE); |
| 2106 | |
| 2107 | CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE); |
| 2108 | CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE); |
| 2109 | } |
| 2110 | |
| 2111 | pi0 = to_next[0] = from[0]; |
| 2112 | pi1 = to_next[1] = from[1]; |
| 2113 | |
| 2114 | from += 2; |
| 2115 | n_left_from -= 2; |
| 2116 | to_next += 2; |
| 2117 | n_left_to_next -= 2; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2119 | p0 = vlib_get_buffer (vm, pi0); |
| 2120 | p1 = vlib_get_buffer (vm, pi1); |
| 2121 | |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2122 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 2123 | adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2124 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2125 | /* |
| 2126 | * pre-fetch the per-adjacency counters |
| 2127 | */ |
| 2128 | if (do_counters) |
| 2129 | { |
| 2130 | vlib_prefetch_combined_counter (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2131 | thread_index, adj_index0); |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2132 | vlib_prefetch_combined_counter (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2133 | thread_index, adj_index1); |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2134 | } |
| 2135 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2136 | ip0 = vlib_buffer_get_current (p0); |
| 2137 | ip1 = vlib_buffer_get_current (p1); |
| 2138 | |
| 2139 | error0 = error1 = IP4_ERROR_NONE; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2140 | next0 = next1 = IP4_REWRITE_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2141 | |
| 2142 | /* Decrement TTL & update checksum. |
| 2143 | Works either endian, so no need for byte swap. */ |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2144 | if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2145 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2146 | i32 ttl0 = ip0->ttl; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2147 | |
| 2148 | /* Input node should have reject packets with ttl 0. */ |
| 2149 | ASSERT (ip0->ttl > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2150 | |
| 2151 | checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2152 | checksum0 += checksum0 >= 0xffff; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2153 | |
| 2154 | ip0->checksum = checksum0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2155 | ttl0 -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2156 | ip0->ttl = ttl0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2157 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2158 | /* |
| 2159 | * If the ttl drops below 1 when forwarding, generate |
| 2160 | * an ICMP response. |
| 2161 | */ |
| 2162 | if (PREDICT_FALSE (ttl0 <= 0)) |
| 2163 | { |
| 2164 | error0 = IP4_ERROR_TIME_EXPIRED; |
| 2165 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 2166 | icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded, |
| 2167 | ICMP4_time_exceeded_ttl_exceeded_in_transit, |
| 2168 | 0); |
| 2169 | next0 = IP4_REWRITE_NEXT_ICMP_ERROR; |
| 2170 | } |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2171 | |
| 2172 | /* Verify checksum. */ |
Dave Barach | 2c0a4f4 | 2017-06-29 09:30:15 -0400 | [diff] [blame] | 2173 | ASSERT ((ip0->checksum == ip4_header_checksum (ip0)) || |
Damjan Marion | fb3288f | 2017-07-19 15:07:10 +0200 | [diff] [blame] | 2174 | (p0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)); |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2175 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2176 | else |
| 2177 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2178 | p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2179 | } |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2180 | if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED))) |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2181 | { |
| 2182 | i32 ttl1 = ip1->ttl; |
| 2183 | |
| 2184 | /* Input node should have reject packets with ttl 0. */ |
| 2185 | ASSERT (ip1->ttl > 0); |
| 2186 | |
| 2187 | checksum1 = ip1->checksum + clib_host_to_net_u16 (0x0100); |
| 2188 | checksum1 += checksum1 >= 0xffff; |
| 2189 | |
| 2190 | ip1->checksum = checksum1; |
| 2191 | ttl1 -= 1; |
| 2192 | ip1->ttl = ttl1; |
| 2193 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2194 | /* |
| 2195 | * If the ttl drops below 1 when forwarding, generate |
| 2196 | * an ICMP response. |
| 2197 | */ |
| 2198 | if (PREDICT_FALSE (ttl1 <= 0)) |
| 2199 | { |
| 2200 | error1 = IP4_ERROR_TIME_EXPIRED; |
| 2201 | vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 2202 | icmp4_error_set_vnet_buffer (p1, ICMP4_time_exceeded, |
| 2203 | ICMP4_time_exceeded_ttl_exceeded_in_transit, |
| 2204 | 0); |
| 2205 | next1 = IP4_REWRITE_NEXT_ICMP_ERROR; |
| 2206 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2207 | |
| 2208 | /* Verify checksum. */ |
Dave Barach | 2c0a4f4 | 2017-06-29 09:30:15 -0400 | [diff] [blame] | 2209 | ASSERT ((ip1->checksum == ip4_header_checksum (ip1)) || |
Damjan Marion | fb3288f | 2017-07-19 15:07:10 +0200 | [diff] [blame] | 2210 | (p1->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2211 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2212 | else |
| 2213 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2214 | p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2215 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2216 | |
| 2217 | /* Rewrite packet header and updates lengths. */ |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 2218 | adj0 = adj_get (adj_index0); |
| 2219 | adj1 = adj_get (adj_index1); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2220 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2221 | /* Worth pipelining. No guarantee that adj0,1 are hot... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2222 | rw_len0 = adj0[0].rewrite_header.data_bytes; |
| 2223 | rw_len1 = adj1[0].rewrite_header.data_bytes; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2224 | vnet_buffer (p0)->ip.save_rewrite_length = rw_len0; |
| 2225 | vnet_buffer (p1)->ip.save_rewrite_length = rw_len1; |
Chris Luke | f2868fc | 2016-06-14 16:26:22 -0400 | [diff] [blame] | 2226 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2227 | /* Check MTU of outgoing interface. */ |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 2228 | ip4_mtu_check (p0, clib_net_to_host_u16 (ip0->length), |
| 2229 | adj0[0].rewrite_header.max_l3_packet_bytes, |
| 2230 | ip0->flags_and_fragment_offset & |
| 2231 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT), |
| 2232 | &next0, &error0); |
| 2233 | ip4_mtu_check (p1, clib_net_to_host_u16 (ip1->length), |
| 2234 | adj1[0].rewrite_header.max_l3_packet_bytes, |
| 2235 | ip1->flags_and_fragment_offset & |
| 2236 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT), |
| 2237 | &next1, &error1); |
Chris Luke | f2868fc | 2016-06-14 16:26:22 -0400 | [diff] [blame] | 2238 | |
Neale Ranns | cf3561b | 2017-12-13 01:44:25 -0800 | [diff] [blame] | 2239 | if (is_mcast) |
| 2240 | { |
| 2241 | error0 = ((adj0[0].rewrite_header.sw_if_index == |
| 2242 | vnet_buffer (p0)->sw_if_index[VLIB_RX]) ? |
| 2243 | IP4_ERROR_SAME_INTERFACE : error0); |
| 2244 | error1 = ((adj1[0].rewrite_header.sw_if_index == |
| 2245 | vnet_buffer (p1)->sw_if_index[VLIB_RX]) ? |
| 2246 | IP4_ERROR_SAME_INTERFACE : error1); |
| 2247 | } |
| 2248 | |
Chun Li | ef56fae | 2018-02-07 09:58:28 +0800 | [diff] [blame] | 2249 | p0->error = error_node->errors[error0]; |
| 2250 | p1->error = error_node->errors[error1]; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2251 | /* Don't adjust the buffer for ttl issue; icmp-error node wants |
| 2252 | * to see the IP headerr */ |
| 2253 | if (PREDICT_TRUE (error0 == IP4_ERROR_NONE)) |
| 2254 | { |
Damjan Marion | 892e076 | 2016-12-09 18:52:05 +0100 | [diff] [blame] | 2255 | next0 = adj0[0].rewrite_header.next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2256 | p0->current_data -= rw_len0; |
| 2257 | p0->current_length += rw_len0; |
| 2258 | tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index; |
| 2259 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 2260 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 2261 | if (PREDICT_FALSE |
| 2262 | (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 2263 | vnet_feature_arc_start (lm->output_feature_arc_index, |
| 2264 | tx_sw_if_index0, &next0, p0); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2265 | } |
| 2266 | if (PREDICT_TRUE (error1 == IP4_ERROR_NONE)) |
| 2267 | { |
Damjan Marion | 892e076 | 2016-12-09 18:52:05 +0100 | [diff] [blame] | 2268 | next1 = adj1[0].rewrite_header.next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2269 | p1->current_data -= rw_len1; |
| 2270 | p1->current_length += rw_len1; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 2271 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2272 | tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index; |
| 2273 | vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 2274 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 2275 | if (PREDICT_FALSE |
| 2276 | (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 2277 | vnet_feature_arc_start (lm->output_feature_arc_index, |
| 2278 | tx_sw_if_index1, &next1, p1); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2279 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2280 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 2281 | /* Guess we are only writing on simple Ethernet header. */ |
| 2282 | vnet_rewrite_two_headers (adj0[0], adj1[0], |
| 2283 | ip0, ip1, sizeof (ethernet_header_t)); |
| 2284 | |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 2285 | /* |
| 2286 | * Bump the per-adjacency counters |
| 2287 | */ |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2288 | if (do_counters) |
| 2289 | { |
| 2290 | vlib_increment_combined_counter |
| 2291 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2292 | thread_index, |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2293 | adj_index0, 1, |
| 2294 | vlib_buffer_length_in_chain (vm, p0) + rw_len0); |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 2295 | |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2296 | vlib_increment_combined_counter |
| 2297 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2298 | thread_index, |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2299 | adj_index1, 1, |
| 2300 | vlib_buffer_length_in_chain (vm, p1) + rw_len1); |
| 2301 | } |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 2302 | |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 2303 | if (is_midchain) |
| 2304 | { |
| 2305 | adj0->sub_type.midchain.fixup_func |
| 2306 | (vm, adj0, p0, adj0->sub_type.midchain.fixup_data); |
| 2307 | adj1->sub_type.midchain.fixup_func |
| 2308 | (vm, adj1, p1, adj0->sub_type.midchain.fixup_data); |
| 2309 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2310 | if (is_mcast) |
| 2311 | { |
| 2312 | /* |
| 2313 | * copy bytes from the IP address into the MAC rewrite |
| 2314 | */ |
Neale Ranns | 889fe94 | 2017-06-01 05:43:19 -0400 | [diff] [blame] | 2315 | vnet_ip_mcast_fixup_header (IP4_MCAST_ADDR_MASK, |
| 2316 | adj0-> |
| 2317 | rewrite_header.dst_mcast_offset, |
| 2318 | &ip0->dst_address.as_u32, |
| 2319 | (u8 *) ip0); |
| 2320 | vnet_ip_mcast_fixup_header (IP4_MCAST_ADDR_MASK, |
| 2321 | adj0-> |
| 2322 | rewrite_header.dst_mcast_offset, |
| 2323 | &ip1->dst_address.as_u32, |
| 2324 | (u8 *) ip1); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2325 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2326 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2327 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 2328 | to_next, n_left_to_next, |
| 2329 | pi0, pi1, next0, next1); |
| 2330 | } |
| 2331 | |
| 2332 | while (n_left_from > 0 && n_left_to_next > 0) |
| 2333 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2334 | ip_adjacency_t *adj0; |
| 2335 | vlib_buffer_t *p0; |
| 2336 | ip4_header_t *ip0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2337 | u32 pi0, rw_len0, adj_index0, next0, error0, checksum0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2338 | u32 tx_sw_if_index0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2339 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2340 | pi0 = to_next[0] = from[0]; |
| 2341 | |
| 2342 | p0 = vlib_get_buffer (vm, pi0); |
| 2343 | |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2344 | adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2345 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 2346 | adj0 = adj_get (adj_index0); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2347 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2348 | ip0 = vlib_buffer_get_current (p0); |
| 2349 | |
| 2350 | error0 = IP4_ERROR_NONE; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2351 | next0 = IP4_REWRITE_NEXT_DROP; /* drop on error */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2352 | |
| 2353 | /* Decrement TTL & update checksum. */ |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2354 | if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2355 | { |
| 2356 | i32 ttl0 = ip0->ttl; |
| 2357 | |
| 2358 | checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100); |
| 2359 | |
| 2360 | checksum0 += checksum0 >= 0xffff; |
| 2361 | |
| 2362 | ip0->checksum = checksum0; |
| 2363 | |
| 2364 | ASSERT (ip0->ttl > 0); |
| 2365 | |
| 2366 | ttl0 -= 1; |
| 2367 | |
| 2368 | ip0->ttl = ttl0; |
| 2369 | |
Dave Barach | 2c0a4f4 | 2017-06-29 09:30:15 -0400 | [diff] [blame] | 2370 | ASSERT ((ip0->checksum == ip4_header_checksum (ip0)) || |
Damjan Marion | fb3288f | 2017-07-19 15:07:10 +0200 | [diff] [blame] | 2371 | (p0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2372 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2373 | if (PREDICT_FALSE (ttl0 <= 0)) |
| 2374 | { |
| 2375 | /* |
| 2376 | * If the ttl drops below 1 when forwarding, generate |
| 2377 | * an ICMP response. |
| 2378 | */ |
| 2379 | error0 = IP4_ERROR_TIME_EXPIRED; |
| 2380 | next0 = IP4_REWRITE_NEXT_ICMP_ERROR; |
| 2381 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 2382 | icmp4_error_set_vnet_buffer (p0, ICMP4_time_exceeded, |
| 2383 | ICMP4_time_exceeded_ttl_exceeded_in_transit, |
| 2384 | 0); |
| 2385 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2386 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2387 | else |
| 2388 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 2389 | p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2390 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2391 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2392 | if (do_counters) |
| 2393 | vlib_prefetch_combined_counter (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2394 | thread_index, adj_index0); |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 2395 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2396 | /* Guess we are only writing on simple Ethernet header. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2397 | vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t)); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2398 | if (is_mcast) |
| 2399 | { |
| 2400 | /* |
| 2401 | * copy bytes from the IP address into the MAC rewrite |
| 2402 | */ |
Neale Ranns | 889fe94 | 2017-06-01 05:43:19 -0400 | [diff] [blame] | 2403 | vnet_ip_mcast_fixup_header (IP4_MCAST_ADDR_MASK, |
| 2404 | adj0-> |
| 2405 | rewrite_header.dst_mcast_offset, |
| 2406 | &ip0->dst_address.as_u32, |
| 2407 | (u8 *) ip0); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2408 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2409 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2410 | /* Update packet buffer attributes/set output interface. */ |
| 2411 | rw_len0 = adj0[0].rewrite_header.data_bytes; |
| 2412 | vnet_buffer (p0)->ip.save_rewrite_length = rw_len0; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2413 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2414 | if (do_counters) |
| 2415 | vlib_increment_combined_counter |
| 2416 | (&adjacency_counters, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 2417 | thread_index, adj_index0, 1, |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 2418 | vlib_buffer_length_in_chain (vm, p0) + rw_len0); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2419 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2420 | /* Check MTU of outgoing interface. */ |
Ole Troan | 8a9c8f1 | 2018-05-18 11:01:31 +0200 | [diff] [blame] | 2421 | ip4_mtu_check (p0, clib_net_to_host_u16 (ip0->length), |
| 2422 | adj0[0].rewrite_header.max_l3_packet_bytes, |
| 2423 | ip0->flags_and_fragment_offset & |
| 2424 | clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT), |
| 2425 | &next0, &error0); |
| 2426 | |
Neale Ranns | cf3561b | 2017-12-13 01:44:25 -0800 | [diff] [blame] | 2427 | if (is_mcast) |
| 2428 | { |
| 2429 | error0 = ((adj0[0].rewrite_header.sw_if_index == |
| 2430 | vnet_buffer (p0)->sw_if_index[VLIB_RX]) ? |
| 2431 | IP4_ERROR_SAME_INTERFACE : error0); |
| 2432 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2433 | p0->error = error_node->errors[error0]; |
Chris Luke | 816f3e1 | 2016-06-14 16:24:47 -0400 | [diff] [blame] | 2434 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2435 | /* Don't adjust the buffer for ttl issue; icmp-error node wants |
| 2436 | * to see the IP headerr */ |
| 2437 | if (PREDICT_TRUE (error0 == IP4_ERROR_NONE)) |
| 2438 | { |
| 2439 | p0->current_data -= rw_len0; |
| 2440 | p0->current_length += rw_len0; |
| 2441 | tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index; |
Chris Luke | 816f3e1 | 2016-06-14 16:24:47 -0400 | [diff] [blame] | 2442 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2443 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0; |
| 2444 | next0 = adj0[0].rewrite_header.next_index; |
Dave Barach | 5331c72 | 2016-08-17 11:54:30 -0400 | [diff] [blame] | 2445 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 2446 | if (is_midchain) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2447 | { |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 2448 | adj0->sub_type.midchain.fixup_func |
| 2449 | (vm, adj0, p0, adj0->sub_type.midchain.fixup_data); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 2452 | if (PREDICT_FALSE |
| 2453 | (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)) |
| 2454 | vnet_feature_arc_start (lm->output_feature_arc_index, |
| 2455 | tx_sw_if_index0, &next0, p0); |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 2456 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2457 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2458 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2459 | from += 1; |
| 2460 | n_left_from -= 1; |
| 2461 | to_next += 1; |
| 2462 | n_left_to_next -= 1; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2463 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2464 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 2465 | to_next, n_left_to_next, |
| 2466 | pi0, next0); |
| 2467 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2468 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2469 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 2470 | } |
| 2471 | |
| 2472 | /* Need to do trace after rewrites to pick up new packet data. */ |
| 2473 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2474 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2475 | |
| 2476 | return frame->n_vectors; |
| 2477 | } |
| 2478 | |
Dave Barach | 132d51d | 2016-07-07 10:10:17 -0400 | [diff] [blame] | 2479 | |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2480 | /** @brief IPv4 rewrite node. |
| 2481 | @node ip4-rewrite |
Dave Barach | 132d51d | 2016-07-07 10:10:17 -0400 | [diff] [blame] | 2482 | |
| 2483 | This is the IPv4 transit-rewrite node: decrement TTL, fix the ipv4 |
| 2484 | header checksum, fetch the ip adjacency, check the outbound mtu, |
| 2485 | apply the adjacency rewrite, and send pkts to the adjacency |
| 2486 | rewrite header's rewrite_next_index. |
| 2487 | |
| 2488 | @param vm vlib_main_t corresponding to the current thread |
| 2489 | @param node vlib_node_runtime_t |
| 2490 | @param frame vlib_frame_t whose contents should be dispatched |
| 2491 | |
| 2492 | @par Graph mechanics: buffer metadata, next index usage |
| 2493 | |
| 2494 | @em Uses: |
| 2495 | - <code>vnet_buffer(b)->ip.adj_index[VLIB_TX]</code> |
| 2496 | - the rewrite adjacency index |
| 2497 | - <code>adj->lookup_next_index</code> |
| 2498 | - Must be IP_LOOKUP_NEXT_REWRITE or IP_LOOKUP_NEXT_ARP, otherwise |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2499 | the packet will be dropped. |
Dave Barach | 132d51d | 2016-07-07 10:10:17 -0400 | [diff] [blame] | 2500 | - <code>adj->rewrite_header</code> |
| 2501 | - Rewrite string length, rewrite string, next_index |
| 2502 | |
| 2503 | @em Sets: |
| 2504 | - <code>b->current_data, b->current_length</code> |
| 2505 | - Updated net of applying the rewrite string |
| 2506 | |
| 2507 | <em>Next Indices:</em> |
| 2508 | - <code> adj->rewrite_header.next_index </code> |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 2509 | or @c ip4-drop |
Dave Barach | 132d51d | 2016-07-07 10:10:17 -0400 | [diff] [blame] | 2510 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2511 | static uword |
Neale Ranns | f06aea5 | 2016-11-29 06:51:37 -0800 | [diff] [blame] | 2512 | ip4_rewrite (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2513 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2514 | { |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2515 | if (adj_are_counters_enabled ()) |
| 2516 | return ip4_rewrite_inline (vm, node, frame, 1, 0, 0); |
| 2517 | else |
| 2518 | return ip4_rewrite_inline (vm, node, frame, 0, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2521 | static uword |
| 2522 | ip4_midchain (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2523 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2524 | { |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2525 | if (adj_are_counters_enabled ()) |
| 2526 | return ip4_rewrite_inline (vm, node, frame, 1, 1, 0); |
| 2527 | else |
| 2528 | return ip4_rewrite_inline (vm, node, frame, 0, 1, 0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2529 | } |
| 2530 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2531 | static uword |
| 2532 | ip4_rewrite_mcast (vlib_main_t * vm, |
| 2533 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2534 | { |
Neale Ranns | 9c6a613 | 2017-02-21 05:33:14 -0800 | [diff] [blame] | 2535 | if (adj_are_counters_enabled ()) |
| 2536 | return ip4_rewrite_inline (vm, node, frame, 1, 0, 1); |
| 2537 | else |
| 2538 | return ip4_rewrite_inline (vm, node, frame, 0, 0, 1); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2539 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2540 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 2541 | static uword |
| 2542 | ip4_mcast_midchain (vlib_main_t * vm, |
| 2543 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 2544 | { |
| 2545 | if (adj_are_counters_enabled ()) |
| 2546 | return ip4_rewrite_inline (vm, node, frame, 1, 1, 1); |
| 2547 | else |
| 2548 | return ip4_rewrite_inline (vm, node, frame, 0, 1, 1); |
| 2549 | } |
| 2550 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2551 | /* *INDENT-OFF* */ |
| 2552 | VLIB_REGISTER_NODE (ip4_rewrite_node) = { |
| 2553 | .function = ip4_rewrite, |
| 2554 | .name = "ip4-rewrite", |
| 2555 | .vector_size = sizeof (u32), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2556 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2557 | .format_trace = format_ip4_rewrite_trace, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2558 | |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 2559 | .n_next_nodes = IP4_REWRITE_N_NEXT, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2560 | .next_nodes = { |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 2561 | [IP4_REWRITE_NEXT_DROP] = "ip4-drop", |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2562 | [IP4_REWRITE_NEXT_ICMP_ERROR] = "ip4-icmp-error", |
Ole Troan | 313f7e2 | 2018-04-10 16:02:51 +0200 | [diff] [blame] | 2563 | [IP4_REWRITE_NEXT_FRAGMENT] = "ip4-frag", |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2564 | }, |
| 2565 | }; |
| 2566 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_node, ip4_rewrite) |
| 2567 | |
| 2568 | VLIB_REGISTER_NODE (ip4_rewrite_mcast_node) = { |
| 2569 | .function = ip4_rewrite_mcast, |
| 2570 | .name = "ip4-rewrite-mcast", |
| 2571 | .vector_size = sizeof (u32), |
| 2572 | |
| 2573 | .format_trace = format_ip4_rewrite_trace, |
| 2574 | .sibling_of = "ip4-rewrite", |
| 2575 | }; |
| 2576 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_rewrite_mcast_node, ip4_rewrite_mcast) |
| 2577 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 2578 | VLIB_REGISTER_NODE (ip4_mcast_midchain_node, static) = { |
| 2579 | .function = ip4_mcast_midchain, |
| 2580 | .name = "ip4-mcast-midchain", |
| 2581 | .vector_size = sizeof (u32), |
| 2582 | |
| 2583 | .format_trace = format_ip4_rewrite_trace, |
| 2584 | .sibling_of = "ip4-rewrite", |
| 2585 | }; |
| 2586 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_mcast_midchain_node, ip4_mcast_midchain) |
| 2587 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2588 | VLIB_REGISTER_NODE (ip4_midchain_node) = { |
| 2589 | .function = ip4_midchain, |
| 2590 | .name = "ip4-midchain", |
| 2591 | .vector_size = sizeof (u32), |
| 2592 | .format_trace = format_ip4_forward_next_trace, |
| 2593 | .sibling_of = "ip4-rewrite", |
| 2594 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2595 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_midchain_node, ip4_midchain); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 2596 | /* *INDENT-ON */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 2597 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2598 | int |
| 2599 | ip4_lookup_validate (ip4_address_t * a, u32 fib_index0) |
| 2600 | { |
| 2601 | ip4_fib_mtrie_t *mtrie0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2602 | ip4_fib_mtrie_leaf_t leaf0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2603 | u32 lbi0; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2604 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2605 | mtrie0 = &ip4_fib_get (fib_index0)->mtrie; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2606 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 2607 | leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2608 | leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 2); |
| 2609 | leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, a, 3); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2610 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 2611 | lbi0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2612 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2613 | return lbi0 == ip4_fib_table_lookup_lb (ip4_fib_get (fib_index0), a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2614 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2615 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2616 | static clib_error_t * |
| 2617 | test_lookup_command_fn (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2618 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2619 | { |
Billy McFall | 309fe06 | 2016-10-14 07:37:33 -0400 | [diff] [blame] | 2620 | ip4_fib_t *fib; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2621 | u32 table_id = 0; |
| 2622 | f64 count = 1; |
| 2623 | u32 n; |
| 2624 | int i; |
| 2625 | ip4_address_t ip4_base_address; |
| 2626 | u64 errors = 0; |
| 2627 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2628 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2629 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2630 | if (unformat (input, "table %d", &table_id)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2631 | { |
| 2632 | /* Make sure the entry exists. */ |
| 2633 | fib = ip4_fib_get (table_id); |
| 2634 | if ((fib) && (fib->index != table_id)) |
| 2635 | return clib_error_return (0, "<fib-index> %d does not exist", |
| 2636 | table_id); |
| 2637 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2638 | else if (unformat (input, "count %f", &count)) |
| 2639 | ; |
| 2640 | |
| 2641 | else if (unformat (input, "%U", |
| 2642 | unformat_ip4_address, &ip4_base_address)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2643 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2644 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2645 | return clib_error_return (0, "unknown input `%U'", |
| 2646 | format_unformat_error, input); |
| 2647 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2648 | |
| 2649 | n = count; |
| 2650 | |
| 2651 | for (i = 0; i < n; i++) |
| 2652 | { |
| 2653 | if (!ip4_lookup_validate (&ip4_base_address, table_id)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2654 | errors++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2655 | |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2656 | ip4_base_address.as_u32 = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2657 | clib_host_to_net_u32 (1 + |
| 2658 | clib_net_to_host_u32 (ip4_base_address.as_u32)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2661 | if (errors) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2662 | vlib_cli_output (vm, "%llu errors out of %d lookups\n", errors, n); |
| 2663 | else |
| 2664 | vlib_cli_output (vm, "No errors in %d lookups\n", n); |
| 2665 | |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2669 | /*? |
| 2670 | * Perform a lookup of an IPv4 Address (or range of addresses) in the |
| 2671 | * given FIB table to determine if there is a conflict with the |
| 2672 | * adjacency table. The fib-id can be determined by using the |
| 2673 | * '<em>show ip fib</em>' command. If fib-id is not entered, default value |
| 2674 | * of 0 is used. |
| 2675 | * |
| 2676 | * @todo This command uses fib-id, other commands use table-id (not |
| 2677 | * just a name, they are different indexes). Would like to change this |
| 2678 | * to table-id for consistency. |
| 2679 | * |
| 2680 | * @cliexpar |
| 2681 | * Example of how to run the test lookup command: |
| 2682 | * @cliexstart{test lookup 172.16.1.1 table 1 count 2} |
| 2683 | * No errors in 2 lookups |
| 2684 | * @cliexend |
| 2685 | ?*/ |
| 2686 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2687 | VLIB_CLI_COMMAND (lookup_test_command, static) = |
| 2688 | { |
| 2689 | .path = "test lookup", |
| 2690 | .short_help = "test lookup <ipv4-addr> [table <fib-id>] [count <nn>]", |
| 2691 | .function = test_lookup_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2692 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2693 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2694 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2695 | int |
| 2696 | vnet_set_ip4_flow_hash (u32 table_id, u32 flow_hash_config) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2697 | { |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 2698 | u32 fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2699 | |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 2700 | fib_index = fib_table_find (FIB_PROTOCOL_IP4, table_id); |
| 2701 | |
| 2702 | if (~0 == fib_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2703 | return VNET_API_ERROR_NO_SUCH_FIB; |
| 2704 | |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 2705 | fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP4, |
| 2706 | flow_hash_config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2707 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2710 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2711 | static clib_error_t * |
| 2712 | set_ip_flow_hash_command_fn (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2713 | unformat_input_t * input, |
| 2714 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2715 | { |
| 2716 | int matched = 0; |
| 2717 | u32 table_id = 0; |
| 2718 | u32 flow_hash_config = 0; |
| 2719 | int rv; |
| 2720 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2721 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2722 | { |
| 2723 | if (unformat (input, "table %d", &table_id)) |
| 2724 | matched = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2725 | #define _(a,v) \ |
| 2726 | else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;} |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2727 | foreach_flow_hash_bit |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2728 | #undef _ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2729 | else |
| 2730 | break; |
| 2731 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2732 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2733 | if (matched == 0) |
| 2734 | return clib_error_return (0, "unknown input `%U'", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2735 | format_unformat_error, input); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2736 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2737 | rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config); |
| 2738 | switch (rv) |
| 2739 | { |
| 2740 | case 0: |
| 2741 | break; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2742 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2743 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 2744 | return clib_error_return (0, "no such FIB table %d", table_id); |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2745 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2746 | default: |
| 2747 | clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config); |
| 2748 | break; |
| 2749 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2750 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2751 | return 0; |
| 2752 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2753 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2754 | /*? |
| 2755 | * Configure the set of IPv4 fields used by the flow hash. |
| 2756 | * |
| 2757 | * @cliexpar |
| 2758 | * Example of how to set the flow hash on a given table: |
| 2759 | * @cliexcmd{set ip flow-hash table 7 dst sport dport proto} |
| 2760 | * Example of display the configured flow hash: |
| 2761 | * @cliexstart{show ip fib} |
Billy McFall | ebb9a6a | 2016-10-17 11:35:32 -0400 | [diff] [blame] | 2762 | * ipv4-VRF:0, fib_index 0, flow hash: src dst sport dport proto |
| 2763 | * 0.0.0.0/0 |
| 2764 | * unicast-ip4-chain |
| 2765 | * [@0]: dpo-load-balance: [index:0 buckets:1 uRPF:0 to:[0:0]] |
| 2766 | * [0] [@0]: dpo-drop ip6 |
| 2767 | * 0.0.0.0/32 |
| 2768 | * unicast-ip4-chain |
| 2769 | * [@0]: dpo-load-balance: [index:1 buckets:1 uRPF:1 to:[0:0]] |
| 2770 | * [0] [@0]: dpo-drop ip6 |
| 2771 | * 224.0.0.0/8 |
| 2772 | * unicast-ip4-chain |
| 2773 | * [@0]: dpo-load-balance: [index:3 buckets:1 uRPF:3 to:[0:0]] |
| 2774 | * [0] [@0]: dpo-drop ip6 |
| 2775 | * 6.0.1.2/32 |
| 2776 | * unicast-ip4-chain |
| 2777 | * [@0]: dpo-load-balance: [index:30 buckets:1 uRPF:29 to:[0:0]] |
| 2778 | * [0] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 2779 | * 7.0.0.1/32 |
| 2780 | * unicast-ip4-chain |
| 2781 | * [@0]: dpo-load-balance: [index:31 buckets:4 uRPF:30 to:[0:0]] |
| 2782 | * [0] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 2783 | * [1] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 2784 | * [2] [@3]: arp-ipv4: via 6.0.0.2 af_packet0 |
| 2785 | * [3] [@3]: arp-ipv4: via 6.0.0.1 af_packet0 |
| 2786 | * 240.0.0.0/8 |
| 2787 | * unicast-ip4-chain |
| 2788 | * [@0]: dpo-load-balance: [index:2 buckets:1 uRPF:2 to:[0:0]] |
| 2789 | * [0] [@0]: dpo-drop ip6 |
| 2790 | * 255.255.255.255/32 |
| 2791 | * unicast-ip4-chain |
| 2792 | * [@0]: dpo-load-balance: [index:4 buckets:1 uRPF:4 to:[0:0]] |
| 2793 | * [0] [@0]: dpo-drop ip6 |
| 2794 | * ipv4-VRF:7, fib_index 1, flow hash: dst sport dport proto |
| 2795 | * 0.0.0.0/0 |
| 2796 | * unicast-ip4-chain |
| 2797 | * [@0]: dpo-load-balance: [index:12 buckets:1 uRPF:11 to:[0:0]] |
| 2798 | * [0] [@0]: dpo-drop ip6 |
| 2799 | * 0.0.0.0/32 |
| 2800 | * unicast-ip4-chain |
| 2801 | * [@0]: dpo-load-balance: [index:13 buckets:1 uRPF:12 to:[0:0]] |
| 2802 | * [0] [@0]: dpo-drop ip6 |
| 2803 | * 172.16.1.0/24 |
| 2804 | * unicast-ip4-chain |
| 2805 | * [@0]: dpo-load-balance: [index:17 buckets:1 uRPF:16 to:[0:0]] |
| 2806 | * [0] [@4]: ipv4-glean: af_packet0 |
| 2807 | * 172.16.1.1/32 |
| 2808 | * unicast-ip4-chain |
| 2809 | * [@0]: dpo-load-balance: [index:18 buckets:1 uRPF:17 to:[1:84]] |
| 2810 | * [0] [@2]: dpo-receive: 172.16.1.1 on af_packet0 |
| 2811 | * 172.16.1.2/32 |
| 2812 | * unicast-ip4-chain |
| 2813 | * [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]] |
| 2814 | * [0] [@5]: ipv4 via 172.16.1.2 af_packet0: IP4: 02:fe:9e:70:7a:2b -> 26:a5:f6:9c:3a:36 |
| 2815 | * 172.16.2.0/24 |
| 2816 | * unicast-ip4-chain |
| 2817 | * [@0]: dpo-load-balance: [index:19 buckets:1 uRPF:18 to:[0:0]] |
| 2818 | * [0] [@4]: ipv4-glean: af_packet1 |
| 2819 | * 172.16.2.1/32 |
| 2820 | * unicast-ip4-chain |
| 2821 | * [@0]: dpo-load-balance: [index:20 buckets:1 uRPF:19 to:[0:0]] |
| 2822 | * [0] [@2]: dpo-receive: 172.16.2.1 on af_packet1 |
| 2823 | * 224.0.0.0/8 |
| 2824 | * unicast-ip4-chain |
| 2825 | * [@0]: dpo-load-balance: [index:15 buckets:1 uRPF:14 to:[0:0]] |
| 2826 | * [0] [@0]: dpo-drop ip6 |
| 2827 | * 240.0.0.0/8 |
| 2828 | * unicast-ip4-chain |
| 2829 | * [@0]: dpo-load-balance: [index:14 buckets:1 uRPF:13 to:[0:0]] |
| 2830 | * [0] [@0]: dpo-drop ip6 |
| 2831 | * 255.255.255.255/32 |
| 2832 | * unicast-ip4-chain |
| 2833 | * [@0]: dpo-load-balance: [index:16 buckets:1 uRPF:15 to:[0:0]] |
| 2834 | * [0] [@0]: dpo-drop ip6 |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2835 | * @cliexend |
| 2836 | ?*/ |
| 2837 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2838 | VLIB_CLI_COMMAND (set_ip_flow_hash_command, static) = |
| 2839 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2840 | .path = "set ip flow-hash", |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2841 | .short_help = |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2842 | "set ip flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2843 | .function = set_ip_flow_hash_command_fn, |
| 2844 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2845 | /* *INDENT-ON* */ |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2846 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2847 | int |
| 2848 | vnet_set_ip4_classify_intfc (vlib_main_t * vm, u32 sw_if_index, |
| 2849 | u32 table_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2850 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2851 | vnet_main_t *vnm = vnet_get_main (); |
| 2852 | vnet_interface_main_t *im = &vnm->interface_main; |
| 2853 | ip4_main_t *ipm = &ip4_main; |
| 2854 | ip_lookup_main_t *lm = &ipm->lookup_main; |
| 2855 | vnet_classify_main_t *cm = &vnet_classify_main; |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2856 | ip4_address_t *if_addr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2857 | |
| 2858 | if (pool_is_free_index (im->sw_interfaces, sw_if_index)) |
| 2859 | return VNET_API_ERROR_NO_MATCHING_INTERFACE; |
| 2860 | |
| 2861 | if (table_index != ~0 && pool_is_free_index (cm->tables, table_index)) |
| 2862 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 2863 | |
| 2864 | vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2865 | lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2866 | |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2867 | if_addr = ip4_interface_first_address (ipm, sw_if_index, NULL); |
| 2868 | |
| 2869 | if (NULL != if_addr) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2870 | { |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2871 | fib_prefix_t pfx = { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2872 | .fp_len = 32, |
| 2873 | .fp_proto = FIB_PROTOCOL_IP4, |
| 2874 | .fp_addr.ip4 = *if_addr, |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2875 | }; |
| 2876 | u32 fib_index; |
| 2877 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2878 | fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 2879 | sw_if_index); |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2880 | |
| 2881 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2882 | if (table_index != (u32) ~ 0) |
| 2883 | { |
| 2884 | dpo_id_t dpo = DPO_INVALID; |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2885 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2886 | dpo_set (&dpo, |
| 2887 | DPO_CLASSIFY, |
| 2888 | DPO_PROTO_IP4, |
| 2889 | classify_dpo_create (DPO_PROTO_IP4, table_index)); |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2890 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2891 | fib_table_entry_special_dpo_add (fib_index, |
| 2892 | &pfx, |
| 2893 | FIB_SOURCE_CLASSIFY, |
| 2894 | FIB_ENTRY_FLAG_NONE, &dpo); |
| 2895 | dpo_reset (&dpo); |
| 2896 | } |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2897 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2898 | { |
| 2899 | fib_table_entry_special_remove (fib_index, |
| 2900 | &pfx, FIB_SOURCE_CLASSIFY); |
| 2901 | } |
| 2902 | } |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 2903 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2904 | return 0; |
| 2905 | } |
| 2906 | |
| 2907 | static clib_error_t * |
| 2908 | set_ip_classify_command_fn (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2909 | unformat_input_t * input, |
| 2910 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2911 | { |
| 2912 | u32 table_index = ~0; |
| 2913 | int table_index_set = 0; |
| 2914 | u32 sw_if_index = ~0; |
| 2915 | int rv; |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2916 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2917 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2918 | { |
| 2919 | if (unformat (input, "table-index %d", &table_index)) |
| 2920 | table_index_set = 1; |
| 2921 | else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, |
| 2922 | vnet_get_main (), &sw_if_index)) |
| 2923 | ; |
| 2924 | else |
| 2925 | break; |
| 2926 | } |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2927 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2928 | if (table_index_set == 0) |
| 2929 | return clib_error_return (0, "classify table-index must be specified"); |
| 2930 | |
| 2931 | if (sw_if_index == ~0) |
| 2932 | return clib_error_return (0, "interface / subif must be specified"); |
| 2933 | |
| 2934 | rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index); |
| 2935 | |
| 2936 | switch (rv) |
| 2937 | { |
| 2938 | case 0: |
| 2939 | break; |
| 2940 | |
| 2941 | case VNET_API_ERROR_NO_MATCHING_INTERFACE: |
| 2942 | return clib_error_return (0, "No such interface"); |
| 2943 | |
| 2944 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 2945 | return clib_error_return (0, "No such classifier table"); |
| 2946 | } |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2950 | /*? |
| 2951 | * Assign a classification table to an interface. The classification |
| 2952 | * table is created using the '<em>classify table</em>' and '<em>classify session</em>' |
| 2953 | * commands. Once the table is create, use this command to filter packets |
| 2954 | * on an interface. |
| 2955 | * |
| 2956 | * @cliexpar |
| 2957 | * Example of how to assign a classification table to an interface: |
| 2958 | * @cliexcmd{set ip classify intfc GigabitEthernet2/0/0 table-index 1} |
| 2959 | ?*/ |
| 2960 | /* *INDENT-OFF* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2961 | VLIB_CLI_COMMAND (set_ip_classify_command, static) = |
| 2962 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2963 | .path = "set ip classify", |
Dave Barach | 75fc854 | 2016-10-11 16:16:02 -0400 | [diff] [blame] | 2964 | .short_help = |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2965 | "set ip classify intfc <interface> table-index <classify-idx>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2966 | .function = set_ip_classify_command_fn, |
| 2967 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 2968 | /* *INDENT-ON* */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2969 | |
Neale Ranns | 1ec3652 | 2017-11-29 05:20:37 -0800 | [diff] [blame] | 2970 | static clib_error_t * |
| 2971 | ip4_config (vlib_main_t * vm, unformat_input_t * input) |
| 2972 | { |
| 2973 | ip4_main_t *im = &ip4_main; |
| 2974 | uword heapsize = 0; |
| 2975 | |
| 2976 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 2977 | { |
| 2978 | if (unformat (input, "heap-size %U", unformat_memory_size, &heapsize)) |
| 2979 | ; |
| 2980 | else |
| 2981 | return clib_error_return (0, |
| 2982 | "invalid heap-size parameter `%U'", |
| 2983 | format_unformat_error, input); |
| 2984 | } |
| 2985 | |
| 2986 | im->mtrie_heap_size = heapsize; |
| 2987 | |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
| 2991 | VLIB_EARLY_CONFIG_FUNCTION (ip4_config, "ip"); |
| 2992 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 2993 | /* |
| 2994 | * fd.io coding-style-patch-verification: ON |
| 2995 | * |
| 2996 | * Local Variables: |
| 2997 | * eval: (c-set-style "gnu") |
| 2998 | * End: |
| 2999 | */ |