virtio: Add RX queue full statisitics
[vpp.git] / src / plugins / acl / dataplane_node_nonip.c
1 /*
2  * Copyright (c) 2016-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 #include <stddef.h>
16 #include <netinet/in.h>
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
22
23
24 #include <acl/acl.h>
25 #include <vnet/ip/icmp46_packet.h>
26
27 #include <plugins/acl/fa_node.h>
28 #include <plugins/acl/acl.h>
29 #include <plugins/acl/lookup_context.h>
30 #include <plugins/acl/public_inlines.h>
31 #include <plugins/acl/session_inlines.h>
32
33 #include <vppinfra/bihash_40_8.h>
34 #include <vppinfra/bihash_template.h>
35
36 typedef struct
37 {
38   u32 next_index;
39   u32 sw_if_index;
40   u32 lc_index;
41   u32 match_acl_in_index;
42   u32 match_rule_index;
43   u64 packet_info[6];
44   u32 trace_bitmap;
45   u8 action;
46 } acl_fa_trace_t;
47
48 #define foreach_acl_fa_error \
49 _(ACL_DROP, "ACL deny packets")  \
50 _(ACL_PERMIT, "ACL permit packets")  \
51 _(ACL_NEW_SESSION, "new sessions added") \
52 _(ACL_EXIST_SESSION, "existing session packets") \
53 _(ACL_CHECK, "checked packets") \
54 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
55 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
56 /* end  of errors */
57
58 typedef enum
59 {
60 #define _(sym,str) ACL_FA_ERROR_##sym,
61   foreach_acl_fa_error
62 #undef _
63     ACL_FA_N_ERROR,
64 } acl_fa_error_t;
65
66
67 typedef struct
68 {
69   u32 next_index;
70   u32 sw_if_index;
71   u16 ethertype;
72 } nonip_in_out_trace_t;
73
74 /* packet trace format function */
75 static u8 *
76 format_nonip_in_out_trace (u8 * s, u32 is_output, va_list * args)
77 {
78   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
79   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
80   nonip_in_out_trace_t *t = va_arg (*args, nonip_in_out_trace_t *);
81
82   s = format (s, "%s: sw_if_index %d next_index %x ethertype %x",
83               is_output ? "OUT-ETHER-WHITELIST" : "IN-ETHER-WHITELIST",
84               t->sw_if_index, t->next_index, t->ethertype);
85   return s;
86 }
87
88 static u8 *
89 format_l2_nonip_in_trace (u8 * s, va_list * args)
90 {
91   return format_nonip_in_out_trace (s, 0, args);
92 }
93
94 static u8 *
95 format_l2_nonip_out_trace (u8 * s, va_list * args)
96 {
97   return format_nonip_in_out_trace (s, 1, args);
98 }
99
100 #define foreach_nonip_in_error                    \
101 _(DROP, "dropped inbound non-whitelisted non-ip packets") \
102 _(PERMIT, "permitted inbound whitelisted non-ip packets") \
103
104
105 #define foreach_nonip_out_error                    \
106 _(DROP, "dropped outbound non-whitelisted non-ip packets") \
107 _(PERMIT, "permitted outbound whitelisted non-ip packets") \
108
109
110
111 typedef enum
112 {
113 #define _(sym,str) FA_IN_NONIP_ERROR_##sym,
114   foreach_nonip_in_error
115 #undef _
116     FA_IN_NONIP_N_ERROR,
117 } l2_in_feat_arc_error_t;
118
119 static char *fa_in_nonip_error_strings[] = {
120 #define _(sym,string) string,
121   foreach_nonip_in_error
122 #undef _
123 };
124
125 typedef enum
126 {
127 #define _(sym,str) FA_OUT_NONIP_ERROR_##sym,
128   foreach_nonip_out_error
129 #undef _
130     FA_OUT_NONIP_N_ERROR,
131 } l2_out_feat_arc_error_t;
132
133 static char *fa_out_nonip_error_strings[] = {
134 #define _(sym,string) string,
135   foreach_nonip_out_error
136 #undef _
137 };
138
139
140 always_inline int
141 is_permitted_ethertype (acl_main_t * am, int sw_if_index0, int is_output,
142                         u16 ethertype)
143 {
144   u16 **v = is_output
145     ? am->output_etype_whitelist_by_sw_if_index
146     : am->input_etype_whitelist_by_sw_if_index;
147   u16 *whitelist = vec_elt (v, sw_if_index0);
148   int i;
149
150   if (vec_len (whitelist) == 0)
151     return 1;
152
153   for (i = 0; i < vec_len (whitelist); i++)
154     if (whitelist[i] == ethertype)
155       return 1;
156   return 0;
157 }
158
159 #define get_u16(addr) ( *((u16 *)(addr)) )
160
161 always_inline uword
162 nonip_in_out_node_fn (vlib_main_t * vm,
163                       vlib_node_runtime_t * node, vlib_frame_t * frame,
164                       int is_output)
165 {
166   acl_main_t *am = &acl_main;
167   u32 n_left, *from;
168   u16 nexts[VLIB_FRAME_SIZE], *next;
169   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
170   vlib_node_runtime_t *error_node;
171
172   from = vlib_frame_vector_args (frame);
173   error_node = vlib_node_get_runtime (vm, node->node_index);
174   vlib_get_buffers (vm, from, bufs, frame->n_vectors);
175   /* set the initial values for the current buffer the next pointers */
176   b = bufs;
177   next = nexts;
178
179   n_left = frame->n_vectors;
180   while (n_left > 0)
181     {
182       u32 next_index = 0;
183       u32 sw_if_index0 =
184         vnet_buffer (b[0])->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
185       u16 ethertype = 0;
186
187       int error0 = 0;
188
189       ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
190       u8 *l3h0 = (u8 *) h0 + vnet_buffer (b[0])->l2.l2_len;
191       ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
192
193       if (is_permitted_ethertype (am, sw_if_index0, is_output, ethertype))
194         vnet_feature_next (&next_index, b[0]);
195
196       next[0] = next_index;
197
198       if (0 == next[0])
199         b[0]->error = error_node->errors[error0];
200
201       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
202                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
203         {
204           nonip_in_out_trace_t *t =
205             vlib_add_trace (vm, node, b[0], sizeof (*t));
206           t->sw_if_index = sw_if_index0;
207           t->ethertype = ethertype;
208           t->next_index = next[0];
209         }
210       next[0] = next[0] < node->n_next_nodes ? next[0] : 0;
211
212       next++;
213       b++;
214       n_left--;
215     }
216   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
217
218   return frame->n_vectors;
219 }
220
221 VLIB_NODE_FN (acl_in_nonip_node) (vlib_main_t * vm,
222                                   vlib_node_runtime_t * node,
223                                   vlib_frame_t * frame)
224 {
225   return nonip_in_out_node_fn (vm, node, frame, 0);
226 }
227
228 VLIB_NODE_FN (acl_out_nonip_node) (vlib_main_t * vm,
229                                    vlib_node_runtime_t * node,
230                                    vlib_frame_t * frame)
231 {
232   return nonip_in_out_node_fn (vm, node, frame, 1);
233 }
234
235
236
237 VLIB_REGISTER_NODE (acl_in_nonip_node) =
238 {
239   .name = "acl-plugin-in-nonip-l2",
240   .vector_size = sizeof (u32),
241   .format_trace = format_l2_nonip_in_trace,
242   .type = VLIB_NODE_TYPE_INTERNAL,
243   .n_errors = ARRAY_LEN (fa_in_nonip_error_strings),
244   .error_strings = fa_in_nonip_error_strings,
245   .n_next_nodes = ACL_FA_N_NEXT,
246   .next_nodes =
247   {
248     [ACL_FA_ERROR_DROP] = "error-drop",
249   }
250 };
251
252 VNET_FEATURE_INIT (acl_in_l2_nonip_fa_feature, static) =
253 {
254   .arc_name = "l2-input-nonip",
255   .node_name = "acl-plugin-in-nonip-l2",
256   .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
257 };
258
259 VLIB_REGISTER_NODE (acl_out_nonip_node) =
260 {
261   .name = "acl-plugin-out-nonip-l2",
262   .vector_size = sizeof (u32),
263   .format_trace = format_l2_nonip_out_trace,
264   .type = VLIB_NODE_TYPE_INTERNAL,
265   .n_errors = ARRAY_LEN (fa_out_nonip_error_strings),
266   .error_strings = fa_out_nonip_error_strings,
267   .n_next_nodes = ACL_FA_N_NEXT,
268   .next_nodes =
269   {
270     [ACL_FA_ERROR_DROP] = "error-drop",
271   }
272 };
273
274 VNET_FEATURE_INIT (acl_out_l2_nonip_fa_feature, static) =
275 {
276   .arc_name = "l2-output-nonip",
277   .node_name = "acl-plugin-out-nonip-l2",
278   .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
279 };
280
281
282 /*
283  * fd.io coding-style-patch-verification: ON
284  *
285  * Local Variables:
286  * eval: (c-set-style "gnu")
287  * End:
288  */