FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
ip6_forward.h
Go to the documentation of this file.
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>
45 
46 /**
47  * @file
48  * @brief IPv6 Forwarding.
49  *
50  * This file contains the source code for IPv6 forwarding.
51  */
52 
53 
57 {
58  ip6_main_t *im = &ip6_main;
60  u32 n_left_from, n_left_to_next, *from, *to_next;
61  ip_lookup_next_t next;
62  u32 thread_index = vm->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 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 
110 
111  lbi0 = ip6_fib_table_fwding_lookup (vnet_buffer (p0)->ip.fib_index,
112  dst_addr0);
113  lbi1 = ip6_fib_table_fwding_lookup (vnet_buffer (p1)->ip.fib_index,
114  dst_addr1);
115 
116  lb0 = load_balance_get (lbi0);
117  lb1 = load_balance_get (lbi1);
118  ASSERT (lb0->lb_n_buckets > 0);
119  ASSERT (lb1->lb_n_buckets > 0);
120  ASSERT (is_pow2 (lb0->lb_n_buckets));
121  ASSERT (is_pow2 (lb1->lb_n_buckets));
122 
123  vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
124 
125  if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
126  {
127  flow_hash_config0 = lb0->lb_hash_config;
128  vnet_buffer (p0)->ip.flow_hash =
129  ip6_compute_flow_hash (ip0, flow_hash_config0);
130  dpo0 =
132  (vnet_buffer (p0)->ip.flow_hash &
133  (lb0->lb_n_buckets_minus_1)));
134  }
135  else
136  {
137  dpo0 = load_balance_get_bucket_i (lb0, 0);
138  }
139  if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
140  {
141  flow_hash_config1 = lb1->lb_hash_config;
142  vnet_buffer (p1)->ip.flow_hash =
143  ip6_compute_flow_hash (ip1, flow_hash_config1);
144  dpo1 =
146  (vnet_buffer (p1)->ip.flow_hash &
147  (lb1->lb_n_buckets_minus_1)));
148  }
149  else
150  {
151  dpo1 = load_balance_get_bucket_i (lb1, 0);
152  }
153  next0 = dpo0->dpoi_next_node;
154  next1 = dpo1->dpoi_next_node;
155 
156  /* Only process the HBH Option Header if explicitly configured to do so */
157  if (PREDICT_FALSE
158  (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
159  {
160  next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
162  }
163  if (PREDICT_FALSE
164  (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
165  {
166  next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
168  }
169  vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
170  vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
171 
173  (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
175  (cm, thread_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
176 
177  from += 2;
178  to_next += 2;
179  n_left_to_next -= 2;
180  n_left_from -= 2;
181 
182  wrong_next = (next0 != next) + 2 * (next1 != next);
183  if (PREDICT_FALSE (wrong_next != 0))
184  {
185  switch (wrong_next)
186  {
187  case 1:
188  /* A B A */
189  to_next[-2] = pi1;
190  to_next -= 1;
191  n_left_to_next += 1;
192  vlib_set_next_frame_buffer (vm, node, next0, pi0);
193  break;
194 
195  case 2:
196  /* A A B */
197  to_next -= 1;
198  n_left_to_next += 1;
199  vlib_set_next_frame_buffer (vm, node, next1, pi1);
200  break;
201 
202  case 3:
203  /* A B C */
204  to_next -= 2;
205  n_left_to_next += 2;
206  vlib_set_next_frame_buffer (vm, node, next0, pi0);
207  vlib_set_next_frame_buffer (vm, node, next1, pi1);
208  if (next0 == next1)
209  {
210  /* A B B */
211  vlib_put_next_frame (vm, node, next, n_left_to_next);
212  next = next1;
213  vlib_get_next_frame (vm, node, next, to_next,
214  n_left_to_next);
215  }
216  }
217  }
218  }
219 
220  while (n_left_from > 0 && n_left_to_next > 0)
221  {
222  vlib_buffer_t *p0;
223  ip6_header_t *ip0;
224  u32 pi0, lbi0;
225  ip_lookup_next_t next0;
226  load_balance_t *lb0;
227  ip6_address_t *dst_addr0;
228  u32 flow_hash_config0;
229  const dpo_id_t *dpo0;
230 
231  pi0 = from[0];
232  to_next[0] = pi0;
233 
234  p0 = vlib_get_buffer (vm, pi0);
235  ip0 = vlib_buffer_get_current (p0);
236  dst_addr0 = &ip0->dst_address;
238  lbi0 = ip6_fib_table_fwding_lookup (vnet_buffer (p0)->ip.fib_index,
239  dst_addr0);
240 
241  lb0 = load_balance_get (lbi0);
242  flow_hash_config0 = lb0->lb_hash_config;
243 
244  vnet_buffer (p0)->ip.flow_hash = 0;
245  ASSERT (lb0->lb_n_buckets > 0);
246  ASSERT (is_pow2 (lb0->lb_n_buckets));
247 
248  if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
249  {
250  flow_hash_config0 = lb0->lb_hash_config;
251  vnet_buffer (p0)->ip.flow_hash =
252  ip6_compute_flow_hash (ip0, flow_hash_config0);
253  dpo0 =
255  (vnet_buffer (p0)->ip.flow_hash &
256  (lb0->lb_n_buckets_minus_1)));
257  }
258  else
259  {
260  dpo0 = load_balance_get_bucket_i (lb0, 0);
261  }
262 
263  dpo0 = load_balance_get_bucket_i (lb0,
264  (vnet_buffer (p0)->ip.flow_hash &
265  lb0->lb_n_buckets_minus_1));
266  next0 = dpo0->dpoi_next_node;
267 
268  /* Only process the HBH Option Header if explicitly configured to do so */
269  if (PREDICT_FALSE
270  (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
271  {
272  next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
274  }
275  vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
276 
278  (cm, thread_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
279 
280  from += 1;
281  to_next += 1;
282  n_left_to_next -= 1;
283  n_left_from -= 1;
284 
285  if (PREDICT_FALSE (next0 != next))
286  {
287  n_left_to_next += 1;
288  vlib_put_next_frame (vm, node, next, n_left_to_next);
289  next = next0;
290  vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
291  to_next[0] = pi0;
292  to_next += 1;
293  n_left_to_next -= 1;
294  }
295  }
296 
297  vlib_put_next_frame (vm, node, next, n_left_to_next);
298  }
299 
300  if (node->flags & VLIB_NODE_FLAG_TRACE)
301  ip6_forward_next_trace (vm, node, frame, VLIB_TX);
302 
303  return frame->n_vectors;
304 }
305 
306 #endif /*__included_ip6_forward_h__ */
307 
308 /*
309  * fd.io coding-style-patch-verification: ON
310  *
311  * Local Variables:
312  * eval: (c-set-style "gnu")
313  * End:
314  */
u16 lb_n_buckets
number of buckets in the load-balance.
Definition: load_balance.h:116
vlib_combined_counter_main_t lbm_to_counters
Definition: load_balance.h:46
static u32 ip6_fib_table_fwding_lookup(u32 fib_index, const ip6_address_t *dst)
Definition: ip6_fib.h:67
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
int dpo_is_adj(const dpo_id_t *dpo)
Return TRUE is the DPO is any type of adjacency.
Definition: dpo.c:278
static void vlib_set_next_frame_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 buffer_index)
Definition: node_funcs.h:371
flow_hash_config_t lb_hash_config
the hash config to use when selecting a bucket.
Definition: load_balance.h:161
static const dpo_id_t * load_balance_get_fwd_bucket(const load_balance_t *lb, u16 bucket)
u32 thread_index
Definition: main.h:218
static uword ip6_lookup_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip6_forward.h:55
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:402
ip_lookup_next_t
An adjacency is a representation of an attached L3 peer.
Definition: adj.h:50
u16 lb_n_buckets_minus_1
number of buckets in the load-balance - 1.
Definition: load_balance.h:121
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
unsigned int u32
Definition: types.h:88
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:170
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
static void ip_lookup_set_buffer_fib_index(u32 *fib_index_by_sw_if_index, vlib_buffer_t *b)
Definition: lookup.h:192
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:229
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
static u32 ip6_compute_flow_hash(const ip6_header_t *ip, flow_hash_config_t flow_hash_config)
Definition: ip6.h:382
The FIB DPO provieds;.
Definition: load_balance.h:106
#define PREDICT_FALSE(x)
Definition: clib.h:118
#define always_inline
Definition: ipsec.h:28
ip6_main_t ip6_main
Definition: ip6_forward.c:2784
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:56
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
vlib_main_t * vm
Definition: in2out_ed.c:1599
u16 n_vectors
Definition: node.h:399
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
u8 data[]
Packet data.
Definition: buffer.h:181
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
u8 hbh_enabled
Definition: ip6.h:238
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:517
#define ASSERT(truth)
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:220
static uword is_pow2(uword x)
Definition: clib.h:250
Definition: defs.h:47
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:186
vl_api_address_t ip
Definition: l2.api:501
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
A collection of combined counters.
Definition: counter.h:188
#define vnet_buffer(b)
Definition: buffer.h:417
void ip6_forward_next_trace(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vlib_rx_or_tx_t which_adj_index)
Definition: ip6_forward.c:1003
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1600
u16 flags
Copy of main node flags.
Definition: node.h:511
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:182
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:304
u32 * fib_index_by_sw_if_index
Definition: ip6.h:196
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
ip6_address_t dst_address
Definition: ip6_packet.h:310