2 *------------------------------------------------------------------
3 * gtpu_api.c - gtpu api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/interface.h>
21 #include <vnet/api_errno.h>
22 #include <vnet/feature/feature.h>
23 #include <vnet/fib/fib_table.h>
25 #include <vppinfra/byte_order.h>
26 #include <vlibmemory/api.h>
27 #include <vnet/ip/ip_types_api.h>
28 #include <gtpu/gtpu.h>
30 #include <vnet/format_fns.h>
31 #include <gtpu/gtpu.api_enum.h>
32 #include <gtpu/gtpu.api_types.h>
34 #define REPLY_MSG_ID_BASE gtm->msg_id_base
35 #include <vlibapi/api_helper_macros.h>
38 vl_api_gtpu_offload_rx_t_handler (vl_api_gtpu_offload_rx_t * mp)
40 vl_api_gtpu_offload_rx_reply_t *rmp;
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);
45 if (!vnet_hw_interface_is_valid (vnet_get_main (), hw_if_index))
47 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
50 VALIDATE_SW_IF_INDEX (mp);
52 u32 t_index = vnet_gtpu_get_tunnel_index (sw_if_index);
55 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
59 gtpu_main_t *gtm = >pu_main;
60 gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
61 if (!ip46_address_is_ip4 (&t->dst))
63 rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
67 if ((t->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT) &&
68 (t->decap_next_index != GTPU_INPUT_NEXT_IP6_INPUT))
70 rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
74 vnet_main_t *vnm = vnet_get_main ();
75 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
76 ip4_main_t *im = &ip4_main;
78 vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
80 if (t->encap_fib_index != rx_fib_index)
82 rv = VNET_API_ERROR_NO_SUCH_FIB;
86 if (vnet_gtpu_add_del_rx_flow (hw_if_index, t_index, mp->enable))
88 rv = VNET_API_ERROR_UNSPECIFIED;
91 BAD_SW_IF_INDEX_LABEL;
94 REPLY_MACRO (VL_API_GTPU_OFFLOAD_RX_REPLY);
98 vl_api_sw_interface_set_gtpu_bypass_t_handler
99 (vl_api_sw_interface_set_gtpu_bypass_t * mp)
101 vl_api_sw_interface_set_gtpu_bypass_reply_t *rmp;
103 u32 sw_if_index = ntohl (mp->sw_if_index);
104 gtpu_main_t *gtm = >pu_main;
106 VALIDATE_SW_IF_INDEX (mp);
108 vnet_int_gtpu_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
109 BAD_SW_IF_INDEX_LABEL;
111 REPLY_MACRO (VL_API_SW_INTERFACE_SET_GTPU_BYPASS_REPLY);
114 static void vl_api_gtpu_add_del_tunnel_t_handler
115 (vl_api_gtpu_add_del_tunnel_t * mp)
117 vl_api_gtpu_add_del_tunnel_reply_t *rmp;
119 gtpu_main_t *gtm = >pu_main;
121 vnet_gtpu_add_mod_del_tunnel_args_t a = {
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),
130 .forwarding_type = 0,
132 ip_address_decode (&mp->dst_address, &a.dst);
133 ip_address_decode (&mp->src_address, &a.src);
135 u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
136 a.encap_fib_index = fib_table_find (fib_ip_proto (is_ipv6),
137 ntohl (mp->encap_vrf_id));
138 if (a.encap_fib_index == ~0)
140 rv = VNET_API_ERROR_NO_SUCH_FIB;
144 /* Check src & dst are different */
145 if (ip46_address_cmp (&a.dst, &a.src) == 0)
147 rv = VNET_API_ERROR_SAME_SRC_DST;
150 if (ip46_address_is_multicast (&a.dst) &&
151 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
153 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
157 u32 sw_if_index = ~0;
158 rv = vnet_gtpu_add_mod_del_tunnel (&a, &sw_if_index);
161 REPLY_MACRO2(VL_API_GTPU_ADD_DEL_TUNNEL_REPLY,
163 rmp->sw_if_index = ntohl (sw_if_index);
168 vl_api_gtpu_add_del_tunnel_v2_t_handler (vl_api_gtpu_add_del_tunnel_v2_t *mp)
170 vl_api_gtpu_add_del_tunnel_v2_reply_t *rmp;
172 vlib_counter_t result_rx;
173 vlib_counter_t result_tx;
174 gtpu_main_t *gtm = >pu_main;
176 vnet_gtpu_add_mod_del_tunnel_args_t a = {
177 .opn = mp->is_add ? GTPU_ADD_TUNNEL : GTPU_DEL_TUNNEL,
178 .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
179 .decap_next_index = ntohl (mp->decap_next_index),
180 .teid = ntohl (mp->teid),
181 .tteid = ntohl (mp->tteid),
182 .pdu_extension = mp->pdu_extension ? 1 : 0,
185 .forwarding_type = 0,
187 ip_address_decode (&mp->dst_address, &a.dst);
188 ip_address_decode (&mp->src_address, &a.src);
190 u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
192 fib_table_find (fib_ip_proto (is_ipv6), ntohl (mp->encap_vrf_id));
193 if (a.encap_fib_index == ~0)
195 rv = VNET_API_ERROR_NO_SUCH_FIB;
199 /* Check src & dst are different */
200 if (ip46_address_cmp (&a.dst, &a.src) == 0)
202 rv = VNET_API_ERROR_SAME_SRC_DST;
205 if (ip46_address_is_multicast (&a.dst) &&
206 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
208 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
212 u32 sw_if_index = ~0;
213 rv = vnet_gtpu_add_mod_del_tunnel (&a, &sw_if_index);
214 get_combined_counters (sw_if_index, &result_rx, &result_tx);
218 VL_API_GTPU_ADD_DEL_TUNNEL_V2_REPLY, ({
219 rmp->sw_if_index = ntohl (sw_if_index);
220 rmp->counters.packets_rx = clib_net_to_host_u64 (result_rx.packets);
221 rmp->counters.packets_tx = clib_net_to_host_u64 (result_tx.packets);
222 rmp->counters.bytes_rx = clib_net_to_host_u64 (result_rx.bytes);
223 rmp->counters.bytes_tx = clib_net_to_host_u64 (result_tx.bytes);
227 static void vl_api_gtpu_tunnel_update_tteid_t_handler
228 (vl_api_gtpu_tunnel_update_tteid_t * mp)
230 vl_api_gtpu_tunnel_update_tteid_reply_t *rmp;
232 gtpu_main_t *gtm = >pu_main;
234 vnet_gtpu_add_mod_del_tunnel_args_t a = {
235 .opn = GTPU_UPD_TTEID,
236 .teid = ntohl (mp->teid),
237 .tteid = ntohl (mp->tteid),
239 ip_address_decode (&mp->dst_address, &a.dst);
241 u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
242 a.encap_fib_index = fib_table_find (fib_ip_proto (is_ipv6),
243 ntohl (mp->encap_vrf_id));
244 if (a.encap_fib_index == ~0)
246 rv = VNET_API_ERROR_NO_SUCH_FIB;
250 rv = vnet_gtpu_add_mod_del_tunnel (&a, 0);
253 REPLY_MACRO (VL_API_GTPU_TUNNEL_UPDATE_TTEID_REPLY);
256 static void send_gtpu_tunnel_details
257 (gtpu_tunnel_t * t, vl_api_registration_t * reg, u32 context)
259 vl_api_gtpu_tunnel_details_t *rmp;
260 gtpu_main_t *gtm = >pu_main;
261 ip4_main_t *im4 = &ip4_main;
262 ip6_main_t *im6 = &ip6_main;
263 u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
265 rmp = vl_msg_api_alloc (sizeof (*rmp));
266 clib_memset (rmp, 0, sizeof (*rmp));
267 rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_DETAILS + gtm->msg_id_base);
269 ip_address_encode (&t->src, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
271 ip_address_encode (&t->dst, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
275 is_ipv6 ? htonl (im6->fibs[t->encap_fib_index].ft_table_id) :
276 htonl (im4->fibs[t->encap_fib_index].ft_table_id);
277 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
278 rmp->teid = htonl (t->teid);
279 rmp->tteid = htonl (t->tteid);
280 rmp->decap_next_index = htonl (t->decap_next_index);
281 rmp->sw_if_index = htonl (t->sw_if_index);
282 rmp->context = context;
284 vl_api_send_msg (reg, (u8 *) rmp);
288 vl_api_gtpu_tunnel_dump_t_handler (vl_api_gtpu_tunnel_dump_t * mp)
290 vl_api_registration_t *reg;
291 gtpu_main_t *gtm = >pu_main;
295 reg = vl_api_client_index_to_registration (mp->client_index);
299 sw_if_index = ntohl (mp->sw_if_index);
301 if (~0 == sw_if_index)
304 pool_foreach (t, gtm->tunnels)
306 send_gtpu_tunnel_details(t, reg, mp->context);
312 if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
313 (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
317 t = >m->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
318 send_gtpu_tunnel_details (t, reg, mp->context);
323 send_gtpu_tunnel_details_v2 (gtpu_tunnel_t *t, vl_api_registration_t *reg,
326 vl_api_gtpu_tunnel_v2_details_t *rmp;
327 vlib_counter_t result_rx;
328 vlib_counter_t result_tx;
329 gtpu_main_t *gtm = >pu_main;
330 ip4_main_t *im4 = &ip4_main;
331 ip6_main_t *im6 = &ip6_main;
332 u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
334 rmp = vl_msg_api_alloc (sizeof (*rmp));
335 clib_memset (rmp, 0, sizeof (*rmp));
336 rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_V2_DETAILS + gtm->msg_id_base);
338 ip_address_encode (&t->src, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
340 ip_address_encode (&t->dst, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
343 rmp->encap_vrf_id = is_ipv6 ?
344 htonl (im6->fibs[t->encap_fib_index].ft_table_id) :
345 htonl (im4->fibs[t->encap_fib_index].ft_table_id);
346 rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
347 rmp->teid = htonl (t->teid);
348 rmp->tteid = htonl (t->tteid);
349 rmp->decap_next_index = htonl (t->decap_next_index);
350 rmp->sw_if_index = htonl (t->sw_if_index);
351 rmp->context = context;
352 rmp->pdu_extension = t->pdu_extension;
354 rmp->is_forwarding = t->is_forwarding;
355 rmp->forwarding_type = htonl (t->forwarding_type);
357 get_combined_counters (t->sw_if_index, &result_rx, &result_tx);
358 rmp->counters.packets_rx = clib_net_to_host_u64 (result_rx.packets);
359 rmp->counters.packets_tx = clib_net_to_host_u64 (result_tx.packets);
360 rmp->counters.bytes_rx = clib_net_to_host_u64 (result_rx.bytes);
361 rmp->counters.bytes_tx = clib_net_to_host_u64 (result_tx.bytes);
363 vl_api_send_msg (reg, (u8 *) rmp);
367 vl_api_gtpu_tunnel_v2_dump_t_handler (vl_api_gtpu_tunnel_v2_dump_t *mp)
369 vl_api_registration_t *reg;
370 gtpu_main_t *gtm = >pu_main;
374 reg = vl_api_client_index_to_registration (mp->client_index);
378 sw_if_index = ntohl (mp->sw_if_index);
380 if (~0 == sw_if_index)
382 pool_foreach (t, gtm->tunnels)
384 send_gtpu_tunnel_details_v2 (t, reg, mp->context);
389 if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
390 (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
394 t = >m->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
395 send_gtpu_tunnel_details_v2 (t, reg, mp->context);
400 vl_api_gtpu_add_del_forward_t_handler (vl_api_gtpu_add_del_forward_t *mp)
402 vl_api_gtpu_add_del_forward_reply_t *rmp;
404 gtpu_main_t *gtm = >pu_main;
406 vnet_gtpu_add_mod_del_tunnel_args_t a = {
407 .opn = mp->is_add ? GTPU_ADD_TUNNEL : GTPU_DEL_TUNNEL,
408 .mcast_sw_if_index = 0,
409 .decap_next_index = ntohl (mp->decap_next_index),
415 .forwarding_type = ntohl (mp->forwarding_type),
417 ip_address_decode (&mp->dst_address, &a.dst);
418 /* Will be overwritten later */
419 ip_address_decode (&mp->dst_address, &a.src);
421 u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
423 fib_table_find (fib_ip_proto (is_ipv6), ntohl (mp->encap_vrf_id));
425 if (a.encap_fib_index == ~0)
427 rv = VNET_API_ERROR_NO_SUCH_FIB;
431 if (ip46_address_is_multicast (&a.dst) &&
432 !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
434 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
438 u32 sw_if_index = ~0;
439 rv = vnet_gtpu_add_del_forwarding (&a, &sw_if_index);
442 REPLY_MACRO2 (VL_API_GTPU_ADD_DEL_FORWARD_REPLY,
443 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
447 vl_api_gtpu_get_transfer_counts_t_handler (
448 vl_api_gtpu_get_transfer_counts_t *mp)
450 vl_api_gtpu_get_transfer_counts_reply_t *rmp;
452 vlib_counter_t result_rx;
453 vlib_counter_t result_tx;
454 gtpu_main_t *gtm = >pu_main;
457 u32 capacity = ntohl (mp->capacity);
458 u32 sw_if_index_start = ntohl (mp->sw_if_index_start);
459 int extra_size = sizeof (rmp->tunnels[0]) * capacity;
461 if (sw_if_index_start >= vec_len (gtm->tunnel_index_by_sw_if_index))
466 sw_if_index = sw_if_index_start;
469 VL_API_GTPU_GET_TRANSFER_COUNTS_REPLY, extra_size, ({
470 for (; count < capacity; sw_if_index++)
472 if (sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index))
477 if (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index])
479 // Skip inactive/deleted tunnel
482 rmp->tunnels[count].sw_if_index = htonl (sw_if_index);
483 rmp->tunnels[count].reserved = 0;
485 get_combined_counters (sw_if_index, &result_rx, &result_tx);
486 rmp->tunnels[count].counters.packets_rx =
487 clib_net_to_host_u64 (result_rx.packets);
488 rmp->tunnels[count].counters.packets_tx =
489 clib_net_to_host_u64 (result_tx.packets);
490 rmp->tunnels[count].counters.bytes_rx =
491 clib_net_to_host_u64 (result_rx.bytes);
492 rmp->tunnels[count].counters.bytes_tx =
493 clib_net_to_host_u64 (result_tx.bytes);
496 rmp->count = htonl (count);
500 #include <gtpu/gtpu.api.c>
501 static clib_error_t *
502 gtpu_api_hookup (vlib_main_t * vm)
504 gtpu_main_t *gtm = >pu_main;
506 gtm->msg_id_base = setup_message_id_table ();
510 VLIB_API_INIT_FUNCTION (gtpu_api_hookup);
513 * fd.io coding-style-patch-verification: ON
516 * eval: (c-set-style "gnu")