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