FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
gtp4_d.c
Go to the documentation of this file.
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 
25 
26 static void
28 {
29 }
30 
31 static void
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 = {
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] = {
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 <nhtype>]";
72 
73 static u8 *
74 clb_format_srv6_t_m_gtp4_d (u8 * s, va_list * args)
75 {
76  srv6_end_gtp4_param_t *ls_mem = va_arg (*args, void *);
77 
78  s = format (s, "SRv6 T.M.GTP4.D\n\t");
79 
80  s =
81  format (s, "SR Prefix: %U/%d, ", format_ip6_address, &ls_mem->sr_prefix,
82  ls_mem->sr_prefixlen);
83 
84  s =
85  format (s, "v6src Prefix: %U/%d", format_ip6_address,
86  &ls_mem->v6src_prefix, ls_mem->v6src_prefixlen);
87 
88  if (ls_mem->nhtype != SRV6_NHTYPE_NONE)
89  {
90  if (ls_mem->nhtype == SRV6_NHTYPE_IPV4)
91  s = format (s, ", NHType IPv4\n");
92  else if (ls_mem->nhtype == SRV6_NHTYPE_IPV6)
93  s = format (s, ", NHType IPv6\n");
94  else if (ls_mem->nhtype == SRV6_NHTYPE_NON_IP)
95  s = format (s, ", NHType Non-IP\n");
96  else
97  s = format (s, ", NHType Unknow(%d)\n", ls_mem->nhtype);
98  }
99  else
100  s = format (s, "\n");
101 
102  return s;
103 }
104 
105 static uword
107 {
108  void **plugin_mem_p = va_arg (*args, void **);
109  srv6_end_gtp4_param_t *ls_mem;
110  ip6_address_t sr_prefix;
111  u32 sr_prefixlen;
112  ip6_address_t v6src_prefix;
113  u32 v6src_prefixlen;
114  u8 nhtype;
115 
116  if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv4",
117  unformat_ip6_address, &sr_prefix, &sr_prefixlen,
118  unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
119  {
120  nhtype = SRV6_NHTYPE_IPV4;
121  }
122  else
123  if (unformat
124  (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv6",
125  unformat_ip6_address, &sr_prefix, &sr_prefixlen,
126  unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
127  {
128  nhtype = SRV6_NHTYPE_IPV6;
129  }
130  else
131  if (unformat
132  (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype non-ip",
133  unformat_ip6_address, &sr_prefix, &sr_prefixlen,
134  unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
135  {
136  nhtype = SRV6_NHTYPE_NON_IP;
137  }
138  else if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d",
139  unformat_ip6_address, &sr_prefix, &sr_prefixlen,
140  unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
141  {
142  nhtype = SRV6_NHTYPE_NONE;
143  }
144  else
145  {
146  return 0;
147  }
148 
149  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
150  clib_memset (ls_mem, 0, sizeof *ls_mem);
151  *plugin_mem_p = ls_mem;
152 
153  ls_mem->sr_prefix = sr_prefix;
154  ls_mem->sr_prefixlen = sr_prefixlen;
155 
156  ls_mem->v6src_prefix = v6src_prefix;
157  ls_mem->v6src_prefixlen = v6src_prefixlen;
158 
159  ls_mem->nhtype = nhtype;
160 
161  return 1;
162 }
163 
164 static int
166 {
167  return 0;
168 }
169 
170 static int
172 {
173  srv6_end_gtp4_param_t *ls_mem;
174 
175  ls_mem = (srv6_end_gtp4_param_t *) sr_policy->plugin_mem;
176 
177  clib_mem_free (ls_mem);
178 
179  return 0;
180 }
181 
182 static clib_error_t *
184 {
186  ip6_header_t *ip6;
187  dpo_type_t dpo_type;
188  vlib_node_t *node;
189  int rc;
190 
191  sm->vlib_main = vm;
192  sm->vnet_main = vnet_get_main ();
193 
194  node = vlib_get_node_by_name (vm, (u8 *) "srv6-t-m-gtp4-d");
195  sm->t_m_gtp4_d_node_index = node->index;
196 
197  node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
198  sm->error_node_index = node->index;
199 
200  ip6 = &sm->cache_hdr;
201 
202  clib_memset_u8 (ip6, 0, sizeof (ip6_header_t));
203 
204  // IPv6 header (default)
206  ip6->hop_limit = 64;
207  ip6->protocol = IP_PROTOCOL_IPV6;
208 
209  dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes);
210 
211  rc = sr_policy_register_function (vm, fn_name, keyword_str, def_str, param_str, 128, //prefix len
212  &dpo_type,
217  if (rc < 0)
218  clib_error_return (0, "SRv6 Transit GTP4.D Policy function"
219  "couldn't be registered");
220  return 0;
221 }
222 
223 /* *INDENT-OFF* */
225 {
226  .arc_name = "ip4-unicast",
227  .node_name = "srv6-t-m-gtp4-d",
228  .runs_before = 0,
229 };
230 
232 /* *INDENT-ON* */
233 
234 /*
235  * fd.io coding-style-patch-verification: ON
236  *
237  * Local Variables:
238  * eval: (c-set-style "gnu")
239  * End:
240  */
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:406
#define CLIB_UNUSED(x)
Definition: clib.h:86
A virtual function table regisitered for a DPO type.
Definition: dpo.h:401
static const char *const srv6_t_m_gtp4_d_v6_nodes[]
Definition: gtp4_d.c:56
static void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset, int os_out_of_memory_on_failure)
Definition: mem.h:125
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:282
static u8 * clb_dpo_format_srv6_t_m_gtp4_d(u8 *s, va_list *args)
Definition: gtp4_d.c:37
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unsigned char u8
Definition: types.h:56
static const char *const *const dpo_nodes[DPO_PROTO_NUM]
Definition: gtp4_d.c:61
VNET_FEATURE_INIT(srv6_t_m_gtp4_d, static)
enum dpo_type_t_ dpo_type_t
Common types of data-path objects New types can be dynamically added using dpo_register_new_type() ...
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vl_api_ip6_address_t ip6
Definition: one.api:424
static void clb_dpo_lock_srv6_t_m_gtp4_d(dpo_id_t *dpo)
Definition: gtp4_d.c:27
#define clib_error_return(e, args...)
Definition: error.h:99
static u8 def_str[]
Definition: gtp4_d.c:68
#define SRV6_NHTYPE_NON_IP
Definition: mobile.h:34
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: mobile.h:233
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:342
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:170
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
#define SRV6_NHTYPE_IPV4
Definition: mobile.h:32
#define SRV6_NHTYPE_IPV6
Definition: mobile.h:33
struct _unformat_input_t unformat_input_t
vlib_main_t * vm
Definition: in2out_ed.c:1599
static u8 fn_name[]
Definition: gtp4_d.c:66
static const char *const srv6_t_m_gtp4_d_nodes[]
Definition: gtp4_d.c:51
unformat_function_t unformat_ip6_address
Definition: format.h:89
ip6_address_t v6src_prefix
Definition: mobile.h:210
format_function_t format_ip6_address
Definition: format.h:91
vlib_node_registration_t srv6_t_m_gtp4_d
(constructor) VLIB_REGISTER_NODE (srv6_t_m_gtp4_d)
Definition: node.c:1225
static int clb_creation_srv6_t_m_gtp4_d(ip6_sr_policy_t *sr_policy)
Definition: gtp4_d.c:165
#define SRV6_NHTYPE_NONE
Definition: mobile.h:31
srv6_t_main_v4_decap_t srv6_t_main_v4_decap
Definition: gtp4_d.c:24
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
static int clb_removal_srv6_t_m_gtp4_d(ip6_sr_policy_t *sr_policy)
Definition: gtp4_d.c:171
static void clb_dpo_unlock_srv6_t_m_gtp4_d(dpo_id_t *dpo)
Definition: gtp4_d.c:32
static void clib_mem_free(void *p)
Definition: mem.h:215
int sr_policy_register_function(vlib_main_t *vm, u8 *fn_name, u8 *keyword_str, u8 *def_str, u8 *params_str, u8 prefix_length, dpo_type_t *dpo, format_function_t *ls_format, unformat_function_t *ls_unformat, sr_p_plugin_callback_t *creation_fn, sr_p_plugin_callback_t *removal_fn)
SR Policy plugin registry.
static uword clb_unformat_srv6_t_m_gtp4_d(unformat_input_t *input, va_list *args)
Definition: gtp4_d.c:106
static_always_inline void clib_memset_u8(void *p, u8 val, uword count)
Definition: string.h:424
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
#define DPO_PROTO_NUM
Definition: dpo.h:70
SR Policy.
Definition: sr.h:93
u64 uword
Definition: types.h:112
static u8 param_str[]
Definition: gtp4_d.c:70
vnet_main_t * vnet_main
Definition: mobile.h:234
static u8 keyword_str[]
Definition: gtp4_d.c:67
static u8 * clb_format_srv6_t_m_gtp4_d(u8 *s, va_list *args)
Definition: gtp4_d.c:74
ip6_address_t sr_prefix
Definition: mobile.h:207
ip6_header_t cache_hdr
Definition: mobile.h:239
void * plugin_mem
Definition: sr.h:112
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static clib_error_t * srv6_t_m_gtp4_d_init(vlib_main_t *vm)
Definition: gtp4_d.c:183