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