FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
ad.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  *------------------------------------------------------------------
17  * ad.c - SRv6 Dynamic Proxy (AD) function
18  *------------------------------------------------------------------
19  */
20 
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>
26 
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>";
32 
33 
34 /*****************************************/
35 /* SRv6 LocalSID instantiation and removal functions */
36 static int
38 {
39  ip6_sr_main_t *srm = &sr_main;
41  srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
42  u32 localsid_index = localsid - srm->localsids;
43 
44  /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
45 
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)
57  return -5;
58 
59  ls_mem->nh_adj = nh_adj_index;
60 
61 
62  /* Step 2: Prepare inbound policy for packets returning from the VNF */
63 
64  /* Sanitise the SW_IF_INDEX */
66  ls_mem->sw_if_index_in))
67  return -3;
68 
70  ls_mem->sw_if_index_in);
72  return -3;
73 
74  int ret = -1;
75  if (ls_mem->ip_version == DA_IP4)
76  {
77  ret = vnet_feature_enable_disable ("ip4-unicast", "srv6-ad4-rewrite",
78  ls_mem->sw_if_index_in, 1, 0, 0);
79  if (ret != 0)
80  return -1;
81 
82  /* FIB API calls - Recursive route through the BindingSID */
83  if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid4))
84  {
85  sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
86  }
87  else
88  {
91  - vec_len (sm->sw_iface_localsid4)));
92  sm->sw_iface_localsid4[ls_mem->sw_if_index_in] = localsid_index;
93  }
94  }
95  else if (ls_mem->ip_version == DA_IP6)
96  {
97  ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-ad6-rewrite",
98  ls_mem->sw_if_index_in, 1, 0, 0);
99  if (ret != 0)
100  return -1;
101 
102  if (ls_mem->sw_if_index_in < vec_len (sm->sw_iface_localsid6))
103  {
104  sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
105  }
106  else
107  {
110  - vec_len (sm->sw_iface_localsid6)));
111  sm->sw_iface_localsid6[ls_mem->sw_if_index_in] = localsid_index;
112  }
113  }
114 
115  ls_mem->rw_len = 0;
116 
117  return 0;
118 }
119 
120 static int
122 {
124  srv6_ad_localsid_t *ls_mem = localsid->plugin_mem;
125 
126  int ret = -1;
127  if (ls_mem->ip_version == DA_IP4)
128  {
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);
132  if (ret != 0)
133  return -1;
134 
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;
137  }
138  else if (ls_mem->ip_version == DA_IP6)
139  {
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);
143  if (ret != 0)
144  return -1;
145 
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;
148  }
149 
150 
151  /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
152  adj_unlock (ls_mem->nh_adj);
153 
154  /* Clean up local SID memory */
155  clib_mem_free (localsid->plugin_mem);
156 
157  return 0;
158 }
159 
160 /**********************************/
161 /* SRv6 LocalSID format functions */
162 /*
163  * Prints nicely the parameters of a localsid
164  * Example: print "Table 5"
165  */
166 u8 *
167 format_srv6_ad_localsid (u8 * s, va_list * args)
168 {
169  srv6_ad_localsid_t *ls_mem = va_arg (*args, void *);
170 
171  vnet_main_t *vnm = vnet_get_main ();
172 
173  if (ls_mem->ip_version == DA_IP4)
174  {
175  return (format (s,
176  "Next-hop:\t%U\n"
177  "\tOutgoing iface: %U\n"
178  "\tIncoming iface: %U",
179  format_ip4_address, &ls_mem->nh_addr.ip4,
182  vnm, ls_mem->sw_if_index_in));
183  }
184  else
185  {
186  return (format (s,
187  "Next-hop:\t%U\n"
188  "\tOutgoing iface: %U\n"
189  "\tIncoming iface: %U",
190  format_ip6_address, &ls_mem->nh_addr.ip6,
193  vnm, ls_mem->sw_if_index_in));
194  }
195 }
196 
197 /*
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.
203  */
204 uword
206 {
207  void **plugin_mem_p = va_arg (*args, void **);
208  srv6_ad_localsid_t *ls_mem;
209 
210  vnet_main_t *vnm = vnet_get_main ();
211 
212  ip46_address_t nh_addr;
213  u32 sw_if_index_out;
214  u32 sw_if_index_in;
215 
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))
220  {
221  /* Allocate a portion of memory */
222  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
223 
224  /* Set to zero the memory */
225  memset (ls_mem, 0, sizeof *ls_mem);
226 
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;
233 
234  /* Dont forget to add it to the localsid */
235  *plugin_mem_p = ls_mem;
236  return 1;
237  }
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))
242  {
243  /* Allocate a portion of memory */
244  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
245 
246  /* Set to zero the memory */
247  memset (ls_mem, 0, sizeof *ls_mem);
248 
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;
255 
256  /* Dont forget to add it to the localsid */
257  *plugin_mem_p = ls_mem;
258  return 1;
259  }
260  return 0;
261 }
262 
263 /*************************/
264 /* SRv6 LocalSID FIB DPO */
265 static u8 *
266 format_srv6_ad_dpo (u8 * s, va_list * args)
267 {
268  index_t index = va_arg (*args, index_t);
269  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
270 
271  return (format (s, "SR: dynamic_proxy_index:[%u]", index));
272 }
273 
274 void
276 {
277 }
278 
279 void
281 {
282 }
283 
284 const static dpo_vft_t srv6_ad_vft = {
286  .dv_unlock = srv6_ad_dpo_unlock,
287  .dv_format = format_srv6_ad_dpo,
288 };
289 
290 const static char *const srv6_ad_ip6_nodes[] = {
291  "srv6-ad-localsid",
292  NULL,
293 };
294 
295 const static char *const *const srv6_ad_nodes[DPO_PROTO_NUM] = {
297 };
298 
299 /**********************/
300 static clib_error_t *
302 {
304  int rv = 0;
305 
306  sm->vlib_main = vm;
307  sm->vnet_main = vnet_get_main ();
308 
309  /* Create DPO */
311 
312  /* Register SRv6 LocalSID */
315  keyword_str,
316  def_str,
317  params_str,
318  &sm->srv6_ad_dpo_type,
323  if (rv < 0)
324  clib_error_return (0, "SRv6 LocalSID function could not be registered.");
325  else
326  sm->srv6_localsid_behavior_id = rv;
327 
328  return 0;
329 }
330 
331 /* *INDENT-OFF* */
332 VNET_FEATURE_INIT (srv6_ad4_rewrite, static) =
333 {
334  .arc_name = "ip4-unicast",
335  .node_name = "srv6-ad4-rewrite",
336  .runs_before = 0,
337 };
338 
339 VNET_FEATURE_INIT (srv6_ad6_rewrite, static) =
340 {
341  .arc_name = "ip6-unicast",
342  .node_name = "srv6-ad6-rewrite",
343  .runs_before = 0,
344 };
345 
347 
349  .version = VPP_BUILD_VER,
350  .description = "Dynamic SRv6 proxy",
351 };
352 /* *INDENT-ON* */
353 
354 /*
355 * fd.io coding-style-patch-verification: ON
356 *
357 * Local Variables:
358 * eval: (c-set-style "gnu")
359 * End:
360 */
ip6_sr_main_t sr_main
Definition: sr.c:31
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:404
vnet_main_t * vnet_main
[convenience] vnet main
Definition: ad.h:34
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:399
static void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset, int os_out_of_memory_on_failure)
Definition: mem.h:75
SR LocalSID.
Definition: sr.h:102
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define NULL
Definition: clib.h:55
u32 * sw_iface_localsid4
Retrieve local SID from iface.
Definition: ad.h:40
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unformat_function_t unformat_vnet_sw_interface
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:140
#define DA_IP4
Definition: ad.h:26
format_function_t format_ip4_address
Definition: format.h:81
u32 srv6_localsid_behavior_id
SRv6 LocalSID behavior number.
Definition: ad.h:38
#define DA_IP6
Definition: ad.h:27
unformat_function_t unformat_ip4_address
Definition: format.h:76
u8 ip_version
Definition: ad.h:53
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
#define clib_error_return(e, args...)
Definition: error.h:99
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:240
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:240
unsigned int u32
Definition: types.h:88
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:341
u32 nh_adj
Adjacency index for out.
Definition: ad.h:52
ip46_address_t nh_addr
Proxied device address.
Definition: ad.h:50
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:168
#define ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
u32 sw_if_index_out
Outgoing iface to proxied dev.
Definition: ad.h:51
struct _unformat_input_t unformat_input_t
u32 rw_len
Number of bits to be rewritten.
Definition: ad.h:56
vlib_main_t * vlib_main
[convenience] vlib main
Definition: ad.h:33
unformat_function_t unformat_ip6_address
Definition: format.h:97
ip6_sr_localsid_t * localsids
Definition: sr.h:204
format_function_t format_ip6_address
Definition: format.h:99
vlib_main_t * vm
Definition: buffer.c:294
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:271
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
void * plugin_mem
Memory to be used by the plugin callback functions.
Definition: sr.h:124
static void clib_mem_free(void *p)
Definition: mem.h:179
dpo_type_t srv6_ad_dpo_type
DPO type.
Definition: ad.h:36
#define DPO_PROTO_NUM
Definition: dpo.h:70
u32 * sw_iface_localsid6
Retrieve local SID from iface.
Definition: ad.h:41
u32 sw_if_index_in
Incoming iface from proxied dev.
Definition: ad.h:55
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
srv6_ad_main_t srv6_ad_main
Definition: ad.h:60
u64 uword
Definition: types.h:112
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:802
Segment Routing main datastructure.
Definition: sr.h:189
vnet_sw_interface_type_t type
Definition: interface.h:656
int sr_localsid_register_function(vlib_main_t *vm, u8 *fn_name, u8 *keyword_str, u8 *def_str, u8 *params_str, dpo_type_t *dpo, format_function_t *ls_format, unformat_function_t *ls_unformat, sr_plugin_callback_t *creation_fn, sr_plugin_callback_t *removal_fn)
SR LocalSID plugin registry.
Definition: sr_localsid.c:1525
adj_index_t adj_nbr_add_or_lock(fib_protocol_t nh_proto, vnet_link_t link_type, const ip46_address_t *nh_addr, u32 sw_if_index)
Neighbour Adjacency sub-type.
Definition: adj_nbr.c:214
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:233