vxlan: multiarch optimization of vxlan
[vpp.git] / extras / deprecated / vnet / vxlan-gbp / vxlan_gbp_api.c
1 /*
2  *------------------------------------------------------------------
3  * vxlan_gbp_api.c - vxlan gbp api
4  *
5  * Copyright (c) 2018 Cisco 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/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/feature/feature.h>
26 #include <vnet/vxlan-gbp/vxlan_gbp.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ip/ip_types_api.h>
29 #include <vnet/format_fns.h>
30
31 #include <vxlan-gbp/vxlan_gbp.api_enum.h>
32 #include <vxlan-gbp/vxlan_gbp.api_types.h>
33
34 #define REPLY_MSG_ID_BASE msg_id_base
35 #include <vlibapi/api_helper_macros.h>
36
37 static u16 msg_id_base;
38
39 static void
40   vl_api_sw_interface_set_vxlan_gbp_bypass_t_handler
41   (vl_api_sw_interface_set_vxlan_gbp_bypass_t * mp)
42 {
43   vl_api_sw_interface_set_vxlan_gbp_bypass_reply_t *rmp;
44   int rv = 0;
45   u32 sw_if_index = ntohl (mp->sw_if_index);
46
47   VALIDATE_SW_IF_INDEX (mp);
48
49   vnet_int_vxlan_gbp_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
50   BAD_SW_IF_INDEX_LABEL;
51
52   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_GBP_BYPASS_REPLY);
53 }
54
55 static int
56 vxlan_gbp_tunnel_mode_decode (vl_api_vxlan_gbp_api_tunnel_mode_t in,
57                               vxlan_gbp_tunnel_mode_t * out)
58 {
59   in = clib_net_to_host_u32 (in);
60
61   switch (in)
62     {
63     case VXLAN_GBP_API_TUNNEL_MODE_L2:
64       *out = VXLAN_GBP_TUNNEL_MODE_L2;
65       return (0);
66     case VXLAN_GBP_API_TUNNEL_MODE_L3:
67       *out = VXLAN_GBP_TUNNEL_MODE_L3;
68       return (0);
69     }
70   return (VNET_API_ERROR_INVALID_VALUE);
71 }
72
73 static void vl_api_vxlan_gbp_tunnel_add_del_t_handler
74   (vl_api_vxlan_gbp_tunnel_add_del_t * mp)
75 {
76   vl_api_vxlan_gbp_tunnel_add_del_reply_t *rmp;
77   vxlan_gbp_tunnel_mode_t mode;
78   ip46_address_t src, dst;
79   ip46_type_t itype;
80   int rv = 0;
81   u32 sw_if_index = ~0;
82   u32 fib_index;
83
84   itype = ip_address_decode (&mp->tunnel.src, &src);
85   itype = ip_address_decode (&mp->tunnel.dst, &dst);
86
87   fib_index = fib_table_find (fib_proto_from_ip46 (itype),
88                               ntohl (mp->tunnel.encap_table_id));
89   if (fib_index == ~0)
90     {
91       rv = VNET_API_ERROR_NO_SUCH_FIB;
92       goto out;
93     }
94
95   rv = vxlan_gbp_tunnel_mode_decode (mp->tunnel.mode, &mode);
96
97   if (rv)
98     goto out;
99
100   vnet_vxlan_gbp_tunnel_add_del_args_t a = {
101     .is_add = mp->is_add,
102     .is_ip6 = (itype == IP46_TYPE_IP6),
103     .instance = ntohl (mp->tunnel.instance),
104     .mcast_sw_if_index = ntohl (mp->tunnel.mcast_sw_if_index),
105     .encap_fib_index = fib_index,
106     .vni = ntohl (mp->tunnel.vni),
107     .dst = dst,
108     .src = src,
109     .mode = mode,
110   };
111
112   /* Check src & dst are different */
113   if (ip46_address_cmp (&a.dst, &a.src) == 0)
114     {
115       rv = VNET_API_ERROR_SAME_SRC_DST;
116       goto out;
117     }
118   if (ip46_address_is_multicast (&a.dst) &&
119       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
120     {
121       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
122       goto out;
123     }
124
125   rv = vnet_vxlan_gbp_tunnel_add_del (&a, &sw_if_index);
126
127 out:
128   /* *INDENT-OFF* */
129   REPLY_MACRO2(VL_API_VXLAN_GBP_TUNNEL_ADD_DEL_REPLY,
130   ({
131     rmp->sw_if_index = ntohl (sw_if_index);
132   }));
133   /* *INDENT-ON* */
134 }
135
136 static void send_vxlan_gbp_tunnel_details
137   (vxlan_gbp_tunnel_t * t, vl_api_registration_t * reg, u32 context)
138 {
139   vl_api_vxlan_gbp_tunnel_details_t *rmp;
140   ip46_type_t itype = (ip46_address_is_ip4 (&t->dst) ?
141                        IP46_TYPE_IP4 : IP46_TYPE_IP6);
142
143   rmp = vl_msg_api_alloc (sizeof (*rmp));
144   clib_memset (rmp, 0, sizeof (*rmp));
145   rmp->_vl_msg_id =
146     ntohs (VL_API_VXLAN_GBP_TUNNEL_DETAILS + REPLY_MSG_ID_BASE);
147
148   ip_address_encode (&t->src, itype, &rmp->tunnel.src);
149   ip_address_encode (&t->dst, itype, &rmp->tunnel.dst);
150   rmp->tunnel.encap_table_id =
151     fib_table_get_table_id (t->encap_fib_index, fib_proto_from_ip46 (itype));
152
153   rmp->tunnel.instance = htonl (t->user_instance);
154   rmp->tunnel.mcast_sw_if_index = htonl (t->mcast_sw_if_index);
155   rmp->tunnel.vni = htonl (t->vni);
156   rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
157   rmp->context = context;
158
159   vl_api_send_msg (reg, (u8 *) rmp);
160 }
161
162 static void vl_api_vxlan_gbp_tunnel_dump_t_handler
163   (vl_api_vxlan_gbp_tunnel_dump_t * mp)
164 {
165   vl_api_registration_t *reg;
166   vxlan_gbp_main_t *vxm = &vxlan_gbp_main;
167   vxlan_gbp_tunnel_t *t;
168   u32 sw_if_index;
169
170   reg = vl_api_client_index_to_registration (mp->client_index);
171   if (!reg)
172     return;
173
174   sw_if_index = ntohl (mp->sw_if_index);
175
176   if (~0 == sw_if_index)
177     {
178       /* *INDENT-OFF* */
179       pool_foreach (t, vxm->tunnels)
180        {
181         send_vxlan_gbp_tunnel_details(t, reg, mp->context);
182       }
183       /* *INDENT-ON* */
184     }
185   else
186     {
187       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
188           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
189         {
190           return;
191         }
192       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
193       send_vxlan_gbp_tunnel_details (t, reg, mp->context);
194     }
195 }
196
197 #include <vxlan-gbp/vxlan_gbp.api.c>
198 static clib_error_t *
199 vxlan_gbp_api_hookup (vlib_main_t * vm)
200 {
201   /*
202    * Set up the (msg_name, crc, message-id) table
203    */
204   msg_id_base = setup_message_id_table ();
205
206   return 0;
207 }
208
209 VLIB_API_INIT_FUNCTION (vxlan_gbp_api_hookup);
210
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "gnu")
216  * End:
217  */