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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <srv6-ad/ad.h>
21 /******************************* Packet tracing *******************************/
26 } srv6_ad_localsid_trace_t;
31 ip6_address_t src, dst;
32 } srv6_ad_rewrite_trace_t;
35 format_srv6_ad_localsid_trace (u8 * s, va_list * args)
37 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39 srv6_ad_localsid_trace_t *t = va_arg (*args, srv6_ad_localsid_trace_t *);
41 return format (s, "SRv6-AD-localsid: localsid_index %d", t->localsid_index);
45 format_srv6_ad_rewrite_trace (u8 * s, va_list * args)
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 srv6_ad_rewrite_trace_t *t = va_arg (*args, srv6_ad_rewrite_trace_t *);
51 if (PREDICT_FALSE (t->error != 0))
53 return format (s, "SRv6-AD-rewrite: cache is empty");
56 return format (s, "SRv6-AD-rewrite: src %U dst %U",
57 format_ip6_address, &t->src, format_ip6_address, &t->dst);
61 /***************************** Nodes registration *****************************/
63 vlib_node_registration_t srv6_ad4_rewrite_node;
64 vlib_node_registration_t srv6_ad6_rewrite_node;
67 /****************************** Packet counters *******************************/
69 #define foreach_srv6_ad_rewrite_counter \
70 _(PROCESSED, "srv6-ad rewritten packets") \
71 _(NO_RW, "(Error) No header for rewriting.")
75 #define _(sym,str) SRV6_AD_REWRITE_COUNTER_##sym,
76 foreach_srv6_ad_rewrite_counter
78 SRV6_AD_REWRITE_N_COUNTERS,
79 } srv6_ad_rewrite_counters;
81 static char *srv6_ad_rewrite_counter_strings[] = {
82 #define _(sym,string) string,
83 foreach_srv6_ad_rewrite_counter
88 /********************************* Next nodes *********************************/
92 SRV6_AD_LOCALSID_NEXT_ERROR,
93 SRV6_AD_LOCALSID_NEXT_REWRITE4,
94 SRV6_AD_LOCALSID_NEXT_REWRITE6,
95 SRV6_AD_LOCALSID_N_NEXT,
96 } srv6_ad_localsid_next_t;
100 SRV6_AD_REWRITE_NEXT_ERROR,
101 SRV6_AD_REWRITE_NEXT_LOOKUP,
102 SRV6_AD_REWRITE_N_NEXT,
103 } srv6_ad_rewrite_next_t;
106 /******************************* Local SID node *******************************/
109 * @brief Function doing SRH processing for AD behavior
111 static_always_inline void
112 end_ad_processing (vlib_buffer_t * b0,
114 ip6_sr_header_t * sr0,
115 ip6_sr_localsid_t * ls0, u32 * next0)
117 ip6_address_t *new_dst0;
119 ip6_ext_header_t *next_ext_header;
121 srv6_ad_localsid_t *ls0_mem;
123 if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_IPV6_ROUTE ||
124 sr0->type != ROUTING_HEADER_TYPE_SR))
129 if (PREDICT_FALSE (sr0->segments_left == 0))
134 /* Decrement Segments Left and update Destination Address */
135 sr0->segments_left -= 1;
136 new_dst0 = (ip6_address_t *) (sr0->segments) + sr0->segments_left;
137 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
138 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
140 /* Compute the total size of the IPv6 header and extensions */
141 total_size = sizeof (ip6_header_t);
142 next_ext_header = (ip6_ext_header_t *) (ip0 + 1);
143 next_hdr = ip0->protocol;
145 while (ip6_ext_hdr (next_hdr))
147 total_size += ip6_ext_header_len (next_ext_header);
148 next_hdr = next_ext_header->next_hdr;
149 next_ext_header = ip6_ext_next_header (next_ext_header);
152 /* Make sure next header is IP */
154 (next_hdr != IP_PROTOCOL_IPV6 && next_hdr != IP_PROTOCOL_IP_IN_IP))
159 /* Retrieve SID memory */
160 ls0_mem = ls0->plugin_mem;
162 /* Cache IP header and extensions */
163 vec_validate (ls0_mem->rewrite, total_size - 1);
164 clib_memcpy (ls0_mem->rewrite, ip0, total_size);
166 /* Remove IP header and extensions */
167 vlib_buffer_advance (b0, total_size);
169 /* Set Xconnect adjacency to VNF */
170 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0_mem->nh_adj;
172 if (ls0_mem->ip_version == DA_IP4)
173 *next0 = SRV6_AD_LOCALSID_NEXT_REWRITE4;
174 else if (ls0_mem->ip_version == DA_IP6)
175 *next0 = SRV6_AD_LOCALSID_NEXT_REWRITE6;
179 * @brief SRv6 AD Localsid graph node
182 srv6_ad_localsid_fn (vlib_main_t * vm,
183 vlib_node_runtime_t * node, vlib_frame_t * frame)
185 ip6_sr_main_t *sm = &sr_main;
186 u32 n_left_from, next_index, *from, *to_next;
189 from = vlib_frame_vector_args (frame);
190 n_left_from = frame->n_vectors;
191 next_index = node->cached_next_index;
193 u32 thread_index = vlib_get_thread_index ();
195 while (n_left_from > 0)
199 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
201 /* TODO: Dual/quad loop */
203 while (n_left_from > 0 && n_left_to_next > 0)
207 ip6_header_t *ip0 = 0;
208 ip6_sr_header_t *sr0;
209 ip6_sr_localsid_t *ls0;
210 u32 next0 = SRV6_AD_LOCALSID_NEXT_ERROR;
219 b0 = vlib_get_buffer (vm, bi0);
220 ip0 = vlib_buffer_get_current (b0);
221 sr0 = (ip6_sr_header_t *) (ip0 + 1);
223 /* Lookup the SR End behavior based on IP DA (adj) */
224 ls0 = pool_elt_at_index (sm->localsids,
225 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
228 end_ad_processing (b0, ip0, sr0, ls0, &next0);
230 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
232 srv6_ad_localsid_trace_t *tr =
233 vlib_add_trace (vm, node, b0, sizeof *tr);
234 tr->localsid_index = ls0 - sm->localsids;
237 /* This increments the SRv6 per LocalSID counters. */
238 vlib_increment_combined_counter (((next0 ==
239 SRV6_AD_LOCALSID_NEXT_ERROR) ?
240 &(sm->sr_ls_invalid_counters) :
241 &(sm->sr_ls_valid_counters)),
242 thread_index, ls0 - sm->localsids,
243 1, vlib_buffer_length_in_chain (vm,
246 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
247 n_left_to_next, bi0, next0);
252 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
255 return frame->n_vectors;
259 VLIB_REGISTER_NODE (srv6_ad_localsid_node) = {
260 .function = srv6_ad_localsid_fn,
261 .name = "srv6-ad-localsid",
262 .vector_size = sizeof (u32),
263 .format_trace = format_srv6_ad_localsid_trace,
264 .type = VLIB_NODE_TYPE_INTERNAL,
265 .n_next_nodes = SRV6_AD_LOCALSID_N_NEXT,
267 [SRV6_AD_LOCALSID_NEXT_REWRITE4] = "ip4-rewrite",
268 [SRV6_AD_LOCALSID_NEXT_REWRITE6] = "ip6-rewrite",
269 [SRV6_AD_LOCALSID_NEXT_ERROR] = "error-drop",
275 /******************************* Rewriting node *******************************/
278 * @brief Graph node for applying a SR policy into an IPv6 packet. Encapsulation
281 srv6_ad4_rewrite_fn (vlib_main_t * vm,
282 vlib_node_runtime_t * node, vlib_frame_t * frame)
284 ip6_sr_main_t *srm = &sr_main;
285 srv6_ad_main_t *sm = &srv6_ad_main;
286 u32 n_left_from, next_index, *from, *to_next;
289 from = vlib_frame_vector_args (frame);
290 n_left_from = frame->n_vectors;
291 next_index = node->cached_next_index;
293 while (n_left_from > 0)
297 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
299 /* TODO: Dual/quad loop */
301 while (n_left_from > 0 && n_left_to_next > 0)
305 ip4_header_t *ip0_encap = 0;
306 ip6_header_t *ip0 = 0;
307 ip6_sr_localsid_t *ls0;
308 srv6_ad_localsid_t *ls0_mem;
309 u32 next0 = SRV6_AD_REWRITE_NEXT_LOOKUP;
319 b0 = vlib_get_buffer (vm, bi0);
320 ip0_encap = vlib_buffer_get_current (b0);
321 ls0 = pool_elt_at_index (srm->localsids,
322 sm->sw_iface_localsid4[vnet_buffer
325 ls0_mem = ls0->plugin_mem;
327 if (PREDICT_FALSE (ls0_mem == NULL || ls0_mem->rewrite == NULL))
329 next0 = SRV6_AD_REWRITE_NEXT_ERROR;
330 b0->error = node->errors[SRV6_AD_REWRITE_COUNTER_NO_RW];
334 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
335 (vec_len (ls0_mem->rewrite) + b0->current_data));
337 clib_memcpy (((u8 *) ip0_encap) - vec_len (ls0_mem->rewrite),
338 ls0_mem->rewrite, vec_len (ls0_mem->rewrite));
339 vlib_buffer_advance (b0, -(word) vec_len (ls0_mem->rewrite));
341 ip0 = vlib_buffer_get_current (b0);
343 /* Update inner IPv4 TTL and checksum */
346 checksum0 = ip0_encap->checksum + clib_host_to_net_u16 (0x0100);
347 checksum0 += checksum0 >= 0xffff;
348 ip0_encap->checksum = checksum0;
350 /* Update outer IPv6 length (in case it has changed) */
351 new_l0 = vec_len (ls0_mem->rewrite) - sizeof (ip6_header_t) +
352 clib_net_to_host_u16 (ip0_encap->length);
353 ip0->payload_length = clib_host_to_net_u16 (new_l0);
356 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
357 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
359 srv6_ad_rewrite_trace_t *tr =
360 vlib_add_trace (vm, node, b0, sizeof *tr);
363 if (next0 == SRV6_AD_REWRITE_NEXT_ERROR)
369 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
370 sizeof tr->src.as_u8);
371 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
372 sizeof tr->dst.as_u8);
376 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
377 n_left_to_next, bi0, next0);
382 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
385 /* Update counters */
386 vlib_node_increment_counter (vm, srv6_ad4_rewrite_node.index,
387 SRV6_AD_REWRITE_COUNTER_PROCESSED,
390 return frame->n_vectors;
394 VLIB_REGISTER_NODE (srv6_ad4_rewrite_node) = {
395 .function = srv6_ad4_rewrite_fn,
396 .name = "srv6-ad4-rewrite",
397 .vector_size = sizeof (u32),
398 .format_trace = format_srv6_ad_rewrite_trace,
399 .type = VLIB_NODE_TYPE_INTERNAL,
400 .n_errors = SRV6_AD_REWRITE_N_COUNTERS,
401 .error_strings = srv6_ad_rewrite_counter_strings,
402 .n_next_nodes = SRV6_AD_REWRITE_N_NEXT,
404 [SRV6_AD_REWRITE_NEXT_LOOKUP] = "ip6-lookup",
405 [SRV6_AD_REWRITE_NEXT_ERROR] = "error-drop",
412 * @brief Graph node for applying a SR policy into an IPv6 packet. Encapsulation
415 srv6_ad6_rewrite_fn (vlib_main_t * vm,
416 vlib_node_runtime_t * node, vlib_frame_t * frame)
418 ip6_sr_main_t *srm = &sr_main;
419 srv6_ad_main_t *sm = &srv6_ad_main;
420 u32 n_left_from, next_index, *from, *to_next;
423 from = vlib_frame_vector_args (frame);
424 n_left_from = frame->n_vectors;
425 next_index = node->cached_next_index;
427 while (n_left_from > 0)
431 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
433 /* TODO: Dual/quad loop */
435 while (n_left_from > 0 && n_left_to_next > 0)
439 ip6_header_t *ip0 = 0, *ip0_encap = 0;
440 ip6_sr_localsid_t *ls0;
441 srv6_ad_localsid_t *ls0_mem;
442 u32 next0 = SRV6_AD_REWRITE_NEXT_LOOKUP;
452 b0 = vlib_get_buffer (vm, bi0);
453 ip0_encap = vlib_buffer_get_current (b0);
454 ls0 = pool_elt_at_index (srm->localsids,
455 sm->sw_iface_localsid6[vnet_buffer
458 ls0_mem = ls0->plugin_mem;
460 if (PREDICT_FALSE (ls0_mem == NULL || ls0_mem->rewrite == NULL))
462 next0 = SRV6_AD_REWRITE_NEXT_ERROR;
463 b0->error = node->errors[SRV6_AD_REWRITE_COUNTER_NO_RW];
467 ASSERT (VLIB_BUFFER_PRE_DATA_SIZE >=
468 (vec_len (ls0_mem->rewrite) + b0->current_data));
470 clib_memcpy (((u8 *) ip0_encap) - vec_len (ls0_mem->rewrite),
471 ls0_mem->rewrite, vec_len (ls0_mem->rewrite));
472 vlib_buffer_advance (b0, -(word) vec_len (ls0_mem->rewrite));
474 ip0 = vlib_buffer_get_current (b0);
476 /* Update inner IPv6 hop limit */
477 ip0_encap->hop_limit -= 1;
479 /* Update outer IPv6 length (in case it has changed) */
480 new_l0 = vec_len (ls0_mem->rewrite) +
481 clib_net_to_host_u16 (ip0_encap->payload_length);
482 ip0->payload_length = clib_host_to_net_u16 (new_l0);
485 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
486 PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
488 srv6_ad_rewrite_trace_t *tr =
489 vlib_add_trace (vm, node, b0, sizeof *tr);
492 if (next0 == SRV6_AD_REWRITE_NEXT_ERROR)
498 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
499 sizeof tr->src.as_u8);
500 clib_memcpy (tr->dst.as_u8, ip0->dst_address.as_u8,
501 sizeof tr->dst.as_u8);
505 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
506 n_left_to_next, bi0, next0);
511 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
514 /* Update counters */
515 vlib_node_increment_counter (vm, srv6_ad6_rewrite_node.index,
516 SRV6_AD_REWRITE_COUNTER_PROCESSED,
519 return frame->n_vectors;
523 VLIB_REGISTER_NODE (srv6_ad6_rewrite_node) = {
524 .function = srv6_ad6_rewrite_fn,
525 .name = "srv6-ad6-rewrite",
526 .vector_size = sizeof (u32),
527 .format_trace = format_srv6_ad_rewrite_trace,
528 .type = VLIB_NODE_TYPE_INTERNAL,
529 .n_errors = SRV6_AD_REWRITE_N_COUNTERS,
530 .error_strings = srv6_ad_rewrite_counter_strings,
531 .n_next_nodes = SRV6_AD_REWRITE_N_NEXT,
533 [SRV6_AD_REWRITE_NEXT_LOOKUP] = "ip6-lookup",
534 [SRV6_AD_REWRITE_NEXT_ERROR] = "error-drop",
540 * fd.io coding-style-patch-verification: ON
543 * eval: (c-set-style "gnu")