FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
gtpu_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * gtpu_api.c - gtpu api
4  *
5  * Copyright (c) 2017 Intel and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/interface.h>
21 #include <vnet/api_errno.h>
22 #include <vnet/feature/feature.h>
23 #include <vnet/fib/fib_table.h>
24 
25 #include <vppinfra/byte_order.h>
26 #include <vlibmemory/api.h>
27 #include <vnet/ip/ip_types_api.h>
28 #include <gtpu/gtpu.h>
29 
30 #include <vnet/format_fns.h>
31 #include <gtpu/gtpu.api_enum.h>
32 #include <gtpu/gtpu.api_types.h>
33 
34 #define REPLY_MSG_ID_BASE gtm->msg_id_base
36 
37 static void
39 {
40  vl_api_gtpu_offload_rx_reply_t *rmp;
41  int rv = 0;
42  vl_api_interface_index_t hw_if_index = ntohl (mp->hw_if_index);
43  vl_api_interface_index_t sw_if_index = ntohl (mp->sw_if_index);
44 
45  if (!vnet_hw_interface_is_valid (vnet_get_main (), hw_if_index))
46  {
47  rv = VNET_API_ERROR_NO_SUCH_ENTRY;
48  goto err;
49  }
51 
53  if (t_index == ~0)
54  {
55  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
56  goto err;
57  }
58 
59  gtpu_main_t *gtm = &gtpu_main;
60  gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
61  if (!ip46_address_is_ip4 (&t->dst))
62  {
63  rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
64  goto err;
65  }
66 
67  if ((t->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT) &&
68  (t->decap_next_index != GTPU_INPUT_NEXT_IP6_INPUT))
69  {
70  rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
71  goto err;
72  }
73 
74  vnet_main_t *vnm = vnet_get_main ();
75  vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
77  u32 rx_fib_index =
78  vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
79 
80  if (t->encap_fib_index != rx_fib_index)
81  {
82  rv = VNET_API_ERROR_NO_SUCH_FIB;
83  goto err;
84  }
85 
86  if (vnet_gtpu_add_del_rx_flow (hw_if_index, t_index, mp->enable))
87  {
88  rv = VNET_API_ERROR_UNSPECIFIED;
89  goto err;
90  }
92 err:
93 
94  REPLY_MACRO (VL_API_GTPU_OFFLOAD_RX_REPLY);
95 }
96 
97 static void
100 {
101  vl_api_sw_interface_set_gtpu_bypass_reply_t *rmp;
102  int rv = 0;
103  u32 sw_if_index = ntohl (mp->sw_if_index);
104  gtpu_main_t *gtm = &gtpu_main;
105 
107 
110 
111  REPLY_MACRO (VL_API_SW_INTERFACE_SET_GTPU_BYPASS_REPLY);
112 }
113 
116 {
118  int rv = 0;
119  gtpu_main_t *gtm = &gtpu_main;
120 
122  .opn = mp->is_add ? GTPU_ADD_TUNNEL : GTPU_DEL_TUNNEL,
123  .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
124  .decap_next_index = ntohl (mp->decap_next_index),
125  .teid = ntohl (mp->teid),
126  .tteid = ntohl (mp->tteid),
127  };
128  ip_address_decode (&mp->dst_address, &a.dst);
129  ip_address_decode (&mp->src_address, &a.src);
130 
131  u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
132  a.encap_fib_index = fib_table_find (fib_ip_proto (is_ipv6),
133  ntohl (mp->encap_vrf_id));
134  if (a.encap_fib_index == ~0)
135  {
136  rv = VNET_API_ERROR_NO_SUCH_FIB;
137  goto out;
138  }
139 
140  /* Check src & dst are different */
141  if (ip46_address_cmp (&a.dst, &a.src) == 0)
142  {
143  rv = VNET_API_ERROR_SAME_SRC_DST;
144  goto out;
145  }
146  if (ip46_address_is_multicast (&a.dst) &&
147  !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
148  {
149  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
150  goto out;
151  }
152 
153  u32 sw_if_index = ~0;
155 
156 out:
157  /* *INDENT-OFF* */
158  REPLY_MACRO2(VL_API_GTPU_ADD_DEL_TUNNEL_REPLY,
159  ({
160  rmp->sw_if_index = ntohl (sw_if_index);
161  }));
162  /* *INDENT-ON* */
163 }
164 
167 {
168  vl_api_gtpu_tunnel_update_tteid_reply_t *rmp;
169  int rv = 0;
170  gtpu_main_t *gtm = &gtpu_main;
171 
173  .opn = GTPU_UPD_TTEID,
174  .teid = ntohl (mp->teid),
175  .tteid = ntohl (mp->tteid),
176  };
177  ip_address_decode (&mp->dst_address, &a.dst);
178 
179  u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
180  a.encap_fib_index = fib_table_find (fib_ip_proto (is_ipv6),
181  ntohl (mp->encap_vrf_id));
182  if (a.encap_fib_index == ~0)
183  {
184  rv = VNET_API_ERROR_NO_SUCH_FIB;
185  goto out;
186  }
187 
189 
190 out:
191  REPLY_MACRO (VL_API_GTPU_TUNNEL_UPDATE_TTEID_REPLY);
192 }
193 
194 static void send_gtpu_tunnel_details
196 {
198  gtpu_main_t *gtm = &gtpu_main;
199  ip4_main_t *im4 = &ip4_main;
200  ip6_main_t *im6 = &ip6_main;
202 
203  rmp = vl_msg_api_alloc (sizeof (*rmp));
204  clib_memset (rmp, 0, sizeof (*rmp));
205  rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_DETAILS + gtm->msg_id_base);
206 
208  &rmp->src_address);
210  &rmp->dst_address);
211 
212  rmp->encap_vrf_id =
213  is_ipv6 ? htonl (im6->fibs[t->encap_fib_index].ft_table_id) :
214  htonl (im4->fibs[t->encap_fib_index].ft_table_id);
215  rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
216  rmp->teid = htonl (t->teid);
217  rmp->tteid = htonl (t->tteid);
218  rmp->decap_next_index = htonl (t->decap_next_index);
219  rmp->sw_if_index = htonl (t->sw_if_index);
220  rmp->context = context;
221 
222  vl_api_send_msg (reg, (u8 *) rmp);
223 }
224 
225 static void
227 {
229  gtpu_main_t *gtm = &gtpu_main;
230  gtpu_tunnel_t *t;
232 
234  if (!reg)
235  return;
236 
237  sw_if_index = ntohl (mp->sw_if_index);
238 
239  if (~0 == sw_if_index)
240  {
241  /* *INDENT-OFF* */
242  pool_foreach (t, gtm->tunnels)
243  {
244  send_gtpu_tunnel_details(t, reg, mp->context);
245  }
246  /* *INDENT-ON* */
247  }
248  else
249  {
252  {
253  return;
254  }
256  send_gtpu_tunnel_details (t, reg, mp->context);
257  }
258 }
259 
260 #include <gtpu/gtpu.api.c>
261 static clib_error_t *
263 {
264  gtpu_main_t *gtm = &gtpu_main;
265 
267  return 0;
268 }
269 
271 
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */
vl_api_gtpu_tunnel_dump_t::client_index
u32 client_index
Definition: gtpu.api:85
send_gtpu_tunnel_details
static void send_gtpu_tunnel_details(gtpu_tunnel_t *t, vl_api_registration_t *reg, u32 context)
Definition: gtpu_api.c:195
im
vnet_interface_main_t * im
Definition: interface_output.c:415
VALIDATE_SW_IF_INDEX
#define VALIDATE_SW_IF_INDEX(mp)
Definition: api_helper_macros.h:281
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
vl_api_gtpu_tunnel_details_t::encap_vrf_id
u32 encap_vrf_id
Definition: gtpu.api:109
vl_api_gtpu_add_del_tunnel_t::decap_next_index
u32 decap_next_index
Definition: gtpu.api:41
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
fib_ip_proto
static fib_protocol_t fib_ip_proto(bool is_ip6)
Convert from boolean is_ip6 to FIB protocol.
Definition: fib_types.h:79
gtpu_main_t::msg_id_base
u16 msg_id_base
Definition: gtpu.h:233
is_ipv6
bool is_ipv6
Definition: dhcp.api:202
vnet_gtpu_add_mod_del_tunnel
int vnet_gtpu_add_mod_del_tunnel(vnet_gtpu_add_mod_del_tunnel_args_t *a, u32 *sw_if_indexp)
Definition: gtpu.c:353
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
vl_api_sw_interface_set_gtpu_bypass_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gtpu.api:126
gtpu.h
ip4_main
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1104
gtpu_main_t
Definition: gtpu.h:205
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_gtpu_tunnel_update_tteid_t_handler
static void vl_api_gtpu_tunnel_update_tteid_t_handler(vl_api_gtpu_tunnel_update_tteid_t *mp)
Definition: gtpu_api.c:166
ip46_address_is_multicast
static uword ip46_address_is_multicast(const ip46_address_t *a)
Definition: ip46_address.h:154
vl_api_gtpu_add_del_tunnel_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gtpu.api:56
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
vnet_hw_interface_t::sw_if_index
u32 sw_if_index
Definition: interface.h:670
vnet_gtpu_get_tunnel_index
u32 vnet_gtpu_get_tunnel_index(u32 sw_if_index)
Definition: gtpu.c:1151
vl_api_gtpu_tunnel_update_tteid_t::teid
u32 teid
Definition: gtpu.api:73
vl_api_gtpu_add_del_tunnel_reply_t
reply for set or delete an GTPU tunnel
Definition: gtpu.api:52
ip46_address_cmp
static int ip46_address_cmp(const ip46_address_t *ip46_1, const ip46_address_t *ip46_2)
Definition: ip46_address.h:80
ip46_address_is_ip4
static u8 ip46_address_is_ip4(const ip46_address_t *ip46)
Definition: ip46_address.h:55
IP46_TYPE_IP4
@ IP46_TYPE_IP4
Definition: ip46_address.h:26
fib_table.h
vl_api_gtpu_offload_rx_t
Offload gtpu rx request.
Definition: gtpu.api:139
vl_api_gtpu_tunnel_details_t::teid
u32 teid
Definition: gtpu.api:111
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_gtpu_add_del_tunnel_t_handler
static void vl_api_gtpu_add_del_tunnel_t_handler(vl_api_gtpu_add_del_tunnel_t *mp)
Definition: gtpu_api.c:115
byte_order.h
GTPU_DEL_TUNNEL
#define GTPU_DEL_TUNNEL
Definition: gtpu.h:259
vl_api_sw_interface_set_gtpu_bypass_t
Interface set gtpu-bypass request.
Definition: gtpu.api:122
vl_api_sw_interface_set_gtpu_bypass_t_handler
static void vl_api_sw_interface_set_gtpu_bypass_t_handler(vl_api_sw_interface_set_gtpu_bypass_t *mp)
Definition: gtpu_api.c:99
vl_api_gtpu_tunnel_dump_t_handler
static void vl_api_gtpu_tunnel_dump_t_handler(vl_api_gtpu_tunnel_dump_t *mp)
Definition: gtpu_api.c:226
ip_address_decode
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:172
vl_api_gtpu_offload_rx_t::sw_if_index
u32 sw_if_index
Definition: gtpu.api:144
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
gtpu_tunnel_t::src
ip46_address_t src
Definition: gtpu.h:144
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
gtpu_tunnel_t::sw_if_index
u32 sw_if_index
Definition: gtpu.h:157
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vl_api_gtpu_add_del_tunnel_t::is_add
bool is_add
Definition: gtpu.api:36
feature.h
vl_api_gtpu_add_del_tunnel_t::mcast_sw_if_index
vl_api_interface_index_t mcast_sw_if_index
Definition: gtpu.api:39
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
fib_table_t_::ft_table_id
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
vl_api_gtpu_add_del_tunnel_t::encap_vrf_id
u32 encap_vrf_id
Definition: gtpu.api:40
vl_api_gtpu_tunnel_details_t::context
u32 context
Definition: gtpu.api:104
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
gtpu_tunnel_t::decap_next_index
u32 decap_next_index
Definition: gtpu.h:151
vl_api_gtpu_add_del_tunnel_t::tteid
u32 tteid
Definition: gtpu.api:43
vl_api_gtpu_tunnel_details_t::tteid
u32 tteid
Definition: gtpu.api:112
vl_api_gtpu_tunnel_update_tteid_t::tteid
u32 tteid
Definition: gtpu.api:74
vl_api_gtpu_tunnel_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gtpu.api:105
vl_api_sw_interface_set_gtpu_bypass_t::is_ipv6
bool is_ipv6
Definition: gtpu.api:127
vnet_hw_interface_is_valid
static uword vnet_hw_interface_is_valid(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:258
vl_api_gtpu_tunnel_dump_t::context
u32 context
Definition: gtpu.api:86
vl_api_gtpu_tunnel_details_t::dst_address
vl_api_address_t dst_address
Definition: gtpu.api:107
interface.h
ip6_main_t::fibs
struct fib_table_t_ * fibs
Definition: ip6.h:115
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(gtpu_api_hookup)
gtpu_tunnel_t::dst
ip46_address_t dst
Definition: gtpu.h:145
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
vnet_int_gtpu_bypass_mode
void vnet_int_gtpu_bypass_mode(u32 sw_if_index, u8 is_ip6, u8 is_enable)
Definition: gtpu.c:943
BAD_SW_IF_INDEX_LABEL
#define BAD_SW_IF_INDEX_LABEL
Definition: api_helper_macros.h:289
vl_api_gtpu_offload_rx_t_handler
static void vl_api_gtpu_offload_rx_t_handler(vl_api_gtpu_offload_rx_t *mp)
Definition: gtpu_api.c:38
vl_api_gtpu_offload_rx_t::hw_if_index
u32 hw_if_index
Definition: gtpu.api:143
ip6_main
ip6_main_t ip6_main
Definition: ip6_forward.c:2785
vnet_sw_if_index_is_api_valid
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
Definition: api_helper_macros.h:276
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
format_fns.h
vl_api_gtpu_add_del_tunnel_t::teid
u32 teid
Definition: gtpu.api:42
u32
unsigned int u32
Definition: types.h:88
gtpu_tunnel_t::encap_fib_index
u32 encap_fib_index
Definition: gtpu.h:154
ip4_main_t::fibs
struct fib_table_t_ * fibs
Vector of FIBs.
Definition: ip4.h:112
api_helper_macros.h
gtpu_tunnel_t::tteid
u32 tteid
Definition: gtpu.h:141
vl_api_sw_interface_set_gtpu_bypass_t::enable
bool enable
Definition: gtpu.api:128
vl_api_gtpu_tunnel_update_tteid_t::dst_address
vl_api_address_t dst_address
Definition: gtpu.api:71
vl_api_gtpu_offload_rx_t::enable
u8 enable
Definition: gtpu.api:145
gtpu_tunnel_t
Definition: gtpu.h:128
vl_api_gtpu_tunnel_update_tteid_t::encap_vrf_id
u32 encap_vrf_id
Definition: gtpu.api:72
ip6_main_t
Definition: ip6.h:110
gtpu_api_hookup
static clib_error_t * gtpu_api_hookup(vlib_main_t *vm)
Definition: gtpu_api.c:262
GTPU_UPD_TTEID
#define GTPU_UPD_TTEID
Definition: gtpu.h:261
vnet_gtpu_add_mod_del_tunnel_args_t
Definition: gtpu.h:256
vl_api_gtpu_tunnel_dump_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: gtpu.api:87
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vl_api_gtpu_tunnel_details_t
dump details of an GTPU tunnel
Definition: gtpu.api:102
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:206
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:525
vl_api_gtpu_tunnel_update_tteid_t
Update GTPU tunnel TX TEID.
Definition: gtpu.api:67
gtpu_main_t::tunnel_index_by_sw_if_index
u32 * tunnel_index_by_sw_if_index
Definition: gtpu.h:225
gtpu_tunnel_t::teid
u32 teid
Definition: gtpu.h:140
vnet_gtpu_add_del_rx_flow
int vnet_gtpu_add_del_rx_flow(u32 hw_if_index, u32 t_index, int is_add)
Definition: gtpu.c:1112
vl_api_gtpu_add_del_tunnel_t
Create or delete a GTPU tunnel.
Definition: gtpu.api:32
gtpu_tunnel_t::mcast_sw_if_index
u32 mcast_sw_if_index
Definition: gtpu.h:148
context
u32 context
Definition: ip.api:852
vl_api_gtpu_tunnel_dump_t
Dump GTPU tunnel.
Definition: gtpu.api:83
gtpu_main
gtpu_main_t gtpu_main
Definition: gtpu.c:36
rv
int __clib_unused rv
Definition: application.c:491
vl_api_gtpu_add_del_tunnel_t::src_address
vl_api_address_t src_address
Definition: gtpu.api:37
gtpu_main_t::tunnels
gtpu_tunnel_t * tunnels
Definition: gtpu.h:208
vl_api_gtpu_tunnel_details_t::decap_next_index
u32 decap_next_index
Definition: gtpu.api:110
GTPU_ADD_TUNNEL
#define GTPU_ADD_TUNNEL
Definition: gtpu.h:260
api_errno.h
vl_api_gtpu_tunnel_details_t::src_address
vl_api_address_t src_address
Definition: gtpu.api:106
ip4_main_t
IPv4 main type.
Definition: ip4.h:107
fib_table_find
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1111
vl_api_gtpu_tunnel_details_t::mcast_sw_if_index
vl_api_interface_index_t mcast_sw_if_index
Definition: gtpu.api:108
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ip_types_api.h
vl_api_gtpu_add_del_tunnel_t::dst_address
vl_api_address_t dst_address
Definition: gtpu.api:38
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199