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;
117 /* Step 3: Initialize rewrite counters */
118 srv6_ad_localsid_t **ls_p;
119 pool_get (sm->sids, ls_p);
121 ls_mem->index = ls_p - sm->sids;
123 vlib_validate_combined_counter (&(sm->valid_counters), ls_mem->index);
124 vlib_validate_combined_counter (&(sm->invalid_counters), ls_mem->index);
126 vlib_zero_combined_counter (&(sm->valid_counters), ls_mem->index);
127 vlib_zero_combined_counter (&(sm->invalid_counters), ls_mem->index);
133 srv6_ad_localsid_removal_fn (ip6_sr_localsid_t * localsid)
135 srv6_ad_main_t *sm = &srv6_ad_main;
136 srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
139 if (ls_mem->ip_version == DA_IP4)
141 /* Remove hardware indirection (from sr_steering.c:137) */
142 ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
143 ls_mem->sw_if_index_in, 0, 0, 0);
147 /* Remove local SID pointer from interface table (from sr_steering.c:139) */
148 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
150 else if (ls_mem->ip_version == DA_IP6)
152 /* Remove hardware indirection (from sr_steering.c:137) */
153 ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
154 ls_mem->sw_if_index_in, 0, 0, 0);
158 /* Remove local SID pointer from interface table (from sr_steering.c:139) */
159 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
163 /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
164 adj_unlock (ls_mem->nh_adj);
166 /* Delete SID entry */
167 pool_put (sm->sids, pool_elt_at_index (sm->sids, ls_mem->index));
169 /* Clean up local SID memory */
170 clib_mem_free (localsid->plugin_mem);
175 /**********************************/
176 /* SRv6 LocalSID format functions */
178 * Prints nicely the parameters of a localsid
179 * Example: print "Table 5"
182 format_srv6_ad_localsid (u8 * s, va_list * args)
184 srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
186 vnet_main_t *vnm = vnet_get_main ();
187 srv6_ad_main_t *sm = &srv6_ad_main;
189 if (ls_mem->ip_version == DA_IP4)
192 format (s, "Next-hop:\t%U\n", format_ip4_address,
193 &ls_mem->nh_addr.ip4);
198 format (s, "Next-hop:\t%U\n", format_ip6_address,
199 &ls_mem->nh_addr.ip6);
202 s = format (s, "\tOutgoing iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
203 ls_mem->sw_if_index_out);
204 s = format (s, "\tIncoming iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
205 ls_mem->sw_if_index_in);
207 vlib_counter_t valid, invalid;
208 vlib_get_combined_counter (&(sm->valid_counters), ls_mem->index, &valid);
209 vlib_get_combined_counter (&(sm->invalid_counters), ls_mem->index,
211 s = format (s, "\tGood rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
212 valid.packets, valid.bytes);
213 s = format (s, "\tBad rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
214 invalid.packets, invalid.bytes);
220 * Process the parameters of a localsid
221 * Example: process from:
222 * sr localsid address cafe::1 behavior new_srv6_localsid 5
223 * everything from behavior on... so in this case 'new_srv6_localsid 5'
224 * Notice that it MUST match the keyword_str and params_str defined above.
227 unformat_srv6_ad_localsid (unformat_input_t * input, va_list * args)
229 void **plugin_mem_p = va_arg (*args, void **);
230 srv6_ad_localsid_t *ls_mem;
232 vnet_main_t *vnm = vnet_get_main ();
234 ip46_address_t nh_addr;
238 if (unformat (input, "end.ad nh %U oif %U iif %U",
239 unformat_ip4_address, &nh_addr.ip4,
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_IP4;
251 clib_memcpy (&ls_mem->nh_addr.ip4, &nh_addr.ip4,
252 sizeof (ip4_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;
260 else if (unformat (input, "end.ad nh %U oif %U iif %U",
261 unformat_ip6_address, &nh_addr.ip6,
262 unformat_vnet_sw_interface, vnm, &sw_if_index_out,
263 unformat_vnet_sw_interface, vnm, &sw_if_index_in))
265 /* Allocate a portion of memory */
266 ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
268 /* Set to zero the memory */
269 memset (ls_mem, 0, sizeof *ls_mem);
271 /* Our brand-new car is ready */
272 ls_mem->ip_version = DA_IP6;
273 clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
274 sizeof (ip6_address_t));
275 ls_mem->sw_if_index_out = sw_if_index_out;
276 ls_mem->sw_if_index_in = sw_if_index_in;
278 /* Dont forget to add it to the localsid */
279 *plugin_mem_p = ls_mem;
285 /*************************/
286 /* SRv6 LocalSID FIB DPO */
288 format_srv6_ad_dpo (u8 * s, va_list * args)
290 index_t index = va_arg (*args, index_t);
291 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
293 return (format (s, "SR: dynamic_proxy_index:[%u]", index));
297 srv6_ad_dpo_lock (dpo_id_t * dpo)
302 srv6_ad_dpo_unlock (dpo_id_t * dpo)
306 const static dpo_vft_t srv6_ad_vft = {
307 .dv_lock = srv6_ad_dpo_lock,
308 .dv_unlock = srv6_ad_dpo_unlock,
309 .dv_format = format_srv6_ad_dpo,
312 const static char *const srv6_ad_ip6_nodes[] = {
317 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
318 [DPO_PROTO_IP6] = srv6_ad_ip6_nodes,
321 /**********************/
322 static clib_error_t *
323 srv6_ad_init (vlib_main_t * vm)
325 srv6_ad_main_t *sm = &srv6_ad_main;
329 sm->vnet_main = vnet_get_main ();
332 sm->srv6_ad_dpo_type = dpo_register_new_type (&srv6_ad_vft, srv6_ad_nodes);
334 /* Register SRv6 LocalSID */
335 rv = sr_localsid_register_function (vm,
340 &sm->srv6_ad_dpo_type,
341 format_srv6_ad_localsid,
342 unformat_srv6_ad_localsid,
343 srv6_ad_localsid_creation_fn,
344 srv6_ad_localsid_removal_fn);
346 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
348 sm->srv6_localsid_behavior_id = rv;
354 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
356 .arc_name = "ip4-unicast",
357 .node_name = "srv6-ad4-rewrite",
361 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
363 .arc_name = "ip6-unicast",
364 .node_name = "srv6-ad6-rewrite",
368 VLIB_INIT_FUNCTION (srv6_ad_init);
370 VLIB_PLUGIN_REGISTER () = {
371 .version = VPP_BUILD_VER,
372 .description = "Dynamic SRv6 proxy",
377 * fd.io coding-style-patch-verification: ON
380 * eval: (c-set-style "gnu")