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 #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-AD-plugin";
33 unsigned char keyword_str[] = "End.AD";
34 unsigned char def_str[] =
35 "Endpoint with dynamic proxy to SR-unaware appliance";
36 unsigned char params_str[] = "nh <next-hop> oif <iface-out> iif <iface-in>";
38 srv6_ad_main_t srv6_ad_main;
40 /*****************************************/
41 /* SRv6 LocalSID instantiation and removal functions */
43 srv6_ad_localsid_creation_fn (ip6_sr_localsid_t * localsid)
45 ip6_sr_main_t *srm = &sr_main;
46 srv6_ad_main_t *sm = &srv6_ad_main;
47 srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
48 u32 localsid_index = localsid - srm->localsids;
50 /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
52 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
53 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
54 if (ls_mem->inner_type != AD_TYPE_L2)
56 if (ls_mem->inner_type == AD_TYPE_IP4)
57 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4,
58 VNET_LINK_IP4, &ls_mem->nh_addr,
59 ls_mem->sw_if_index_out);
60 else if (ls_mem->inner_type == AD_TYPE_IP6)
61 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
62 VNET_LINK_IP6, &ls_mem->nh_addr,
63 ls_mem->sw_if_index_out);
64 if (nh_adj_index == ADJ_INDEX_INVALID)
66 clib_mem_free (ls_mem);
67 return SID_CREATE_INVALID_ADJ_INDEX;
71 ls_mem->nh_adj = nh_adj_index;
74 /* Step 2: Prepare inbound policy for packets returning from the VNF */
76 /* Sanitise the SW_IF_INDEX */
77 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
78 ls_mem->sw_if_index_in))
80 adj_unlock (ls_mem->nh_adj);
81 clib_mem_free (ls_mem);
82 return SID_CREATE_INVALID_IFACE_INDEX;
85 vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
86 ls_mem->sw_if_index_in);
87 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
89 adj_unlock (ls_mem->nh_adj);
90 clib_mem_free (ls_mem);
91 return SID_CREATE_INVALID_IFACE_TYPE;
94 if (ls_mem->inner_type == AD_TYPE_L2)
96 /* Enable End.AD2 rewrite node for this interface */
98 vnet_feature_enable_disable ("device-input", "srv6-ad2-rewrite",
99 ls_mem->sw_if_index_in, 1, 0, 0);
102 clib_mem_free (ls_mem);
103 return SID_CREATE_IFACE_FEATURE_ERROR;
106 /* Set interface in promiscuous mode */
107 vnet_main_t *vnm = vnet_get_main ();
108 vnet_hw_interface_t *hi =
109 vnet_get_sup_hw_interface (vnm, ls_mem->sw_if_index_in);
110 /* Make sure it is main interface */
111 if (hi->sw_if_index == ls_mem->sw_if_index_in)
112 ethernet_set_flags (vnm, hi->hw_if_index,
113 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
115 /* Associate local SID index to this interface (resize vector if needed) */
116 if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid2))
118 vec_resize (sm->sw_iface_localsid2,
119 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
120 - vec_len (sm->sw_iface_localsid2)));
122 sm->sw_iface_localsid2[ls_mem->sw_if_index_in] = localsid_index;
124 else if (ls_mem->inner_type == AD_TYPE_IP4)
126 /* Enable End.AD4 rewrite node for this interface */
128 vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
129 ls_mem->sw_if_index_in, 1, 0, 0);
132 adj_unlock (ls_mem->nh_adj);
133 clib_mem_free (ls_mem);
134 return SID_CREATE_IFACE_FEATURE_ERROR;
137 /* Associate local SID index to this interface (resize vector if needed) */
138 if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid4))
140 vec_resize (sm->sw_iface_localsid4,
141 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
142 - vec_len (sm->sw_iface_localsid4)));
144 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
146 else if (ls_mem->inner_type == AD_TYPE_IP6)
148 /* Enable End.AD6 rewrite node for this interface */
150 vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
151 ls_mem->sw_if_index_in, 1, 0, 0);
154 adj_unlock (ls_mem->nh_adj);
155 clib_mem_free (ls_mem);
156 return SID_CREATE_IFACE_FEATURE_ERROR;
159 /* Associate local SID index to this interface (resize vector if needed) */
160 if (ls_mem->sw_if_index_in >= vec_len (sm->sw_iface_localsid6))
162 vec_resize (sm->sw_iface_localsid6,
163 (pool_len (sm->vnet_main->interface_main.sw_interfaces)
164 - vec_len (sm->sw_iface_localsid6)));
166 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
171 /* Step 3: Initialize rewrite counters */
172 srv6_ad_localsid_t **ls_p;
173 pool_get (sm->sids, ls_p);
175 ls_mem->index = ls_p - sm->sids;
177 vlib_validate_combined_counter (&(sm->valid_counters), ls_mem->index);
178 vlib_validate_combined_counter (&(sm->invalid_counters), ls_mem->index);
180 vlib_zero_combined_counter (&(sm->valid_counters), ls_mem->index);
181 vlib_zero_combined_counter (&(sm->invalid_counters), ls_mem->index);
187 srv6_ad_localsid_removal_fn (ip6_sr_localsid_t * localsid)
189 srv6_ad_main_t *sm = &srv6_ad_main;
190 srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
192 if (ls_mem->inner_type == AD_TYPE_L2)
194 /* Disable End.AD2 rewrite node for this interface */
196 vnet_feature_enable_disable ("device-input", "srv6-ad2-rewrite",
197 ls_mem->sw_if_index_in, 0, 0, 0);
201 /* Disable promiscuous mode on the interface */
202 vnet_main_t *vnm = vnet_get_main ();
203 vnet_hw_interface_t *hi =
204 vnet_get_sup_hw_interface (vnm, ls_mem->sw_if_index_in);
205 /* Make sure it is main interface */
206 if (hi->sw_if_index == ls_mem->sw_if_index_in)
207 ethernet_set_flags (vnm, hi->hw_if_index, 0);
209 /* Remove local SID index from interface table */
210 sm->sw_iface_localsid2[ls_mem->sw_if_index_in] = ~(u32) 0;
212 else if (ls_mem->inner_type == AD_TYPE_IP4)
214 /* Disable End.AD4 rewrite node for this interface */
216 vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
217 ls_mem->sw_if_index_in, 0, 0, 0);
221 /* Remove local SID pointer from interface table */
222 sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = ~(u32) 0;
224 else if (ls_mem->inner_type == AD_TYPE_IP6)
226 /* Disable End.AD6 rewrite node for this interface */
228 vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
229 ls_mem->sw_if_index_in, 0, 0, 0);
233 /* Remove local SID pointer from interface table */
234 sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = ~(u32) 0;
238 /* Unlock (OIF, NHOP) adjacency */
239 adj_unlock (ls_mem->nh_adj);
241 /* Delete SID entry */
242 pool_put (sm->sids, pool_elt_at_index (sm->sids, ls_mem->index));
244 /* Clean up local SID memory */
245 vec_free (ls_mem->rewrite);
246 clib_mem_free (localsid->plugin_mem);
251 /**********************************/
252 /* SRv6 LocalSID format functions */
254 * Prints nicely the parameters of a localsid
255 * Example: print "Table 5"
258 format_srv6_ad_localsid (u8 * s, va_list * args)
260 srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
262 vnet_main_t *vnm = vnet_get_main ();
263 srv6_ad_main_t *sm = &srv6_ad_main;
265 if (ls_mem->inner_type == AD_TYPE_IP4)
268 format (s, "Next-hop:\t%U\n\t", format_ip4_address,
269 &ls_mem->nh_addr.ip4);
271 else if (ls_mem->inner_type == AD_TYPE_IP6)
274 format (s, "Next-hop:\t%U\n\t", format_ip6_address,
275 &ls_mem->nh_addr.ip6);
278 s = format (s, "Outgoing iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
279 ls_mem->sw_if_index_out);
280 s = format (s, "\tIncoming iface:\t%U\n", format_vnet_sw_if_index_name, vnm,
281 ls_mem->sw_if_index_in);
283 vlib_counter_t valid, invalid;
284 vlib_get_combined_counter (&(sm->valid_counters), ls_mem->index, &valid);
285 vlib_get_combined_counter (&(sm->invalid_counters), ls_mem->index,
287 s = format (s, "\tGood rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
288 valid.packets, valid.bytes);
289 s = format (s, "\tBad rewrite traffic: \t[%Ld packets : %Ld bytes]\n",
290 invalid.packets, invalid.bytes);
296 * Process the parameters of a localsid
297 * Example: process from:
298 * sr localsid address cafe::1 behavior new_srv6_localsid 5
299 * everything from behavior on... so in this case 'new_srv6_localsid 5'
300 * Notice that it MUST match the keyword_str and params_str defined above.
303 unformat_srv6_ad_localsid (unformat_input_t * input, va_list * args)
305 void **plugin_mem_p = va_arg (*args, void **);
306 srv6_ad_localsid_t *ls_mem;
308 vnet_main_t *vnm = vnet_get_main ();
310 u8 inner_type = AD_TYPE_L2;
311 ip46_address_t nh_addr;
316 #define PARAM_AD_NH (1 << 0)
317 #define PARAM_AD_OIF (1 << 1)
318 #define PARAM_AD_IIF (1 << 2)
320 if (!unformat (input, "end.ad"))
323 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
325 if (!(params & PARAM_AD_NH) && unformat (input, "nh %U",
326 unformat_ip4_address,
329 inner_type = AD_TYPE_IP4;
330 params |= PARAM_AD_NH;
332 if (!(params & PARAM_AD_NH) && unformat (input, "nh %U",
333 unformat_ip6_address,
336 inner_type = AD_TYPE_IP6;
337 params |= PARAM_AD_NH;
339 else if (!(params & PARAM_AD_OIF) && unformat (input, "oif %U",
340 unformat_vnet_sw_interface,
341 vnm, &sw_if_index_out))
343 params |= PARAM_AD_OIF;
345 else if (!(params & PARAM_AD_IIF) && unformat (input, "iif %U",
346 unformat_vnet_sw_interface,
347 vnm, &sw_if_index_in))
349 params |= PARAM_AD_IIF;
357 /* Make sure that all parameters are supplied */
358 u8 params_chk = (PARAM_AD_OIF | PARAM_AD_IIF);
359 if ((params & params_chk) != params_chk)
364 /* Allocate and initialize memory block for local SID parameters */
365 ls_mem = clib_mem_alloc (sizeof *ls_mem);
366 clib_memset (ls_mem, 0, sizeof *ls_mem);
367 *plugin_mem_p = ls_mem;
369 /* Set local SID parameters */
370 ls_mem->inner_type = inner_type;
371 if (inner_type == AD_TYPE_IP4)
372 ls_mem->nh_addr.ip4 = nh_addr.ip4;
373 else if (inner_type == AD_TYPE_IP6)
374 ls_mem->nh_addr.ip6 = nh_addr.ip6;
375 ls_mem->sw_if_index_out = sw_if_index_out;
376 ls_mem->sw_if_index_in = sw_if_index_in;
381 /*************************/
382 /* SRv6 LocalSID FIB DPO */
384 format_srv6_ad_dpo (u8 * s, va_list * args)
386 index_t index = va_arg (*args, index_t);
387 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
389 return (format (s, "SR: dynamic_proxy_index:[%u]", index));
393 srv6_ad_dpo_lock (dpo_id_t * dpo)
398 srv6_ad_dpo_unlock (dpo_id_t * dpo)
402 const static dpo_vft_t srv6_ad_vft = {
403 .dv_lock = srv6_ad_dpo_lock,
404 .dv_unlock = srv6_ad_dpo_unlock,
405 .dv_format = format_srv6_ad_dpo,
408 const static char *const srv6_ad_ip6_nodes[] = {
413 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
414 [DPO_PROTO_IP6] = srv6_ad_ip6_nodes,
417 /**********************/
418 static clib_error_t *
419 srv6_ad_init (vlib_main_t * vm)
421 srv6_ad_main_t *sm = &srv6_ad_main;
425 sm->vnet_main = vnet_get_main ();
428 sm->srv6_ad_dpo_type = dpo_register_new_type (&srv6_ad_vft, srv6_ad_nodes);
430 /* Register SRv6 LocalSID */
431 rv = sr_localsid_register_function (vm,
437 &sm->srv6_ad_dpo_type,
438 format_srv6_ad_localsid,
439 unformat_srv6_ad_localsid,
440 srv6_ad_localsid_creation_fn,
441 srv6_ad_localsid_removal_fn);
443 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
445 sm->srv6_localsid_behavior_id = rv;
450 VNET_FEATURE_INIT (srv6_ad2_rewrite, static) =
452 .arc_name = "device-input",
453 .node_name = "srv6-ad2-rewrite",
454 .runs_before = VNET_FEATURES ("ethernet-input"),
457 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
459 .arc_name = "ip4-unicast",
460 .node_name = "srv6-ad4-rewrite",
464 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
466 .arc_name = "ip6-unicast",
467 .node_name = "srv6-ad6-rewrite",
471 VLIB_INIT_FUNCTION (srv6_ad_init);
473 VLIB_PLUGIN_REGISTER () = {
474 .version = VPP_BUILD_VER,
475 .description = "Dynamic Segment Routing for IPv6 (SRv6) Proxy",
479 * fd.io coding-style-patch-verification: ON
482 * eval: (c-set-style "gnu")