udp: fix csum computation when offload disabled
[vpp.git] / src / plugins / gtpu / gtpu_api.c
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
35 #include <vlibapi/api_helper_macros.h>
36
37 static void
38 vl_api_gtpu_offload_rx_t_handler (vl_api_gtpu_offload_rx_t * mp)
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     }
50   VALIDATE_SW_IF_INDEX (mp);
51
52   u32 t_index = vnet_gtpu_get_tunnel_index (sw_if_index);
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);
76   ip4_main_t *im = &ip4_main;
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     }
91   BAD_SW_IF_INDEX_LABEL;
92 err:
93
94   REPLY_MACRO (VL_API_GTPU_OFFLOAD_RX_REPLY);
95 }
96
97 static void
98   vl_api_sw_interface_set_gtpu_bypass_t_handler
99   (vl_api_sw_interface_set_gtpu_bypass_t * mp)
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
106   VALIDATE_SW_IF_INDEX (mp);
107
108   vnet_int_gtpu_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
109   BAD_SW_IF_INDEX_LABEL;
110
111   REPLY_MACRO (VL_API_SW_INTERFACE_SET_GTPU_BYPASS_REPLY);
112 }
113
114 static void vl_api_gtpu_add_del_tunnel_t_handler
115   (vl_api_gtpu_add_del_tunnel_t * mp)
116 {
117   vl_api_gtpu_add_del_tunnel_reply_t *rmp;
118   int rv = 0;
119   gtpu_main_t *gtm = &gtpu_main;
120
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),
127     .pdu_extension = 0,
128     .qfi = 0,
129     .is_forwarding = 0,
130     .forwarding_type = 0,
131   };
132   ip_address_decode (&mp->dst_address, &a.dst);
133   ip_address_decode (&mp->src_address, &a.src);
134
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)
139     {
140       rv = VNET_API_ERROR_NO_SUCH_FIB;
141       goto out;
142     }
143
144   /* Check src & dst are different */
145   if (ip46_address_cmp (&a.dst, &a.src) == 0)
146     {
147       rv = VNET_API_ERROR_SAME_SRC_DST;
148       goto out;
149     }
150   if (ip46_address_is_multicast (&a.dst) &&
151       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
152     {
153       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
154       goto out;
155     }
156
157   u32 sw_if_index = ~0;
158   rv = vnet_gtpu_add_mod_del_tunnel (&a, &sw_if_index);
159
160 out:
161   REPLY_MACRO2(VL_API_GTPU_ADD_DEL_TUNNEL_REPLY,
162   ({
163     rmp->sw_if_index = ntohl (sw_if_index);
164   }));
165 }
166
167 static void
168 vl_api_gtpu_add_del_tunnel_v2_t_handler (vl_api_gtpu_add_del_tunnel_v2_t *mp)
169 {
170   vl_api_gtpu_add_del_tunnel_v2_reply_t *rmp;
171   int rv = 0;
172   vlib_counter_t result_rx;
173   vlib_counter_t result_tx;
174   gtpu_main_t *gtm = &gtpu_main;
175
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,
183     .qfi = mp->qfi,
184     .is_forwarding = 0,
185     .forwarding_type = 0,
186   };
187   ip_address_decode (&mp->dst_address, &a.dst);
188   ip_address_decode (&mp->src_address, &a.src);
189
190   u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
191   a.encap_fib_index =
192     fib_table_find (fib_ip_proto (is_ipv6), ntohl (mp->encap_vrf_id));
193   if (a.encap_fib_index == ~0)
194     {
195       rv = VNET_API_ERROR_NO_SUCH_FIB;
196       goto out;
197     }
198
199   /* Check src & dst are different */
200   if (ip46_address_cmp (&a.dst, &a.src) == 0)
201     {
202       rv = VNET_API_ERROR_SAME_SRC_DST;
203       goto out;
204     }
205   if (ip46_address_is_multicast (&a.dst) &&
206       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
207     {
208       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
209       goto out;
210     }
211
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);
215
216 out:
217   REPLY_MACRO2 (
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);
224     }));
225 }
226
227 static void vl_api_gtpu_tunnel_update_tteid_t_handler
228   (vl_api_gtpu_tunnel_update_tteid_t * mp)
229 {
230   vl_api_gtpu_tunnel_update_tteid_reply_t *rmp;
231   int rv = 0;
232   gtpu_main_t *gtm = &gtpu_main;
233
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),
238   };
239   ip_address_decode (&mp->dst_address, &a.dst);
240
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)
245     {
246       rv = VNET_API_ERROR_NO_SUCH_FIB;
247       goto out;
248     }
249
250   rv = vnet_gtpu_add_mod_del_tunnel (&a, 0);
251
252 out:
253   REPLY_MACRO (VL_API_GTPU_TUNNEL_UPDATE_TTEID_REPLY);
254 }
255
256 static void send_gtpu_tunnel_details
257   (gtpu_tunnel_t * t, vl_api_registration_t * reg, u32 context)
258 {
259   vl_api_gtpu_tunnel_details_t *rmp;
260   gtpu_main_t *gtm = &gtpu_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);
264
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);
268
269   ip_address_encode (&t->src, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
270                      &rmp->src_address);
271   ip_address_encode (&t->dst, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
272                      &rmp->dst_address);
273
274   rmp->encap_vrf_id =
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;
283
284   vl_api_send_msg (reg, (u8 *) rmp);
285 }
286
287 static void
288 vl_api_gtpu_tunnel_dump_t_handler (vl_api_gtpu_tunnel_dump_t * mp)
289 {
290   vl_api_registration_t *reg;
291   gtpu_main_t *gtm = &gtpu_main;
292   gtpu_tunnel_t *t;
293   u32 sw_if_index;
294
295   reg = vl_api_client_index_to_registration (mp->client_index);
296   if (!reg)
297     return;
298
299   sw_if_index = ntohl (mp->sw_if_index);
300
301   if (~0 == sw_if_index)
302     {
303       pool_foreach (t, gtm->tunnels)
304        {
305         send_gtpu_tunnel_details(t, reg, mp->context);
306        }
307     }
308   else
309     {
310       if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
311           (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
312         {
313           return;
314         }
315       t = &gtm->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
316       send_gtpu_tunnel_details (t, reg, mp->context);
317     }
318 }
319
320 static void
321 send_gtpu_tunnel_details_v2 (gtpu_tunnel_t *t, vl_api_registration_t *reg,
322                              u32 context)
323 {
324   vl_api_gtpu_tunnel_v2_details_t *rmp;
325   vlib_counter_t result_rx;
326   vlib_counter_t result_tx;
327   gtpu_main_t *gtm = &gtpu_main;
328   ip4_main_t *im4 = &ip4_main;
329   ip6_main_t *im6 = &ip6_main;
330   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
331
332   rmp = vl_msg_api_alloc (sizeof (*rmp));
333   clib_memset (rmp, 0, sizeof (*rmp));
334   rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_V2_DETAILS + gtm->msg_id_base);
335
336   ip_address_encode (&t->src, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
337                      &rmp->src_address);
338   ip_address_encode (&t->dst, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
339                      &rmp->dst_address);
340
341   rmp->encap_vrf_id = is_ipv6 ?
342                               htonl (im6->fibs[t->encap_fib_index].ft_table_id) :
343                               htonl (im4->fibs[t->encap_fib_index].ft_table_id);
344   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
345   rmp->teid = htonl (t->teid);
346   rmp->tteid = htonl (t->tteid);
347   rmp->decap_next_index = htonl (t->decap_next_index);
348   rmp->sw_if_index = htonl (t->sw_if_index);
349   rmp->context = context;
350   rmp->pdu_extension = t->pdu_extension;
351   rmp->qfi = t->qfi;
352   rmp->is_forwarding = t->is_forwarding;
353   rmp->forwarding_type = htonl (t->forwarding_type);
354
355   get_combined_counters (t->sw_if_index, &result_rx, &result_tx);
356   rmp->counters.packets_rx = clib_net_to_host_u64 (result_rx.packets);
357   rmp->counters.packets_tx = clib_net_to_host_u64 (result_tx.packets);
358   rmp->counters.bytes_rx = clib_net_to_host_u64 (result_rx.bytes);
359   rmp->counters.bytes_tx = clib_net_to_host_u64 (result_tx.bytes);
360
361   vl_api_send_msg (reg, (u8 *) rmp);
362 }
363
364 static void
365 vl_api_gtpu_tunnel_v2_dump_t_handler (vl_api_gtpu_tunnel_v2_dump_t *mp)
366 {
367   vl_api_registration_t *reg;
368   gtpu_main_t *gtm = &gtpu_main;
369   gtpu_tunnel_t *t;
370   u32 sw_if_index;
371
372   reg = vl_api_client_index_to_registration (mp->client_index);
373   if (!reg)
374     return;
375
376   sw_if_index = ntohl (mp->sw_if_index);
377
378   if (~0 == sw_if_index)
379     {
380       pool_foreach (t, gtm->tunnels)
381         {
382           send_gtpu_tunnel_details_v2 (t, reg, mp->context);
383         }
384     }
385   else
386     {
387       if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
388           (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
389         {
390           return;
391         }
392       t = &gtm->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
393       send_gtpu_tunnel_details_v2 (t, reg, mp->context);
394     }
395 }
396
397 static void
398 vl_api_gtpu_add_del_forward_t_handler (vl_api_gtpu_add_del_forward_t *mp)
399 {
400   vl_api_gtpu_add_del_forward_reply_t *rmp;
401   int rv = 0;
402   gtpu_main_t *gtm = &gtpu_main;
403
404   vnet_gtpu_add_mod_del_tunnel_args_t a = {
405     .opn = mp->is_add ? GTPU_ADD_TUNNEL : GTPU_DEL_TUNNEL,
406     .mcast_sw_if_index = 0,
407     .decap_next_index = ntohl (mp->decap_next_index),
408     .teid = 0,
409     .tteid = 0,
410     .pdu_extension = 0,
411     .qfi = 0,
412     .is_forwarding = 1,
413     .forwarding_type = ntohl (mp->forwarding_type),
414   };
415   ip_address_decode (&mp->dst_address, &a.dst);
416   /* Will be overwritten later */
417   ip_address_decode (&mp->dst_address, &a.src);
418
419   u8 is_ipv6 = !ip46_address_is_ip4 (&a.dst);
420   a.encap_fib_index =
421     fib_table_find (fib_ip_proto (is_ipv6), ntohl (mp->encap_vrf_id));
422
423   if (a.encap_fib_index == ~0)
424     {
425       rv = VNET_API_ERROR_NO_SUCH_FIB;
426       goto out;
427     }
428
429   if (ip46_address_is_multicast (&a.dst) &&
430       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
431     {
432       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
433       goto out;
434     }
435
436   u32 sw_if_index = ~0;
437   rv = vnet_gtpu_add_del_forwarding (&a, &sw_if_index);
438
439 out:
440   REPLY_MACRO2 (VL_API_GTPU_ADD_DEL_FORWARD_REPLY,
441                 ({ rmp->sw_if_index = ntohl (sw_if_index); }));
442 }
443
444 static void
445 vl_api_gtpu_get_transfer_counts_t_handler (
446   vl_api_gtpu_get_transfer_counts_t *mp)
447 {
448   vl_api_gtpu_get_transfer_counts_reply_t *rmp;
449   int rv = 0;
450   vlib_counter_t result_rx;
451   vlib_counter_t result_tx;
452   gtpu_main_t *gtm = &gtpu_main;
453   u32 count = 0;
454   u32 sw_if_index;
455   u32 capacity = ntohl (mp->capacity);
456   u32 sw_if_index_start = ntohl (mp->sw_if_index_start);
457   int extra_size = sizeof (rmp->tunnels[0]) * capacity;
458
459   if (sw_if_index_start >= vec_len (gtm->tunnel_index_by_sw_if_index))
460     {
461       capacity = 0;
462       extra_size = 0;
463     }
464   sw_if_index = sw_if_index_start;
465
466   REPLY_MACRO4 (
467     VL_API_GTPU_GET_TRANSFER_COUNTS_REPLY, extra_size, ({
468       for (; count < capacity; sw_if_index++)
469         {
470           if (sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index))
471             {
472               // No more tunnels
473               break;
474             }
475           if (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index])
476             {
477               // Skip inactive/deleted tunnel
478               continue;
479             }
480           rmp->tunnels[count].sw_if_index = htonl (sw_if_index);
481           rmp->tunnels[count].reserved = 0;
482
483           get_combined_counters (sw_if_index, &result_rx, &result_tx);
484           rmp->tunnels[count].counters.packets_rx =
485             clib_net_to_host_u64 (result_rx.packets);
486           rmp->tunnels[count].counters.packets_tx =
487             clib_net_to_host_u64 (result_tx.packets);
488           rmp->tunnels[count].counters.bytes_rx =
489             clib_net_to_host_u64 (result_rx.bytes);
490           rmp->tunnels[count].counters.bytes_tx =
491             clib_net_to_host_u64 (result_tx.bytes);
492           count++;
493         }
494       rmp->count = htonl (count);
495     }));
496 }
497
498 #include <gtpu/gtpu.api.c>
499 static clib_error_t *
500 gtpu_api_hookup (vlib_main_t * vm)
501 {
502   gtpu_main_t *gtm = &gtpu_main;
503
504   gtm->msg_id_base = setup_message_id_table ();
505   return 0;
506 }
507
508 VLIB_API_INIT_FUNCTION (gtpu_api_hookup);
509
510 /*
511  * fd.io coding-style-patch-verification: ON
512  *
513  * Local Variables:
514  * eval: (c-set-style "gnu")
515  * End:
516  */