FD.io VPP  v21.10.1-2-g0a485f517
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 <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 #include <srv6-localsid/srv6_localsid_sample.h>
20 
21 typedef struct {
24 
25 /* packet trace format function */
26 static u8 * format_srv6_localsid_sample_trace (u8 * s, va_list * args)
27 {
28  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
29  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
31  s = format (s, "SRv6-sample-localsid: localsid_index %d\n",
32  t->localsid_index);
33  return s;
34 }
35 
37 
38 #define foreach_srv6_localsid_counter \
39 _(PROCESSED, "srv6-sample-localsid processed packets") \
40 _(NO_SRH, "(Error) No SRH.")
41 
42 typedef enum {
43 #define _(sym,str) SRV6_LOCALSID_COUNTER_##sym,
45 #undef _
48 
49 static char * srv6_localsid_counter_strings[] = {
50 #define _(sym,string) string,
52 #undef _
53 };
54 
55 typedef enum {
60 
61 /**
62  * @brief Function doing End processing.
63  */
66  vlib_buffer_t * b0,
67  ip6_header_t * ip0,
68  ip6_sr_header_t * sr0,
69  ip6_sr_localsid_t * ls0,
70  u32 * next0,
71  u8 psp,
72  ip6_ext_header_t * prev0)
73 {
74  ip6_address_t *new_dst0;
75 
77  {
78  if (sr0->segments_left == 1 && psp)
79  {
80  u32 new_l0, sr_len;
81  u64 *copy_dst0, *copy_src0;
82  u32 copy_len_u64s0 = 0;
83 
84  ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
85  ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
86 
87  /* Remove the SRH taking care of the rest of IPv6 ext header */
88  if (prev0)
89  prev0->next_hdr = sr0->protocol;
90  else
91  ip0->protocol = sr0->protocol;
92 
93  sr_len = ip6_ext_header_len (sr0);
94  vlib_buffer_advance (b0, sr_len);
95  new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
96  ip0->payload_length = clib_host_to_net_u16 (new_l0);
97  copy_src0 = (u64 *) ip0;
98  copy_dst0 = copy_src0 + (sr0->length + 1);
99  /* number of 8 octet units to copy
100  * By default in absence of extension headers it is equal to length of ip6 header
101  * With extension headers it number of 8 octet units of ext headers preceding
102  * SR header
103  */
104  copy_len_u64s0 =
105  (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
106  copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
107  copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
108  copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
109  copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
110  copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
111 
112  int i;
113  for (i = copy_len_u64s0 - 1; i >= 0; i--)
114  {
115  copy_dst0[i] = copy_src0[i];
116  }
117 
118  if (ls0->behavior == SR_BEHAVIOR_X)
119  {
120  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
121  *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
122  }
123  else if(ls0->behavior == SR_BEHAVIOR_T)
124  {
125  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
126  }
127  }
128  else if (PREDICT_TRUE(sr0->segments_left > 0))
129  {
130  sr0->segments_left -= 1;
131  new_dst0 = (ip6_address_t *) (sr0->segments);
132  new_dst0 += sr0->segments_left;
133  ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
134  ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
135 
136  if (ls0->behavior == SR_BEHAVIOR_X)
137  {
138  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
139  *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
140  }
141  else if(ls0->behavior == SR_BEHAVIOR_T)
142  {
143  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
144  }
145  }
146  else
147  {
148  *next0 = SR_LOCALSID_NEXT_ERROR;
149  b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
150  }
151  }
152  else
153  {
154  /* Error. Routing header of type != SR */
155  *next0 = SR_LOCALSID_NEXT_ERROR;
156  b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
157  }
158 }
159 
160 /*
161  * @brief SRv6 Sample Localsid graph node
162  * WARNING: YOU MUST DO THE DUAL LOOP
163  */
164 static uword
166 {
167  u32 n_left_from, * from, * to_next;
168  u32 next_index;
169  u32 pkts_swapped = 0;
170 
171  ip6_sr_main_t * sm = &sr_main;
172 
174  n_left_from = frame->n_vectors;
175  next_index = node->cached_next_index;
177 
178  while (n_left_from > 0)
179  {
180  u32 n_left_to_next;
181 
183  to_next, n_left_to_next);
184 
185  while (n_left_from > 0 && n_left_to_next > 0)
186  {
187  u32 bi0;
188  vlib_buffer_t * b0;
189  ip6_header_t * ip0 = 0;
190  ip6_sr_header_t * sr0;
192  ip6_sr_localsid_t *ls0;
194 
195  bi0 = from[0];
196  to_next[0] = bi0;
197  from += 1;
198  to_next += 1;
199  n_left_from -= 1;
200  n_left_to_next -= 1;
201 
202  b0 = vlib_get_buffer (vm, bi0);
203  ip0 = vlib_buffer_get_current (b0);
204  sr0 = (ip6_sr_header_t *)(ip0+1);
205 
206  /* Lookup the SR End behavior based on IP DA (adj) */
207  ls0 = pool_elt_at_index (sm->localsids, vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
208  ls0_mem = ls0->plugin_mem;
209 
210  /* SRH processing */
211  sr0 = ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
212  end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
213 
214  /* ==================================================================== */
215  /* INSERT CODE HERE */
216  /* Example starts here */
217  //In this example we are changing the next VRF table by the one in CLI
218  vnet_buffer(b0)->sw_if_index[VLIB_TX] = ls0_mem->fib_table;
219  /* Example finishes here */
220  /* ==================================================================== */
221 
222  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
223  {
224  srv6_localsid_sample_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
225  tr->localsid_index = ls0 - sm->localsids;
226  }
227 
228  /* This increments the SRv6 per LocalSID counters.*/
231  thread_index,
232  ls0 - sm->localsids,
234 
236  n_left_to_next, bi0, next0);
237 
238  pkts_swapped ++;
239  }
240  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
241 
242  }
243 
244  return frame->n_vectors;
245 }
246 
248  .function = srv6_localsid_sample_fn,
249  .name = "srv6-localsid-sample",
250  .vector_size = sizeof (u32),
251  .format_trace = format_srv6_localsid_sample_trace,
253  .n_errors = SRV6_LOCALSID_N_COUNTERS,
254  .error_strings = srv6_localsid_counter_strings,
255  .n_next_nodes = SRV6_SAMPLE_LOCALSID_N_NEXT,
256  .next_nodes = {
257  [SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP] = "ip6-lookup",
258  [SRV6_SAMPLE_LOCALSID_NEXT_ERROR] = "error-drop",
259  },
260 };
vlib.h
ip6_sr_header_t::protocol
u8 protocol
Definition: sr_packet.h:122
srv6_localsid_sample_next_t
srv6_localsid_sample_next_t
Definition: node.c:55
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
ip6_sr_main_t::sr_ls_valid_counters
vlib_combined_counter_main_t sr_ls_valid_counters
Definition: sr.h:303
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
srv6_localsid_counter_strings
static char * srv6_localsid_counter_strings[]
Definition: node.c:49
pg.h
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:549
ip6_sr_localsid_t
SR LocalSID.
Definition: sr.h:123
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
SRV6_LOCALSID_N_COUNTERS
@ SRV6_LOCALSID_N_COUNTERS
Definition: node.c:46
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
ip6_sr_localsid_t::plugin_mem
void * plugin_mem
Memory to be used by the plugin callback functions.
Definition: sr.h:156
ip6_ext_header_len
#define ip6_ext_header_len(p)
Definition: ip6_packet.h:545
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
SR_BEHAVIOR_X
#define SR_BEHAVIOR_X
Definition: sr.h:38
format_srv6_localsid_sample_trace
static u8 * format_srv6_localsid_sample_trace(u8 *s, va_list *args)
Definition: node.c:26
srv6_localsid_sample_trace_t
Definition: node.c:21
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
SR_BEHAVIOR_T
#define SR_BEHAVIOR_T
Definition: sr.h:39
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
ip6_sr_header_t::segments
ip6_address_t segments[0]
Definition: sr_packet.h:149
ip6_sr_header_t::type
u8 type
Definition: sr_packet.h:130
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
ip6_sr_main_t::sr_ls_invalid_counters
vlib_combined_counter_main_t sr_ls_invalid_counters
Definition: sr.h:304
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
error.h
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
end_srh_processing
static_always_inline void end_srh_processing(vlib_node_runtime_t *node, vlib_buffer_t *b0, ip6_header_t *ip0, ip6_sr_header_t *sr0, ip6_sr_localsid_t *ls0, u32 *next0, u8 psp, ip6_ext_header_t *prev0)
Function doing End processing.
Definition: node.c:65
srv6_localsid_sample_node
vlib_node_registration_t srv6_localsid_sample_node
(constructor) VLIB_REGISTER_NODE (srv6_localsid_sample_node)
Definition: node.c:36
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:187
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_localsid_sample_counters
srv6_localsid_sample_counters
Definition: node.c:42
ip6_ext_header_find
static void * ip6_ext_header_find(vlib_main_t *vm, vlib_buffer_t *b, ip6_header_t *ip6_header, u8 header_type, ip6_ext_header_t **prev_ext_header)
Definition: ip6_packet.h:575
static_always_inline
#define static_always_inline
Definition: clib.h:112
ip6_sr_header_t::segments_left
u8 segments_left
Definition: sr_packet.h:133
uword
u64 uword
Definition: types.h:112
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
srv6_localsid_sample_per_sid_memory_t
Definition: srv6_localsid_sample.h:47
srv6_localsid_sample_fn
static uword srv6_localsid_sample_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:165
ip6_sr_header_t::length
u8 length
Definition: sr_packet.h:127
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:145
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:273
SRV6_SAMPLE_LOCALSID_NEXT_ERROR
@ SRV6_SAMPLE_LOCALSID_NEXT_ERROR
Definition: node.c:56
foreach_srv6_localsid_counter
#define foreach_srv6_localsid_counter
Definition: node.c:38
SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP
@ SRV6_SAMPLE_LOCALSID_NEXT_IP6LOOKUP
Definition: node.c:57
ip6_sr_main_t
Segment Routing main datastructure.
Definition: sr.h:258
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
u64
unsigned long u64
Definition: types.h:89
SRV6_SAMPLE_LOCALSID_N_NEXT
@ SRV6_SAMPLE_LOCALSID_N_NEXT
Definition: node.c:58
format
description fragment has unexpected format
Definition: map.api:433
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
ip6_sr_localsid_t::vrf_index
u32 vrf_index
vrf only
Definition: sr.h:136
u32
unsigned int u32
Definition: types.h:88
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
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
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_decaps_srh_processing
static_always_inline void end_decaps_srh_processing(vlib_node_runtime_t *node, vlib_buffer_t *b0, ip6_header_t *ip0, ip6_sr_header_t *sr0, ip6_sr_localsid_t *ls0, u32 *next0)
Definition: sr_localsid.c:1073
i
int i
Definition: flowhash_template.h:376
srv6_localsid_sample_trace_t::localsid_index
u32 localsid_index
Definition: node.c:22
vnet.h
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
ip6_sr_localsid_t::behavior
u16 behavior
Behavior associated to this localsid.
Definition: sr.h:131
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
srv6_localsid_sample_per_sid_memory_t::fib_table
u32 fib_table
Definition: srv6_localsid_sample.h:48
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