2 * l2_input_vtr.c : layer 2 input vlan tag rewrite processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/l2/feat_bitmap.h>
24 #include <vnet/l2/l2_vtr.h>
25 #include <vnet/l2/l2_input_vtr.h>
26 #include <vnet/l2/l2_output.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/cache.h>
34 /* per-pkt trace data */
37 u8 raw[12]; /* raw data (vlans) */
41 /* packet trace format function */
43 format_l2_invtr_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 l2_invtr_trace_t *t = va_arg (*args, l2_invtr_trace_t *);
49 s = format (s, "l2-input-vtr: sw_if_index %d dst %U src %U data "
50 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
52 format_ethernet_address, t->dst,
53 format_ethernet_address, t->src,
54 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
55 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
56 t->raw[10], t->raw[11]);
60 l2_invtr_main_t l2_invtr_main;
62 static vlib_node_registration_t l2_invtr_node;
64 #define foreach_l2_invtr_error \
65 _(L2_INVTR, "L2 inverter packets") \
66 _(DROP, "L2 input tag rewrite drops")
70 #define _(sym,str) L2_INVTR_ERROR_##sym,
71 foreach_l2_invtr_error
76 static char *l2_invtr_error_strings[] = {
77 #define _(sym,string) string,
78 foreach_l2_invtr_error
90 l2_invtr_node_fn (vlib_main_t * vm,
91 vlib_node_runtime_t * node, vlib_frame_t * frame)
93 u32 n_left_from, *from, *to_next;
94 l2_invtr_next_t next_index;
95 l2_invtr_main_t *msm = &l2_invtr_main;
97 from = vlib_frame_vector_args (frame);
98 n_left_from = frame->n_vectors; /* number of packets to process */
99 next_index = node->cached_next_index;
101 while (n_left_from > 0)
105 /* get space to enqueue frame to graph node "next_index" */
106 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
108 while (n_left_from >= 6 && n_left_to_next >= 2)
111 vlib_buffer_t *b0, *b1;
113 u32 sw_if_index0, sw_if_index1;
114 u32 feature_bitmap0, feature_bitmap1;
116 /* Prefetch next iteration. */
118 vlib_buffer_t *p2, *p3, *p4, *p5;
119 u32 sw_if_index2, sw_if_index3;
121 p2 = vlib_get_buffer (vm, from[2]);
122 p3 = vlib_get_buffer (vm, from[3]);
123 p4 = vlib_get_buffer (vm, from[4]);
124 p5 = vlib_get_buffer (vm, from[5]);
126 /* Prefetch the buffer header and packet for the N+2 loop iteration */
127 vlib_prefetch_buffer_header (p4, LOAD);
128 vlib_prefetch_buffer_header (p5, LOAD);
130 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
131 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
134 * Prefetch the input config for the N+1 loop iteration
135 * This depends on the buffer header above
137 sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_RX];
138 sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_RX];
139 CLIB_PREFETCH (vec_elt_at_index
140 (l2output_main.configs, sw_if_index2),
141 CLIB_CACHE_LINE_BYTES, LOAD);
142 CLIB_PREFETCH (vec_elt_at_index
143 (l2output_main.configs, sw_if_index3),
144 CLIB_CACHE_LINE_BYTES, LOAD);
147 /* speculatively enqueue b0 and b1 to the current next frame */
148 /* bi is "buffer index", b is pointer to the buffer */
149 to_next[0] = bi0 = from[0];
150 to_next[1] = bi1 = from[1];
156 b0 = vlib_get_buffer (vm, bi0);
157 b1 = vlib_get_buffer (vm, bi1);
159 /* RX interface handles */
160 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
161 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
163 /* process 2 packets */
165 /* Remove ourself from the feature bitmap */
167 vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
169 vnet_buffer (b1)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
171 /* save for next feature graph nodes */
172 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
173 vnet_buffer (b1)->l2.feature_bitmap = feature_bitmap1;
175 /* Determine the next node */
176 next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
178 next1 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
181 /* perform the tag rewrite on two packets */
185 (l2output_main.configs, sw_if_index0)->input_vtr)))
188 next0 = L2_INVTR_NEXT_DROP;
189 b0->error = node->errors[L2_INVTR_ERROR_DROP];
194 (l2output_main.configs, sw_if_index1)->input_vtr)))
197 next1 = L2_INVTR_NEXT_DROP;
198 b1->error = node->errors[L2_INVTR_ERROR_DROP];
201 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
203 if (b0->flags & VLIB_BUFFER_IS_TRACED)
205 l2_invtr_trace_t *t =
206 vlib_add_trace (vm, node, b0, sizeof (*t));
207 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
208 t->sw_if_index = sw_if_index0;
209 clib_memcpy (t->src, h0->src_address, 6);
210 clib_memcpy (t->dst, h0->dst_address, 6);
211 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
213 if (b1->flags & VLIB_BUFFER_IS_TRACED)
215 l2_invtr_trace_t *t =
216 vlib_add_trace (vm, node, b1, sizeof (*t));
217 ethernet_header_t *h1 = vlib_buffer_get_current (b1);
218 t->sw_if_index = sw_if_index0;
219 clib_memcpy (t->src, h1->src_address, 6);
220 clib_memcpy (t->dst, h1->dst_address, 6);
221 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
225 /* verify speculative enqueues, maybe switch current next frame */
226 /* if next0==next1==next_index then nothing special needs to be done */
227 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
228 to_next, n_left_to_next,
229 bi0, bi1, next0, next1);
232 while (n_left_from > 0 && n_left_to_next > 0)
240 /* speculatively enqueue b0 to the current next frame */
248 b0 = vlib_get_buffer (vm, bi0);
250 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
252 /* process 1 packet */
254 /* Remove ourself from the feature bitmap */
256 vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
258 /* save for next feature graph nodes */
259 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
261 /* Determine the next node */
262 next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
265 /* perform the tag rewrite on one packet */
269 (l2output_main.configs, sw_if_index0)->input_vtr)))
272 next0 = L2_INVTR_NEXT_DROP;
273 b0->error = node->errors[L2_INVTR_ERROR_DROP];
276 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
277 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
279 l2_invtr_trace_t *t =
280 vlib_add_trace (vm, node, b0, sizeof (*t));
281 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
282 t->sw_if_index = sw_if_index0;
283 clib_memcpy (t->src, h0->src_address, 6);
284 clib_memcpy (t->dst, h0->dst_address, 6);
285 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
288 /* verify speculative enqueue, maybe switch current next frame */
289 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
290 to_next, n_left_to_next,
294 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
297 return frame->n_vectors;
302 VLIB_REGISTER_NODE (l2_invtr_node,static) = {
303 .function = l2_invtr_node_fn,
304 .name = "l2-input-vtr",
305 .vector_size = sizeof (u32),
306 .format_trace = format_l2_invtr_trace,
307 .type = VLIB_NODE_TYPE_INTERNAL,
309 .n_errors = ARRAY_LEN(l2_invtr_error_strings),
310 .error_strings = l2_invtr_error_strings,
312 .n_next_nodes = L2_INVTR_N_NEXT,
314 /* edit / add dispositions here */
316 [L2_INVTR_NEXT_DROP] = "error-drop",
321 VLIB_NODE_FUNCTION_MULTIARCH (l2_invtr_node, l2_invtr_node_fn)
322 clib_error_t *l2_invtr_init (vlib_main_t * vm)
324 l2_invtr_main_t *mp = &l2_invtr_main;
327 mp->vnet_main = vnet_get_main ();
329 /* Initialize the feature next-node indexes */
330 feat_bitmap_init_next_nodes (vm,
333 l2input_get_feat_names (),
334 mp->feat_next_node_index);
339 VLIB_INIT_FUNCTION (l2_invtr_init);
343 * fd.io coding-style-patch-verification: ON
346 * eval: (c-set-style "gnu")