acl: API cleanup
[vpp.git] / src / plugins / acl / manual_fns.h
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 #ifndef included_manual_fns_h
17 #define included_manual_fns_h
18
19 #include <vnet/ip/format.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/ip_types_api.h>
22
23 #define vl_endianfun            /* define message structures */
24 #include <acl/acl_types.api.h>
25 #undef vl_endianfun
26
27 /* Macro to finish up custom dump fns */
28 #define PRINT_S \
29     vec_add1 (s, 0);                            \
30     vl_print (handle, (char *)s);               \
31     vec_free (s);
32
33 static inline void
34 vl_api_acl_rule_t_array_endian(vl_api_acl_rule_t *rules, u32 count)
35 {
36   u32 i;
37   for(i=0; i<count; i++) {
38     vl_api_acl_rule_t_endian (&rules[i]);
39   }
40 }
41
42 static inline void
43 vl_api_macip_acl_rule_t_array_endian(vl_api_macip_acl_rule_t *rules, u32 count)
44 {
45   u32 i;
46   for(i=0; i<count; i++) {
47     vl_api_macip_acl_rule_t_endian (&rules[i]);
48   }
49 }
50
51 static inline void
52 vl_api_acl_details_t_endian (vl_api_acl_details_t * a)
53 {
54   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
55   a->context = clib_net_to_host_u32 (a->context);
56   a->acl_index = clib_net_to_host_u32 (a->acl_index);
57   /* a->tag[0..63] = a->tag[0..63] (no-op) */
58   a->count = clib_net_to_host_u32 (a->count);
59   vl_api_acl_rule_t_array_endian (a->r, a->count);
60 }
61
62 static inline void
63 vl_api_macip_acl_details_t_endian (vl_api_macip_acl_details_t * a)
64 {
65   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
66   a->context = clib_net_to_host_u32 (a->context);
67   a->acl_index = clib_net_to_host_u32 (a->acl_index);
68   /* a->tag[0..63] = a->tag[0..63] (no-op) */
69   a->count = clib_net_to_host_u32 (a->count);
70   vl_api_macip_acl_rule_t_array_endian (a->r, a->count);
71 }
72
73
74 static inline void
75 vl_api_acl_add_replace_t_endian (vl_api_acl_add_replace_t * a)
76 {
77   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
78   a->client_index = clib_net_to_host_u32 (a->client_index);
79   a->context = clib_net_to_host_u32 (a->context);
80   a->acl_index = clib_net_to_host_u32 (a->acl_index);
81   /* a->tag[0..63] = a->tag[0..63] (no-op) */
82   a->count = clib_net_to_host_u32 (a->count);
83   vl_api_acl_rule_t_array_endian (a->r, a->count);
84 }
85
86 static inline void
87 vl_api_macip_acl_add_t_endian (vl_api_macip_acl_add_t * a)
88 {
89   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
90   a->client_index = clib_net_to_host_u32 (a->client_index);
91   a->context = clib_net_to_host_u32 (a->context);
92   /* a->tag[0..63] = a->tag[0..63] (no-op) */
93   a->count = clib_net_to_host_u32 (a->count);
94   vl_api_macip_acl_rule_t_array_endian (a->r, a->count);
95 }
96
97 static inline void
98 vl_api_macip_acl_add_replace_t_endian (vl_api_macip_acl_add_replace_t * a)
99 {
100   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
101   a->client_index = clib_net_to_host_u32 (a->client_index);
102   a->context = clib_net_to_host_u32 (a->context);
103   a->acl_index = clib_net_to_host_u32 (a->acl_index);
104   /* a->tag[0..63] = a->tag[0..63] (no-op) */
105   a->count = clib_net_to_host_u32 (a->count);
106   vl_api_macip_acl_rule_t_array_endian (a->r, a->count);
107 }
108
109 static inline u8 *
110 format_acl_action(u8 *s, u8 action)
111 {
112   switch(action) {
113     case 0:
114       s = format (s, "deny");
115       break;
116     case 1:
117       s = format (s, "permit");
118       break;
119     case 2:
120       s = format (s, "permit+reflect");
121       break;
122     default:
123       s = format (s, "action %d", action);
124   }
125   return(s);
126 }
127
128 static inline void *
129 vl_api_acl_rule_t_print (vl_api_acl_rule_t * a, void *handle)
130 {
131   u8 *s;
132   fib_prefix_t src, dst;
133
134   ip_prefix_decode (&a->src_prefix, &src);
135   ip_prefix_decode (&a->dst_prefix, &dst);
136
137   s = format (0, "  %s ", a->src_prefix.address.af ? "ipv6" : "ipv4");
138   s = format_acl_action (s, a->is_permit);
139   s = format (s, " \\\n");
140
141   s = format (s, "    src %U dst %U \\\n",
142               format_fib_prefix, &src,
143               format_fib_prefix, &dst);
144   s = format (s, "    proto %d \\\n", a->proto);
145   s = format (s, "    sport %d-%d dport %d-%d \\\n",
146               clib_net_to_host_u16 (a->srcport_or_icmptype_first),
147               clib_net_to_host_u16 (a->srcport_or_icmptype_last),
148               clib_net_to_host_u16 (a->dstport_or_icmpcode_first),
149               clib_net_to_host_u16 (a->dstport_or_icmpcode_last));
150
151   s = format (s, "    tcpflags %u mask %u, \\",
152               a->tcp_flags_value, a->tcp_flags_mask);
153   PRINT_S;
154   return handle;
155 }
156
157 static inline void *
158 vl_api_macip_acl_rule_t_print (vl_api_macip_acl_rule_t * a, void *handle)
159 {
160   u8 *s;
161   fib_prefix_t src;
162
163   ip_prefix_decode (&a->src_prefix, &src);
164
165   s = format (0, "  %s %s \\\n", a->src_prefix.address.af ? "ipv6" : "ipv4",
166               a->is_permit ? "permit" : "deny");
167
168   s = format (s, "    src mac %U mask %U \\\n",
169               format_ethernet_address, a->src_mac,
170               format_ethernet_address, a->src_mac_mask);
171
172   s = format (s, "    src ip %U, \\",
173                 format_fib_prefix, &src);
174
175   PRINT_S;
176   return handle;
177 }
178
179 static inline void *
180 vl_api_acl_add_replace_t_print (vl_api_acl_add_replace_t * a, void *handle)
181 {
182   u8 *s = 0;
183   int i;
184   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
185   u32 count = clib_net_to_host_u32 (a->count);
186   if (count > 0x100000)
187     {
188       s = format (s, "WARN: acl_add_replace count endianness wrong? Fixup to avoid long loop.\n");
189       count = a->count;
190     }
191
192   s = format (s, "SCRIPT: acl_add_replace %d count %d ",
193               acl_index, count);
194
195   if (a->tag[0])
196     s = format (s, "tag %s ", a->tag);
197
198   s = format(s, "\\\n");
199   PRINT_S;
200
201   for (i = 0; i < count; i++)
202     vl_api_acl_rule_t_print (&a->r[i], handle);
203
204   s = format(s, "\n");
205   PRINT_S;
206   return handle;
207 }
208
209 static inline void *
210 vl_api_acl_del_t_print (vl_api_macip_acl_del_t * a, void *handle)
211 {
212   u8 *s;
213
214   s = format (0, "SCRIPT: acl_del %d ",
215               clib_host_to_net_u32 (a->acl_index));
216
217   PRINT_S;
218   return handle;
219 }
220
221
222 static inline void *
223 vl_api_acl_details_t_print (vl_api_acl_details_t * a, void *handle)
224 {
225   u8 *s = 0;
226   int i;
227   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
228   u32 count = clib_net_to_host_u32 (a->count);
229   if (count > 0x100000)
230     {
231       s = format (s, "WARN: acl_details count endianness wrong? Fixup to avoid long loop.\n");
232       count = a->count;
233     }
234
235   s = format (s, "acl_details index %d count %d ",
236               acl_index, count);
237
238   if (a->tag[0])
239     s = format (s, "tag %s ", a->tag);
240
241   s = format(s, "\n");
242   PRINT_S;
243
244   for (i = 0; i < count; i++)
245     vl_api_acl_rule_t_print (&a->r[i], handle);
246
247   return handle;
248 }
249
250 static inline void *
251 vl_api_macip_acl_details_t_print (vl_api_macip_acl_details_t * a,
252                                   void *handle)
253 {
254   u8 *s = 0;
255   int i;
256   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
257   u32 count = clib_net_to_host_u32 (a->count);
258   if (count > 0x100000)
259     {
260       s = format (s, "WARN: macip_acl_details count endianness wrong? Fixup to avoid long loop.\n");
261       count = a->count;
262     }
263
264   s = format (s, "macip_acl_details index %d count %d ",
265               acl_index, count);
266
267   if (a->tag[0])
268     s = format (s, "tag %s ", a->tag);
269
270   s = format(s, "\n");
271   PRINT_S;
272
273   for (i = 0; i < count; i++)
274     vl_api_macip_acl_rule_t_print (&a->r[i], handle);
275
276   return handle;
277 }
278
279 static inline void *
280 vl_api_macip_acl_add_t_print (vl_api_macip_acl_add_t * a, void *handle)
281 {
282   u8 *s = 0;
283   int i;
284   u32 count = clib_net_to_host_u32 (a->count);
285   if (count > 0x100000)
286     {
287       s = format (s, "WARN: macip_acl_add count endianness wrong? Fixup to avoid long loop.\n");
288       count = a->count;
289     }
290
291   s = format (s, "SCRIPT: macip_acl_add ");
292   if (a->tag[0])
293     s = format (s, "tag %s ", a->tag);
294
295   s = format (s, "count %d \\\n", count);
296
297   PRINT_S;
298
299   for (i = 0; i < count; i++)
300     vl_api_macip_acl_rule_t_print (&a->r[i], handle);
301
302   s = format (0, "\n");
303   PRINT_S;
304
305   return handle;
306 }
307
308 static inline void *
309 vl_api_macip_acl_add_replace_t_print (vl_api_macip_acl_add_replace_t * a, void *handle)
310 {
311   u8 *s = 0;
312   int i;
313   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
314   u32 count = clib_net_to_host_u32 (a->count);
315   if (count > 0x100000)
316     {
317       s = format (s, "WARN: macip_acl_add_replace count endianness wrong? Fixup to avoid long loop.\n");
318       count = a->count;
319     }
320
321   s = format (s, "SCRIPT: macip_acl_add_replace %d count %d ",
322         acl_index, count);
323   if (a->tag[0])
324     s = format (s, "tag %s ", a->tag);
325
326   s = format (s, "count %d \\\n", count);
327
328   PRINT_S;
329
330   for (i = 0; i < count; i++)
331     vl_api_macip_acl_rule_t_print (&a->r[i], handle);
332
333   s = format (0, "\n");
334   PRINT_S;
335
336   return handle;
337 }
338
339 static inline void *
340 vl_api_acl_interface_set_acl_list_t_print (vl_api_acl_interface_set_acl_list_t
341                                            * a, void *handle)
342 {
343   u8 *s;
344   int i;
345
346   s = format
347     (0, "SCRIPT: acl_interface_set_acl_list sw_if_index %d count %d\n",
348      clib_net_to_host_u32 (a->sw_if_index), (u32) a->count);
349
350   s = format (s, "    input ");
351
352   for (i = 0; i < a->count; i++)
353     {
354       if (i == a->n_input)
355         s = format (s, "output ");
356       s = format (s, "%d ", clib_net_to_host_u32 (a->acls[i]));
357     }
358
359   PRINT_S;
360   return handle;
361 }
362
363 static inline void *
364 vl_api_acl_interface_set_etype_whitelist_t_print (vl_api_acl_interface_set_etype_whitelist_t
365                                            * a, void *handle)
366 {
367   u8 *s;
368   int i;
369
370   s = format
371     (0, "SCRIPT: acl_interface_set_etype_whitelist sw_if_index %d count %d\n",
372      clib_net_to_host_u32 (a->sw_if_index), (u32) a->count);
373
374   s = format (s, "    input ");
375
376   for (i = 0; i < a->count; i++)
377     {
378       if (i == a->n_input)
379         s = format (s, "output ");
380       s = format (s, "%x ", clib_net_to_host_u16 (a->whitelist[i]));
381     }
382
383   PRINT_S;
384   return handle;
385 }
386
387 static inline void *
388 vl_api_acl_interface_add_del_t_print (vl_api_acl_interface_add_del_t * a,
389                                       void *handle)
390 {
391   u8 *s;
392
393   s = format (0, "SCRIPT: acl_interface_add_del sw_if_index %d acl %d ",
394               clib_net_to_host_u32 (a->sw_if_index),
395               clib_net_to_host_u32 (a->acl_index));
396   s = format (s, "%s %s",
397               a->is_input ? "input" : "output", a->is_add ? "add" : "del");
398
399   PRINT_S;
400   return handle;
401 }
402
403 static inline void *vl_api_macip_acl_interface_add_del_t_print
404   (vl_api_macip_acl_interface_add_del_t * a, void *handle)
405 {
406   u8 *s;
407
408   s = format
409     (0,
410      "SCRIPT: macip_acl_interface_add_del sw_if_index %d acl_index %d ",
411      clib_net_to_host_u32 (a->sw_if_index),
412      clib_net_to_host_u32 (a->acl_index));
413   s = format (s, "%s", a->is_add ? "add" : "del");
414
415   PRINT_S;
416   return handle;
417 }
418
419
420 static inline void *
421 vl_api_macip_acl_del_t_print (vl_api_macip_acl_del_t * a, void *handle)
422 {
423   u8 *s;
424
425   s = format (0, "SCRIPT: macip_acl_del %d ",
426               clib_host_to_net_u32 (a->acl_index));
427
428   PRINT_S;
429   return handle;
430 }
431
432
433 #endif /* included_manual_fns_h */