gtpu: msg id fix in send_gtpu_tunnel_details api
[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
28 #include <gtpu/gtpu.h>
29
30 #include <gtpu/gtpu.api_enum.h>
31 #include <gtpu/gtpu.api_types.h>
32
33 #define REPLY_MSG_ID_BASE gtm->msg_id_base
34 #include <vlibapi/api_helper_macros.h>
35
36 static void
37   vl_api_sw_interface_set_gtpu_bypass_t_handler
38   (vl_api_sw_interface_set_gtpu_bypass_t * mp)
39 {
40   vl_api_sw_interface_set_gtpu_bypass_reply_t *rmp;
41   int rv = 0;
42   u32 sw_if_index = ntohl (mp->sw_if_index);
43   gtpu_main_t *gtm = &gtpu_main;
44
45   VALIDATE_SW_IF_INDEX (mp);
46
47   vnet_int_gtpu_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
48   BAD_SW_IF_INDEX_LABEL;
49
50   REPLY_MACRO (VL_API_SW_INTERFACE_SET_GTPU_BYPASS_REPLY);
51 }
52
53 static void vl_api_gtpu_add_del_tunnel_t_handler
54   (vl_api_gtpu_add_del_tunnel_t * mp)
55 {
56   vl_api_gtpu_add_del_tunnel_reply_t *rmp;
57   int rv = 0;
58   ip4_main_t *im = &ip4_main;
59   gtpu_main_t *gtm = &gtpu_main;
60
61   uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
62   if (!p)
63     {
64       rv = VNET_API_ERROR_NO_SUCH_FIB;
65       goto out;
66     }
67
68   vnet_gtpu_add_del_tunnel_args_t a = {
69     .is_add = mp->is_add,
70     .is_ip6 = mp->is_ipv6,
71     .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
72     .encap_fib_index = p[0],
73     .decap_next_index = ntohl (mp->decap_next_index),
74     .teid = ntohl (mp->teid),
75     .dst = to_ip46 (mp->is_ipv6, mp->dst_address),
76     .src = to_ip46 (mp->is_ipv6, mp->src_address),
77   };
78
79   /* Check src & dst are different */
80   if (ip46_address_cmp (&a.dst, &a.src) == 0)
81     {
82       rv = VNET_API_ERROR_SAME_SRC_DST;
83       goto out;
84     }
85   if (ip46_address_is_multicast (&a.dst) &&
86       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
87     {
88       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
89       goto out;
90     }
91
92   u32 sw_if_index = ~0;
93   rv = vnet_gtpu_add_del_tunnel (&a, &sw_if_index);
94
95 out:
96   /* *INDENT-OFF* */
97   REPLY_MACRO2(VL_API_GTPU_ADD_DEL_TUNNEL_REPLY,
98   ({
99     rmp->sw_if_index = ntohl (sw_if_index);
100   }));
101   /* *INDENT-ON* */
102 }
103
104 static void send_gtpu_tunnel_details
105   (gtpu_tunnel_t * t, vl_api_registration_t * reg, u32 context)
106 {
107   vl_api_gtpu_tunnel_details_t *rmp;
108   gtpu_main_t *gtm = &gtpu_main;
109   ip4_main_t *im4 = &ip4_main;
110   ip6_main_t *im6 = &ip6_main;
111   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
112
113   rmp = vl_msg_api_alloc (sizeof (*rmp));
114   clib_memset (rmp, 0, sizeof (*rmp));
115   rmp->_vl_msg_id = ntohs (VL_API_GTPU_TUNNEL_DETAILS + gtm->msg_id_base);
116   if (is_ipv6)
117     {
118       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
119       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
120       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
121     }
122   else
123     {
124       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
125       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
126       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
127     }
128   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
129   rmp->teid = htonl (t->teid);
130   rmp->decap_next_index = htonl (t->decap_next_index);
131   rmp->sw_if_index = htonl (t->sw_if_index);
132   rmp->is_ipv6 = is_ipv6;
133   rmp->context = context;
134
135   vl_api_send_msg (reg, (u8 *) rmp);
136 }
137
138 static void
139 vl_api_gtpu_tunnel_dump_t_handler (vl_api_gtpu_tunnel_dump_t * mp)
140 {
141   vl_api_registration_t *reg;
142   gtpu_main_t *gtm = &gtpu_main;
143   gtpu_tunnel_t *t;
144   u32 sw_if_index;
145
146   reg = vl_api_client_index_to_registration (mp->client_index);
147   if (!reg)
148     return;
149
150   sw_if_index = ntohl (mp->sw_if_index);
151
152   if (~0 == sw_if_index)
153     {
154       /* *INDENT-OFF* */
155       pool_foreach (t, gtm->tunnels,
156       ({
157         send_gtpu_tunnel_details(t, reg, mp->context);
158       }));
159       /* *INDENT-ON* */
160     }
161   else
162     {
163       if ((sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index)) ||
164           (~0 == gtm->tunnel_index_by_sw_if_index[sw_if_index]))
165         {
166           return;
167         }
168       t = &gtm->tunnels[gtm->tunnel_index_by_sw_if_index[sw_if_index]];
169       send_gtpu_tunnel_details (t, reg, mp->context);
170     }
171 }
172
173 #include <gtpu/gtpu.api.c>
174 static clib_error_t *
175 gtpu_api_hookup (vlib_main_t * vm)
176 {
177   gtpu_main_t *gtm = &gtpu_main;
178
179   gtm->msg_id_base = setup_message_id_table ();
180   return 0;
181 }
182
183 VLIB_API_INIT_FUNCTION (gtpu_api_hookup);
184
185 /*
186  * fd.io coding-style-patch-verification: ON
187  *
188  * Local Variables:
189  * eval: (c-set-style "gnu")
190  * End:
191  */