654a074471d83d1766bdda779a273d9804f66497
[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   fproto = fib_proto_from_api_address_family (mp->af);
70
71   if (mp->is_add)
72     {
73       rv = svs_table_add (fproto, ntohl (mp->table_id));
74     }
75   else
76     {
77       rv = svs_table_delete (fproto, ntohl (mp->table_id));
78     }
79
80   REPLY_MACRO (VL_API_SVS_TABLE_ADD_DEL_REPLY + svs_base_msg_id);
81 }
82
83 static void
84 vl_api_svs_route_add_del_t_handler (vl_api_svs_route_add_del_t * mp)
85 {
86   vl_api_svs_route_add_del_reply_t *rmp;
87   fib_prefix_t pfx;
88   int rv = 0;
89
90   ip_prefix_decode (&mp->prefix, &pfx);
91
92   if (mp->is_add)
93     {
94       rv = svs_route_add (ntohl (mp->table_id), &pfx,
95                           ntohl (mp->source_table_id));
96     }
97   else
98     {
99       rv = svs_route_delete (ntohl (mp->table_id), &pfx);
100     }
101
102   REPLY_MACRO (VL_API_SVS_ROUTE_ADD_DEL_REPLY + svs_base_msg_id);
103 }
104
105 static void
106 vl_api_svs_enable_disable_t_handler (vl_api_svs_enable_disable_t * mp)
107 {
108   vl_api_svs_enable_disable_reply_t *rmp;
109   fib_protocol_t fproto;
110   int rv = 0;
111
112   VALIDATE_SW_IF_INDEX (mp);
113
114   fproto = fib_proto_from_api_address_family (mp->af);
115
116   if (mp->is_enable)
117     {
118       rv = svs_enable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
119     }
120   else
121     {
122       rv =
123         svs_disable (fproto, ntohl (mp->table_id), ntohl (mp->sw_if_index));
124     }
125
126   BAD_SW_IF_INDEX_LABEL;
127   REPLY_MACRO (VL_API_SVS_ENABLE_DISABLE_REPLY + svs_base_msg_id);
128 }
129
130 typedef struct svs_dump_walk_ctx_t_
131 {
132   vl_api_registration_t *rp;
133   u32 context;
134 } svs_dump_walk_ctx_t;
135
136
137 static walk_rc_t
138 svs_send_details (fib_protocol_t fproto,
139                   u32 table_id, u32 sw_if_index, void *args)
140 {
141   vl_api_svs_details_t *mp;
142   svs_dump_walk_ctx_t *ctx;
143
144   ctx = args;
145
146   mp = vl_msg_api_alloc (sizeof (*mp));
147   mp->_vl_msg_id = ntohs (VL_API_SVS_DETAILS + svs_base_msg_id);
148
149   mp->context = ctx->context;
150   mp->sw_if_index = htonl (sw_if_index);
151   mp->table_id = htonl (table_id);
152   mp->af = fib_proto_to_api_address_family (fproto);
153
154   vl_api_send_msg (ctx->rp, (u8 *) mp);
155
156   return (WALK_CONTINUE);
157 }
158
159 static void
160 vl_api_svs_dump_t_handler (vl_api_svs_dump_t * mp)
161 {
162   vl_api_registration_t *rp;
163
164   rp = vl_api_client_index_to_registration (mp->client_index);
165   if (rp == 0)
166     return;
167
168   svs_dump_walk_ctx_t ctx = {
169     .rp = rp,
170     .context = mp->context,
171   };
172
173   svs_walk (svs_send_details, &ctx);
174 }
175
176 #include <svs/svs.api.c>
177 static clib_error_t *
178 svs_api_init (vlib_main_t * vm)
179 {
180   /* Ask for a correctly-sized block of API message decode slots */
181   svs_base_msg_id = setup_message_id_table ();
182
183   return 0;
184 }
185
186 VLIB_INIT_FUNCTION (svs_api_init);
187
188 /* *INDENT-OFF* */
189 VLIB_PLUGIN_REGISTER () = {
190     .version = VPP_BUILD_VER,
191     .description = "Source Virtual Routing and Fowarding (VRF) Select",
192 };
193 /* *INDENT-ON* */
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */