blob: 79d15418af2bef3c24cc6b2772063b4f8beb8269 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 * llc_node.c: llc packet processing
17 *
18 * Copyright (c) 2010 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vlib/vlib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070041#include <vnet/llc/llc.h>
42
43#define foreach_llc_input_next \
44 _ (PUNT, "error-punt") \
45 _ (DROP, "error-drop")
46
Calvin151fb722016-08-24 10:35:59 -040047typedef enum
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049#define _(s,n) LLC_INPUT_NEXT_##s,
50 foreach_llc_input_next
51#undef _
Calvin151fb722016-08-24 10:35:59 -040052 LLC_INPUT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070053} llc_input_next_t;
54
Calvin151fb722016-08-24 10:35:59 -040055typedef struct
56{
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 u8 packet_data[32];
58} llc_input_trace_t;
59
Calvin151fb722016-08-24 10:35:59 -040060static u8 *
61format_llc_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Calvin151fb722016-08-24 10:35:59 -040065 llc_input_trace_t *t = va_arg (*va, llc_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67 s = format (s, "%U", format_llc_header, t->packet_data);
68
69 return s;
70}
71
72static uword
73llc_input (vlib_main_t * vm,
Calvin151fb722016-08-24 10:35:59 -040074 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
Calvin151fb722016-08-24 10:35:59 -040076 llc_main_t *lm = &llc_main;
77 u32 n_left_from, next_index, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 from = vlib_frame_vector_args (from_frame);
80 n_left_from = from_frame->n_vectors;
81
82 if (node->flags & VLIB_NODE_FLAG_TRACE)
83 vlib_trace_frame_buffers_only (vm, node,
84 from,
85 n_left_from,
86 sizeof (from[0]),
87 sizeof (llc_input_trace_t));
88
89 next_index = node->cached_next_index;
90
91 while (n_left_from > 0)
92 {
93 u32 n_left_to_next;
94
Calvin151fb722016-08-24 10:35:59 -040095 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 while (n_left_from >= 4 && n_left_to_next >= 2)
98 {
99 u32 bi0, bi1;
Calvin151fb722016-08-24 10:35:59 -0400100 vlib_buffer_t *b0, *b1;
101 llc_header_t *h0, *h1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 u8 next0, next1, len0, len1, enqueue_code;
103
104 /* Prefetch next iteration. */
105 {
Calvin151fb722016-08-24 10:35:59 -0400106 vlib_buffer_t *b2, *b3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
108 b2 = vlib_get_buffer (vm, from[2]);
109 b3 = vlib_get_buffer (vm, from[3]);
110
111 vlib_prefetch_buffer_header (b2, LOAD);
112 vlib_prefetch_buffer_header (b3, LOAD);
113
114 CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
115 CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
116 }
117
118 bi0 = from[0];
119 bi1 = from[1];
120 to_next[0] = bi0;
121 to_next[1] = bi1;
122 from += 2;
123 to_next += 2;
124 n_left_to_next -= 2;
125 n_left_from -= 2;
126
127 b0 = vlib_get_buffer (vm, bi0);
128 b1 = vlib_get_buffer (vm, bi1);
129
Zhiyong Yangd7d24992019-05-28 00:57:39 -0400130 h0 = vlib_buffer_get_current (b0);
131 h1 = vlib_buffer_get_current (b1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
133 len0 = llc_header_length (h0);
134 len1 = llc_header_length (h1);
135
Zhiyong Yangd7d24992019-05-28 00:57:39 -0400136 vlib_buffer_advance (b0, len0);
137 vlib_buffer_advance (b1, len1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 next0 = lm->input_next_by_protocol[h0->dst_sap];
140 next1 = lm->input_next_by_protocol[h1->dst_sap];
141
Calvin151fb722016-08-24 10:35:59 -0400142 b0->error =
143 node->errors[next0 ==
144 LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
145 LLC_ERROR_NONE];
146 b1->error =
147 node->errors[next1 ==
148 LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
149 LLC_ERROR_NONE];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Calvin151fb722016-08-24 10:35:59 -0400151 enqueue_code = (next0 != next_index) + 2 * (next1 != next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
153 if (PREDICT_FALSE (enqueue_code != 0))
154 {
155 switch (enqueue_code)
156 {
157 case 1:
158 /* A B A */
159 to_next[-2] = bi1;
160 to_next -= 1;
161 n_left_to_next += 1;
162 vlib_set_next_frame_buffer (vm, node, next0, bi0);
163 break;
164
165 case 2:
166 /* A A B */
167 to_next -= 1;
168 n_left_to_next += 1;
169 vlib_set_next_frame_buffer (vm, node, next1, bi1);
170 break;
171
172 case 3:
173 /* A B B or A B C */
174 to_next -= 2;
175 n_left_to_next += 2;
176 vlib_set_next_frame_buffer (vm, node, next0, bi0);
177 vlib_set_next_frame_buffer (vm, node, next1, bi1);
178 if (next0 == next1)
179 {
180 vlib_put_next_frame (vm, node, next_index,
181 n_left_to_next);
182 next_index = next1;
Calvin151fb722016-08-24 10:35:59 -0400183 vlib_get_next_frame (vm, node, next_index, to_next,
184 n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 }
186 }
187 }
188 }
Calvin151fb722016-08-24 10:35:59 -0400189
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190 while (n_left_from > 0 && n_left_to_next > 0)
191 {
192 u32 bi0;
Calvin151fb722016-08-24 10:35:59 -0400193 vlib_buffer_t *b0;
194 llc_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 u8 next0, len0;
196
197 bi0 = from[0];
198 to_next[0] = bi0;
199 from += 1;
200 to_next += 1;
201 n_left_from -= 1;
202 n_left_to_next -= 1;
203
204 b0 = vlib_get_buffer (vm, bi0);
205
Zhiyong Yangd7d24992019-05-28 00:57:39 -0400206 h0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
208 len0 = llc_header_length (h0);
209
Zhiyong Yangd7d24992019-05-28 00:57:39 -0400210 vlib_buffer_advance (b0, len0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 next0 = lm->input_next_by_protocol[h0->dst_sap];
213
Calvin151fb722016-08-24 10:35:59 -0400214 b0->error =
215 node->errors[next0 ==
216 LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
217 LLC_ERROR_NONE];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219 /* Sent packet to wrong next? */
220 if (PREDICT_FALSE (next0 != next_index))
221 {
222 /* Return old frame; remove incorrectly enqueued packet. */
223 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
224
225 /* Send to correct next. */
226 next_index = next0;
227 vlib_get_next_frame (vm, node, next_index,
228 to_next, n_left_to_next);
229
230 to_next[0] = bi0;
231 to_next += 1;
232 n_left_to_next -= 1;
233 }
234 }
235
236 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
237 }
238
239 return from_frame->n_vectors;
240}
241
Calvin151fb722016-08-24 10:35:59 -0400242static char *llc_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243#define _(f,s) s,
244 foreach_llc_error
245#undef _
246};
247
Calvin151fb722016-08-24 10:35:59 -0400248/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249VLIB_REGISTER_NODE (llc_input_node) = {
250 .function = llc_input,
251 .name = "llc-input",
252 /* Takes a vector of packets. */
253 .vector_size = sizeof (u32),
254
255 .n_errors = LLC_N_ERROR,
256 .error_strings = llc_error_strings,
257
258 .n_next_nodes = LLC_INPUT_N_NEXT,
259 .next_nodes = {
260#define _(s,n) [LLC_INPUT_NEXT_##s] = n,
261 foreach_llc_input_next
262#undef _
263 },
264
265 .format_buffer = format_llc_header_with_length,
266 .format_trace = format_llc_input_trace,
267 .unformat_buffer = unformat_llc_header,
268};
Calvin151fb722016-08-24 10:35:59 -0400269/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Calvin151fb722016-08-24 10:35:59 -0400271static clib_error_t *
272llc_input_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273{
Calvin151fb722016-08-24 10:35:59 -0400274 llc_main_t *lm = &llc_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
276 {
Calvin151fb722016-08-24 10:35:59 -0400277 clib_error_t *error = vlib_call_init_function (vm, llc_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 if (error)
279 clib_error_report (error);
280 }
281
282 llc_setup_node (vm, llc_input_node.index);
283
284 {
285 int i;
286 for (i = 0; i < ARRAY_LEN (lm->input_next_by_protocol); i++)
287 lm->input_next_by_protocol[i] = LLC_INPUT_NEXT_DROP;
288 }
289
290 return 0;
291}
292
293VLIB_INIT_FUNCTION (llc_input_init);
294
295void
296llc_register_input_protocol (vlib_main_t * vm,
Calvin151fb722016-08-24 10:35:59 -0400297 llc_protocol_t protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298{
Calvin151fb722016-08-24 10:35:59 -0400299 llc_main_t *lm = &llc_main;
300 llc_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
302 {
Calvin151fb722016-08-24 10:35:59 -0400303 clib_error_t *error = vlib_call_init_function (vm, llc_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 if (error)
305 clib_error_report (error);
Dave Barachced48e72016-02-08 15:57:35 -0500306 /* Otherwise, osi_input_init will wipe out e.g. the snap init */
307 error = vlib_call_init_function (vm, osi_input_init);
308 if (error)
309 clib_error_report (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 }
311
312 pi = llc_get_protocol_info (lm, protocol);
313 pi->node_index = node_index;
Calvin151fb722016-08-24 10:35:59 -0400314 pi->next_index = vlib_node_add_next (vm, llc_input_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
316 lm->input_next_by_protocol[protocol] = pi->next_index;
317}
Calvin151fb722016-08-24 10:35:59 -0400318
319/*
320 * fd.io coding-style-patch-verification: ON
321 *
322 * Local Variables:
323 * eval: (c-set-style "gnu")
324 * End:
325 */