blob: 884ff32444bef95ef7399b414d0bf8876012a973 [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 * snap_node.c: snap 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>
41#include <vnet/pg/pg.h>
42#include <vnet/llc/llc.h>
43#include <vnet/snap/snap.h>
44
Calvin Ferencecc6ca882016-08-25 10:04:09 -040045typedef enum
46{
Ed Warnickecb9cada2015-12-08 15:45:58 -070047 SNAP_INPUT_NEXT_DROP,
48 SNAP_INPUT_NEXT_PUNT,
49 SNAP_INPUT_NEXT_ETHERNET_TYPE,
50 SNAP_INPUT_N_NEXT,
51} snap_input_next_t;
52
Calvin Ferencecc6ca882016-08-25 10:04:09 -040053typedef struct
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 u8 packet_data[32];
56} snap_input_trace_t;
57
Calvin Ferencecc6ca882016-08-25 10:04:09 -040058static u8 *
59format_snap_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
62 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Calvin Ferencecc6ca882016-08-25 10:04:09 -040063 snap_input_trace_t *t = va_arg (*va, snap_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
65 s = format (s, "%U", format_snap_header, t->packet_data);
66
67 return s;
68}
69
70static uword
71snap_input (vlib_main_t * vm,
Calvin Ferencecc6ca882016-08-25 10:04:09 -040072 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Calvin Ferencecc6ca882016-08-25 10:04:09 -040074 snap_main_t *sm = &snap_main;
75 u32 n_left_from, next_index, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 from = vlib_frame_vector_args (from_frame);
78 n_left_from = from_frame->n_vectors;
79
80 if (node->flags & VLIB_NODE_FLAG_TRACE)
81 vlib_trace_frame_buffers_only (vm, node,
82 from,
83 n_left_from,
84 sizeof (from[0]),
85 sizeof (snap_input_trace_t));
86
87 next_index = node->cached_next_index;
88
89 while (n_left_from > 0)
90 {
91 u32 n_left_to_next;
92
Calvin Ferencecc6ca882016-08-25 10:04:09 -040093 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 while (n_left_from >= 4 && n_left_to_next >= 2)
96 {
97 u32 bi0, bi1;
Calvin Ferencecc6ca882016-08-25 10:04:09 -040098 vlib_buffer_t *b0, *b1;
99 snap_header_t *h0, *h1;
100 snap_protocol_info_t *pi0, *pi1;
101 u8 next0, next1, is_ethernet0, is_ethernet1, len0, len1,
102 enqueue_code;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 u32 oui0, oui1;
104
105 /* Prefetch next iteration. */
106 {
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400107 vlib_buffer_t *b2, *b3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 b2 = vlib_get_buffer (vm, from[2]);
110 b3 = vlib_get_buffer (vm, from[3]);
111
112 vlib_prefetch_buffer_header (b2, LOAD);
113 vlib_prefetch_buffer_header (b3, LOAD);
114
115 CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
116 CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
117 }
118
119 bi0 = from[0];
120 bi1 = from[1];
121 to_next[0] = bi0;
122 to_next[1] = bi1;
123 from += 2;
124 to_next += 2;
125 n_left_to_next -= 2;
126 n_left_from -= 2;
127
128 b0 = vlib_get_buffer (vm, bi0);
129 b1 = vlib_get_buffer (vm, bi1);
130
131 h0 = (void *) (b0->data + b0->current_data);
132 h1 = (void *) (b1->data + b1->current_data);
133
134 oui0 = snap_header_get_oui (h0);
135 oui1 = snap_header_get_oui (h1);
136
137 is_ethernet0 = oui0 == IEEE_OUI_ethernet;
138 is_ethernet1 = oui1 == IEEE_OUI_ethernet;
139
140 len0 = sizeof (h0[0]) - (is_ethernet0 ? sizeof (h0->protocol) : 0);
141 len1 = sizeof (h1[0]) - (is_ethernet1 ? sizeof (h1->protocol) : 0);
142
143 b0->current_data += len0;
144 b1->current_data += len1;
145
146 b0->current_length -= len0;
147 b1->current_length -= len1;
148
149 pi0 = snap_get_protocol_info (sm, h0);
150 pi1 = snap_get_protocol_info (sm, h1);
151
152 next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
153 next1 = pi1 ? pi1->next_index : SNAP_INPUT_NEXT_DROP;
154
155 next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
156 next1 = is_ethernet1 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next1;
157
158 /* In case of error. */
159 b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
160 b1->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
161
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400162 enqueue_code = (next0 != next_index) + 2 * (next1 != next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
164 if (PREDICT_FALSE (enqueue_code != 0))
165 {
166 switch (enqueue_code)
167 {
168 case 1:
169 /* A B A */
170 to_next[-2] = bi1;
171 to_next -= 1;
172 n_left_to_next += 1;
173 vlib_set_next_frame_buffer (vm, node, next0, bi0);
174 break;
175
176 case 2:
177 /* A A B */
178 to_next -= 1;
179 n_left_to_next += 1;
180 vlib_set_next_frame_buffer (vm, node, next1, bi1);
181 break;
182
183 case 3:
184 /* A B B or A B C */
185 to_next -= 2;
186 n_left_to_next += 2;
187 vlib_set_next_frame_buffer (vm, node, next0, bi0);
188 vlib_set_next_frame_buffer (vm, node, next1, bi1);
189 if (next0 == next1)
190 {
191 vlib_put_next_frame (vm, node, next_index,
192 n_left_to_next);
193 next_index = next1;
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400194 vlib_get_next_frame (vm, node, next_index, to_next,
195 n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 }
197 }
198 }
199 }
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400200
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 while (n_left_from > 0 && n_left_to_next > 0)
202 {
203 u32 bi0;
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400204 vlib_buffer_t *b0;
205 snap_header_t *h0;
206 snap_protocol_info_t *pi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 u8 next0, is_ethernet0, len0;
208 u32 oui0;
209
210 bi0 = from[0];
211 to_next[0] = bi0;
212 from += 1;
213 to_next += 1;
214 n_left_from -= 1;
215 n_left_to_next -= 1;
216
217 b0 = vlib_get_buffer (vm, bi0);
218
219 h0 = (void *) (b0->data + b0->current_data);
220
221 oui0 = snap_header_get_oui (h0);
222
223 is_ethernet0 = oui0 == IEEE_OUI_ethernet;
224
225 len0 = sizeof (h0[0]) - (is_ethernet0 ? sizeof (h0->protocol) : 0);
226
227 b0->current_data += len0;
228
229 b0->current_length -= len0;
230
231 pi0 = snap_get_protocol_info (sm, h0);
232
233 next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
234
235 next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
236
237 /* In case of error. */
238 b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
239
240 /* Sent packet to wrong next? */
241 if (PREDICT_FALSE (next0 != next_index))
242 {
243 /* Return old frame; remove incorrectly enqueued packet. */
244 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
245
246 /* Send to correct next. */
247 next_index = next0;
248 vlib_get_next_frame (vm, node, next_index,
249 to_next, n_left_to_next);
250
251 to_next[0] = bi0;
252 to_next += 1;
253 n_left_to_next -= 1;
254 }
255 }
256
257 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
258 }
259
260 return from_frame->n_vectors;
261}
262
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400263static char *snap_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264#define _(f,s) s,
265 foreach_snap_error
266#undef _
267};
268
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400269/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270VLIB_REGISTER_NODE (snap_input_node) = {
271 .function = snap_input,
272 .name = "snap-input",
273 /* Takes a vector of packets. */
274 .vector_size = sizeof (u32),
275
276 .n_errors = SNAP_N_ERROR,
277 .error_strings = snap_error_strings,
278
279 .n_next_nodes = SNAP_INPUT_N_NEXT,
280 .next_nodes = {
281 [SNAP_INPUT_NEXT_DROP] = "error-drop",
282 [SNAP_INPUT_NEXT_PUNT] = "error-punt",
283 [SNAP_INPUT_NEXT_ETHERNET_TYPE] = "ethernet-input-type",
284 },
285
286 .format_buffer = format_snap_header_with_length,
287 .format_trace = format_snap_input_trace,
288 .unformat_buffer = unformat_snap_header,
289};
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400290/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400292static clib_error_t *
293snap_input_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294{
295 {
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400296 clib_error_t *error = vlib_call_init_function (vm, snap_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 if (error)
298 clib_error_report (error);
299 }
300
301 snap_setup_node (vm, snap_input_node.index);
302
303 llc_register_input_protocol (vm, LLC_PROTOCOL_snap, snap_input_node.index);
304
305 return 0;
306}
307
308VLIB_INIT_FUNCTION (snap_input_init);
309
310void
311snap_register_input_protocol (vlib_main_t * vm,
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400312 char *name,
313 u32 ieee_oui, u16 protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314{
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400315 snap_main_t *sm = &snap_main;
316 snap_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 snap_header_t h;
318 snap_oui_and_protocol_t key;
319
320 {
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400321 clib_error_t *error = vlib_call_init_function (vm, snap_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 if (error)
323 clib_error_report (error);
324 }
325
326 h.protocol = clib_host_to_net_u16 (protocol);
327 h.oui[0] = (ieee_oui >> 16) & 0xff;
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400328 h.oui[1] = (ieee_oui >> 8) & 0xff;
329 h.oui[2] = (ieee_oui >> 0) & 0xff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 pi = snap_get_protocol_info (sm, &h);
331 if (pi)
332 return;
333
334 vec_add2 (sm->protocols, pi, 1);
335
336 pi->name = format (0, "%s", name);
337 pi->node_index = node_index;
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400338 pi->next_index = vlib_node_add_next (vm, snap_input_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339
340 key.oui = ieee_oui;
341 key.protocol = clib_host_to_net_u16 (protocol);
342
343 mhash_set (&sm->protocol_hash, &key, pi - sm->protocols, /* old_value */ 0);
344 hash_set_mem (sm->protocol_info_by_name, name, pi - sm->protocols);
345}
Calvin Ferencecc6ca882016-08-25 10:04:09 -0400346
347/*
348 * fd.io coding-style-patch-verification: ON
349 *
350 * Local Variables:
351 * eval: (c-set-style "gnu")
352 * End:
353 */