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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
20 #include <vnet/span/span.h>
22 #include <vppinfra/error.h>
23 #include <vppinfra/elog.h>
25 vlib_node_registration_t span_node;
27 /* packet trace format function */
29 format_span_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 span_trace_t *t = va_arg (*args, span_trace_t *);
35 vnet_main_t *vnm = &vnet_main;
36 s = format (s, "SPAN: mirrored %U -> %U",
37 format_vnet_sw_if_index_name, vnm, t->src_sw_if_index,
38 format_vnet_sw_if_index_name, vnm, t->mirror_sw_if_index);
43 #define foreach_span_error \
44 _(HITS, "SPAN incomming packets processed")
48 #define _(sym,str) SPAN_ERROR_##sym,
54 static char *span_error_strings[] = {
55 #define _(sym,string) string,
61 span_node_fn (vlib_main_t * vm,
62 vlib_node_runtime_t * node, vlib_frame_t * frame)
64 span_main_t *sm = &span_main;
65 vnet_main_t *vnm = &vnet_main;
66 u32 n_left_from, *from, *to_next, *to_mirror_next = 0;
67 u32 n_span_packets = 0;
68 u32 next_index, mirror_sw_if_index0, mirror_sw_if_index1;
69 u32 last_mirror_sw_if_index = ~0;
70 vlib_frame_t *mirror_frame = 0;
72 from = vlib_frame_vector_args (frame);
73 n_left_from = frame->n_vectors;
74 next_index = node->cached_next_index;
77 while (n_left_from > 0)
81 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
83 while (n_left_from >= 4 && n_left_to_next >= 2)
87 vlib_buffer_t *b0, *c0;
88 vlib_buffer_t *b1, *c1;
90 u32 next0 = 0; //SPAN_NEXT_ORIG_ETHERNET;
92 u32 next1 = 1; //SPAN_NEXT_ORIG_ETHERNET;
94 /* speculatively enqueue b0, b1 to the current next frame */
95 to_next[0] = bi0 = from[0];
96 to_next[1] = bi1 = from[1];
102 b0 = vlib_get_buffer (vm, bi0);
103 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
104 mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
105 b1 = vlib_get_buffer (vm, bi1);
106 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
107 mirror_sw_if_index1 = sm->dst_by_src_sw_if_index[sw_if_index1];
109 /* get frame to mirror interface */
111 ((last_mirror_sw_if_index != mirror_sw_if_index0)
112 || mirror_frame == 0))
115 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
117 last_mirror_sw_if_index = mirror_sw_if_index0;
119 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
120 to_mirror_next = vlib_frame_vector_args (mirror_frame);
122 /* get frame to mirror interface */
124 ((last_mirror_sw_if_index != mirror_sw_if_index1)
125 || mirror_frame == 0))
128 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
130 last_mirror_sw_if_index = mirror_sw_if_index1;
132 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
133 to_mirror_next = vlib_frame_vector_args (mirror_frame);
135 c0 = vlib_buffer_copy (vm, b0);
136 vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
137 to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
139 mirror_frame->n_vectors++;
141 vnet_feature_next (sw_if_index0, &next0, b0);
143 c1 = vlib_buffer_copy (vm, b1);
144 vnet_buffer (c1)->sw_if_index[VLIB_TX] = mirror_sw_if_index1;
145 to_mirror_next[0] = vlib_get_buffer_index (vm, c1);
147 mirror_frame->n_vectors++;
149 vnet_feature_next (sw_if_index1, &next1, b1);
151 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
153 span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
154 t->src_sw_if_index = sw_if_index0;
155 t->mirror_sw_if_index =
156 sm->dst_by_src_sw_if_index[sw_if_index0];
159 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
161 span_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
162 t->src_sw_if_index = sw_if_index1;
163 t->mirror_sw_if_index =
164 sm->dst_by_src_sw_if_index[sw_if_index1];
166 /* verify speculative enqueue, maybe switch current next frame */
167 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
168 to_next, n_left_to_next,
169 bi0, bi1, next0, next1);
171 while (n_left_from > 0 && n_left_to_next > 0)
174 vlib_buffer_t *b0, *c0;
176 u32 next0 = 0; //SPAN_NEXT_ORIG_ETHERNET;
178 /* speculatively enqueue b0 to the current next frame */
179 to_next[0] = bi0 = from[0];
185 b0 = vlib_get_buffer (vm, bi0);
186 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
187 mirror_sw_if_index0 = sm->dst_by_src_sw_if_index[sw_if_index0];
189 /* get frame to mirror interface */
191 ((last_mirror_sw_if_index != mirror_sw_if_index0)
192 || mirror_frame == 0))
195 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index,
197 last_mirror_sw_if_index = mirror_sw_if_index0;
199 vnet_get_frame_to_sw_interface (vnm, mirror_sw_if_index0);
200 to_mirror_next = vlib_frame_vector_args (mirror_frame);
202 c0 = vlib_buffer_copy (vm, b0);
203 vnet_buffer (c0)->sw_if_index[VLIB_TX] = mirror_sw_if_index0;
204 to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
206 mirror_frame->n_vectors++;
208 vnet_feature_next (sw_if_index0, &next0, b0);
210 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
212 span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
213 t->src_sw_if_index = sw_if_index0;
214 t->mirror_sw_if_index =
215 sm->dst_by_src_sw_if_index[sw_if_index0];
217 /* verify speculative enqueue, maybe switch current next frame */
218 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
219 n_left_to_next, bi0, next0);
222 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
225 vnet_put_frame_to_sw_interface (vnm, last_mirror_sw_if_index, mirror_frame);
226 vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS,
229 return frame->n_vectors;
233 VLIB_REGISTER_NODE (span_node) = {
234 .function = span_node_fn,
235 .name = "span-input",
236 .vector_size = sizeof (u32),
237 .format_trace = format_span_trace,
238 .type = VLIB_NODE_TYPE_INTERNAL,
240 .n_errors = ARRAY_LEN(span_error_strings),
241 .error_strings = span_error_strings,
245 /* edit / add dispositions here */
253 VLIB_NODE_FUNCTION_MULTIARCH (span_node, span_node_fn)
255 * fd.io coding-style-patch-verification: ON
258 * eval: (c-set-style "gnu")