790a2bb1ac17b98dd3af19c9ef13d73471a91be7
[vpp.git] / src / vnet / ip / ip6_forward.h
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;
62   u32 thread_index = vlib_get_thread_index ();
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;
79           u32 fib_index0, fib_index1;
80           u32 flow_hash_config0, flow_hash_config1;
81           const dpo_id_t *dpo0, *dpo1;
82           const load_balance_t *lb0, *lb1;
83
84           /* Prefetch next iteration. */
85           {
86             vlib_buffer_t *p2, *p3;
87
88             p2 = vlib_get_buffer (vm, from[2]);
89             p3 = vlib_get_buffer (vm, from[3]);
90
91             vlib_prefetch_buffer_header (p2, LOAD);
92             vlib_prefetch_buffer_header (p3, LOAD);
93             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
94             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
95           }
96
97           pi0 = to_next[0] = from[0];
98           pi1 = to_next[1] = from[1];
99
100           p0 = vlib_get_buffer (vm, pi0);
101           p1 = vlib_get_buffer (vm, pi1);
102
103           ip0 = vlib_buffer_get_current (p0);
104           ip1 = vlib_buffer_get_current (p1);
105
106           dst_addr0 = &ip0->dst_address;
107           dst_addr1 = &ip1->dst_address;
108
109           fib_index0 =
110             vec_elt (im->fib_index_by_sw_if_index,
111                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
112           fib_index1 =
113             vec_elt (im->fib_index_by_sw_if_index,
114                      vnet_buffer (p1)->sw_if_index[VLIB_RX]);
115
116           fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
117             fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
118           fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
119             fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
120
121           lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
122           lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
123
124           lb0 = load_balance_get (lbi0);
125           lb1 = load_balance_get (lbi1);
126           ASSERT (lb0->lb_n_buckets > 0);
127           ASSERT (lb1->lb_n_buckets > 0);
128           ASSERT (is_pow2 (lb0->lb_n_buckets));
129           ASSERT (is_pow2 (lb1->lb_n_buckets));
130
131           vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
132
133           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
134             {
135               flow_hash_config0 = lb0->lb_hash_config;
136               vnet_buffer (p0)->ip.flow_hash =
137                 ip6_compute_flow_hash (ip0, flow_hash_config0);
138               dpo0 =
139                 load_balance_get_fwd_bucket (lb0,
140                                              (vnet_buffer (p0)->ip.flow_hash &
141                                               (lb0->lb_n_buckets_minus_1)));
142             }
143           else
144             {
145               dpo0 = load_balance_get_bucket_i (lb0, 0);
146             }
147           if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
148             {
149               flow_hash_config1 = lb1->lb_hash_config;
150               vnet_buffer (p1)->ip.flow_hash =
151                 ip6_compute_flow_hash (ip1, flow_hash_config1);
152               dpo1 =
153                 load_balance_get_fwd_bucket (lb1,
154                                              (vnet_buffer (p1)->ip.flow_hash &
155                                               (lb1->lb_n_buckets_minus_1)));
156             }
157           else
158             {
159               dpo1 = load_balance_get_bucket_i (lb1, 0);
160             }
161           next0 = dpo0->dpoi_next_node;
162           next1 = dpo1->dpoi_next_node;
163
164           /* Only process the HBH Option Header if explicitly configured to do so */
165           if (PREDICT_FALSE
166               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
167             {
168               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
169                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
170             }
171           if (PREDICT_FALSE
172               (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
173             {
174               next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
175                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
176             }
177           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
178           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
179
180           vlib_increment_combined_counter
181             (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
182           vlib_increment_combined_counter
183             (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
184
185           from += 2;
186           to_next += 2;
187           n_left_to_next -= 2;
188           n_left_from -= 2;
189
190           wrong_next = (next0 != next) + 2 * (next1 != next);
191           if (PREDICT_FALSE (wrong_next != 0))
192             {
193               switch (wrong_next)
194                 {
195                 case 1:
196                   /* A B A */
197                   to_next[-2] = pi1;
198                   to_next -= 1;
199                   n_left_to_next += 1;
200                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
201                   break;
202
203                 case 2:
204                   /* A A B */
205                   to_next -= 1;
206                   n_left_to_next += 1;
207                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
208                   break;
209
210                 case 3:
211                   /* A B C */
212                   to_next -= 2;
213                   n_left_to_next += 2;
214                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
215                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
216                   if (next0 == next1)
217                     {
218                       /* A B B */
219                       vlib_put_next_frame (vm, node, next, n_left_to_next);
220                       next = next1;
221                       vlib_get_next_frame (vm, node, next, to_next,
222                                            n_left_to_next);
223                     }
224                 }
225             }
226         }
227
228       while (n_left_from > 0 && n_left_to_next > 0)
229         {
230           vlib_buffer_t *p0;
231           ip6_header_t *ip0;
232           u32 pi0, lbi0;
233           ip_lookup_next_t next0;
234           load_balance_t *lb0;
235           ip6_address_t *dst_addr0;
236           u32 fib_index0, flow_hash_config0;
237           const dpo_id_t *dpo0;
238
239           pi0 = from[0];
240           to_next[0] = pi0;
241
242           p0 = vlib_get_buffer (vm, pi0);
243
244           ip0 = vlib_buffer_get_current (p0);
245
246           dst_addr0 = &ip0->dst_address;
247
248           fib_index0 =
249             vec_elt (im->fib_index_by_sw_if_index,
250                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
251           fib_index0 =
252             (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
253              (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
254
255           lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
256
257           lb0 = load_balance_get (lbi0);
258           flow_hash_config0 = lb0->lb_hash_config;
259
260           vnet_buffer (p0)->ip.flow_hash = 0;
261           ASSERT (lb0->lb_n_buckets > 0);
262           ASSERT (is_pow2 (lb0->lb_n_buckets));
263
264           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
265             {
266               flow_hash_config0 = lb0->lb_hash_config;
267               vnet_buffer (p0)->ip.flow_hash =
268                 ip6_compute_flow_hash (ip0, flow_hash_config0);
269               dpo0 =
270                 load_balance_get_fwd_bucket (lb0,
271                                              (vnet_buffer (p0)->ip.flow_hash &
272                                               (lb0->lb_n_buckets_minus_1)));
273             }
274           else
275             {
276               dpo0 = load_balance_get_bucket_i (lb0, 0);
277             }
278
279           dpo0 = load_balance_get_bucket_i (lb0,
280                                             (vnet_buffer (p0)->ip.flow_hash &
281                                              lb0->lb_n_buckets_minus_1));
282           next0 = dpo0->dpoi_next_node;
283
284           /* Only process the HBH Option Header if explicitly configured to do so */
285           if (PREDICT_FALSE
286               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
287             {
288               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
289                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
290             }
291           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
292
293           vlib_increment_combined_counter
294             (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
295
296           from += 1;
297           to_next += 1;
298           n_left_to_next -= 1;
299           n_left_from -= 1;
300
301           if (PREDICT_FALSE (next0 != next))
302             {
303               n_left_to_next += 1;
304               vlib_put_next_frame (vm, node, next, n_left_to_next);
305               next = next0;
306               vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
307               to_next[0] = pi0;
308               to_next += 1;
309               n_left_to_next -= 1;
310             }
311         }
312
313       vlib_put_next_frame (vm, node, next, n_left_to_next);
314     }
315
316   if (node->flags & VLIB_NODE_FLAG_TRACE)
317     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
318
319   return frame->n_vectors;
320 }
321
322 #endif /*__included_ip6_forward_h__ */
323
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */