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 * ad.c - SRv6 Dynamic Proxy (AD) 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-ad/ad.h>
27 unsigned char function_name[] = "SRv6-AD-plugin";
28 unsigned char keyword_str[] = "End.AD";
29 unsigned char def_str[] =
30 "Endpoint with dynamic proxy to SR-unaware appliance";
31 unsigned char params_str[] = "nh <next-hop> oif <iface-out> iif <iface-in>";
34 /*****************************************/
35 /* SRv6 LocalSID instantiation and removal functions */
37 srv6_ad_localsid_creation_fn (ip6_sr_localsid_t * localsid)
39 ip6_sr_main_t *srm = &sr_main;
40 srv6_ad_main_t *sm = &srv6_ad_main;
41 srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
42 u32 localsid_index = localsid - srm->localsids;
44 /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
46 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
47 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
48 if (ls_mem->ip_version == DA_IP4)
49 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
50 VNET_LINK_IP4, &ls_mem->nh_addr,
51 ls_mem->sw_if_index_out);
52 else if (ls_mem->ip_version == DA_IP6)
53 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
54 VNET_LINK_IP6, &ls_mem->nh_addr,
55 ls_mem->sw_if_index_out);
56 if (nh_adj_index == ADJ_INDEX_INVALID)
59 ls_mem->nh_adj = nh_adj_index;
62 /* Step 2: Prepare inbound policy for packets returning from the VNF */
64 /* Sanitise the SW_IF_INDEX */
65 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
66 ls_mem->sw_if_index_in))
69 vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
70 ls_mem->sw_if_index_in);
71 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
75 if (ls_mem->ip_version == DA_IP4)
77 ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
78 ls_mem->sw_if_index_in, 1, 0, 0);
82 /* FIB API calls - Recursive route through the BindingSID */
83 if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid4))
85 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
89 vec_resize (sm->sw_iface_localsid4,
90 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
91 - vec_len (sm->sw_iface_localsid4)));
92 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
95 else if (ls_mem->ip_version == DA_IP6)
97 ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
98 ls_mem->sw_if_index_in, 1, 0, 0);
102 if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid6))
104 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
108 vec_resize (sm->sw_iface_localsid6,
109 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
110 - vec_len (sm->sw_iface_localsid6)));
111 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
121 srv6_ad_localsid_removal_fn (ip6_sr_localsid_t * localsid)
123 srv6_ad_main_t *sm = &srv6_ad_main;
124 srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
127 if (ls_mem->ip_version == DA_IP4)
129 /* Remove hardware indirection (from sr_steering.c:137) */
130 ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
131 ls_mem->sw_if_index_in, 0, 0, 0);
135 /* Remove local SID pointer from interface table (from sr_steering.c:139) */
136 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
138 else if (ls_mem->ip_version == DA_IP6)
140 /* Remove hardware indirection (from sr_steering.c:137) */
141 ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
142 ls_mem->sw_if_index_in, 0, 0, 0);
146 /* Remove local SID pointer from interface table (from sr_steering.c:139) */
147 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
151 /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
152 adj_unlock (ls_mem->nh_adj);
154 /* Clean up local SID memory */
155 clib_mem_free (localsid->plugin_mem);
160 /**********************************/
161 /* SRv6 LocalSID format functions */
163 * Prints nicely the parameters of a localsid
164 * Example: print "Table 5"
167 format_srv6_ad_localsid (u8 * s, va_list * args)
169 srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
171 vnet_main_t *vnm = vnet_get_main ();
173 if (ls_mem->ip_version == DA_IP4)
177 "\tOutgoing iface: %U\n"
178 "\tIncoming iface: %U",
179 format_ip4_address, &ls_mem->nh_addr.ip4,
180 format_vnet_sw_if_index_name, vnm,
181 ls_mem->sw_if_index_out, format_vnet_sw_if_index_name,
182 vnm, ls_mem->sw_if_index_in));
188 "\tOutgoing iface: %U\n"
189 "\tIncoming iface: %U",
190 format_ip6_address, &ls_mem->nh_addr.ip6,
191 format_vnet_sw_if_index_name, vnm,
192 ls_mem->sw_if_index_out, format_vnet_sw_if_index_name,
193 vnm, ls_mem->sw_if_index_in));
198 * Process the parameters of a localsid
199 * Example: process from:
200 * sr localsid address cafe::1 behavior new_srv6_localsid 5
201 * everything from behavior on... so in this case 'new_srv6_localsid 5'
202 * Notice that it MUST match the keyword_str and params_str defined above.
205 unformat_srv6_ad_localsid (unformat_input_t * input, va_list * args)
207 void **plugin_mem_p = va_arg (*args, void **);
208 srv6_ad_localsid_t *ls_mem;
210 vnet_main_t *vnm = vnet_get_main ();
212 ip46_address_t nh_addr;
216 if (unformat (input, "end.ad nh %U oif %U iif %U",
217 unformat_ip4_address, &nh_addr.ip4,
218 unformat_vnet_sw_interface, vnm, &sw_if_index_out,
219 unformat_vnet_sw_interface, vnm, &sw_if_index_in))
221 /* Allocate a portion of memory */
222 ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
224 /* Set to zero the memory */
225 memset (ls_mem, 0, sizeof *ls_mem);
227 /* Our brand-new car is ready */
228 ls_mem->ip_version = DA_IP4;
229 clib_memcpy (&ls_mem->nh_addr.ip4, &nh_addr.ip4,
230 sizeof (ip4_address_t));
231 ls_mem->sw_if_index_out = sw_if_index_out;
232 ls_mem->sw_if_index_in = sw_if_index_in;
234 /* Dont forget to add it to the localsid */
235 *plugin_mem_p = ls_mem;
238 else if (unformat (input, "end.ad nh %U oif %U iif %U",
239 unformat_ip6_address, &nh_addr.ip6,
240 unformat_vnet_sw_interface, vnm, &sw_if_index_out,
241 unformat_vnet_sw_interface, vnm, &sw_if_index_in))
243 /* Allocate a portion of memory */
244 ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
246 /* Set to zero the memory */
247 memset (ls_mem, 0, sizeof *ls_mem);
249 /* Our brand-new car is ready */
250 ls_mem->ip_version = DA_IP6;
251 clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
252 sizeof (ip6_address_t));
253 ls_mem->sw_if_index_out = sw_if_index_out;
254 ls_mem->sw_if_index_in = sw_if_index_in;
256 /* Dont forget to add it to the localsid */
257 *plugin_mem_p = ls_mem;
263 /*************************/
264 /* SRv6 LocalSID FIB DPO */
266 format_srv6_ad_dpo (u8 * s, va_list * args)
268 index_t index = va_arg (*args, index_t);
269 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
271 return (format (s, "SR: dynamic_proxy_index:[%u]", index));
275 srv6_ad_dpo_lock (dpo_id_t * dpo)
280 srv6_ad_dpo_unlock (dpo_id_t * dpo)
284 const static dpo_vft_t srv6_ad_vft = {
285 .dv_lock = srv6_ad_dpo_lock,
286 .dv_unlock = srv6_ad_dpo_unlock,
287 .dv_format = format_srv6_ad_dpo,
290 const static char *const srv6_ad_ip6_nodes[] = {
295 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
296 [DPO_PROTO_IP6] = srv6_ad_ip6_nodes,
299 /**********************/
300 static clib_error_t *
301 srv6_ad_init (vlib_main_t * vm)
303 srv6_ad_main_t *sm = &srv6_ad_main;
307 sm->vnet_main = vnet_get_main ();
310 sm->srv6_ad_dpo_type = dpo_register_new_type (&srv6_ad_vft, srv6_ad_nodes);
312 /* Register SRv6 LocalSID */
313 rv = sr_localsid_register_function (vm,
318 &sm->srv6_ad_dpo_type,
319 format_srv6_ad_localsid,
320 unformat_srv6_ad_localsid,
321 srv6_ad_localsid_creation_fn,
322 srv6_ad_localsid_removal_fn);
324 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
326 sm->srv6_localsid_behavior_id = rv;
332 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
334 .arc_name = "ip4-unicast",
335 .node_name = "srv6-ad4-rewrite",
339 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
341 .arc_name = "ip6-unicast",
342 .node_name = "srv6-ad6-rewrite",
346 VLIB_INIT_FUNCTION (srv6_ad_init);
348 VLIB_PLUGIN_REGISTER () = {
349 .version = VPP_BUILD_VER,
350 .description = "Dynamic SRv6 proxy",
355 * fd.io coding-style-patch-verification: ON
358 * eval: (c-set-style "gnu")