FD.io VPP  v21.06-3-gbb25fbf28
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 
33 
34 /*****************************************/
35 /* SRv6 LocalSID instantiation and removal functions */
36 static int
38 {
40  srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
41  adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
42 
43  /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
44 
45  /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
46  nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
47  VNET_LINK_IP6, &ls_mem->nh_addr,
48  ls_mem->sw_if_index_out);
49  if (nh_adj_index == ADJ_INDEX_INVALID)
50  return -5;
51 
52  localsid->nh_adj = nh_adj_index;
53 
54 
55  /* Step 2: Prepare inbound policy for packets returning from the VNF */
56 
57  /* Sanitise the SW_IF_INDEX */
59  ls_mem->sw_if_index_in))
60  return -3;
61 
63  ls_mem->sw_if_index_in);
65  return -3;
66 
67  int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
68  ls_mem->sw_if_index_in, 1, 0, 0);
69  if (ret != 0)
70  return -1;
71 
72  return 0;
73 }
74 
75 static int
77 {
78  srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
79 
80  /* Remove hardware indirection (from sr_steering.c:137) */
81  int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
82  ls_mem->sw_if_index_in, 0, 0, 0);
83  if (ret != 0)
84  return -1;
85 
86  /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
87  adj_unlock (localsid->nh_adj);
88 
89  /* Clean up local SID memory */
90  clib_mem_free (localsid->plugin_mem);
91 
92  return 0;
93 }
94 
95 /**********************************/
96 /* SRv6 LocalSID format functions */
97 /*
98  * Prints nicely the parameters of a localsid
99  * Example: print "Table 5"
100  */
101 u8 *
102 format_srv6_am_localsid (u8 * s, va_list * args)
103 {
104  srv6_am_localsid_t *ls_mem = va_arg (*args, void *);
105 
106  vnet_main_t *vnm = vnet_get_main ();
107 
108  return (format (s,
109  "Next-hop:\t%U\n"
110  "\tOutgoing iface: %U\n"
111  "\tIncoming iface: %U",
112  format_ip6_address, &ls_mem->nh_addr.ip6,
115 }
116 
117 /*
118  * Process the parameters of a localsid
119  * Example: process from:
120  * sr localsid address cafe::1 behavior new_srv6_localsid 5
121  * everything from behavior on... so in this case 'new_srv6_localsid 5'
122  * Notice that it MUST match the keyword_str and params_str defined above.
123  */
124 uword
126 {
127  void **plugin_mem_p = va_arg (*args, void **);
128  srv6_am_localsid_t *ls_mem;
129 
130  vnet_main_t *vnm = vnet_get_main ();
131 
132  ip46_address_t nh_addr;
133  u32 sw_if_index_out;
134  u32 sw_if_index_in;
135 
136  if (unformat (input, "end.am nh %U oif %U iif %U",
138  unformat_vnet_sw_interface, vnm, &sw_if_index_out,
139  unformat_vnet_sw_interface, vnm, &sw_if_index_in))
140  {
141  /* Allocate a portion of memory */
142  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
143 
144  /* Set to zero the memory */
145  clib_memset (ls_mem, 0, sizeof *ls_mem);
146 
147  /* Our brand-new car is ready */
148  clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
149  sizeof (ip6_address_t));
150  ls_mem->sw_if_index_out = sw_if_index_out;
151  ls_mem->sw_if_index_in = sw_if_index_in;
152 
153  /* Dont forget to add it to the localsid */
154  *plugin_mem_p = ls_mem;
155  return 1;
156  }
157  return 0;
158 }
159 
160 /*************************/
161 /* SRv6 LocalSID FIB DPO */
162 static u8 *
163 format_srv6_am_dpo (u8 * s, va_list * args)
164 {
165  index_t index = va_arg (*args, index_t);
166  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
167 
168  return (format (s, "SR: dynamic_proxy_index:[%u]", index));
169 }
170 
171 void
173 {
174 }
175 
176 void
178 {
179 }
180 
181 const static dpo_vft_t srv6_am_vft = {
183  .dv_unlock = srv6_am_dpo_unlock,
184  .dv_format = format_srv6_am_dpo,
185 };
186 
187 const static char *const srv6_am_ip6_nodes[] = {
188  "srv6-am-localsid",
189  NULL,
190 };
191 
192 const static char *const *const srv6_am_nodes[DPO_PROTO_NUM] = {
194 };
195 
196 /**********************/
197 static clib_error_t *
199 {
201  int rv = 0;
202 
203  sm->vlib_main = vm;
204  sm->vnet_main = vnet_get_main ();
205 
206  /* Create DPO */
208 
209  /* Register SRv6 LocalSID */
212  keyword_str,
213  def_str,
214  params_str,
215  128,
216  &sm->srv6_am_dpo_type,
221  if (rv < 0)
222  clib_error_return (0, "SRv6 LocalSID function could not be registered.");
223  else
225 
226  return 0;
227 }
228 
229 /* *INDENT-OFF* */
230 VNET_FEATURE_INIT (srv6_am_rewrite, static) =
231 {
232  .arc_name = "ip6-unicast",
233  .node_name = "srv6-am-rewrite",
234  .runs_before = 0,
235 };
236 
238 
240  .version = VPP_BUILD_VER,
241  .description = "Masquerading Segment Routing for IPv6 (SRv6) Proxy",
242 };
243 /* *INDENT-ON* */
244 
245 /*
246 * fd.io coding-style-patch-verification: ON
247 *
248 * Local Variables:
249 * eval: (c-set-style "gnu")
250 * End:
251 */
srv6_am_localsid_t
Definition: am.h:42
adj.h
dpo_register_new_type
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:349
vnet_sw_interface_t::type
vnet_sw_interface_type_t type
Definition: interface.h:870
vnet_sw_interface_t
Definition: interface.h:868
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
adj_nbr_add_or_lock
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:257
srv6_am_main_t
Definition: am.h:26
ADJ_INDEX_INVALID
#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
ip6_sr_localsid_t
SR LocalSID.
Definition: sr.h:122
sr_localsid_register_function
int sr_localsid_register_function(vlib_main_t *vm, u8 *fn_name, u8 *keyword_str, u8 *def_str, u8 *params_str, u8 prefix_length, 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:2347
srv6_am_localsid_t::sw_if_index_out
u32 sw_if_index_out
Outgoing iface to proxied device.
Definition: am.h:45
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
ip6_sr_localsid_t::plugin_mem
void * plugin_mem
Memory to be used by the plugin callback functions.
Definition: sr.h:155
keyword_str
unsigned char keyword_str[]
Definition: am.c:28
srv6_am_ip6_nodes
const static char *const srv6_am_ip6_nodes[]
Definition: am.c:187
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
adj_unlock
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:358
VNET_FEATURE_INIT
VNET_FEATURE_INIT(srv6_am_rewrite, static)
srv6_am_localsid_t::sw_if_index_in
u32 sw_if_index_in
Incoming iface from proxied device.
Definition: am.h:46
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vnet_get_sw_interface
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:58
unformat_input_t
struct _unformat_input_t unformat_input_t
am.h
srv6_am_dpo_unlock
void srv6_am_dpo_unlock(dpo_id_t *dpo)
Definition: am.c:177
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
format_srv6_am_dpo
static u8 * format_srv6_am_dpo(u8 *s, va_list *args)
Definition: am.c:163
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
unformat_srv6_am_localsid
uword unformat_srv6_am_localsid(unformat_input_t *input, va_list *args)
Definition: am.c:125
srv6_am_localsid_t::nh_addr
ip46_address_t nh_addr
Proxied device address.
Definition: am.h:44
srv6_am_main_t::vnet_main
vnet_main_t * vnet_main
[convenience] vnet main
Definition: am.h:31
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
params_str
unsigned char params_str[]
Definition: am.c:30
index_t
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:43
srv6_am_main
srv6_am_main_t srv6_am_main
Definition: am.c:32
format_srv6_am_localsid
u8 * format_srv6_am_localsid(u8 *s, va_list *args)
Definition: am.c:102
uword
u64 uword
Definition: types.h:112
srv6_am_localsid_removal_fn
static int srv6_am_localsid_removal_fn(ip6_sr_localsid_t *localsid)
Definition: am.c:76
dpo_vft_t_::dv_lock
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:428
ip6_sr_localsid_t::nh_adj
u32 nh_adj
Next_adj for xconnect usage only.
Definition: sr.h:144
vnet_interface_main_t::sw_interfaces
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:1014
plugin.h
srv6_am_nodes
const static char *const *const srv6_am_nodes[DPO_PROTO_NUM]
Definition: am.c:192
vnet_main_t
Definition: vnet.h:76
index
u32 index
Definition: flow_types.api:221
srv6_am_vft
const static dpo_vft_t srv6_am_vft
Definition: am.c:181
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
DPO_PROTO_IP6
@ DPO_PROTO_IP6
Definition: dpo.h:65
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
srv6_am_localsid_creation_fn
static int srv6_am_localsid_creation_fn(ip6_sr_localsid_t *localsid)
Definition: am.c:37
nh_addr
vl_api_address_t nh_addr
Definition: lisp_gpe.api:222
srv6_am_dpo_lock
void srv6_am_dpo_lock(dpo_id_t *dpo)
Definition: am.c:172
vnet_feature_enable_disable
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: pnat_test_stubs.h:50
adj_index_t
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
srv6_am_init
static clib_error_t * srv6_am_init(vlib_main_t *vm)
Definition: am.c:198
dpo_vft_t_
A virtual function table regisitered for a DPO type.
Definition: dpo.h:423
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
VNET_LINK_IP6
@ VNET_LINK_IP6
Definition: interface.h:348
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
format_ip6_address
format_function_t format_ip6_address
Definition: format.h:91
clib_mem_alloc_aligned_at_offset
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:222
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
unformat_ip6_address
unformat_function_t unformat_ip6_address
Definition: format.h:89
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
srv6_am_main_t::srv6_am_dpo_type
dpo_type_t srv6_am_dpo_type
DPO type.
Definition: am.h:33
VNET_SW_INTERFACE_TYPE_HARDWARE
@ VNET_SW_INTERFACE_TYPE_HARDWARE
Definition: interface.h:763
srv6_am_main_t::vlib_main
vlib_main_t * vlib_main
[convenience] vlib main
Definition: am.h:30
def_str
unsigned char def_str[]
Definition: am.c:29
srv6_am_main_t::srv6_localsid_behavior_id
u32 srv6_localsid_behavior_id
SRv6 LocalSID behavior number.
Definition: am.h:35
DPO_PROTO_NUM
#define DPO_PROTO_NUM
Definition: dpo.h:72
function_name
unsigned char function_name[]
Definition: am.c:27
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()