udp: fix csum computation when offload disabled
[vpp.git] / src / plugins / srv6-mobile / gtp4_dt.c
1 /*
2  * srv6_t_m_gtp4_dt.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/fib/fib_table.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vpp/app/version.h>
23 #include <srv6-mobile/mobile.h>
24
25 srv6_t_main_v4_dt_t srv6_t_main_v4_dt;
26
27 static void
28 clb_dpo_lock_srv6_t_m_gtp4_dt (dpo_id_t * dpo)
29 {
30 }
31
32 static void
33 clb_dpo_unlock_srv6_t_m_gtp4_dt (dpo_id_t * dpo)
34 {
35 }
36
37 static u8 *
38 clb_dpo_format_srv6_t_m_gtp4_dt (u8 * s, va_list * args)
39 {
40   index_t index = va_arg (*args, index_t);
41   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
42
43   return (format (s, "SR: dynamic_proxy_index:[%u]", index));
44 }
45
46 const static dpo_vft_t dpo_vft = {
47   .dv_lock = clb_dpo_lock_srv6_t_m_gtp4_dt,
48   .dv_unlock = clb_dpo_unlock_srv6_t_m_gtp4_dt,
49   .dv_format = clb_dpo_format_srv6_t_m_gtp4_dt,
50 };
51
52 const static char *const srv6_t_m_gtp4_dt_nodes[] = {
53   "srv6-t-m-gtp4-dt",
54   NULL,
55 };
56
57 const static char *const srv6_t_m_gtp4_dt_v6_nodes[] = {
58   "error-drop",
59   NULL,
60 };
61
62 const static char *const *const dpo_nodes[DPO_PROTO_NUM] = {
63   [DPO_PROTO_IP6] = srv6_t_m_gtp4_dt_v6_nodes,
64   [DPO_PROTO_IP4] = srv6_t_m_gtp4_dt_nodes,
65 };
66
67 static u8 fn_name[] = "SRv6-T.M.GTP4.DT-plugin";
68 static u8 keyword_str[] = "t.m.gtp4.dt";
69 static u8 def_str[] = "Transit function with DT for IPv4/GTP tunnel";
70 static u8 param_str[] = "fib-index <index> [local-fib-table <index>]";
71
72 static u8 *
73 clb_format_srv6_t_m_gtp4_dt (u8 * s, va_list * args)
74 {
75   srv6_t_gtp4_dt_param_t *ls_mem = va_arg (*args, void *);
76
77   s = format (s, "SRv6 Transit gtp4.dt\n\t");
78
79   if (ls_mem->type == SRV6_GTP4_DT4)
80     s = format (s, " Type GTP4.DT4 fib-table %u\n", ls_mem->fib4_index);
81   else if (ls_mem->type == SRV6_GTP4_DT6)
82     s = format (s, " Type GTP4.DT6, fib-table %u, local-fib-table %u\n",
83                 ls_mem->fib6_index, ls_mem->local_fib_index);
84   else if (ls_mem->type == SRV6_GTP4_DT46)
85     s = format (s, " Type GTP4.DT46, fib-table %u, local-fib-table %u\n",
86                 ls_mem->fib6_index, ls_mem->local_fib_index);
87   else
88     s = format (s, "\n");
89
90   return s;
91 }
92
93 void
94 alloc_param_srv6_t_m_gtp4_dt (void **plugin_mem_p, const u32 fib_index,
95                               const u32 local_fib_index, const u8 type)
96 {
97   srv6_t_gtp4_dt_param_t *ls_mem;
98   ls_mem = clib_mem_alloc (sizeof *ls_mem);
99   clib_memset (ls_mem, 0, sizeof *ls_mem);
100   *plugin_mem_p = ls_mem;
101
102   ls_mem->fib4_index = fib_table_find (FIB_PROTOCOL_IP4, fib_index);
103   ls_mem->fib6_index = fib_table_find (FIB_PROTOCOL_IP6, fib_index);
104
105   if (type == SRV6_GTP4_DT6 || type == SRV6_GTP4_DT46)
106     {
107       ls_mem->local_fib_index =
108         fib_table_find (FIB_PROTOCOL_IP6, local_fib_index);
109     }
110
111   ls_mem->type = type;
112 }
113
114 static uword
115 clb_unformat_srv6_t_m_gtp4_dt (unformat_input_t * input, va_list * args)
116 {
117   void **plugin_mem_p = va_arg (*args, void **);
118   u32 fib_index = 0;
119   u32 local_fib_index = 0;
120   u32 type;
121
122   if (unformat (input, "t.m.gtp4.dt4 fib-table %u", &fib_index))
123     {
124       type = SRV6_GTP4_DT4;
125     }
126   else if (unformat (input, "t.m.gtp4.dt6 fib-table %u local-fib-table %u",
127                      &fib_index, &local_fib_index))
128     {
129       type = SRV6_GTP4_DT6;
130     }
131   else if (unformat (input, "t.m.gtp4.dt46 fib-table %u local-fib-table %u",
132                      &fib_index, &local_fib_index))
133     {
134       type = SRV6_GTP4_DT46;
135     }
136   else
137     {
138       return 0;
139     }
140
141   alloc_param_srv6_t_m_gtp4_dt (plugin_mem_p, fib_index, local_fib_index,
142                                 type);
143
144   return 1;
145 }
146
147 static int
148 clb_creation_srv6_t_m_gtp4_dt (ip6_sr_policy_t * sr_policy)
149 {
150   return 0;
151 }
152
153 static int
154 clb_removal_srv6_t_m_gtp4_dt (ip6_sr_policy_t * sr_policy)
155 {
156   srv6_t_gtp4_dt_param_t *ls_mem;
157
158   ls_mem = (srv6_t_gtp4_dt_param_t *) sr_policy->plugin_mem;
159
160   clib_mem_free (ls_mem);
161
162   return 0;
163 }
164
165 static clib_error_t *
166 srv6_t_m_gtp4_dt_init (vlib_main_t * vm)
167 {
168   srv6_t_main_v4_dt_t *sm = &srv6_t_main_v4_dt;
169   dpo_type_t dpo_type;
170   vlib_node_t *node;
171   int rc;
172
173   sm->vlib_main = vm;
174   sm->vnet_main = vnet_get_main ();
175
176   node = vlib_get_node_by_name (vm, (u8 *) "srv6-t-m-gtp4-dt");
177   sm->t_m_gtp4_dt_node_index = node->index;
178
179   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
180   sm->error_node_index = node->index;
181
182   dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes);
183
184   rc = sr_policy_register_function (vm, fn_name, keyword_str, def_str, param_str, 128,  //prefix len
185                                     &dpo_type,
186                                     clb_format_srv6_t_m_gtp4_dt,
187                                     clb_unformat_srv6_t_m_gtp4_dt,
188                                     clb_creation_srv6_t_m_gtp4_dt,
189                                     clb_removal_srv6_t_m_gtp4_dt);
190   if (rc < 0)
191     clib_error_return (0, "SRv6 Transit GTP4.DT Policy function"
192                        "couldn't be registered");
193   return 0;
194 }
195
196 VNET_FEATURE_INIT (srv6_t_m_gtp4_dt, static) =
197 {
198   .arc_name = "ip4-unicast",
199   .node_name = "srv6-t-m-gtp4-dt",
200   .runs_before = 0,
201 };
202
203 VLIB_INIT_FUNCTION (srv6_t_m_gtp4_dt_init);
204
205 /*
206  * fd.io coding-style-patch-verification: ON
207  *
208  * Local Variables:
209  * eval: (c-set-style "gnu")
210  * End:
211  */