Line data Source code
1 : /*
2 : * Copyright (c) 2016 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 <abf/abf_policy.h>
21 : #include <abf/abf_itf_attach.h>
22 : #include <vnet/mpls/mpls_types.h>
23 : #include <vnet/fib/fib_path_list.h>
24 : #include <vnet/fib/fib_api.h>
25 :
26 : #include <vpp/app/version.h>
27 :
28 : #include <vlibapi/api.h>
29 : #include <vlibmemory/api.h>
30 :
31 : /* define message IDs */
32 : #include <vnet/format_fns.h>
33 : #include <abf/abf.api_enum.h>
34 : #include <abf/abf.api_types.h>
35 :
36 : /**
37 : * Base message ID for the plugin
38 : */
39 : static u32 abf_base_msg_id;
40 :
41 : #define REPLY_MSG_ID_BASE (abf_base_msg_id)
42 : #include <vlibapi/api_helper_macros.h>
43 :
44 : static void
45 0 : vl_api_abf_plugin_get_version_t_handler (vl_api_abf_plugin_get_version_t * mp)
46 : {
47 : vl_api_abf_plugin_get_version_reply_t *rmp;
48 : vl_api_registration_t *rp;
49 :
50 0 : rp = vl_api_client_index_to_registration (mp->client_index);
51 0 : if (rp == 0)
52 0 : return;
53 :
54 0 : rmp = vl_msg_api_alloc (sizeof (*rmp));
55 0 : rmp->_vl_msg_id =
56 0 : ntohs (VL_API_ABF_PLUGIN_GET_VERSION_REPLY + abf_base_msg_id);
57 0 : rmp->context = mp->context;
58 0 : rmp->major = htonl (ABF_PLUGIN_VERSION_MAJOR);
59 0 : rmp->minor = htonl (ABF_PLUGIN_VERSION_MINOR);
60 :
61 0 : vl_api_send_msg (rp, (u8 *) rmp);
62 : }
63 :
64 : static void
65 14 : vl_api_abf_policy_add_del_t_handler (vl_api_abf_policy_add_del_t * mp)
66 : {
67 : vl_api_abf_policy_add_del_reply_t *rmp;
68 14 : fib_route_path_t *paths = NULL, *path;
69 14 : int rv = 0;
70 : u8 pi;
71 :
72 14 : if (mp->policy.n_paths == 0)
73 : {
74 0 : rv = VNET_API_ERROR_INVALID_VALUE;
75 0 : goto done;
76 : }
77 :
78 14 : vec_validate (paths, mp->policy.n_paths - 1);
79 :
80 28 : for (pi = 0; pi < mp->policy.n_paths; pi++)
81 : {
82 14 : path = &paths[pi];
83 14 : rv = fib_api_path_decode (&mp->policy.paths[pi], path);
84 :
85 14 : if (0 != rv)
86 : {
87 0 : goto done;
88 : }
89 : }
90 :
91 14 : if (mp->is_add)
92 : {
93 7 : rv = abf_policy_update (ntohl (mp->policy.policy_id),
94 : ntohl (mp->policy.acl_index), paths);
95 : }
96 : else
97 : {
98 7 : rv = abf_policy_delete (ntohl (mp->policy.policy_id), paths);
99 : }
100 14 : done:
101 14 : vec_free (paths);
102 :
103 14 : REPLY_MACRO (VL_API_ABF_POLICY_ADD_DEL_REPLY);
104 : }
105 :
106 : static void
107 16 : vl_api_abf_itf_attach_add_del_t_handler (vl_api_abf_itf_attach_add_del_t * mp)
108 : {
109 : vl_api_abf_itf_attach_add_del_reply_t *rmp;
110 16 : fib_protocol_t fproto = (mp->attach.is_ipv6 ?
111 16 : FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
112 16 : int rv = 0;
113 :
114 16 : if (mp->is_add)
115 : {
116 8 : rv = abf_itf_attach (fproto, ntohl (mp->attach.policy_id),
117 : ntohl (mp->attach.priority),
118 : ntohl (mp->attach.sw_if_index));
119 : }
120 : else
121 : {
122 8 : rv = abf_itf_detach (fproto, ntohl (mp->attach.policy_id),
123 : ntohl (mp->attach.sw_if_index));
124 : }
125 :
126 16 : REPLY_MACRO (VL_API_ABF_ITF_ATTACH_ADD_DEL_REPLY);
127 : }
128 :
129 : typedef struct abf_dump_walk_ctx_t_
130 : {
131 : vl_api_registration_t *rp;
132 : u32 context;
133 : } abf_dump_walk_ctx_t;
134 :
135 : static int
136 19 : abf_policy_send_details (u32 api, void *args)
137 : {
138 19 : fib_path_encode_ctx_t walk_ctx = {
139 : .rpaths = NULL,
140 : };
141 : vl_api_abf_policy_details_t *mp;
142 : abf_dump_walk_ctx_t *ctx;
143 : fib_route_path_t *rpath;
144 : vl_api_fib_path_t *fp;
145 : size_t msg_size;
146 : abf_policy_t *ap;
147 : u8 n_paths;
148 :
149 19 : ctx = args;
150 19 : ap = abf_policy_get (api);
151 19 : n_paths = fib_path_list_get_n_paths (ap->ap_pl);
152 19 : msg_size = sizeof (*mp) + sizeof (mp->policy.paths[0]) * n_paths;
153 :
154 19 : mp = vl_msg_api_alloc (msg_size);
155 19 : clib_memset (mp, 0, msg_size);
156 19 : mp->_vl_msg_id = ntohs (VL_API_ABF_POLICY_DETAILS + abf_base_msg_id);
157 :
158 : /* fill in the message */
159 19 : mp->context = ctx->context;
160 19 : mp->policy.n_paths = n_paths;
161 19 : mp->policy.acl_index = htonl (ap->ap_acl);
162 19 : mp->policy.policy_id = htonl (ap->ap_id);
163 :
164 19 : fib_path_list_walk_w_ext (ap->ap_pl, NULL, fib_path_encode, &walk_ctx);
165 :
166 19 : fp = mp->policy.paths;
167 38 : vec_foreach (rpath, walk_ctx.rpaths)
168 : {
169 19 : fib_api_path_encode (rpath, fp);
170 19 : fp++;
171 : }
172 :
173 19 : vl_api_send_msg (ctx->rp, (u8 *) mp);
174 :
175 19 : vec_free (walk_ctx.rpaths);
176 :
177 19 : return (1);
178 : }
179 :
180 : static void
181 14 : vl_api_abf_policy_dump_t_handler (vl_api_abf_policy_dump_t * mp)
182 : {
183 : vl_api_registration_t *rp;
184 :
185 14 : rp = vl_api_client_index_to_registration (mp->client_index);
186 14 : if (rp == 0)
187 0 : return;
188 :
189 14 : abf_dump_walk_ctx_t ctx = {
190 : .rp = rp,
191 14 : .context = mp->context,
192 : };
193 :
194 14 : abf_policy_walk (abf_policy_send_details, &ctx);
195 : }
196 :
197 : static int
198 15 : abf_itf_attach_send_details (u32 aiai, void *args)
199 : {
200 : vl_api_abf_itf_attach_details_t *mp;
201 : abf_dump_walk_ctx_t *ctx;
202 : abf_itf_attach_t *aia;
203 : abf_policy_t *ap;
204 :
205 15 : ctx = args;
206 15 : aia = abf_itf_attach_get (aiai);
207 15 : ap = abf_policy_get (aia->aia_abf);
208 :
209 15 : mp = vl_msg_api_alloc (sizeof (*mp));
210 15 : mp->_vl_msg_id = ntohs (VL_API_ABF_ITF_ATTACH_DETAILS + abf_base_msg_id);
211 :
212 15 : mp->context = ctx->context;
213 15 : mp->attach.policy_id = htonl (ap->ap_id);
214 15 : mp->attach.sw_if_index = htonl (aia->aia_sw_if_index);
215 15 : mp->attach.priority = htonl (aia->aia_prio);
216 15 : mp->attach.is_ipv6 = (aia->aia_proto == FIB_PROTOCOL_IP6);
217 :
218 15 : vl_api_send_msg (ctx->rp, (u8 *) mp);
219 :
220 15 : return (1);
221 : }
222 :
223 : static void
224 14 : vl_api_abf_itf_attach_dump_t_handler (vl_api_abf_itf_attach_dump_t * mp)
225 : {
226 : vl_api_registration_t *rp;
227 :
228 14 : rp = vl_api_client_index_to_registration (mp->client_index);
229 14 : if (rp == 0)
230 0 : return;
231 :
232 14 : abf_dump_walk_ctx_t ctx = {
233 : .rp = rp,
234 14 : .context = mp->context,
235 : };
236 :
237 14 : abf_itf_attach_walk (abf_itf_attach_send_details, &ctx);
238 : }
239 :
240 : #include <abf/abf.api.c>
241 :
242 : static clib_error_t *
243 559 : abf_api_init (vlib_main_t * vm)
244 : {
245 : /* Ask for a correctly-sized block of API message decode slots */
246 559 : abf_base_msg_id = setup_message_id_table ();
247 :
248 559 : return 0;
249 : }
250 :
251 1119 : VLIB_INIT_FUNCTION (abf_api_init);
252 :
253 : /* *INDENT-OFF* */
254 : VLIB_PLUGIN_REGISTER () = {
255 : .version = VPP_BUILD_VER,
256 : .description = "Access Control List (ACL) Based Forwarding",
257 : };
258 : /* *INDENT-ON* */
259 :
260 : /*
261 : * fd.io coding-style-patch-verification: ON
262 : *
263 : * Local Variables:
264 : * eval: (c-set-style "gnu")
265 : * End:
266 : */
|