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 * am.c - SRv6 Masquerading Proxy (AM) 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-am/am.h>
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>";
33 /*****************************************/
34 /* SRv6 LocalSID instantiation and removal functions */
36 srv6_am_localsid_creation_fn (ip6_sr_localsid_t * localsid)
38 srv6_am_main_t *sm = &srv6_am_main;
39 srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
40 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
42 /* Step 1: Prepare xconnect adjacency for sending packets to the VNF */
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)
51 localsid->nh_adj = nh_adj_index;
54 /* Step 2: Prepare inbound policy for packets returning from the VNF */
56 /* Sanitise the SW_IF_INDEX */
57 if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
58 ls_mem->sw_if_index_in))
61 vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
62 ls_mem->sw_if_index_in);
63 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
66 int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
67 ls_mem->sw_if_index_in, 1, 0, 0);
75 srv6_am_localsid_removal_fn (ip6_sr_localsid_t * localsid)
77 srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
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);
85 /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
86 adj_unlock (localsid->nh_adj);
88 /* Clean up local SID memory */
89 clib_mem_free (localsid->plugin_mem);
94 /**********************************/
95 /* SRv6 LocalSID format functions */
97 * Prints nicely the parameters of a localsid
98 * Example: print "Table 5"
101 format_srv6_am_localsid (u8 * s, va_list * args)
103 srv6_am_localsid_t *ls_mem = va_arg (*args, void *);
105 vnet_main_t *vnm = vnet_get_main ();
109 "\tOutgoing iface: %U\n"
110 "\tIncoming iface: %U",
111 format_ip6_address, &ls_mem->nh_addr.ip6,
112 format_vnet_sw_if_index_name, vnm, ls_mem->sw_if_index_out,
113 format_vnet_sw_if_index_name, vnm, ls_mem->sw_if_index_in));
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.
124 unformat_srv6_am_localsid (unformat_input_t * input, va_list * args)
126 void **plugin_mem_p = va_arg (*args, void **);
127 srv6_am_localsid_t *ls_mem;
129 vnet_main_t *vnm = vnet_get_main ();
131 ip46_address_t nh_addr;
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))
140 /* Allocate a portion of memory */
141 ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
143 /* Set to zero the memory */
144 memset (ls_mem, 0, sizeof *ls_mem);
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;
152 /* Dont forget to add it to the localsid */
153 *plugin_mem_p = ls_mem;
159 /*************************/
160 /* SRv6 LocalSID FIB DPO */
162 format_srv6_am_dpo (u8 * s, va_list * args)
164 index_t index = va_arg (*args, index_t);
165 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
167 return (format (s, "SR: dynamic_proxy_index:[%u]", index));
171 srv6_am_dpo_lock (dpo_id_t * dpo)
176 srv6_am_dpo_unlock (dpo_id_t * dpo)
180 const static dpo_vft_t srv6_am_vft = {
181 .dv_lock = srv6_am_dpo_lock,
182 .dv_unlock = srv6_am_dpo_unlock,
183 .dv_format = format_srv6_am_dpo,
186 const static char *const srv6_am_ip6_nodes[] = {
191 const static char *const *const srv6_am_nodes[DPO_PROTO_NUM] = {
192 [DPO_PROTO_IP6] = srv6_am_ip6_nodes,
195 /**********************/
196 static clib_error_t *
197 srv6_am_init (vlib_main_t * vm)
199 srv6_am_main_t *sm = &srv6_am_main;
203 sm->vnet_main = vnet_get_main ();
206 sm->srv6_am_dpo_type = dpo_register_new_type (&srv6_am_vft, srv6_am_nodes);
208 /* Register SRv6 LocalSID */
209 rv = sr_localsid_register_function (vm,
214 &sm->srv6_am_dpo_type,
215 format_srv6_am_localsid,
216 unformat_srv6_am_localsid,
217 srv6_am_localsid_creation_fn,
218 srv6_am_localsid_removal_fn);
220 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
222 sm->srv6_localsid_behavior_id = rv;
228 VNET_FEATURE_INIT (srv6_am_rewrite, static) =
230 .arc_name = "ip6-unicast",
231 .node_name = "srv6-am-rewrite",
235 VLIB_INIT_FUNCTION (srv6_am_init);
237 VLIB_PLUGIN_REGISTER () = {
238 .version = VPP_BUILD_VER,
239 .description = "Masquerading SRv6 proxy",
244 * fd.io coding-style-patch-verification: ON
247 * eval: (c-set-style "gnu")