ip: change ip API enums address_family and ip_proto size to u8
[vpp.git] / src / plugins / svs / svs_api.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stddef.h>
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <svs/svs.h>
21 #include <vnet/fib/fib_api.h>
22 #include <vnet/ip/ip_types_api.h>
23
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 /* define message IDs */
30 #include <vnet/format_fns.h>
31 #include <svs/svs.api_enum.h>
32 #include <svs/svs.api_types.h>
33
34 /**
35  * Base message ID fot the plugin
36  */
37 static u32 svs_base_msg_id;
38 #include <vlibapi/api_helper_macros.h>
39
40 static void
41 vl_api_svs_plugin_get_version_t_handler (vl_api_svs_plugin_get_version_t * mp)
42 {
43   vl_api_svs_plugin_get_version_reply_t *rmp;
44   int msg_size = sizeof (*rmp);
45   vl_api_registration_t *rp;
46
47   rp = vl_api_client_index_to_registration (mp->client_index);
48   if (rp == 0)
49     return;
50
51   rmp = vl_msg_api_alloc (msg_size);
52   clib_memset (rmp, 0, msg_size);
53   rmp->_vl_msg_id =
54     ntohs (VL_API_SVS_PLUGIN_GET_VERSION_REPLY + svs_base_msg_id);
55   rmp->context = mp->context;
56   rmp->major = htonl (SVS_PLUGIN_VERSION_MAJOR);
57   rmp->minor = htonl (SVS_PLUGIN_VERSION_MINOR);
58
59   vl_api_send_msg (rp, (u8 *) rmp);
60 }
61
62 static void
63 vl_api_svs_table_add_del_t_handler (vl_api_svs_table_add_del_t * mp)
64 {
65   vl_api_svs_table_add_del_reply_t *rmp;
66   fib_protocol_t fproto;
67   int rv = 0;
68
69   rv = fib_proto_from_api_address_family (mp->af, &fproto);
70   if (rv < 0)
71     goto error;
72
73   if (mp->is_add)
74     {
75       rv = svs_table_add (fproto, ntohl (mp->table_id));
76     }
77   else
78     {
79       rv = svs_table_delete (fproto, ntohl (mp->table_id));
80     }
81
82 error:
83   REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
84 }
85
86 static void
87 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
88 {
89   vl_api_svs_route_add_del_reply_t *rmp;
90   fib_prefix_t pfx;
91   int rv = 0;
92
93   ip_prefix_decode (&mp->prefix, &pfx);
94
95   if (mp->is_add)
96     {
97       rv = svs_route_add (ntohl (mp->table_id), &pfx,
98                           ntohl (mp->source_table_id));
99     }
100   else
101     {
102       rv = svs_route_delete (ntohl (mp->table_id), &pfx);
103     }
104
105   REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
106 }
107
108 static void
109 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
110 {
111   vl_api_svs_enable_disable_reply_t *rmp;
112   fib_protocol_t fproto;
113   int rv = 0;
114
115   VALIDATE_SW_IF_INDEX (mp);
116
117   rv = fib_proto_from_api_address_family (mp->af, &fproto);
118   if (rv < 0)
119     goto error;
120
121   if (mp->is_enable)
122     {
123       rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
124     }
125   else
126     {
127       rv =
128         svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
129     }
130
131   BAD_SW_IF_INDEX_LABEL;
132 error:
133   REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
134 }
135
136 typedef struct svs_dump_walk_ctx_t_
137 {
138   vl_api_registration_t *rp;
139   u32 context;
140 } svs_dump_walk_ctx_t;
141
142
143 static walk_rc_t
144 svs_send_details (fib_protocol_t fproto,
145                   u32 table_id, u32 sw_if_index, void *args)
146 {
147   vl_api_svs_details_t *mp;
148   svs_dump_walk_ctx_t *ctx;
149
150   ctx = args;
151
152   mp = vl_msg_api_alloc (sizeof (*mp));
153   mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
154
155   mp->context = ctx->context;
156   mp->sw_if_index = htonl (sw_if_index);
157   mp->table_id = htonl (table_id);
158   mp->af = fib_proto_to_api_address_family (fproto);
159
160   vl_api_send_msg (ctx->rp, (u8 *) mp);
161
162   return (WALK_CONTINUE);
163 }
164
165 static void
166 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
167 {
168   vl_api_registration_t *rp;
169
170   rp = vl_api_client_index_to_registration (mp->client_index);
171   if (rp == 0)
172     return;
173
174   svs_dump_walk_ctx_t ctx = {
175     .rp = rp,
176     .context = mp->context,
177   };
178
179   svs_walk (svs_send_details, &ctx);
180 }
181
182 #include <svs/svs.api.c>
183 static clib_error_t *
184 svs_api_init (vlib_main_t * vm)
185 {
186   /* Ask for a correctly-sized block of API message decode slots */
187   svs_base_msg_id = setup_message_id_table ();
188
189   return 0;
190 }
191
192 VLIB_INIT_FUNCTION (svs_api_init);
193
194 /* *INDENT-OFF* */
195 VLIB_PLUGIN_REGISTER () = {
196     .version = VPP_BUILD_VER,
197     .description = "Source Virtual Routing and Fowarding (VRF) Select",
198 };
199 /* *INDENT-ON* */
200
201 /*
202  * fd.io coding-style-patch-verification: ON
203  *
204  * Local Variables:
205  * eval: (c-set-style "gnu")
206  * End:
207  */