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 | * snap.c: snap support |
| 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 <vnet/vnet.h> |
| 41 | #include <vnet/snap/snap.h> |
| 42 | #include <vnet/ethernet/ethernet.h> |
| 43 | |
| 44 | /* Global main structure. */ |
| 45 | snap_main_t snap_main; |
| 46 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 47 | static u8 * |
| 48 | format_cisco_snap_protocol (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 50 | snap_header_t *h = va_arg (*args, snap_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u16 protocol = clib_net_to_host_u16 (h->protocol); |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 52 | char *t = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | switch (protocol) |
| 54 | { |
| 55 | #define _(n,f) case n: t = #f; break; |
| 56 | foreach_snap_cisco_protocol; |
| 57 | #undef _ |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 58 | default: |
| 59 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } |
| 61 | if (t) |
| 62 | return format (s, "%s", t); |
| 63 | else |
| 64 | return format (s, "unknown 0x%x", protocol); |
| 65 | } |
| 66 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 67 | u8 * |
| 68 | format_snap_protocol (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 70 | snap_header_t *h = va_arg (*args, snap_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | u32 oui = snap_header_get_oui (h); |
| 72 | u16 protocol = clib_net_to_host_u16 (h->protocol); |
| 73 | |
| 74 | switch (oui) |
| 75 | { |
| 76 | case IEEE_OUI_ethernet: |
| 77 | return format (s, "ethernet %U", format_ethernet_type, h->protocol); |
| 78 | |
| 79 | case IEEE_OUI_cisco: |
| 80 | return format (s, "cisco %U", format_cisco_snap_protocol, h); |
| 81 | |
| 82 | default: |
| 83 | return format (s, "oui 0x%06x 0x%04x", oui, protocol); |
| 84 | } |
| 85 | } |
| 86 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 87 | u8 * |
| 88 | format_snap_header_with_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 90 | snap_main_t *sm = &snap_main; |
| 91 | snap_header_t *h = va_arg (*args, snap_header_t *); |
| 92 | snap_protocol_info_t *pi = snap_get_protocol_info (sm, h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | u32 max_header_bytes = va_arg (*args, u32); |
| 94 | uword indent, header_bytes; |
| 95 | |
| 96 | header_bytes = sizeof (h[0]); |
| 97 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 98 | return format (s, "snap header truncated"); |
| 99 | |
| 100 | indent = format_get_indent (s); |
| 101 | |
| 102 | s = format (s, "SNAP %U", format_snap_protocol, h); |
| 103 | |
| 104 | if (max_header_bytes != 0 && header_bytes > max_header_bytes && pi != 0) |
| 105 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 106 | vlib_node_t *node = vlib_get_node (sm->vlib_main, pi->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | if (node->format_buffer) |
| 108 | s = format (s, "\n%U%U", |
| 109 | format_white_space, indent, |
| 110 | node->format_buffer, (void *) (h + 1), |
| 111 | max_header_bytes - header_bytes); |
| 112 | } |
| 113 | |
| 114 | return s; |
| 115 | } |
| 116 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 117 | u8 * |
| 118 | format_snap_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 120 | snap_header_t *h = va_arg (*args, snap_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | return format (s, "%U", format_snap_header_with_length, h, 0); |
| 122 | } |
| 123 | |
| 124 | /* Returns snap protocol as an int in host byte order. */ |
| 125 | uword |
| 126 | unformat_snap_protocol (unformat_input_t * input, va_list * args) |
| 127 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 128 | snap_header_t *result = va_arg (*args, snap_header_t *); |
| 129 | snap_main_t *sm = &snap_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | snap_oui_and_protocol_t p; |
| 131 | u32 i; |
| 132 | |
| 133 | /* Numeric type. */ |
| 134 | if (unformat (input, "0x%x 0x%x", &p.oui, &p.protocol)) |
| 135 | { |
| 136 | if (p.oui >= (1 << 24)) |
| 137 | return 0; |
| 138 | if (p.protocol >= (1 << 16)) |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Named type. */ |
| 143 | else if (unformat_user (input, unformat_vlib_number_by_name, |
| 144 | sm->protocol_info_by_name, &i)) |
| 145 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 146 | snap_protocol_info_t *pi = vec_elt_at_index (sm->protocols, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | p = pi->oui_and_protocol; |
| 148 | } |
| 149 | |
| 150 | else |
| 151 | return 0; |
| 152 | |
| 153 | snap_header_set_protocol (result, &p); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | uword |
| 158 | unformat_snap_header (unformat_input_t * input, va_list * args) |
| 159 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 160 | u8 **result = va_arg (*args, u8 **); |
| 161 | snap_header_t _h, *h = &_h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 163 | if (!unformat (input, "%U", unformat_snap_protocol, h)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | |
| 166 | /* Add header to result. */ |
| 167 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 168 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | u32 n_bytes = sizeof (h[0]); |
| 170 | |
| 171 | vec_add2 (*result, p, n_bytes); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 172 | clib_memcpy (p, h, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | } |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 174 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | return 1; |
| 176 | } |
| 177 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 178 | static clib_error_t * |
| 179 | snap_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | { |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 181 | snap_main_t *sm = &snap_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
| 183 | memset (sm, 0, sizeof (sm[0])); |
| 184 | sm->vlib_main = vm; |
| 185 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 186 | mhash_init (&sm->protocol_hash, sizeof (uword), |
| 187 | sizeof (snap_oui_and_protocol_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
| 189 | sm->protocol_info_by_name |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 190 | = hash_create_string ( /* elts */ 0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
| 192 | return vlib_call_init_function (vm, snap_input_init); |
| 193 | } |
| 194 | |
| 195 | VLIB_INIT_FUNCTION (snap_init); |
| 196 | |
Calvin Ference | cc6ca88 | 2016-08-25 10:04:09 -0400 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * fd.io coding-style-patch-verification: ON |
| 200 | * |
| 201 | * Local Variables: |
| 202 | * eval: (c-set-style "gnu") |
| 203 | * End: |
| 204 | */ |