blob: 7d051326ee0bdc620458b0a1deea74f845506849 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_output_acl.c : layer 2 output acl processing
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/vnet.h>
20#include <vnet/pg/pg.h>
21#include <vnet/ethernet/ethernet.h>
22#include <vnet/ethernet/packet.h>
23#include <vnet/ip/ip_packet.h>
24#include <vnet/ip/ip4_packet.h>
25#include <vnet/ip/ip6_packet.h>
26#include <vlib/cli.h>
27#include <vnet/l2/feat_bitmap.h>
28#include <vnet/l2/l2_output.h>
29
30#include <vppinfra/error.h>
31#include <vppinfra/hash.h>
32#include <vppinfra/cache.h>
33
34
Dave Barach97d8dc22016-08-15 15:31:15 -040035typedef struct
36{
John Lobeb0b2e2017-07-22 00:21:36 -040037 /* Next nodes for L2 output features */
38 u32 l2_out_feat_next[32];
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
40 /* convenience variables */
Dave Barach97d8dc22016-08-15 15:31:15 -040041 vlib_main_t *vlib_main;
42 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070043} l2_outacl_main_t;
44
45
46
Dave Barach97d8dc22016-08-15 15:31:15 -040047typedef struct
48{
49 /* per-pkt trace data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 u8 src[6];
51 u8 dst[6];
52 u32 next_index;
53 u32 sw_if_index;
54} l2_outacl_trace_t;
55
56/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040057static u8 *
58format_l2_outacl_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040062 l2_outacl_trace_t *t = va_arg (*args, l2_outacl_trace_t *);
63
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 s = format (s, "l2-output-acl: sw_if_index %d dst %U src %U",
65 t->sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -040066 format_ethernet_address, t->dst,
67 format_ethernet_address, t->src);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 return s;
69}
70
71l2_outacl_main_t l2_outacl_main;
72
73static vlib_node_registration_t l2_outacl_node;
74
75#define foreach_l2_outacl_error \
76_(L2_OUTACL, "L2 output ACL packets") \
77_(DROP, "L2 output drops")
78
Dave Barach97d8dc22016-08-15 15:31:15 -040079typedef enum
80{
Ed Warnickecb9cada2015-12-08 15:45:58 -070081#define _(sym,str) L2_OUTACL_ERROR_##sym,
82 foreach_l2_outacl_error
83#undef _
Dave Barach97d8dc22016-08-15 15:31:15 -040084 L2_OUTACL_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070085} l2_outacl_error_t;
86
Dave Barach97d8dc22016-08-15 15:31:15 -040087static char *l2_outacl_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070088#define _(sym,string) string,
89 foreach_l2_outacl_error
90#undef _
91};
92
Dave Barach97d8dc22016-08-15 15:31:15 -040093typedef enum
94{
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 L2_OUTACL_NEXT_DROP,
96 L2_OUTACL_N_NEXT,
97} l2_outacl_next_t;
98
99
100
101static uword
102l2_outacl_node_fn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400103 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104{
Dave Barach97d8dc22016-08-15 15:31:15 -0400105 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 l2_outacl_next_t next_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400107 l2_outacl_main_t *msm = &l2_outacl_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 vlib_node_t *n = vlib_get_node (vm, l2_outacl_node.index);
109 u32 node_counter_base_index = n->error_heap_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400110 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
112 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400113 n_left_from = frame->n_vectors; /* number of packets to process */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 next_index = node->cached_next_index;
115
116 while (n_left_from > 0)
117 {
118 u32 n_left_to_next;
119
120 /* get space to enqueue frame to graph node "next_index" */
Dave Barach97d8dc22016-08-15 15:31:15 -0400121 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
123 while (0 && n_left_from >= 4 && n_left_to_next >= 2)
124 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400125 u32 bi0, bi1;
126 vlib_buffer_t *b0, *b1;
127 u32 next0, next1;
128 u32 sw_if_index0, sw_if_index1;
129 ethernet_header_t *h0, *h1;
130
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 /* Prefetch next iteration. */
132 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400133 vlib_buffer_t *p2, *p3;
134
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 p2 = vlib_get_buffer (vm, from[2]);
136 p3 = vlib_get_buffer (vm, from[3]);
Dave Barach97d8dc22016-08-15 15:31:15 -0400137
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 vlib_prefetch_buffer_header (p2, LOAD);
139 vlib_prefetch_buffer_header (p3, LOAD);
140
141 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
142 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
143 }
144
Dave Barach97d8dc22016-08-15 15:31:15 -0400145 /* speculatively enqueue b0 and b1 to the current next frame */
146 /* bi is "buffer index", b is pointer to the buffer */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 to_next[0] = bi0 = from[0];
148 to_next[1] = bi1 = from[1];
149 from += 2;
150 to_next += 2;
151 n_left_from -= 2;
152 n_left_to_next -= 2;
153
154 b0 = vlib_get_buffer (vm, bi0);
155 b1 = vlib_get_buffer (vm, bi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 /* TX interface handles */
158 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
159 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Dave Barach97d8dc22016-08-15 15:31:15 -0400161 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
162 {
163 if (b0->flags & VLIB_BUFFER_IS_TRACED)
164 {
165 l2_outacl_trace_t *t =
166 vlib_add_trace (vm, node, b0, sizeof (*t));
167 t->sw_if_index = sw_if_index0;
168 t->next_index = next0;
169 clib_memcpy (t->src, h0->src_address, 6);
170 clib_memcpy (t->dst, h0->dst_address, 6);
171 }
172 if (b1->flags & VLIB_BUFFER_IS_TRACED)
173 {
174 l2_outacl_trace_t *t =
175 vlib_add_trace (vm, node, b1, sizeof (*t));
176 t->sw_if_index = sw_if_index1;
177 t->next_index = next1;
178 clib_memcpy (t->src, h1->src_address, 6);
179 clib_memcpy (t->dst, h1->dst_address, 6);
180 }
181 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Dave Barach97d8dc22016-08-15 15:31:15 -0400183 em->counters[node_counter_base_index + L2_OUTACL_ERROR_L2_OUTACL] +=
184 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach97d8dc22016-08-15 15:31:15 -0400186 /* add core loop code here */
187
188 /* verify speculative enqueues, maybe switch current next frame */
189 /* if next0==next1==next_index then nothing special needs to be done */
190 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
191 to_next, n_left_to_next,
192 bi0, bi1, next0, next1);
193 }
194
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 while (n_left_from > 0 && n_left_to_next > 0)
196 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400197 u32 bi0;
198 vlib_buffer_t *b0;
199 u32 next0;
200 u32 sw_if_index0;
201 ethernet_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Dave Barach97d8dc22016-08-15 15:31:15 -0400203 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 bi0 = from[0];
205 to_next[0] = bi0;
206 from += 1;
207 to_next += 1;
208 n_left_from -= 1;
209 n_left_to_next -= 1;
210
211 b0 = vlib_get_buffer (vm, bi0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 h0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Dave Barach97d8dc22016-08-15 15:31:15 -0400214 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Dave Barach97d8dc22016-08-15 15:31:15 -0400216 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
217 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
218 {
219 l2_outacl_trace_t *t =
220 vlib_add_trace (vm, node, b0, sizeof (*t));
221 t->sw_if_index = sw_if_index0;
222 clib_memcpy (t->src, h0->src_address, 6);
223 clib_memcpy (t->dst, h0->dst_address, 6);
224 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Dave Barach97d8dc22016-08-15 15:31:15 -0400226 em->counters[node_counter_base_index + L2_OUTACL_ERROR_L2_OUTACL] +=
227 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Dave Barach97d8dc22016-08-15 15:31:15 -0400229 /*
230 * L2_OUTACL code
231 * Dummy for now, just go to next feature node
232 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Dave Barach97d8dc22016-08-15 15:31:15 -0400234 /* Determine next node */
John Lobeb0b2e2017-07-22 00:21:36 -0400235 next0 = vnet_l2_feature_next (b0, msm->l2_out_feat_next,
236 L2OUTPUT_FEAT_ACL);
Dave Barach97d8dc22016-08-15 15:31:15 -0400237
238 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
240 to_next, n_left_to_next,
241 bi0, next0);
242 }
243
244 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
245 }
246
247 return frame->n_vectors;
248}
249
250
Dave Barach97d8dc22016-08-15 15:31:15 -0400251/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252VLIB_REGISTER_NODE (l2_outacl_node,static) = {
253 .function = l2_outacl_node_fn,
254 .name = "l2-output-acl",
255 .vector_size = sizeof (u32),
256 .format_trace = format_l2_outacl_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400258
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 .n_errors = ARRAY_LEN(l2_outacl_error_strings),
260 .error_strings = l2_outacl_error_strings,
261
262 .n_next_nodes = L2_OUTACL_N_NEXT,
263
264 /* edit / add dispositions here */
265 .next_nodes = {
266 [L2_OUTACL_NEXT_DROP] = "error-drop",
267 },
268};
Dave Barach97d8dc22016-08-15 15:31:15 -0400269/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Damjan Marion1c80e832016-05-11 23:07:18 +0200271VLIB_NODE_FUNCTION_MULTIARCH (l2_outacl_node, l2_outacl_node_fn)
Dave Barach97d8dc22016-08-15 15:31:15 -0400272 clib_error_t *l2_outacl_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273{
Dave Barach97d8dc22016-08-15 15:31:15 -0400274 l2_outacl_main_t *mp = &l2_outacl_main;
275
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -0400277 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barach97d8dc22016-08-15 15:31:15 -0400279 /* Initialize the feature next-node indexes */
280 feat_bitmap_init_next_nodes (vm,
281 l2_outacl_node.index,
282 L2OUTPUT_N_FEAT,
283 l2output_get_feat_names (),
John Lobeb0b2e2017-07-22 00:21:36 -0400284 mp->l2_out_feat_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 return 0;
287}
288
289VLIB_INIT_FUNCTION (l2_outacl_init);
290
John Lobbb76822016-06-08 16:08:48 -0400291#if 0
Chris Luke16bcf7d2016-09-01 14:31:46 -0400292/** @todo maybe someone will add output ACL's in the future.
293 * Set subinterface outacl enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400294 * The CLI format is:
295 * set interface acl output <interface> [disable]
296 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297static clib_error_t *
298int_l2_outacl (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400299 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300{
Dave Barach97d8dc22016-08-15 15:31:15 -0400301 vnet_main_t *vnm = vnet_get_main ();
302 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 u32 sw_if_index;
304 u32 enable;
305
Dave Barach97d8dc22016-08-15 15:31:15 -0400306 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 {
308 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400309 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 goto done;
311 }
312
313 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400314 if (unformat (input, "disable"))
315 {
316 enable = 0;
317 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
Dave Barach97d8dc22016-08-15 15:31:15 -0400319 /* set the interface flag */
320 l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_ACL, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
Dave Barach97d8dc22016-08-15 15:31:15 -0400322done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 return error;
324}
325
Dave Barach97d8dc22016-08-15 15:31:15 -0400326/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327VLIB_CLI_COMMAND (int_l2_outacl_cli, static) = {
328 .path = "set interface acl output",
329 .short_help = "set interface acl output <interface> [disable]",
330 .function = int_l2_outacl,
331};
Dave Barach97d8dc22016-08-15 15:31:15 -0400332/* *INDENT-ON* */
John Lobbb76822016-06-08 16:08:48 -0400333#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400334
335/*
336 * fd.io coding-style-patch-verification: ON
337 *
338 * Local Variables:
339 * eval: (c-set-style "gnu")
340 * End:
341 */