1 ;;; Copyright (c) 2016 Cisco and/or its affiliates.
2 ;;; Licensed under the Apache License, Version 2.0 (the "License");
3 ;;; you may not use this file except in compliance with the License.
4 ;;; You may obtain a copy of the License at:
6 ;;; http://www.apache.org/licenses/LICENSE-2.0
8 ;;; Unless required by applicable law or agreed to in writing, software
9 ;;; distributed under the License is distributed on an "AS IS" BASIS,
10 ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 ;;; See the License for the specific language governing permissions and
12 ;;; limitations under the License.
14 ;;; dual-loop-skel.el - Eliotic dual-loop node skeleton
18 (define-skeleton skel-dual-loop
19 "Insert a skeleton dual-loop graph node"
21 '(setq node-name (skeleton-read "Node Name: "))
22 '(setq uc-node-name (upcase node-name))
24 #include <vlib/vlib.h>
25 #include <vnet/vnet.h>
26 #include <vnet/pg/pg.h>
28 #include <vnet/ip/ip.h>
29 #include <vnet/ethernet/ethernet.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/error.h>
33 #include <vppinfra/elog.h>
37 vlib_main_t * vlib_main;
38 vnet_main_t * vnet_main;
39 ethernet_main_t * ethernet_main;
40 } " node-name "_main_t;
42 " node-name "_main_t " node-name "_main;
44 vlib_node_registration_t " node-name "_node;
49 } " node-name "_trace_t;
51 /* packet trace format function */
52 static u8 * format_" node-name "_trace (u8 * s, va_list * args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *);
58 s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\",
59 t->sw_if_index, t->next_index);
63 vlib_node_registration_t " node-name "_node;
65 #define foreach_" node-name "_error \\
66 _(SWAPPED, \"Mac swap packets processed\")
69 #define _(sym,str) " uc-node-name "_ERROR_##sym,
70 foreach_" node-name "_error
72 " uc-node-name "_N_ERROR,
73 } " node-name "_error_t;
75 static char * " node-name "_error_strings[] = {
76 #define _(sym,string) string,
77 foreach_" node-name "_error
82 " uc-node-name "_NEXT_INTERFACE_OUTPUT,
83 " uc-node-name "_N_NEXT,
84 } " node-name "_next_t;
86 #define foreach_mac_address_offset \\
95 " node-name "_node_fn (vlib_main_t * vm,
96 vlib_node_runtime_t * node,
99 u32 n_left_from, * from, * to_next;
100 " node-name "_next_t next_index;
101 u32 pkts_swapped = 0;
103 from = vlib_frame_vector_args (frame);
104 n_left_from = frame->n_vectors;
105 next_index = node->cached_next_index;
107 while (n_left_from > 0)
111 vlib_get_next_frame (vm, node, next_index,
112 to_next, n_left_to_next);
114 while (n_left_from >= 4 && n_left_to_next >= 2)
116 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
117 u32 next1 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
118 u32 sw_if_index0, sw_if_index1;
120 ethernet_header_t *en0, *en1;
122 vlib_buffer_t * b0, * b1;
124 /* Prefetch next iteration. */
126 vlib_buffer_t * p2, * p3;
128 p2 = vlib_get_buffer (vm, from[2]);
129 p3 = vlib_get_buffer (vm, from[3]);
131 vlib_prefetch_buffer_header (p2, LOAD);
132 vlib_prefetch_buffer_header (p3, LOAD);
134 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
135 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
138 /* speculatively enqueue b0 and b1 to the current next frame */
139 to_next[0] = bi0 = from[0];
140 to_next[1] = bi1 = from[1];
146 b0 = vlib_get_buffer (vm, bi0);
147 b1 = vlib_get_buffer (vm, bi1);
149 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
150 ASSERT (b0->current_data == 0);
151 ASSERT (b1->current_data == 0);
153 en0 = vlib_buffer_get_current (b0);
154 en1 = vlib_buffer_get_current (b1);
156 /* This is not the fastest way to swap src + dst mac addresses */
157 #define _(a) tmp0[a] = en0->src_address[a];
158 foreach_mac_address_offset;
160 #define _(a) en0->src_address[a] = en0->dst_address[a];
161 foreach_mac_address_offset;
163 #define _(a) en0->dst_address[a] = tmp0[a];
164 foreach_mac_address_offset;
167 #define _(a) tmp1[a] = en1->src_address[a];
168 foreach_mac_address_offset;
170 #define _(a) en1->src_address[a] = en1->dst_address[a];
171 foreach_mac_address_offset;
173 #define _(a) en1->dst_address[a] = tmp1[a];
174 foreach_mac_address_offset;
177 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
178 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
180 /* Send pkt back out the RX interface */
181 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
182 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
185 /* $$$$$ End of processing 2 x packets $$$$$ */
187 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
189 if (b0->flags & VLIB_BUFFER_IS_TRACED)
191 " node-name "_trace_t *t =
192 vlib_add_trace (vm, node, b0, sizeof (*t));
193 t->sw_if_index = sw_if_index0;
194 t->next_index = next0;
196 if (b1->flags & VLIB_BUFFER_IS_TRACED)
198 " node-name "_trace_t *t =
199 vlib_add_trace (vm, node, b1, sizeof (*t));
200 t->sw_if_index = sw_if_index1;
201 t->next_index = next1;
205 /* verify speculative enqueues, maybe switch current next frame */
206 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
207 to_next, n_left_to_next,
208 bi0, bi1, next0, next1);
211 while (n_left_from > 0 && n_left_to_next > 0)
215 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
218 ethernet_header_t *en0;
220 /* speculatively enqueue b0 to the current next frame */
228 b0 = vlib_get_buffer (vm, bi0);
230 /* $$$$$ Single loop: process 1 packet here $$$$$ */
233 * Direct from the driver, we should be at offset 0
234 * aka at &b0->data[0]
236 ASSERT (b0->current_data == 0);
238 en0 = vlib_buffer_get_current (b0);
240 /* This is not the fastest way to swap src + dst mac addresses */
241 #define _(a) tmp0[a] = en0->src_address[a];
242 foreach_mac_address_offset;
244 #define _(a) en0->src_address[a] = en0->dst_address[a];
245 foreach_mac_address_offset;
247 #define _(a) en0->dst_address[a] = tmp0[a];
248 foreach_mac_address_offset;
251 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
253 /* Send pkt back out the RX interface */
254 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
256 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
257 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
259 " node-name "_trace_t *t =
260 vlib_add_trace (vm, node, b0, sizeof (*t));
261 t->sw_if_index = sw_if_index0;
262 t->next_index = next0;
267 /* $$$$$ Done processing 1 packet here $$$$$ */
269 /* verify speculative enqueue, maybe switch current next frame */
270 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
271 to_next, n_left_to_next,
275 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
278 vlib_node_increment_counter (vm, " node-name "_node.index,
279 " uc-node-name "_ERROR_SWAPPED, pkts_swapped);
280 return frame->n_vectors;
283 VLIB_REGISTER_NODE (" node-name "_node) = {
284 .function = " node-name "_node_fn,
285 .name = \"" node-name "\",
286 .vector_size = sizeof (u32),
287 .format_trace = format_" node-name "_trace,
288 .type = VLIB_NODE_TYPE_INTERNAL,
290 .n_errors = ARRAY_LEN(" node-name "_error_strings),
291 .error_strings = " node-name "_error_strings,
293 .n_next_nodes = " uc-node-name "_N_NEXT,
295 /* edit / add dispositions here */
297 [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
301 clib_error_t *" node-name "_init (vlib_main_t *vm)
303 " node-name "_main_t *msm = &" node-name "_main;
305 /* $$$$$ Initialize " node-name "_main_t structure here. $$$$$ */
307 msm->vnet_main = vnet_get_main();
308 msm->ethernet_main = ethernet_get_main(vm);
313 VLIB_INIT_FUNCTION(" node-name "_init);