Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 41 | #include <vnet/pg/pg.h> |
| 42 | #include <vnet/llc/llc.h> |
| 43 | |
| 44 | #define foreach_llc_input_next \ |
| 45 | _ (PUNT, "error-punt") \ |
| 46 | _ (DROP, "error-drop") |
| 47 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 48 | typedef enum |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #define _(s,n) LLC_INPUT_NEXT_##s, |
| 51 | foreach_llc_input_next |
| 52 | #undef _ |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 53 | LLC_INPUT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | } llc_input_next_t; |
| 55 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 56 | typedef struct |
| 57 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | u8 packet_data[32]; |
| 59 | } llc_input_trace_t; |
| 60 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 61 | static u8 * |
| 62 | format_llc_input_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | { |
| 64 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 65 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 66 | llc_input_trace_t *t = va_arg (*va, llc_input_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | s = format (s, "%U", format_llc_header, t->packet_data); |
| 69 | |
| 70 | return s; |
| 71 | } |
| 72 | |
| 73 | static uword |
| 74 | llc_input (vlib_main_t * vm, |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 75 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 77 | llc_main_t *lm = &llc_main; |
| 78 | u32 n_left_from, next_index, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | |
| 80 | from = vlib_frame_vector_args (from_frame); |
| 81 | n_left_from = from_frame->n_vectors; |
| 82 | |
| 83 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 84 | vlib_trace_frame_buffers_only (vm, node, |
| 85 | from, |
| 86 | n_left_from, |
| 87 | sizeof (from[0]), |
| 88 | sizeof (llc_input_trace_t)); |
| 89 | |
| 90 | next_index = node->cached_next_index; |
| 91 | |
| 92 | while (n_left_from > 0) |
| 93 | { |
| 94 | u32 n_left_to_next; |
| 95 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 96 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
| 98 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 99 | { |
| 100 | u32 bi0, bi1; |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 101 | vlib_buffer_t *b0, *b1; |
| 102 | llc_header_t *h0, *h1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | u8 next0, next1, len0, len1, enqueue_code; |
| 104 | |
| 105 | /* Prefetch next iteration. */ |
| 106 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 107 | vlib_buffer_t *b2, *b3; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
| 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 | |
Zhiyong Yang | d7d2499 | 2019-05-28 00:57:39 -0400 | [diff] [blame] | 131 | h0 = vlib_buffer_get_current (b0); |
| 132 | h1 = vlib_buffer_get_current (b1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
| 134 | len0 = llc_header_length (h0); |
| 135 | len1 = llc_header_length (h1); |
| 136 | |
Zhiyong Yang | d7d2499 | 2019-05-28 00:57:39 -0400 | [diff] [blame] | 137 | vlib_buffer_advance (b0, len0); |
| 138 | vlib_buffer_advance (b1, len1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
| 140 | next0 = lm->input_next_by_protocol[h0->dst_sap]; |
| 141 | next1 = lm->input_next_by_protocol[h1->dst_sap]; |
| 142 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 143 | b0->error = |
| 144 | node->errors[next0 == |
| 145 | LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL : |
| 146 | LLC_ERROR_NONE]; |
| 147 | b1->error = |
| 148 | node->errors[next1 == |
| 149 | LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL : |
| 150 | LLC_ERROR_NONE]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 152 | enqueue_code = (next0 != next_index) + 2 * (next1 != next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
| 154 | if (PREDICT_FALSE (enqueue_code != 0)) |
| 155 | { |
| 156 | switch (enqueue_code) |
| 157 | { |
| 158 | case 1: |
| 159 | /* A B A */ |
| 160 | to_next[-2] = bi1; |
| 161 | to_next -= 1; |
| 162 | n_left_to_next += 1; |
| 163 | vlib_set_next_frame_buffer (vm, node, next0, bi0); |
| 164 | break; |
| 165 | |
| 166 | case 2: |
| 167 | /* A A B */ |
| 168 | to_next -= 1; |
| 169 | n_left_to_next += 1; |
| 170 | vlib_set_next_frame_buffer (vm, node, next1, bi1); |
| 171 | break; |
| 172 | |
| 173 | case 3: |
| 174 | /* A B B or A B C */ |
| 175 | to_next -= 2; |
| 176 | n_left_to_next += 2; |
| 177 | vlib_set_next_frame_buffer (vm, node, next0, bi0); |
| 178 | vlib_set_next_frame_buffer (vm, node, next1, bi1); |
| 179 | if (next0 == next1) |
| 180 | { |
| 181 | vlib_put_next_frame (vm, node, next_index, |
| 182 | n_left_to_next); |
| 183 | next_index = next1; |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 184 | vlib_get_next_frame (vm, node, next_index, to_next, |
| 185 | n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | while (n_left_from > 0 && n_left_to_next > 0) |
| 192 | { |
| 193 | u32 bi0; |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 194 | vlib_buffer_t *b0; |
| 195 | llc_header_t *h0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | u8 next0, len0; |
| 197 | |
| 198 | bi0 = from[0]; |
| 199 | to_next[0] = bi0; |
| 200 | from += 1; |
| 201 | to_next += 1; |
| 202 | n_left_from -= 1; |
| 203 | n_left_to_next -= 1; |
| 204 | |
| 205 | b0 = vlib_get_buffer (vm, bi0); |
| 206 | |
Zhiyong Yang | d7d2499 | 2019-05-28 00:57:39 -0400 | [diff] [blame] | 207 | h0 = vlib_buffer_get_current (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
| 209 | len0 = llc_header_length (h0); |
| 210 | |
Zhiyong Yang | d7d2499 | 2019-05-28 00:57:39 -0400 | [diff] [blame] | 211 | vlib_buffer_advance (b0, len0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
| 213 | next0 = lm->input_next_by_protocol[h0->dst_sap]; |
| 214 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 215 | b0->error = |
| 216 | node->errors[next0 == |
| 217 | LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL : |
| 218 | LLC_ERROR_NONE]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | |
| 220 | /* Sent packet to wrong next? */ |
| 221 | if (PREDICT_FALSE (next0 != next_index)) |
| 222 | { |
| 223 | /* Return old frame; remove incorrectly enqueued packet. */ |
| 224 | vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1); |
| 225 | |
| 226 | /* Send to correct next. */ |
| 227 | next_index = next0; |
| 228 | vlib_get_next_frame (vm, node, next_index, |
| 229 | to_next, n_left_to_next); |
| 230 | |
| 231 | to_next[0] = bi0; |
| 232 | to_next += 1; |
| 233 | n_left_to_next -= 1; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 238 | } |
| 239 | |
| 240 | return from_frame->n_vectors; |
| 241 | } |
| 242 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 243 | static char *llc_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | #define _(f,s) s, |
| 245 | foreach_llc_error |
| 246 | #undef _ |
| 247 | }; |
| 248 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 249 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | VLIB_REGISTER_NODE (llc_input_node) = { |
| 251 | .function = llc_input, |
| 252 | .name = "llc-input", |
| 253 | /* Takes a vector of packets. */ |
| 254 | .vector_size = sizeof (u32), |
| 255 | |
| 256 | .n_errors = LLC_N_ERROR, |
| 257 | .error_strings = llc_error_strings, |
| 258 | |
| 259 | .n_next_nodes = LLC_INPUT_N_NEXT, |
| 260 | .next_nodes = { |
| 261 | #define _(s,n) [LLC_INPUT_NEXT_##s] = n, |
| 262 | foreach_llc_input_next |
| 263 | #undef _ |
| 264 | }, |
| 265 | |
| 266 | .format_buffer = format_llc_header_with_length, |
| 267 | .format_trace = format_llc_input_trace, |
| 268 | .unformat_buffer = unformat_llc_header, |
| 269 | }; |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 270 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 272 | static clib_error_t * |
| 273 | llc_input_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 275 | llc_main_t *lm = &llc_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
| 277 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 278 | clib_error_t *error = vlib_call_init_function (vm, llc_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | if (error) |
| 280 | clib_error_report (error); |
| 281 | } |
| 282 | |
| 283 | llc_setup_node (vm, llc_input_node.index); |
| 284 | |
| 285 | { |
| 286 | int i; |
| 287 | for (i = 0; i < ARRAY_LEN (lm->input_next_by_protocol); i++) |
| 288 | lm->input_next_by_protocol[i] = LLC_INPUT_NEXT_DROP; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | VLIB_INIT_FUNCTION (llc_input_init); |
| 295 | |
| 296 | void |
| 297 | llc_register_input_protocol (vlib_main_t * vm, |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 298 | llc_protocol_t protocol, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 300 | llc_main_t *lm = &llc_main; |
| 301 | llc_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 302 | |
| 303 | { |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 304 | clib_error_t *error = vlib_call_init_function (vm, llc_input_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | if (error) |
| 306 | clib_error_report (error); |
Dave Barach | ced48e7 | 2016-02-08 15:57:35 -0500 | [diff] [blame] | 307 | /* Otherwise, osi_input_init will wipe out e.g. the snap init */ |
| 308 | error = vlib_call_init_function (vm, osi_input_init); |
| 309 | if (error) |
| 310 | clib_error_report (error); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | pi = llc_get_protocol_info (lm, protocol); |
| 314 | pi->node_index = node_index; |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 315 | pi->next_index = vlib_node_add_next (vm, llc_input_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
| 317 | lm->input_next_by_protocol[protocol] = pi->next_index; |
| 318 | } |
Calvin | 151fb72 | 2016-08-24 10:35:59 -0400 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * fd.io coding-style-patch-verification: ON |
| 322 | * |
| 323 | * Local Variables: |
| 324 | * eval: (c-set-style "gnu") |
| 325 | * End: |
| 326 | */ |