FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
am.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  * am.c - SRv6 Masquerading Proxy (AM) 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-am/am.h>
26 
27 unsigned char function_name[] = "SRv6-AM-plugin";
28 unsigned char keyword_str[] = "End.AM";
29 unsigned char def_str[] = "Endpoint to SR-unaware appliance via masquerading";
30 unsigned char params_str[] = "nh <next-hop> oif <iface-out> iif <iface-in>";
31 
32 
33 /*****************************************/
34 /* SRv6 LocalSID instantiation and removal functions */
35 static int
37 {
39  srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
40  adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
41 
42  /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
43 
44  /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
45  nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
46  VNET_LINK_IP6, &ls_mem->nh_addr,
47  ls_mem->sw_if_index_out);
48  if (nh_adj_index == ADJ_INDEX_INVALID)
49  return -5;
50 
51  localsid->nh_adj = nh_adj_index;
52 
53 
54  /* Step 2: Prepare inbound policy for packets returning from the VNF */
55 
56  /* Sanitise the SW_IF_INDEX */
58  ls_mem->sw_if_index_in))
59  return -3;
60 
62  ls_mem->sw_if_index_in);
64  return -3;
65 
66  int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
67  ls_mem->sw_if_index_in, 1, 0, 0);
68  if (ret != 0)
69  return -1;
70 
71  return 0;
72 }
73 
74 static int
76 {
77  srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
78 
79  /* Remove hardware indirection (from sr_steering.c:137) */
80  int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
81  ls_mem->sw_if_index_in, 0, 0, 0);
82  if (ret != 0)
83  return -1;
84 
85  /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
86  adj_unlock (localsid->nh_adj);
87 
88  /* Clean up local SID memory */
89  clib_mem_free (localsid->plugin_mem);
90 
91  return 0;
92 }
93 
94 /**********************************/
95 /* SRv6 LocalSID format functions */
96 /*
97  * Prints nicely the parameters of a localsid
98  * Example: print "Table 5"
99  */
100 u8 *
101 format_srv6_am_localsid (u8 * s, va_list * args)
102 {
103  srv6_am_localsid_t *ls_mem = va_arg (*args, void *);
104 
105  vnet_main_t *vnm = vnet_get_main ();
106 
107  return (format (s,
108  "Next-hop:\t%U\n"
109  "\tOutgoing iface: %U\n"
110  "\tIncoming iface: %U",
111  format_ip6_address, &ls_mem->nh_addr.ip6,
114 }
115 
116 /*
117  * Process the parameters of a localsid
118  * Example: process from:
119  * sr localsid address cafe::1 behavior new_srv6_localsid 5
120  * everything from behavior on... so in this case 'new_srv6_localsid 5'
121  * Notice that it MUST match the keyword_str and params_str defined above.
122  */
123 uword
125 {
126  void **plugin_mem_p = va_arg (*args, void **);
127  srv6_am_localsid_t *ls_mem;
128 
129  vnet_main_t *vnm = vnet_get_main ();
130 
131  ip46_address_t nh_addr;
132  u32 sw_if_index_out;
133  u32 sw_if_index_in;
134 
135  if (unformat (input, "end.am nh %U oif %U iif %U",
136  unformat_ip6_address, &nh_addr.ip6,
137  unformat_vnet_sw_interface, vnm, &sw_if_index_out,
138  unformat_vnet_sw_interface, vnm, &sw_if_index_in))
139  {
140  /* Allocate a portion of memory */
141  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
142 
143  /* Set to zero the memory */
144  memset (ls_mem, 0, sizeof *ls_mem);
145 
146  /* Our brand-new car is ready */
147  clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
148  sizeof (ip6_address_t));
149  ls_mem->sw_if_index_out = sw_if_index_out;
150  ls_mem->sw_if_index_in = sw_if_index_in;
151 
152  /* Dont forget to add it to the localsid */
153  *plugin_mem_p = ls_mem;
154  return 1;
155  }
156  return 0;
157 }
158 
159 /*************************/
160 /* SRv6 LocalSID FIB DPO */
161 static u8 *
162 format_srv6_am_dpo (u8 * s, va_list * args)
163 {
164  index_t index = va_arg (*args, index_t);
165  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
166 
167  return (format (s, "SR: dynamic_proxy_index:[%u]", index));
168 }
169 
170 void
172 {
173 }
174 
175 void
177 {
178 }
179 
180 const static dpo_vft_t srv6_am_vft = {
182  .dv_unlock = srv6_am_dpo_unlock,
183  .dv_format = format_srv6_am_dpo,
184 };
185 
186 const static char *const srv6_am_ip6_nodes[] = {
187  "srv6-am-localsid",
188  NULL,
189 };
190 
191 const static char *const *const srv6_am_nodes[DPO_PROTO_NUM] = {
193 };
194 
195 /**********************/
196 static clib_error_t *
198 {
200  int rv = 0;
201 
202  sm->vlib_main = vm;
203  sm->vnet_main = vnet_get_main ();
204 
205  /* Create DPO */
207 
208  /* Register SRv6 LocalSID */
211  keyword_str,
212  def_str,
213  params_str,
214  &sm->srv6_am_dpo_type,
219  if (rv < 0)
220  clib_error_return (0, "SRv6 LocalSID function could not be registered.");
221  else
222  sm->srv6_localsid_behavior_id = rv;
223 
224  return 0;
225 }
226 
227 /* *INDENT-OFF* */
228 VNET_FEATURE_INIT (srv6_am_rewrite, static) =
229 {
230  .arc_name = "ip6-unicast",
231  .node_name = "srv6-am-rewrite",
232  .runs_before = 0,
233 };
234 
236 
238  .version = VPP_BUILD_VER,
239  .description = "Masquerading SRv6 proxy",
240 };
241 /* *INDENT-ON* */
242 
243 /*
244 * fd.io coding-style-patch-verification: ON
245 *
246 * Local Variables:
247 * eval: (c-set-style "gnu")
248 * End:
249 */
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:404
u32 nh_adj
Next_adj for xconnect usage only.
Definition: sr.h:122
#define CLIB_UNUSED(x)
Definition: clib.h:81
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:81
unsigned char def_str[]
Definition: am.c:29
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
static const char *const srv6_am_ip6_nodes[]
Definition: am.c:186
u32 sw_if_index_out
Outgoing iface to proxied device.
Definition: am.h:45
#define NULL
Definition: clib.h:57
VLIB_PLUGIN_REGISTER()
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 u8 * format_srv6_am_dpo(u8 *s, va_list *args)
Definition: am.c:162
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
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
void srv6_am_dpo_lock(dpo_id_t *dpo)
Definition: am.c:171
#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:252
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
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
struct _unformat_input_t unformat_input_t
static int srv6_am_localsid_creation_fn(ip6_sr_localsid_t *localsid)
Definition: am.c:36
dpo_type_t srv6_am_dpo_type
DPO type.
Definition: am.h:33
unformat_function_t unformat_ip6_address
Definition: format.h:91
uword unformat_srv6_am_localsid(unformat_input_t *input, va_list *args)
Definition: am.c:124
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:294
vnet_main_t * vnet_main
[convenience] vnet main
Definition: am.h:31
static clib_error_t * srv6_am_init(vlib_main_t *vm)
Definition: am.c:197
u32 srv6_localsid_behavior_id
SRv6 LocalSID behavior number.
Definition: am.h:35
#define clib_memcpy(a, b, c)
Definition: string.h:75
u8 * format_srv6_am_localsid(u8 *s, va_list *args)
Definition: am.c:101
#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
unsigned char keyword_str[]
Definition: am.c:28
static const char *const *const srv6_am_nodes[DPO_PROTO_NUM]
Definition: am.c:191
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:205
ip46_address_t nh_addr
Proxied device address.
Definition: am.h:44
unsigned char function_name[]
Definition: am.c:27
VNET_FEATURE_INIT(srv6_am_rewrite, static)
#define DPO_PROTO_NUM
Definition: dpo.h:70
void srv6_am_dpo_unlock(dpo_id_t *dpo)
Definition: am.c:176
u64 uword
Definition: types.h:112
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:846
srv6_am_main_t srv6_am_main
Definition: am.h:49
unsigned char params_str[]
Definition: am.c:30
vnet_sw_interface_type_t type
Definition: interface.h:718
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
static int srv6_am_localsid_removal_fn(ip6_sr_localsid_t *localsid)
Definition: am.c:75
vlib_main_t * vlib_main
[convenience] vlib main
Definition: am.h:30
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
u32 sw_if_index_in
Incoming iface from proxied device.
Definition: am.h:46