Line data Source code
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 : srv6_am_main_t srv6_am_main;
33 :
34 : /*****************************************/
35 : /* SRv6 LocalSID instantiation and removal functions */
36 : static int
37 0 : srv6_am_localsid_creation_fn (ip6_sr_localsid_t * localsid)
38 : {
39 0 : srv6_am_main_t *sm = &srv6_am_main;
40 0 : srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
41 0 : 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 0 : nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
47 0 : VNET_LINK_IP6, &ls_mem->nh_addr,
48 : ls_mem->sw_if_index_out);
49 0 : if (nh_adj_index == ADJ_INDEX_INVALID)
50 0 : return -5;
51 :
52 0 : 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 */
58 0 : if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces,
59 0 : ls_mem->sw_if_index_in))
60 0 : return -3;
61 :
62 0 : vnet_sw_interface_t *sw = vnet_get_sw_interface (sm->vnet_main,
63 : ls_mem->sw_if_index_in);
64 0 : if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
65 0 : return -3;
66 :
67 0 : int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
68 : ls_mem->sw_if_index_in, 1, 0, 0);
69 0 : if (ret != 0)
70 0 : return -1;
71 :
72 0 : return 0;
73 : }
74 :
75 : static int
76 0 : srv6_am_localsid_removal_fn (ip6_sr_localsid_t * localsid)
77 : {
78 0 : srv6_am_localsid_t *ls_mem = localsid->plugin_mem;
79 :
80 : /* Remove hardware indirection (from sr_steering.c:137) */
81 0 : int ret = vnet_feature_enable_disable ("ip6-unicast", "srv6-am-rewrite",
82 : ls_mem->sw_if_index_in, 0, 0, 0);
83 0 : if (ret != 0)
84 0 : return -1;
85 :
86 : /* Unlock (OIF, NHOP) adjacency (from sr_localsid.c:103) */
87 0 : adj_unlock (localsid->nh_adj);
88 :
89 : /* Clean up local SID memory */
90 0 : clib_mem_free (localsid->plugin_mem);
91 :
92 0 : 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 0 : format_srv6_am_localsid (u8 * s, va_list * args)
103 : {
104 0 : srv6_am_localsid_t *ls_mem = va_arg (*args, void *);
105 :
106 0 : vnet_main_t *vnm = vnet_get_main ();
107 :
108 0 : 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,
113 : format_vnet_sw_if_index_name, vnm, ls_mem->sw_if_index_out,
114 : format_vnet_sw_if_index_name, vnm, ls_mem->sw_if_index_in));
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
125 9 : unformat_srv6_am_localsid (unformat_input_t * input, va_list * args)
126 : {
127 9 : void **plugin_mem_p = va_arg (*args, void **);
128 : srv6_am_localsid_t *ls_mem;
129 :
130 9 : 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 9 : if (unformat (input, "end.am nh %U oif %U iif %U",
137 : unformat_ip6_address, &nh_addr.ip6,
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 0 : ls_mem = clib_mem_alloc (sizeof *ls_mem);
143 :
144 : /* Set to zero the memory */
145 0 : clib_memset (ls_mem, 0, sizeof *ls_mem);
146 :
147 : /* Our brand-new car is ready */
148 0 : clib_memcpy (&ls_mem->nh_addr.ip6, &nh_addr.ip6,
149 : sizeof (ip6_address_t));
150 0 : ls_mem->sw_if_index_out = sw_if_index_out;
151 0 : ls_mem->sw_if_index_in = sw_if_index_in;
152 :
153 : /* Dont forget to add it to the localsid */
154 0 : *plugin_mem_p = ls_mem;
155 0 : return 1;
156 : }
157 9 : return 0;
158 : }
159 :
160 : /*************************/
161 : /* SRv6 LocalSID FIB DPO */
162 : static u8 *
163 0 : format_srv6_am_dpo (u8 * s, va_list * args)
164 : {
165 0 : index_t index = va_arg (*args, index_t);
166 0 : CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
167 :
168 0 : return (format (s, "SR: dynamic_proxy_index:[%u]", index));
169 : }
170 :
171 : void
172 0 : srv6_am_dpo_lock (dpo_id_t * dpo)
173 : {
174 0 : }
175 :
176 : void
177 0 : srv6_am_dpo_unlock (dpo_id_t * dpo)
178 : {
179 0 : }
180 :
181 : const static dpo_vft_t srv6_am_vft = {
182 : .dv_lock = srv6_am_dpo_lock,
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] = {
193 : [DPO_PROTO_IP6] = srv6_am_ip6_nodes,
194 : };
195 :
196 : /**********************/
197 : static clib_error_t *
198 559 : srv6_am_init (vlib_main_t * vm)
199 : {
200 559 : srv6_am_main_t *sm = &srv6_am_main;
201 559 : int rv = 0;
202 :
203 559 : sm->vlib_main = vm;
204 559 : sm->vnet_main = vnet_get_main ();
205 :
206 : /* Create DPO */
207 559 : sm->srv6_am_dpo_type = dpo_register_new_type (&srv6_am_vft, srv6_am_nodes);
208 :
209 : /* Register SRv6 LocalSID */
210 559 : rv = sr_localsid_register_function (vm,
211 : function_name,
212 : keyword_str,
213 : def_str,
214 : params_str,
215 : 128,
216 : &sm->srv6_am_dpo_type,
217 : format_srv6_am_localsid,
218 : unformat_srv6_am_localsid,
219 : srv6_am_localsid_creation_fn,
220 : srv6_am_localsid_removal_fn);
221 559 : if (rv < 0)
222 0 : clib_error_return (0, "SRv6 LocalSID function could not be registered.");
223 : else
224 559 : sm->srv6_localsid_behavior_id = rv;
225 :
226 559 : return 0;
227 : }
228 :
229 : /* *INDENT-OFF* */
230 17359 : 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 :
237 1119 : VLIB_INIT_FUNCTION (srv6_am_init);
238 :
239 : VLIB_PLUGIN_REGISTER () = {
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 : */
|