udp: fix csum computation when offload disabled
[vpp.git] / src / plugins / srv6-mobile / gtp6_d_di.c
1 /*
2  * srv6_end_m_gtp6_d_di_di.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_end_main_v6_decap_di_t srv6_end_main_v6_decap_di;
25
26 static void
27 clb_dpo_lock_srv6_end_m_gtp6_d_di (dpo_id_t * dpo)
28 {
29 }
30
31 static void
32 clb_dpo_unlock_srv6_end_m_gtp6_d_di (dpo_id_t * dpo)
33 {
34 }
35
36 static u8 *
37 clb_dpo_format_srv6_end_m_gtp6_d_di (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_end_m_gtp6_d_di,
47   .dv_unlock = clb_dpo_unlock_srv6_end_m_gtp6_d_di,
48   .dv_format = clb_dpo_format_srv6_end_m_gtp6_d_di,
49 };
50
51 const static char *const srv6_end_m_gtp6_d_di_nodes[] = {
52   "srv6-end-m-gtp6-d-di",
53   NULL,
54 };
55
56 const static char *const *const dpo_nodes[DPO_PROTO_NUM] = {
57   [DPO_PROTO_IP6] = srv6_end_m_gtp6_d_di_nodes,
58 };
59
60 static u8 fn_name[] = "SRv6-End.M.GTP6.D.DI-plugin";
61 static u8 keyword_str[] = "end.m.gtp6.d.di";
62 static u8 def_str[] =
63   "Endpoint function with drop-in dencapsulation for IPv6/GTP tunnel";
64 static u8 param_str[] = "<sr-prefix>/<sr-prefixlen> [nhtype <nhtype>]";
65
66 static u8 *
67 clb_format_srv6_end_m_gtp6_d_di (u8 * s, va_list * args)
68 {
69   srv6_end_gtp6_d_param_t *ls_mem = va_arg (*args, void *);
70
71   s = format (s, "SRv6 End gtp6.d Drop-in\n\t");
72
73   s =
74     format (s, "SR Prefix: %U/%d", format_ip6_address, &ls_mem->sr_prefix,
75             ls_mem->sr_prefixlen);
76
77   if (ls_mem->nhtype != SRV6_NHTYPE_NONE)
78     {
79       if (ls_mem->nhtype == SRV6_NHTYPE_IPV4)
80         s = format (s, ", NHType IPv4\n");
81       else if (ls_mem->nhtype == SRV6_NHTYPE_IPV6)
82         s = format (s, ", NHType IPv6\n");
83       else if (ls_mem->nhtype == SRV6_NHTYPE_NON_IP)
84         s = format (s, ", NHType Non-IP\n");
85       else
86         s = format (s, ", NHType Unknow(%d)\n", ls_mem->nhtype);
87     }
88   else
89     s = format (s, "\n");
90
91   return s;
92 }
93
94 void
95 alloc_param_srv6_end_m_gtp6_di (void **plugin_mem_p, const void *sr_prefix,
96                                 const u32 sr_prefixlen, const u8 nhtype)
97 {
98   srv6_end_gtp6_d_param_t *ls_mem;
99   ls_mem = clib_mem_alloc (sizeof *ls_mem);
100   clib_memset (ls_mem, 0, sizeof *ls_mem);
101   *plugin_mem_p = ls_mem;
102
103   ls_mem->sr_prefixlen = sr_prefixlen;
104   memcpy (&ls_mem->sr_prefix, sr_prefix, sizeof (ip6_address_t));
105   ls_mem->nhtype = nhtype;
106 }
107
108 static uword
109 clb_unformat_srv6_end_m_gtp6_d_di (unformat_input_t * input, va_list * args)
110 {
111   void **plugin_mem_p = va_arg (*args, void **);
112   ip6_address_t sr_prefix;
113   u32 sr_prefixlen = 0;
114   u8 nhtype;
115
116   if (unformat (input, "end.m.gtp6.d.di %U/%d nhtype ipv4",
117                 unformat_ip6_address, &sr_prefix, &sr_prefixlen))
118     {
119       nhtype = SRV6_NHTYPE_IPV4;
120     }
121   else if (unformat (input, "end.m.gtp6.d.di %U/%d nhtype ipv6",
122                      unformat_ip6_address, &sr_prefix, &sr_prefixlen))
123     {
124       nhtype = SRV6_NHTYPE_IPV6;
125     }
126   else if (unformat (input, "end.m.gtp6.d.di %U/%d nhtype non-ip",
127                      unformat_ip6_address, &sr_prefix, &sr_prefixlen))
128     {
129       nhtype = SRV6_NHTYPE_NON_IP;
130     }
131   else if (unformat (input, "end.m.gtp6.d.di %U/%d",
132                      unformat_ip6_address, &sr_prefix, &sr_prefixlen))
133     {
134       nhtype = SRV6_NHTYPE_NONE;
135     }
136   else
137     {
138       return 0;
139     }
140
141   alloc_param_srv6_end_m_gtp6_di (plugin_mem_p, &sr_prefix, sr_prefixlen,
142                                   nhtype);
143
144   return 1;
145 }
146
147 static int
148 clb_creation_srv6_end_m_gtp6_d_di (ip6_sr_localsid_t * localsid)
149 {
150   return 0;
151 }
152
153 static int
154 clb_removal_srv6_end_m_gtp6_d_di (ip6_sr_localsid_t * localsid)
155 {
156   srv6_end_gtp6_d_param_t *ls_mem;
157
158   ls_mem = localsid->plugin_mem;
159
160   clib_mem_free (ls_mem);
161
162   return 0;
163 }
164
165 static clib_error_t *
166 srv6_end_m_gtp6_d_di_init (vlib_main_t * vm)
167 {
168   srv6_end_main_v6_decap_di_t *sm = &srv6_end_main_v6_decap_di;
169   ip6srv_combo_header_t *ip6;
170   dpo_type_t dpo_type;
171   vlib_node_t *node;
172   int rc;
173
174   sm->vlib_main = vm;
175   sm->vnet_main = vnet_get_main ();
176
177   node = vlib_get_node_by_name (vm, (u8 *) "srv6-end-m-gtp6-d-di");
178   sm->end_m_gtp6_d_di_node_index = node->index;
179
180   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
181   sm->error_node_index = node->index;
182
183   ip6 = &sm->cache_hdr;
184
185   clib_memset_u8 (ip6, 0, sizeof (ip6srv_combo_header_t));
186
187   // IPv6 header (default)
188   ip6->ip.ip_version_traffic_class_and_flow_label = 0x60;
189   ip6->ip.hop_limit = 64;
190   ip6->ip.protocol = IPPROTO_IPV6_ROUTE;
191
192   // SR header (default)
193   ip6->sr.type = 4;
194
195   dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes);
196
197   rc = sr_localsid_register_function (vm, fn_name, keyword_str, def_str, param_str, 128,        //prefix len
198                                       &dpo_type,
199                                       clb_format_srv6_end_m_gtp6_d_di,
200                                       clb_unformat_srv6_end_m_gtp6_d_di,
201                                       clb_creation_srv6_end_m_gtp6_d_di,
202                                       clb_removal_srv6_end_m_gtp6_d_di);
203   if (rc < 0)
204     clib_error_return (0, "SRv6 Endpoint GTP6.D.DI LocalSID function"
205                        "couldn't be registered");
206   return 0;
207 }
208
209 VNET_FEATURE_INIT (srv6_end_m_gtp6_d_di, static) =
210 {
211   .arc_name = "ip6-unicast",
212   .node_name = "srv6-end-m-gtp6-d-di",
213   .runs_before = 0,
214 };
215
216 VLIB_INIT_FUNCTION (srv6_end_m_gtp6_d_di_init);
217
218 /*
219  * fd.io coding-style-patch-verification: ON
220  *
221  * Local Variables:
222  * eval: (c-set-style "gnu")
223  * End:
224  */