2 * Copyright (c) 2015 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 <vnet/pg/pg.h>
19 #include <vppinfra/error.h>
21 #include <vnet/ip/ip.h>
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
29 vlib_main_t * vlib_main;
30 vnet_main_t * vnet_main;
31 } ip4_hop_by_hop_main_t;
33 ip4_hop_by_hop_main_t ip4_hop_by_hop_main;
35 vlib_node_registration_t ip4_hop_by_hop_node;
39 } ip4_hop_by_hop_trace_t;
41 /* packet trace format function */
42 static u8 * format_ip4_hop_by_hop_trace (u8 * s, va_list * args)
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 ip4_hop_by_hop_trace_t * t = va_arg (*args, ip4_hop_by_hop_trace_t *);
48 s = format (s, "IP4_HOP_BY_HOP: next index %d",
53 vlib_node_registration_t ip4_hop_by_hop_node;
55 #define foreach_ip4_hop_by_hop_error \
56 _(PROCESSED, "Pkts with ip4 hop-by-hop options")
59 #define _(sym,str) IP4_HOP_BY_HOP_ERROR_##sym,
60 foreach_ip4_hop_by_hop_error
62 IP4_HOP_BY_HOP_N_ERROR,
63 } ip4_hop_by_hop_error_t;
65 static char * ip4_hop_by_hop_error_strings[] = {
66 #define _(sym,string) string,
67 foreach_ip4_hop_by_hop_error
72 ip4_hop_by_hop_node_fn (vlib_main_t * vm,
73 vlib_node_runtime_t * node,
76 ip4_main_t * im = &ip4_main;
77 ip_lookup_main_t * lm = &im->lookup_main;
78 u32 n_left_from, * from, * to_next;
79 ip_lookup_next_t next_index;
82 from = vlib_frame_vector_args (frame);
83 n_left_from = frame->n_vectors;
84 next_index = node->cached_next_index;
86 while (n_left_from > 0)
90 vlib_get_next_frame (vm, node, next_index,
91 to_next, n_left_to_next);
94 while (n_left_from >= 4 && n_left_to_next >= 2)
96 u32 next0 = IP4_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
97 u32 next1 = IP4_HOP_BY_HOP_NEXT_INTERFACE_OUTPUT;
98 u32 sw_if_index0, sw_if_index1;
100 ethernet_header_t *en0, *en1;
102 vlib_buffer_t * b0, * b1;
104 /* Prefetch next iteration. */
106 vlib_buffer_t * p2, * p3;
108 p2 = vlib_get_buffer (vm, from[2]);
109 p3 = vlib_get_buffer (vm, from[3]);
111 vlib_prefetch_buffer_header (p2, LOAD);
112 vlib_prefetch_buffer_header (p3, LOAD);
114 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
115 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
118 /* speculatively enqueue b0 and b1 to the current next frame */
119 to_next[0] = bi0 = from[0];
120 to_next[1] = bi1 = from[1];
126 b0 = vlib_get_buffer (vm, bi0);
127 b1 = vlib_get_buffer (vm, bi1);
129 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
130 ASSERT (b0->current_data == 0);
131 ASSERT (b1->current_data == 0);
133 ip0 = vlib_buffer_get_current (b0);
134 ip1 = vlib_buffer_get_current (b0);
136 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
137 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
139 /* $$$$$ End of processing 2 x packets $$$$$ */
141 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
143 if (b0->flags & VLIB_BUFFER_IS_TRACED)
145 ip4_hop_by_hop_trace_t *t =
146 vlib_add_trace (vm, node, b0, sizeof (*t));
147 t->sw_if_index = sw_if_index0;
148 t->next_index = next0;
150 if (b1->flags & VLIB_BUFFER_IS_TRACED)
152 ip4_hop_by_hop_trace_t *t =
153 vlib_add_trace (vm, node, b1, sizeof (*t));
154 t->sw_if_index = sw_if_index1;
155 t->next_index = next1;
159 /* verify speculative enqueues, maybe switch current next frame */
160 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
161 to_next, n_left_to_next,
162 bi0, bi1, next0, next1);
166 while (n_left_from > 0 && n_left_to_next > 0)
172 ip_adjacency_t * adj0;
174 /* speculatively enqueue b0 to the current next frame */
182 b0 = vlib_get_buffer (vm, bi0);
184 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
185 adj0 = ip_get_adjacency (lm, adj_index0);
187 /* $$$$$$$$$$$$ process one (or more) hop-by-hop header(s) here */
192 /* Send the packet e.g. to ip4_rewrite */
193 next0 = adj0->lookup_next_index;
195 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
196 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
198 ip4_hop_by_hop_trace_t *t =
199 vlib_add_trace (vm, node, b0, sizeof (*t));
200 t->next_index = next0;
205 /* $$$$$ Done processing 1 packet here $$$$$ */
207 /* verify speculative enqueue, maybe switch current next frame */
208 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
209 to_next, n_left_to_next,
213 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
216 vlib_node_increment_counter (vm, ip4_hop_by_hop_node.index,
217 IP4_HOP_BY_HOP_ERROR_PROCESSED, processed);
218 return frame->n_vectors;
221 VLIB_REGISTER_NODE (ip4_hop_by_hop_node) = {
222 .function = ip4_hop_by_hop_node_fn,
223 .name = "ip4-hop-by-hop",
224 .vector_size = sizeof (u32),
225 .format_trace = format_ip4_hop_by_hop_trace,
226 .type = VLIB_NODE_TYPE_INTERNAL,
228 .n_errors = ARRAY_LEN(ip4_hop_by_hop_error_strings),
229 .error_strings = ip4_hop_by_hop_error_strings,
231 /* See ip/lookup.h */
232 .n_next_nodes = IP_LOOKUP_N_NEXT,
233 .next_nodes = IP4_LOOKUP_NEXT_NODES,
236 VLIB_REGISTER_NODE (ip4_add_hop_by_hop_node) = {
237 .function = ip4_hop_by_hop_node_fn,
238 .name = "ip4-add-hop-by-hop",
239 .vector_size = sizeof (u32),
240 .format_trace = format_ip4_hop_by_hop_trace,
241 .type = VLIB_NODE_TYPE_INTERNAL,
243 .n_errors = ARRAY_LEN(ip4_hop_by_hop_error_strings),
244 .error_strings = ip4_hop_by_hop_error_strings,
246 /* See ip/lookup.h */
247 .n_next_nodes = IP_LOOKUP_N_NEXT,
248 .next_nodes = IP4_LOOKUP_NEXT_NODES,
251 VLIB_REGISTER_NODE (ip4_pop_hop_by_hop_node) = {
252 .function = ip4_hop_by_hop_node_fn,
253 .name = "ip4-pop-hop-by-hop",
254 .vector_size = sizeof (u32),
255 .format_trace = format_ip4_hop_by_hop_trace,
256 .type = VLIB_NODE_TYPE_INTERNAL,
258 .n_errors = ARRAY_LEN(ip4_hop_by_hop_error_strings),
259 .error_strings = ip4_hop_by_hop_error_strings,
261 /* See ip/lookup.h */
262 .n_next_nodes = IP_LOOKUP_N_NEXT,
263 .next_nodes = IP4_LOOKUP_NEXT_NODES,
266 static clib_error_t *
267 ip4_hop_by_hop_init (vlib_main_t * vm)
269 ip4_hop_by_hop_main_t * hm = &ip4_hop_by_hop_main;
272 hm->vnet_main = vnet_get_main();
277 VLIB_INIT_FUNCTION (ip4_hop_by_hop_init);