srv6-mobile: Implement SRv6 mobile API funcs
[vpp.git] / src / plugins / srv6-mobile / gtp4_d.c
1 /*
2  * srv6_t_m_gtp4_d.c
3  *
4  * Copyright (c) 2019 Arrcus Inc and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/adj/adj.h>
20 #include <vnet/plugin/plugin.h>
21 #include <vpp/app/version.h>
22 #include <srv6-mobile/mobile.h>
23
24 srv6_t_main_v4_decap_t srv6_t_main_v4_decap;
25
26 static void
27 clb_dpo_lock_srv6_t_m_gtp4_d (dpo_id_t * dpo)
28 {
29 }
30
31 static void
32 clb_dpo_unlock_srv6_t_m_gtp4_d (dpo_id_t * dpo)
33 {
34 }
35
36 static u8 *
37 clb_dpo_format_srv6_t_m_gtp4_d (u8 * s, va_list * args)
38 {
39   index_t index = va_arg (*args, index_t);
40   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
41
42   return (format (s, "SR: dynamic_proxy_index:[%u]", index));
43 }
44
45 const static dpo_vft_t dpo_vft = {
46   .dv_lock = clb_dpo_lock_srv6_t_m_gtp4_d,
47   .dv_unlock = clb_dpo_unlock_srv6_t_m_gtp4_d,
48   .dv_format = clb_dpo_format_srv6_t_m_gtp4_d,
49 };
50
51 const static char *const srv6_t_m_gtp4_d_nodes[] = {
52   "srv6-t-m-gtp4-d",
53   NULL,
54 };
55
56 const static char *const srv6_t_m_gtp4_d_v6_nodes[] = {
57   "error-drop",
58   NULL,
59 };
60
61 const static char *const *const dpo_nodes[DPO_PROTO_NUM] = {
62   [DPO_PROTO_IP6] = srv6_t_m_gtp4_d_v6_nodes,
63   [DPO_PROTO_IP4] = srv6_t_m_gtp4_d_nodes,
64 };
65
66 static u8 fn_name[] = "SRv6-T.M.GTP4.D-plugin";
67 static u8 keyword_str[] = "t.m.gtp4.d";
68 static u8 def_str[] =
69   "Transit function with decapsulation for IPv4/GTP tunnel";
70 static u8 param_str[] =
71   "<sr-prefix>/<sr-prefixlen> v6src_prefix <v6src_prefix>/<prefixlen> [nhtype "
72   "<nhtype>] fib-table <id>";
73
74 static u8 *
75 clb_format_srv6_t_m_gtp4_d (u8 * s, va_list * args)
76 {
77   srv6_end_gtp4_d_param_t *ls_mem = va_arg (*args, void *);
78
79   s = format (s, "SRv6 T.M.GTP4.D\n\t");
80
81   s =
82     format (s, "SR Prefix: %U/%d, ", format_ip6_address, &ls_mem->sr_prefix,
83             ls_mem->sr_prefixlen);
84
85   s =
86     format (s, "v6src Prefix: %U/%d", format_ip6_address,
87             &ls_mem->v6src_prefix, ls_mem->v6src_prefixlen);
88
89   if (ls_mem->nhtype != SRV6_NHTYPE_NONE)
90     {
91       if (ls_mem->nhtype == SRV6_NHTYPE_IPV4)
92         s = format (s, ", NHType IPv4");
93       else if (ls_mem->nhtype == SRV6_NHTYPE_IPV6)
94         s = format (s, ", NHType IPv6");
95       else if (ls_mem->nhtype == SRV6_NHTYPE_NON_IP)
96         s = format (s, ", NHType Non-IP");
97       else
98         s = format (s, ", NHType Unknow(%d)", ls_mem->nhtype);
99     }
100
101   s = format (s, ", FIB table %d", ls_mem->fib_table);
102
103   s = format (s, ", Drop In %d\n", ls_mem->drop_in);
104
105   return s;
106 }
107
108 void
109 alloc_param_srv6_t_m_gtp4_d (void **plugin_mem_p, const void *v6src_prefix,
110                              const u32 v6src_prefixlen, const void *sr_prefix,
111                              const u32 sr_prefixlen, const u32 fib_index,
112                              const u8 nhtype, const bool drop_in)
113 {
114   srv6_end_gtp4_d_param_t *ls_mem;
115   ls_mem = clib_mem_alloc (sizeof *ls_mem);
116   clib_memset (ls_mem, 0, sizeof *ls_mem);
117   *plugin_mem_p = ls_mem;
118
119   ls_mem->v6src_prefixlen = v6src_prefixlen;
120   memcpy (&ls_mem->v6src_prefix, v6src_prefix, sizeof (ip6_address_t));
121   ls_mem->sr_prefixlen = sr_prefixlen;
122   memcpy (&ls_mem->sr_prefix, sr_prefix, sizeof (ip6_address_t));
123
124   ls_mem->nhtype = nhtype;
125   ls_mem->drop_in = drop_in;
126   ls_mem->fib_table = fib_index;
127   ls_mem->fib4_index = ip4_fib_index_from_table_id (fib_index);
128   ls_mem->fib6_index = ip6_fib_index_from_table_id (fib_index);
129 }
130
131 static uword
132 clb_unformat_srv6_t_m_gtp4_d (unformat_input_t * input, va_list * args)
133 {
134   void **plugin_mem_p = va_arg (*args, void **);
135   ip6_address_t sr_prefix;
136   u32 sr_prefixlen;
137   ip6_address_t v6src_prefix;
138   u32 v6src_prefixlen;
139   u32 fib_table = 0;
140   bool drop_in = false;
141   u8 nhtype = SRV6_NHTYPE_NONE;
142   bool config = false;
143
144   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
145     {
146       if (unformat (
147             input,
148             "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv4 fib-table %d",
149             unformat_ip6_address, &sr_prefix, &sr_prefixlen,
150             unformat_ip6_address, &v6src_prefix, &v6src_prefixlen, &fib_table))
151         {
152           config = true;
153           nhtype = SRV6_NHTYPE_IPV4;
154         }
155       else if (unformat (input,
156                          "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv6 "
157                          "fib-table %d",
158                          unformat_ip6_address, &sr_prefix, &sr_prefixlen,
159                          unformat_ip6_address, &v6src_prefix, &v6src_prefixlen,
160                          &fib_table))
161         {
162           config = true;
163           nhtype = SRV6_NHTYPE_IPV6;
164         }
165       else if (unformat (
166                  input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype non-ip",
167                  unformat_ip6_address, &sr_prefix, &sr_prefixlen,
168                  unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
169         {
170           config = true;
171           nhtype = SRV6_NHTYPE_NON_IP;
172         }
173       else if (unformat (input,
174                          "t.m.gtp4.d %U/%d v6src_prefix %U/%d fib-table %d",
175                          unformat_ip6_address, &sr_prefix, &sr_prefixlen,
176                          unformat_ip6_address, &v6src_prefix, &v6src_prefixlen,
177                          &fib_table))
178         {
179           config = true;
180           nhtype = SRV6_NHTYPE_NONE;
181         }
182       else if (unformat (input, "drop-in"))
183         {
184           drop_in = true;
185         }
186       else
187         {
188           return 0;
189         }
190     }
191
192   if (!config)
193     {
194       return 0;
195     }
196
197   alloc_param_srv6_t_m_gtp4_d (plugin_mem_p, &v6src_prefix, v6src_prefixlen,
198                                &sr_prefix, sr_prefixlen, fib_table, nhtype,
199                                drop_in);
200
201   return 1;
202 }
203
204 static int
205 clb_creation_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy)
206 {
207   return 0;
208 }
209
210 static int
211 clb_removal_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy)
212 {
213   srv6_end_gtp4_d_param_t *ls_mem;
214
215   ls_mem = (srv6_end_gtp4_d_param_t *) sr_policy->plugin_mem;
216
217   clib_mem_free (ls_mem);
218
219   return 0;
220 }
221
222 static clib_error_t *
223 srv6_t_m_gtp4_d_init (vlib_main_t * vm)
224 {
225   srv6_t_main_v4_decap_t *sm = &srv6_t_main_v4_decap;
226   ip6_header_t *ip6;
227   dpo_type_t dpo_type;
228   vlib_node_t *node;
229   int rc;
230
231   sm->vlib_main = vm;
232   sm->vnet_main = vnet_get_main ();
233
234   node = vlib_get_node_by_name (vm, (u8 *) "srv6-t-m-gtp4-d");
235   sm->t_m_gtp4_d_node_index = node->index;
236
237   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
238   sm->error_node_index = node->index;
239
240   ip6 = &sm->cache_hdr;
241
242   clib_memset_u8 (ip6, 0, sizeof (ip6_header_t));
243
244   // IPv6 header (default)
245   ip6->ip_version_traffic_class_and_flow_label = 0x60;
246   ip6->hop_limit = 64;
247   ip6->protocol = IP_PROTOCOL_IPV6;
248
249   dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes);
250
251   rc = sr_policy_register_function (vm, fn_name, keyword_str, def_str, param_str, 128,  //prefix len
252                                     &dpo_type,
253                                     clb_format_srv6_t_m_gtp4_d,
254                                     clb_unformat_srv6_t_m_gtp4_d,
255                                     clb_creation_srv6_t_m_gtp4_d,
256                                     clb_removal_srv6_t_m_gtp4_d);
257   if (rc < 0)
258     clib_error_return (0, "SRv6 Transit GTP4.D Policy function"
259                        "couldn't be registered");
260   return 0;
261 }
262
263 /* *INDENT-OFF* */
264 VNET_FEATURE_INIT (srv6_t_m_gtp4_d, static) =
265 {
266   .arc_name = "ip4-unicast",
267   .node_name = "srv6-t-m-gtp4-d",
268   .runs_before = 0,
269 };
270
271 VLIB_INIT_FUNCTION (srv6_t_m_gtp4_d_init);
272 /* *INDENT-ON* */
273
274 /*
275  * fd.io coding-style-patch-verification: ON
276  *
277  * Local Variables:
278  * eval: (c-set-style "gnu")
279  * End:
280  */