Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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/ip6_forward.h: IP v6 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 | #ifndef __included_ip6_forward_h__ |
| 41 | #define __included_ip6_forward_h__ |
| 42 | |
| 43 | #include <vnet/fib/ip6_fib.h> |
| 44 | #include <vnet/dpo/load_balance_map.h> |
| 45 | |
| 46 | /** |
| 47 | * @file |
| 48 | * @brief IPv6 Forwarding. |
| 49 | * |
| 50 | * This file contains the source code for IPv6 forwarding. |
| 51 | */ |
| 52 | |
| 53 | |
| 54 | always_inline uword |
| 55 | ip6_lookup_inline (vlib_main_t * vm, |
| 56 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 57 | { |
| 58 | ip6_main_t *im = &ip6_main; |
| 59 | vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters; |
| 60 | u32 n_left_from, n_left_to_next, *from, *to_next; |
| 61 | ip_lookup_next_t next; |
Damjan Marion | 067cd62 | 2018-07-11 12:47:43 +0200 | [diff] [blame] | 62 | u32 thread_index = vm->thread_index; |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 63 | |
| 64 | from = vlib_frame_vector_args (frame); |
| 65 | n_left_from = frame->n_vectors; |
| 66 | next = node->cached_next_index; |
| 67 | |
| 68 | while (n_left_from > 0) |
| 69 | { |
| 70 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
| 71 | |
| 72 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 73 | { |
| 74 | vlib_buffer_t *p0, *p1; |
| 75 | u32 pi0, pi1, lbi0, lbi1, wrong_next; |
| 76 | ip_lookup_next_t next0, next1; |
| 77 | ip6_header_t *ip0, *ip1; |
| 78 | ip6_address_t *dst_addr0, *dst_addr1; |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 79 | u32 flow_hash_config0, flow_hash_config1; |
| 80 | const dpo_id_t *dpo0, *dpo1; |
| 81 | const load_balance_t *lb0, *lb1; |
| 82 | |
| 83 | /* Prefetch next iteration. */ |
| 84 | { |
| 85 | vlib_buffer_t *p2, *p3; |
| 86 | |
| 87 | p2 = vlib_get_buffer (vm, from[2]); |
| 88 | p3 = vlib_get_buffer (vm, from[3]); |
| 89 | |
| 90 | vlib_prefetch_buffer_header (p2, LOAD); |
| 91 | vlib_prefetch_buffer_header (p3, LOAD); |
| 92 | CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); |
| 93 | CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD); |
| 94 | } |
| 95 | |
| 96 | pi0 = to_next[0] = from[0]; |
| 97 | pi1 = to_next[1] = from[1]; |
| 98 | |
| 99 | p0 = vlib_get_buffer (vm, pi0); |
| 100 | p1 = vlib_get_buffer (vm, pi1); |
| 101 | |
| 102 | ip0 = vlib_buffer_get_current (p0); |
| 103 | ip1 = vlib_buffer_get_current (p1); |
| 104 | |
| 105 | dst_addr0 = &ip0->dst_address; |
| 106 | dst_addr1 = &ip1->dst_address; |
| 107 | |
Florin Coras | 3a0325f | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 108 | ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, p0); |
| 109 | ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, p1); |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 110 | |
Florin Coras | 3a0325f | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 111 | lbi0 = ip6_fib_table_fwding_lookup (im, |
| 112 | vnet_buffer (p0)->ip.fib_index, |
| 113 | dst_addr0); |
| 114 | lbi1 = ip6_fib_table_fwding_lookup (im, |
| 115 | vnet_buffer (p1)->ip.fib_index, |
| 116 | dst_addr1); |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 117 | |
| 118 | lb0 = load_balance_get (lbi0); |
| 119 | lb1 = load_balance_get (lbi1); |
| 120 | ASSERT (lb0->lb_n_buckets > 0); |
| 121 | ASSERT (lb1->lb_n_buckets > 0); |
| 122 | ASSERT (is_pow2 (lb0->lb_n_buckets)); |
| 123 | ASSERT (is_pow2 (lb1->lb_n_buckets)); |
| 124 | |
| 125 | vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0; |
| 126 | |
| 127 | if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) |
| 128 | { |
| 129 | flow_hash_config0 = lb0->lb_hash_config; |
| 130 | vnet_buffer (p0)->ip.flow_hash = |
| 131 | ip6_compute_flow_hash (ip0, flow_hash_config0); |
| 132 | dpo0 = |
| 133 | load_balance_get_fwd_bucket (lb0, |
| 134 | (vnet_buffer (p0)->ip.flow_hash & |
| 135 | (lb0->lb_n_buckets_minus_1))); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | dpo0 = load_balance_get_bucket_i (lb0, 0); |
| 140 | } |
| 141 | if (PREDICT_FALSE (lb1->lb_n_buckets > 1)) |
| 142 | { |
| 143 | flow_hash_config1 = lb1->lb_hash_config; |
| 144 | vnet_buffer (p1)->ip.flow_hash = |
| 145 | ip6_compute_flow_hash (ip1, flow_hash_config1); |
| 146 | dpo1 = |
| 147 | load_balance_get_fwd_bucket (lb1, |
| 148 | (vnet_buffer (p1)->ip.flow_hash & |
| 149 | (lb1->lb_n_buckets_minus_1))); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | dpo1 = load_balance_get_bucket_i (lb1, 0); |
| 154 | } |
| 155 | next0 = dpo0->dpoi_next_node; |
| 156 | next1 = dpo1->dpoi_next_node; |
| 157 | |
| 158 | /* Only process the HBH Option Header if explicitly configured to do so */ |
| 159 | if (PREDICT_FALSE |
| 160 | (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)) |
| 161 | { |
| 162 | next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ? |
| 163 | (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0; |
| 164 | } |
| 165 | if (PREDICT_FALSE |
| 166 | (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)) |
| 167 | { |
| 168 | next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ? |
| 169 | (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1; |
| 170 | } |
| 171 | vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; |
| 172 | vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index; |
| 173 | |
| 174 | vlib_increment_combined_counter |
| 175 | (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0)); |
| 176 | vlib_increment_combined_counter |
| 177 | (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1)); |
| 178 | |
| 179 | from += 2; |
| 180 | to_next += 2; |
| 181 | n_left_to_next -= 2; |
| 182 | n_left_from -= 2; |
| 183 | |
| 184 | wrong_next = (next0 != next) + 2 * (next1 != next); |
| 185 | if (PREDICT_FALSE (wrong_next != 0)) |
| 186 | { |
| 187 | switch (wrong_next) |
| 188 | { |
| 189 | case 1: |
| 190 | /* A B A */ |
| 191 | to_next[-2] = pi1; |
| 192 | to_next -= 1; |
| 193 | n_left_to_next += 1; |
| 194 | vlib_set_next_frame_buffer (vm, node, next0, pi0); |
| 195 | break; |
| 196 | |
| 197 | case 2: |
| 198 | /* A A B */ |
| 199 | to_next -= 1; |
| 200 | n_left_to_next += 1; |
| 201 | vlib_set_next_frame_buffer (vm, node, next1, pi1); |
| 202 | break; |
| 203 | |
| 204 | case 3: |
| 205 | /* A B C */ |
| 206 | to_next -= 2; |
| 207 | n_left_to_next += 2; |
| 208 | vlib_set_next_frame_buffer (vm, node, next0, pi0); |
| 209 | vlib_set_next_frame_buffer (vm, node, next1, pi1); |
| 210 | if (next0 == next1) |
| 211 | { |
| 212 | /* A B B */ |
| 213 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 214 | next = next1; |
| 215 | vlib_get_next_frame (vm, node, next, to_next, |
| 216 | n_left_to_next); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | while (n_left_from > 0 && n_left_to_next > 0) |
| 223 | { |
| 224 | vlib_buffer_t *p0; |
| 225 | ip6_header_t *ip0; |
| 226 | u32 pi0, lbi0; |
| 227 | ip_lookup_next_t next0; |
| 228 | load_balance_t *lb0; |
| 229 | ip6_address_t *dst_addr0; |
Florin Coras | 3a0325f | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 230 | u32 flow_hash_config0; |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 231 | const dpo_id_t *dpo0; |
| 232 | |
| 233 | pi0 = from[0]; |
| 234 | to_next[0] = pi0; |
| 235 | |
| 236 | p0 = vlib_get_buffer (vm, pi0); |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 237 | ip0 = vlib_buffer_get_current (p0); |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 238 | dst_addr0 = &ip0->dst_address; |
Florin Coras | 3a0325f | 2018-06-01 12:22:23 -0700 | [diff] [blame] | 239 | ip_lookup_set_buffer_fib_index (im->fib_index_by_sw_if_index, p0); |
| 240 | lbi0 = ip6_fib_table_fwding_lookup (im, |
| 241 | vnet_buffer (p0)->ip.fib_index, |
| 242 | dst_addr0); |
Vijayabhaskar Katamreddy | acbde66 | 2018-01-23 13:39:40 -0800 | [diff] [blame] | 243 | |
| 244 | lb0 = load_balance_get (lbi0); |
| 245 | flow_hash_config0 = lb0->lb_hash_config; |
| 246 | |
| 247 | vnet_buffer (p0)->ip.flow_hash = 0; |
| 248 | ASSERT (lb0->lb_n_buckets > 0); |
| 249 | ASSERT (is_pow2 (lb0->lb_n_buckets)); |
| 250 | |
| 251 | if (PREDICT_FALSE (lb0->lb_n_buckets > 1)) |
| 252 | { |
| 253 | flow_hash_config0 = lb0->lb_hash_config; |
| 254 | vnet_buffer (p0)->ip.flow_hash = |
| 255 | ip6_compute_flow_hash (ip0, flow_hash_config0); |
| 256 | dpo0 = |
| 257 | load_balance_get_fwd_bucket (lb0, |
| 258 | (vnet_buffer (p0)->ip.flow_hash & |
| 259 | (lb0->lb_n_buckets_minus_1))); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | dpo0 = load_balance_get_bucket_i (lb0, 0); |
| 264 | } |
| 265 | |
| 266 | dpo0 = load_balance_get_bucket_i (lb0, |
| 267 | (vnet_buffer (p0)->ip.flow_hash & |
| 268 | lb0->lb_n_buckets_minus_1)); |
| 269 | next0 = dpo0->dpoi_next_node; |
| 270 | |
| 271 | /* Only process the HBH Option Header if explicitly configured to do so */ |
| 272 | if (PREDICT_FALSE |
| 273 | (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)) |
| 274 | { |
| 275 | next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ? |
| 276 | (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0; |
| 277 | } |
| 278 | vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index; |
| 279 | |
| 280 | vlib_increment_combined_counter |
| 281 | (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0)); |
| 282 | |
| 283 | from += 1; |
| 284 | to_next += 1; |
| 285 | n_left_to_next -= 1; |
| 286 | n_left_from -= 1; |
| 287 | |
| 288 | if (PREDICT_FALSE (next0 != next)) |
| 289 | { |
| 290 | n_left_to_next += 1; |
| 291 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 292 | next = next0; |
| 293 | vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); |
| 294 | to_next[0] = pi0; |
| 295 | to_next += 1; |
| 296 | n_left_to_next -= 1; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | vlib_put_next_frame (vm, node, next, n_left_to_next); |
| 301 | } |
| 302 | |
| 303 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 304 | ip6_forward_next_trace (vm, node, frame, VLIB_TX); |
| 305 | |
| 306 | return frame->n_vectors; |
| 307 | } |
| 308 | |
| 309 | #endif /*__included_ip6_forward_h__ */ |
| 310 | |
| 311 | /* |
| 312 | * fd.io coding-style-patch-verification: ON |
| 313 | * |
| 314 | * Local Variables: |
| 315 | * eval: (c-set-style "gnu") |
| 316 | * End: |
| 317 | */ |