acl-plugin: fix pretty-printing in "api trace custom-dump" (VPP-683)
[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
22 /* Macro to finish up custom dump fns */
23 #define PRINT_S \
24     vec_add1 (s, 0);                            \
25     vl_print (handle, (char *)s);               \
26     vec_free (s);
27
28 static inline void
29 vl_api_acl_details_t_endian (vl_api_acl_details_t * a)
30 {
31   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
32   a->context = clib_net_to_host_u32 (a->context);
33   a->acl_index = clib_net_to_host_u32 (a->acl_index);
34   /* a->tag[0..63] = a->tag[0..63] (no-op) */
35   a->count = clib_net_to_host_u32 (a->count);
36   vl_api_acl_rule_t_endian (a->r);
37 }
38
39 static inline void
40 vl_api_macip_acl_details_t_endian (vl_api_macip_acl_details_t * a)
41 {
42   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
43   a->context = clib_net_to_host_u32 (a->context);
44   a->acl_index = clib_net_to_host_u32 (a->acl_index);
45   /* a->tag[0..63] = a->tag[0..63] (no-op) */
46   a->count = clib_net_to_host_u32 (a->count);
47   vl_api_macip_acl_rule_t_endian (a->r);
48 }
49
50
51 static inline void
52 vl_api_acl_add_replace_t_endian (vl_api_acl_add_replace_t * a)
53 {
54   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
55   a->client_index = clib_net_to_host_u32 (a->client_index);
56   a->context = clib_net_to_host_u32 (a->context);
57   a->acl_index = clib_net_to_host_u32 (a->acl_index);
58   /* a->tag[0..63] = a->tag[0..63] (no-op) */
59   a->count = clib_net_to_host_u32 (a->count);
60   vl_api_acl_rule_t_endian (a->r);
61 }
62
63 static inline void
64 vl_api_macip_acl_add_t_endian (vl_api_macip_acl_add_t * a)
65 {
66   a->_vl_msg_id = clib_net_to_host_u16 (a->_vl_msg_id);
67   a->client_index = clib_net_to_host_u32 (a->client_index);
68   a->context = clib_net_to_host_u32 (a->context);
69   /* a->tag[0..63] = a->tag[0..63] (no-op) */
70   a->count = clib_net_to_host_u32 (a->count);
71   vl_api_macip_acl_rule_t_endian (a->r);
72 }
73
74 static inline u8 *
75 format_acl_action(u8 *s, u8 action)
76 {
77   switch(action) {
78     case 0:
79       s = format (s, "deny");
80       break;
81     case 1:
82       s = format (s, "permit");
83       break;
84     case 2:
85       s = format (s, "permit+reflect");
86       break;
87     default:
88       s = format (s, "action %d", action);
89   }
90   return(s);
91 }
92
93 static inline void *
94 vl_api_acl_rule_t_print (vl_api_acl_rule_t * a, void *handle)
95 {
96   u8 *s;
97
98   s = format (0, "  %s ", a->is_ipv6 ? "ipv6" : "ipv4");
99   s = format_acl_action (s, a->is_permit);
100   s = format (s, " \\\n");
101
102   if (a->is_ipv6)
103     s = format (s, "    src %U/%d dst %U/%d \\\n",
104                 format_ip6_address, a->src_ip_addr, a->src_ip_prefix_len,
105                 format_ip6_address, a->dst_ip_addr, a->dst_ip_prefix_len);
106   else
107     s = format (s, "    src %U/%d dst %U/%d \\\n",
108                 format_ip4_address, a->src_ip_addr, a->src_ip_prefix_len,
109                 format_ip4_address, a->dst_ip_addr, a->dst_ip_prefix_len);
110   s = format (s, "    proto %d \\\n", a->proto);
111   s = format (s, "    sport %d-%d dport %d-%d \\\n",
112               clib_net_to_host_u16 (a->srcport_or_icmptype_first),
113               clib_net_to_host_u16 (a->srcport_or_icmptype_last),
114               clib_net_to_host_u16 (a->dstport_or_icmpcode_first),
115               clib_net_to_host_u16 (a->dstport_or_icmpcode_last));
116
117   s = format (s, "    tcpflags %u mask %u, \\",
118               a->tcp_flags_value, a->tcp_flags_mask);
119   PRINT_S;
120   return handle;
121 }
122
123
124
125 static inline void *
126 vl_api_macip_acl_rule_t_print (vl_api_macip_acl_rule_t * a, void *handle)
127 {
128   u8 *s;
129
130   s = format (0, "  %s %s \\\n", a->is_ipv6 ? "ipv6" : "ipv4",
131               a->is_permit ? "permit" : "deny");
132
133   s = format (s, "    src mac %U mask %U \\\n",
134               format_ethernet_address, a->src_mac,
135               format_ethernet_address, a->src_mac_mask);
136
137   if (a->is_ipv6)
138     s = format (s, "    src ip %U/%d, \\",
139                 format_ip6_address, a->src_ip_addr, a->src_ip_prefix_len);
140   else
141     s = format (s, "    src ip %U/%d, \\",
142                 format_ip4_address, a->src_ip_addr, a->src_ip_prefix_len);
143
144   PRINT_S;
145   return handle;
146 }
147
148 static inline void *
149 vl_api_acl_add_replace_t_print (vl_api_acl_add_replace_t * a, void *handle)
150 {
151   u8 *s = 0;
152   int i;
153   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
154   u32 count = clib_net_to_host_u32 (a->count);
155   if (count > 0x100000)
156     {
157       s = format (s, "WARN: acl_add_replace count endianness wrong? Fixup to avoid long loop.\n");
158       count = a->count;
159     }
160
161   s = format (s, "SCRIPT: acl_add_replace %d count %d ",
162               acl_index, count);
163
164   if (a->tag[0])
165     s = format (s, "tag %s ", a->tag);
166
167   s = format(s, "\\\n");
168   PRINT_S;
169
170   for (i = 0; i < count; i++)
171     vl_api_acl_rule_t_print (&a->r[i], handle);
172
173   s = format(s, "\n");
174   PRINT_S;
175   return handle;
176 }
177
178 static inline void *
179 vl_api_acl_del_t_print (vl_api_macip_acl_del_t * a, void *handle)
180 {
181   u8 *s;
182
183   s = format (0, "SCRIPT: acl_del %d ",
184               clib_host_to_net_u32 (a->acl_index));
185
186   PRINT_S;
187   return handle;
188 }
189
190
191 static inline void *
192 vl_api_acl_details_t_print (vl_api_acl_details_t * a, void *handle)
193 {
194   u8 *s = 0;
195   int i;
196   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
197   u32 count = clib_net_to_host_u32 (a->count);
198   if (count > 0x100000)
199     {
200       s = format (s, "WARN: acl_defails count endianness wrong? Fixup to avoid long loop.\n");
201       count = a->count;
202     }
203
204   s = format (s, "acl_details index %d count %d ",
205               acl_index, count);
206
207   if (a->tag[0])
208     s = format (s, "tag %s ", a->tag);
209
210   s = format(s, "\n");
211   PRINT_S;
212
213   for (i = 0; i < count; i++)
214     vl_api_acl_rule_t_print (&a->r[i], handle);
215
216   return handle;
217 }
218
219 static inline void *
220 vl_api_macip_acl_details_t_print (vl_api_macip_acl_details_t * a,
221                                   void *handle)
222 {
223   u8 *s = 0;
224   int i;
225   u32 acl_index = clib_net_to_host_u32 (a->acl_index);
226   u32 count = clib_net_to_host_u32 (a->count);
227   if (count > 0x100000)
228     {
229       s = format (s, "WARN: macip_acl_defails count endianness wrong? Fixup to avoid long loop.\n");
230       count = a->count;
231     }
232
233   s = format (s, "macip_acl_details index %d count %d ",
234               acl_index, count);
235
236   if (a->tag[0])
237     s = format (s, "tag %s ", a->tag);
238
239   s = format(s, "\n");
240   PRINT_S;
241
242   for (i = 0; i < count; i++)
243     vl_api_macip_acl_rule_t_print (&a->r[i], handle);
244
245   return handle;
246 }
247
248 static inline void *
249 vl_api_macip_acl_add_t_print (vl_api_macip_acl_add_t * a, void *handle)
250 {
251   u8 *s = 0;
252   int i;
253   u32 count = clib_net_to_host_u32 (a->count);
254   if (count > 0x100000)
255     {
256       s = format (s, "WARN: macip_acl_add count endianness wrong? Fixup to avoid long loop.\n");
257       count = a->count;
258     }
259
260   s = format (0, "SCRIPT: macip_acl_add ");
261   if (a->tag[0])
262     s = format (s, "tag %s ", a->tag);
263
264   s = format (s, "count %d \\\n", count);
265
266   PRINT_S;
267
268   for (i = 0; i < count; i++)
269     vl_api_macip_acl_rule_t_print (&a->r[i], handle);
270
271   s = format (0, "\n");
272   PRINT_S;
273
274   return handle;
275 }
276
277
278 static inline void *
279 vl_api_acl_interface_set_acl_list_t_print (vl_api_acl_interface_set_acl_list_t
280                                            * a, void *handle)
281 {
282   u8 *s;
283   int i;
284
285   s = format
286     (0, "SCRIPT: acl_interface_set_acl_list sw_if_index %d count %d\n",
287      clib_net_to_host_u32 (a->sw_if_index), (u32) a->count);
288
289   s = format (s, "    input ");
290
291   for (i = 0; i < a->count; i++)
292     {
293       if (i == a->n_input)
294         s = format (s, "output ");
295       s = format (s, "%d ", clib_net_to_host_u32 (a->acls[i]));
296     }
297
298   PRINT_S;
299   return handle;
300 }
301
302 static inline void *
303 vl_api_acl_interface_add_del_t_print (vl_api_acl_interface_add_del_t * a,
304                                       void *handle)
305 {
306   u8 *s;
307
308   s = format (0, "SCRIPT: acl_interface_add_del sw_if_index %d acl %d ",
309               clib_net_to_host_u32 (a->sw_if_index),
310               clib_net_to_host_u32 (a->acl_index));
311   s = format (s, "%s %s",
312               a->is_input ? "input" : "output", a->is_add ? "add" : "del");
313
314   PRINT_S;
315   return handle;
316 }
317
318 static inline void *vl_api_macip_acl_interface_add_del_t_print
319   (vl_api_macip_acl_interface_add_del_t * a, void *handle)
320 {
321   u8 *s;
322
323   s = format
324     (0,
325      "SCRIPT: macip_acl_interface_add_del sw_if_index %d acl_index %d ",
326      clib_net_to_host_u32 (a->sw_if_index),
327      clib_net_to_host_u32 (a->acl_index));
328   s = format (s, "%s", a->is_add ? "add" : "del");
329
330   PRINT_S;
331   return handle;
332 }
333
334
335 static inline void *
336 vl_api_macip_acl_del_t_print (vl_api_macip_acl_del_t * a, void *handle)
337 {
338   u8 *s;
339
340   s = format (0, "SCRIPT: macip_acl_del %d ",
341               clib_host_to_net_u32 (a->acl_index));
342
343   PRINT_S;
344   return handle;
345 }
346
347
348 #endif /* included_manual_fns_h */