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.
16 *------------------------------------------------------------------
17 * as.c - SRv6 Static Proxy (AS) function
18 *------------------------------------------------------------------
21 #include <vnet/vnet.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <srv6-as/as.h>
27 #define SID_CREATE_IFACE_FEATURE_ERROR -1
28 #define SID_CREATE_INVALID_IFACE_TYPE -3
29 #define SID_CREATE_INVALID_IFACE_INDEX -4
30 #define SID_CREATE_INVALID_ADJ_INDEX -5
32 unsigned char function_name[] = "SRv6-AS-plugin";
33 unsigned char keyword_str[] = "End.AS";
34 unsigned char def_str[] =
35 "Endpoint with static proxy to SR-unaware appliance";
36 unsigned char params_str[] =
37 "nh <next-hop> oif <iface-out> iif <iface-in> src <src-addr> next <sid> [next <sid> ...]";
41 prepare_rewrite (ip6_address_t src_addr, ip6_address_t * sid_list,
44 u8 *rewrite_str = NULL;
45 u32 rewrite_len = IPv6_DEFAULT_HEADER_LENGTH;
47 u8 num_sids = vec_len (sid_list);
53 sizeof (ip6_sr_header_t) + num_sids * sizeof (ip6_address_t);
54 rewrite_len += sr_hdr_len;
57 vec_validate (rewrite_str, rewrite_len - 1);
60 ip6_header_t *iph = (ip6_header_t *) rewrite_str;
61 iph->ip_version_traffic_class_and_flow_label =
62 clib_host_to_net_u32 (0 | ((6 & 0xF) << 28));
63 iph->src_address = src_addr;
64 iph->dst_address = sid_list[0];
65 iph->payload_length = sr_hdr_len;
66 iph->hop_limit = IPv6_DEFAULT_HOP_LIMIT;
70 /* Set Next Header value to Routing Extension */
71 iph->protocol = IP_PROTOCOL_IPV6_ROUTE;
74 ip6_sr_header_t *srh = (ip6_sr_header_t *) (iph + 1);
75 srh->protocol = protocol;
76 srh->length = sr_hdr_len / 8 - 1;
77 srh->type = ROUTING_HEADER_TYPE_SR;
78 srh->segments_left = num_sids - 1;
79 srh->first_segment = num_sids - 1;
83 /* Fill segment list */
84 ip6_address_t *this_address;
85 ip6_address_t *addrp = srh->segments + srh->first_segment;
86 vec_foreach (this_address, sid_list)
88 *addrp = *this_address;
94 /* Set Next Header value to inner protocol */
95 iph->protocol = protocol;
102 free_ls_mem (srv6_as_localsid_t * ls_mem)
104 vec_free (ls_mem->rewrite);
105 vec_free (ls_mem->sid_list);
106 clib_mem_free (ls_mem);
110 /*****************************************/
111 /* SRv6 LocalSID instantiation and removal functions */
113 srv6_as_localsid_creation_fn (ip6_sr_localsid_t * localsid)
115 ip6_sr_main_t *srm = &sr_main;
116 srv6_as_main_t *sm = &srv6_as_main;
117 srv6_as_localsid_t *ls_mem = localsid->plugin_mem;
118 u32 localsid_index = localsid - srm->localsids;
120 /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
122 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
123 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
124 if (ls_mem->ip_version == DA_IP4)
125 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
126 VNET_LINK_IP4, &ls_mem->nh_addr,
127 ls_mem->sw_if_index_out);
128 else if (ls_mem->ip_version == DA_IP6)
129 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
130 VNET_LINK_IP6, &ls_mem->nh_addr,
131 ls_mem->sw_if_index_out);
132 if (nh_adj_index == ADJ_INDEX_INVALID)
134 free_ls_mem (ls_mem);
135 return SID_CREATE_INVALID_ADJ_INDEX;
138 ls_mem->nh_adj = nh_adj_index;
141 /* Step 2: Prepare inbound policy for packets returning from the VNF */
143 /* Make sure the provided incoming interface index is valid */
144 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
145 ls_mem->sw_if_index_in))
147 adj_unlock (ls_mem->nh_adj);
148 free_ls_mem (ls_mem);
149 return SID_CREATE_INVALID_IFACE_INDEX;
152 /* Retrieve associated interface structure */
153 vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
154 ls_mem->sw_if_index_in);
155 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
157 adj_unlock (ls_mem->nh_adj);
158 free_ls_mem (ls_mem);
159 return SID_CREATE_INVALID_IFACE_TYPE;
162 if (ls_mem->ip_version == DA_IP4)
164 /* Enable End.AS4 rewrite node for this interface */
166 vnet_feature_enable_disable ("ip4-unicast", "srv6-as4-rewrite",
167 ls_mem->sw_if_index_in, 1, 0, 0);
170 adj_unlock (ls_mem->nh_adj);
171 free_ls_mem (ls_mem);
172 return SID_CREATE_IFACE_FEATURE_ERROR;
175 /* Prepare rewrite string */
176 ls_mem->rewrite = prepare_rewrite (ls_mem->src_addr, ls_mem->sid_list,
177 IP_PROTOCOL_IP_IN_IP);
179 /* Associate local SID index to this interface (resize vector if needed) */
180 if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid4))
182 vec_resize (sm->sw_iface_localsid4,
183 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
184 - vec_len (sm->sw_iface_localsid4)));
186 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
188 else if (ls_mem->ip_version == DA_IP6)
190 /* Enable End.AS6 rewrite node for this interface */
192 vnet_feature_enable_disable ("ip6-unicast", "srv6-as6-rewrite",
193 ls_mem->sw_if_index_in, 1, 0, 0);
196 adj_unlock (ls_mem->nh_adj);
197 free_ls_mem (ls_mem);
198 return SID_CREATE_IFACE_FEATURE_ERROR;
201 /* Prepare rewrite string */
202 ls_mem->rewrite = prepare_rewrite (ls_mem->src_addr, ls_mem->sid_list,
205 /* Associate local SID index to this interface (resize vector if needed) */
206 if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid6))
208 vec_resize (sm->sw_iface_localsid6,
209 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
210 - vec_len (sm->sw_iface_localsid6)));
212 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
219 srv6_as_localsid_removal_fn (ip6_sr_localsid_t * localsid)
221 srv6_as_main_t *sm = &srv6_as_main;
222 srv6_as_localsid_t *ls_mem = localsid->plugin_mem;
224 if (ls_mem->ip_version == DA_IP4)
226 /* Disable End.AS4 rewrite node for this interface */
228 ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-as4-rewrite",
229 ls_mem->sw_if_index_in, 0, 0, 0);
233 /* Remove local SID index from interface table */
234 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
236 else if (ls_mem->ip_version == DA_IP6)
238 /* Disable End.AS6 rewrite node for this interface */
240 ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-as6-rewrite",
241 ls_mem->sw_if_index_in, 0, 0, 0);
245 /* Remove local SID index from interface table */
246 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
250 /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
251 adj_unlock (ls_mem->nh_adj);
253 /* Clean up local SID memory */
254 free_ls_mem (ls_mem);
259 /**********************************/
260 /* SRv6 LocalSID format functions */
262 * Prints nicely the parameters of a localsid
263 * Example: print "Table 5"
266 format_srv6_as_localsid (u8 * s, va_list * args)
268 srv6_as_localsid_t *ls_mem = va_arg (*args, void *);
270 vnet_main_t *vnm = vnet_get_main ();
272 if (ls_mem->ip_version == DA_IP4)
275 format (s, "Next-hop:\t%U\n", format_ip4_address,
276 &ls_mem->nh_addr.ip4);
281 format (s, "Next-hop:\t%U\n", format_ip6_address,
282 &ls_mem->nh_addr.ip6);
285 s = format (s, "\tOutgoing iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
286 ls_mem->sw_if_index_out);
287 s = format (s, "\tIncoming iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
288 ls_mem->sw_if_index_in);
289 s = format (s, "\tSource address:\t%U\n", format_ip6_address,
292 s = format (s, "\tSegment list:\t< ");
294 vec_foreach (addr, ls_mem->sid_list)
296 s = format (s, "%U, ", format_ip6_address, addr);
298 s = format (s, "\b\b > ");
304 * Process the parameters of a localsid
305 * Example: process from:
306 * sr localsid address cafe::1 behavior new_srv6_localsid 5
307 * everything from behavior on... so in this case 'new_srv6_localsid 5'
308 * Notice that it MUST match the keyword_str and params_str defined above.
311 unformat_srv6_as_localsid (unformat_input_t * input, va_list * args)
313 void **plugin_mem_p = va_arg (*args, void **);
314 srv6_as_localsid_t *ls_mem;
316 vnet_main_t *vnm = vnet_get_main ();
319 ip46_address_t nh_addr;
322 ip6_address_t src_addr;
323 ip6_address_t next_sid;
324 ip6_address_t *sid_list = NULL;
327 #define PARAM_AS_NH (1 << 0)
328 #define PARAM_AS_OIF (1 << 1)
329 #define PARAM_AS_IIF (1 << 2)
330 #define PARAM_AS_SRC (1 << 3)
332 if (!unformat (input, "end.as"))
335 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
337 if (!(params & PARAM_AS_NH) && unformat (input, "nh %U",
338 unformat_ip4_address,
342 params |= PARAM_AS_NH;
344 if (!(params & PARAM_AS_NH) && unformat (input, "nh %U",
345 unformat_ip6_address,
349 params |= PARAM_AS_NH;
351 else if (!(params & PARAM_AS_OIF) && unformat (input, "oif %U",
352 unformat_vnet_sw_interface,
353 vnm, &sw_if_index_out))
355 params |= PARAM_AS_OIF;
357 else if (!(params & PARAM_AS_IIF) && unformat (input, "iif %U",
358 unformat_vnet_sw_interface,
359 vnm, &sw_if_index_in))
361 params |= PARAM_AS_IIF;
363 else if (!(params & PARAM_AS_SRC) && unformat (input, "src %U",
364 unformat_ip6_address,
367 params |= PARAM_AS_SRC;
369 else if (unformat (input, "next %U", unformat_ip6_address, &next_sid))
371 vec_add1 (sid_list, next_sid);
379 /* Make sure that all parameters are supplied */
380 u8 params_chk = (PARAM_AS_NH | PARAM_AS_OIF | PARAM_AS_IIF | PARAM_AS_SRC);
381 if ((params & params_chk) != params_chk || sid_list == NULL)
387 /* Allocate and initialize memory block for local SID parameters */
388 ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
389 memset (ls_mem, 0, sizeof *ls_mem);
390 *plugin_mem_p = ls_mem;
392 /* Set local SID parameters */
393 ls_mem->ip_version = ip_version;
394 if (ip_version == DA_IP4)
395 ls_mem->nh_addr.ip4 = nh_addr.ip4;
397 ls_mem->nh_addr.ip6 = nh_addr.ip6;
398 ls_mem->sw_if_index_out = sw_if_index_out;
399 ls_mem->sw_if_index_in = sw_if_index_in;
400 ls_mem->src_addr = src_addr;
401 ls_mem->sid_list = sid_list;
406 /*************************/
407 /* SRv6 LocalSID FIB DPO */
409 format_srv6_as_dpo (u8 * s, va_list * args)
411 index_t index = va_arg (*args, index_t);
412 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
414 return (format (s, "SR: static_proxy_index:[%u]", index));
418 srv6_as_dpo_lock (dpo_id_t * dpo)
423 srv6_as_dpo_unlock (dpo_id_t * dpo)
427 const static dpo_vft_t srv6_as_vft = {
428 .dv_lock = srv6_as_dpo_lock,
429 .dv_unlock = srv6_as_dpo_unlock,
430 .dv_format = format_srv6_as_dpo,
433 const static char *const srv6_as_ip6_nodes[] = {
438 const static char *const *const srv6_as_nodes[DPO_PROTO_NUM] = {
439 [DPO_PROTO_IP6] = srv6_as_ip6_nodes,
442 /**********************/
443 static clib_error_t *
444 srv6_as_init (vlib_main_t * vm)
446 srv6_as_main_t *sm = &srv6_as_main;
450 sm->vnet_main = vnet_get_main ();
453 sm->srv6_as_dpo_type = dpo_register_new_type (&srv6_as_vft, srv6_as_nodes);
455 /* Register SRv6 LocalSID */
456 rv = sr_localsid_register_function (vm,
461 &sm->srv6_as_dpo_type,
462 format_srv6_as_localsid,
463 unformat_srv6_as_localsid,
464 srv6_as_localsid_creation_fn,
465 srv6_as_localsid_removal_fn);
467 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
469 sm->srv6_localsid_behavior_id = rv;
475 VNET_FEATURE_INIT (srv6_as4_rewrite, static) =
477 .arc_name = "ip4-unicast",
478 .node_name = "srv6-as4-rewrite",
482 VNET_FEATURE_INIT (srv6_as6_rewrite, static) =
484 .arc_name = "ip6-unicast",
485 .node_name = "srv6-as6-rewrite",
489 VLIB_INIT_FUNCTION (srv6_as_init);
491 VLIB_PLUGIN_REGISTER () = {
492 .version = VPP_BUILD_VER,
493 .description = "Static SRv6 proxy",
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")