FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
node.c
Go to the documentation of this file.
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 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <srv6-am/am.h>
19 
20 
21 /******************************* Packet tracing *******************************/
22 
23 typedef struct
24 {
27 
28 typedef struct
29 {
30  ip6_address_t src, dst;
32 
33 static u8 *
34 format_srv6_am_localsid_trace (u8 * s, va_list * args)
35 {
36  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39 
40  return format (s, "SRv6-AM-localsid: localsid_index %d", t->localsid_index);
41 }
42 
43 static u8 *
44 format_srv6_am_rewrite_trace (u8 * s, va_list * args)
45 {
46  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
47  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
48  srv6_am_rewrite_trace_t *t = va_arg (*args, srv6_am_rewrite_trace_t *);
49 
50  return format (s, "SRv6-AM-rewrite: src %U dst %U",
52 }
53 
54 
55 /***************************** Node registration ******************************/
56 
58 
59 
60 /****************************** Packet counters *******************************/
61 
62 #define foreach_srv6_am_rewrite_counter \
63 _(PROCESSED, "srv6-am rewritten packets") \
64 _(NO_SRH, "(Error) No SRH.")
65 
66 typedef enum
67 {
68 #define _(sym,str) SRV6_AM_REWRITE_COUNTER_##sym,
70 #undef _
73 
75 #define _(sym,string) string,
77 #undef _
78 };
79 
80 
81 /********************************* Next nodes *********************************/
82 
83 typedef enum
84 {
89 
90 typedef enum
91 {
96 
97 
98 /******************************* Local SID node *******************************/
99 
100 /**
101  * @brief SRv6 masquerading.
102  */
105  ip6_header_t * ip0,
106  ip6_sr_header_t * sr0,
107  ip6_sr_localsid_t * ls0, u32 * next0)
108 {
109  ip6_address_t *new_dst0;
110 
111  if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_IPV6_ROUTE ||
112  sr0->type != ROUTING_HEADER_TYPE_SR))
113  {
115  return;
116  }
117 
118  if (PREDICT_FALSE (sr0->segments_left == 0))
119  {
121  return;
122  }
123 
124  /* Decrement Segments Left */
125  sr0->segments_left -= 1;
126 
127  /* Set Destination Address to Last Segment (index 0) */
128  new_dst0 = (ip6_address_t *) (sr0->segments);
129  ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
130  ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
131 
132  /* Set Xconnect adjacency to VNF */
133  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
134 }
135 
136 /**
137  * @brief Graph node for applying SRv6 masquerading.
138  */
139 static uword
142 {
143  ip6_sr_main_t *sm = &sr_main;
144  u32 n_left_from, next_index, *from, *to_next;
145  u32 cnt_packets = 0;
146 
148  n_left_from = frame->n_vectors;
149  next_index = node->cached_next_index;
150 
152 
153  while (n_left_from > 0)
154  {
155  u32 n_left_to_next;
156 
157  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
158 
159  /* TODO: Dual/quad loop */
160 
161  while (n_left_from > 0 && n_left_to_next > 0)
162  {
163  u32 bi0;
164  vlib_buffer_t *b0;
165  ip6_header_t *ip0 = 0;
166  ip6_sr_header_t *sr0;
167  ip6_sr_localsid_t *ls0;
169 
170  bi0 = from[0];
171  to_next[0] = bi0;
172  from += 1;
173  to_next += 1;
174  n_left_from -= 1;
175  n_left_to_next -= 1;
176 
177  b0 = vlib_get_buffer (vm, bi0);
178  ip0 = vlib_buffer_get_current (b0);
179  sr0 = (ip6_sr_header_t *) (ip0 + 1);
180 
181  /* Lookup the SR End behavior based on IP DA (adj) */
182  ls0 = pool_elt_at_index (sm->localsids,
183  vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
184 
185  /* SRH processing */
186  end_am_processing (b0, ip0, sr0, ls0, &next0);
187 
188  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
189  {
191  vlib_add_trace (vm, node, b0, sizeof *tr);
192  tr->localsid_index = ls0 - sm->localsids;
193  }
194 
195  /* This increments the SRv6 per LocalSID counters. */
198  &(sm->sr_ls_invalid_counters) :
199  &(sm->sr_ls_valid_counters)),
200  thread_index, ls0 - sm->localsids,
202  b0));
203 
205  n_left_to_next, bi0, next0);
206 
207  cnt_packets++;
208  }
209 
210  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
211  }
212 
213  return frame->n_vectors;
214 }
215 
216 /* *INDENT-OFF* */
218  .function = srv6_am_localsid_fn,
219  .name = "srv6-am-localsid",
220  .vector_size = sizeof (u32),
221  .format_trace = format_srv6_am_localsid_trace,
223  .n_next_nodes = SRV6_AM_LOCALSID_N_NEXT,
224  .next_nodes = {
225  [SRV6_AM_LOCALSID_NEXT_REWRITE] = "ip6-rewrite",
226  [SRV6_AM_LOCALSID_NEXT_ERROR] = "error-drop",
227  },
228 };
229 /* *INDENT-ON* */
230 
231 
232 /******************************* Rewriting node *******************************/
233 
234 /**
235  * @brief SRv6 de-masquerading.
236  */
239  vlib_buffer_t * b0,
240  ip6_header_t * ip0, ip6_sr_header_t * sr0, u32 * next0)
241 {
242  if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_IPV6_ROUTE ||
243  sr0->type != ROUTING_HEADER_TYPE_SR))
244  {
245  b0->error = node->errors[SRV6_AM_REWRITE_COUNTER_NO_SRH];
247  return;
248  }
249 
250  /* Restore Destination Address to active segment (index SL) */
251  if (sr0->segments_left != 0)
252  {
253  ip6_address_t *new_dst0;
254  new_dst0 = (ip6_address_t *) (sr0->segments) + sr0->segments_left;
255  ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
256  ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
257  }
258 }
259 
260 /**
261  * @brief Graph node for applying SRv6 de-masquerading.
262  */
263 static uword
266 {
267  u32 n_left_from, next_index, *from, *to_next;
268  u32 cnt_packets = 0;
269 
271  n_left_from = frame->n_vectors;
272  next_index = node->cached_next_index;
273 
274  while (n_left_from > 0)
275  {
276  u32 n_left_to_next;
277 
278  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
279 
280  /* TODO: Dual/quad loop */
281 
282  while (n_left_from > 0 && n_left_to_next > 0)
283  {
284  u32 bi0;
285  vlib_buffer_t *b0;
286  ip6_header_t *ip0 = 0;
287  ip6_sr_header_t *sr0;
289 
290  bi0 = from[0];
291  to_next[0] = bi0;
292  from += 1;
293  to_next += 1;
294  n_left_from -= 1;
295  n_left_to_next -= 1;
296 
297  b0 = vlib_get_buffer (vm, bi0);
298  ip0 = vlib_buffer_get_current (b0);
299  sr0 = (ip6_sr_header_t *) (ip0 + 1);
300 
301  /* SRH processing */
302  end_am_rewriting (node, b0, ip0, sr0, &next0);
303 
304  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
305  PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
306  {
308  vlib_add_trace (vm, node, b0, sizeof *tr);
309  clib_memcpy_fast (tr->src.as_u8, ip0->src_address.as_u8,
310  sizeof tr->src.as_u8);
311  clib_memcpy_fast (tr->dst.as_u8, ip0->dst_address.as_u8,
312  sizeof tr->dst.as_u8);
313  }
314 
316  n_left_to_next, bi0, next0);
317 
318  cnt_packets++;
319  }
320 
321  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
322  }
323 
324  /* Update counters */
326  SRV6_AM_REWRITE_COUNTER_PROCESSED,
327  cnt_packets);
328 
329  return frame->n_vectors;
330 }
331 
332 /* *INDENT-OFF* */
334  .function = srv6_am_rewrite_fn,
335  .name = "srv6-am-rewrite",
336  .vector_size = sizeof (u32),
337  .format_trace = format_srv6_am_rewrite_trace,
339  .n_errors = SRV6_AM_REWRITE_N_COUNTERS,
340  .error_strings = srv6_am_rewrite_counter_strings,
341  .n_next_nodes = SRV6_AM_REWRITE_N_NEXT,
342  .next_nodes = {
343  [SRV6_AM_REWRITE_NEXT_LOOKUP] = "ip6-lookup",
344  [SRV6_AM_REWRITE_NEXT_ERROR] = "error-drop",
345  },
346 };
347 /* *INDENT-ON* */
348 
349 /*
350 * fd.io coding-style-patch-verification: ON
351 *
352 * Local Variables:
353 * eval: (c-set-style "gnu")
354 * End:
355 */
vlib.h
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
srv6_am_rewrite_next_t
srv6_am_rewrite_next_t
Definition: node.c:90
ip6_sr_main_t::sr_ls_valid_counters
vlib_combined_counter_main_t sr_ls_valid_counters
Definition: sr.h:302
SRV6_AM_LOCALSID_NEXT_ERROR
@ SRV6_AM_LOCALSID_NEXT_ERROR
Definition: node.c:85
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
srv6_am_localsid_trace_t
Definition: node.c:23
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
srv6_am_rewrite_trace_t::dst
ip6_address_t dst
Definition: node.c:30
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
SRV6_AM_LOCALSID_N_NEXT
@ SRV6_AM_LOCALSID_N_NEXT
Definition: node.c:87
ip6_sr_localsid_t
SR LocalSID.
Definition: sr.h:122
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
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:433
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
ip6_sr_header_t::segments
ip6_address_t segments[0]
Definition: sr_packet.h:149
am.h
ip6_sr_header_t::type
u8 type
Definition: sr_packet.h:130
ip6_sr_main_t::sr_ls_invalid_counters
vlib_combined_counter_main_t sr_ls_invalid_counters
Definition: sr.h:303
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
error.h
foreach_srv6_am_rewrite_counter
#define foreach_srv6_am_rewrite_counter
Definition: node.c:62
srv6_am_localsid_fn
static uword srv6_am_localsid_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Graph node for applying SRv6 masquerading.
Definition: node.c:140
end_am_processing
static_always_inline void end_am_processing(vlib_buffer_t *b0, ip6_header_t *ip0, ip6_sr_header_t *sr0, ip6_sr_localsid_t *ls0, u32 *next0)
SRv6 masquerading.
Definition: node.c:104
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
srv6_am_rewrite_trace_t
Definition: node.c:28
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
srv6_am_rewrite_fn
static uword srv6_am_rewrite_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Graph node for applying SRv6 de-masquerading.
Definition: node.c:264
static_always_inline
#define static_always_inline
Definition: clib.h:112
SRV6_AM_LOCALSID_NEXT_REWRITE
@ SRV6_AM_LOCALSID_NEXT_REWRITE
Definition: node.c:86
ip6_sr_header_t::segments_left
u8 segments_left
Definition: sr_packet.h:133
uword
u64 uword
Definition: types.h:112
srv6_am_localsid_trace_t::localsid_index
u32 localsid_index
Definition: node.c:25
srv6_am_localsid_next_t
srv6_am_localsid_next_t
Definition: node.c:83
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
SRV6_AM_REWRITE_NEXT_ERROR
@ SRV6_AM_REWRITE_NEXT_ERROR
Definition: node.c:92
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
srv6_am_rewrite_counters
srv6_am_rewrite_counters
Definition: node.c:66
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
ip6_sr_localsid_t::nh_adj
u32 nh_adj
Next_adj for xconnect usage only.
Definition: sr.h:144
sr_main
ip6_sr_main_t sr_main
Definition: sr.c:31
ip6_sr_main_t::localsids
ip6_sr_localsid_t * localsids
Definition: sr.h:272
SRV6_AM_REWRITE_N_NEXT
@ SRV6_AM_REWRITE_N_NEXT
Definition: node.c:94
format_srv6_am_localsid_trace
static u8 * format_srv6_am_localsid_trace(u8 *s, va_list *args)
Definition: node.c:34
ip6_sr_main_t
Segment Routing main datastructure.
Definition: sr.h:257
vlib_validate_buffer_enqueue_x1
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
srv6_am_rewrite_node
vlib_node_registration_t srv6_am_rewrite_node
(constructor) VLIB_REGISTER_NODE (srv6_am_rewrite_node)
Definition: node.c:57
SRV6_AM_REWRITE_N_COUNTERS
@ SRV6_AM_REWRITE_N_COUNTERS
Definition: node.c:71
format
description fragment has unexpected format
Definition: map.api:433
format_srv6_am_rewrite_trace
static u8 * format_srv6_am_rewrite_trace(u8 *s, va_list *args)
Definition: node.c:44
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
SRV6_AM_REWRITE_NEXT_LOOKUP
@ SRV6_AM_REWRITE_NEXT_LOOKUP
Definition: node.c:93
u32
unsigned int u32
Definition: types.h:88
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
ROUTING_HEADER_TYPE_SR
#define ROUTING_HEADER_TYPE_SR
Definition: sr_packet.h:117
ip6_sr_header_t
Definition: sr_packet.h:119
ip6_header_t
Definition: ip6_packet.h:294
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
srv6_am_rewrite_trace_t::src
ip6_address_t src
Definition: node.c:30
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip
vl_api_address_t ip
Definition: l2.api:558
end_am_rewriting
static_always_inline void end_am_rewriting(vlib_node_runtime_t *node, vlib_buffer_t *b0, ip6_header_t *ip0, ip6_sr_header_t *sr0, u32 *next0)
SRv6 de-masquerading.
Definition: node.c:238
format_ip6_address
format_function_t format_ip6_address
Definition: format.h:91
srv6_am_rewrite_counter_strings
static char * srv6_am_rewrite_counter_strings[]
Definition: node.c:74
srv6_am_localsid_node
vlib_node_registration_t srv6_am_localsid_node
(constructor) VLIB_REGISTER_NODE (srv6_am_localsid_node)
Definition: node.c:217
vnet.h
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
vlib_get_next_frame
#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:395
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169