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 | * hdlc.c: hdlc |
| 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/hdlc/hdlc.h> |
| 42 | |
| 43 | /* Global main structure. */ |
| 44 | hdlc_main_t hdlc_main; |
| 45 | |
| 46 | u8 * format_hdlc_protocol (u8 * s, va_list * args) |
| 47 | { |
| 48 | hdlc_protocol_t p = va_arg (*args, u32); |
| 49 | hdlc_main_t * pm = &hdlc_main; |
| 50 | hdlc_protocol_info_t * pi = hdlc_get_protocol_info (pm, p); |
| 51 | |
| 52 | if (pi) |
| 53 | s = format (s, "%s", pi->name); |
| 54 | else |
| 55 | s = format (s, "0x%04x", p); |
| 56 | |
| 57 | return s; |
| 58 | } |
| 59 | |
| 60 | u8 * format_hdlc_header_with_length (u8 * s, va_list * args) |
| 61 | { |
| 62 | hdlc_main_t * pm = &hdlc_main; |
| 63 | hdlc_header_t * h = va_arg (*args, hdlc_header_t *); |
| 64 | u32 max_header_bytes = va_arg (*args, u32); |
| 65 | hdlc_protocol_t p = clib_net_to_host_u16 (h->protocol); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame^] | 66 | u32 indent, header_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | header_bytes = sizeof (h[0]); |
| 69 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 70 | return format (s, "hdlc header truncated"); |
| 71 | |
| 72 | indent = format_get_indent (s); |
| 73 | |
| 74 | s = format (s, "HDLC %U", format_hdlc_protocol, p); |
| 75 | |
| 76 | if (h->address != 0xff) |
| 77 | s = format (s, ", address 0x%02x", h->address); |
| 78 | if (h->control != 0x03) |
| 79 | s = format (s, ", control 0x%02x", h->control); |
| 80 | |
| 81 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 82 | { |
| 83 | hdlc_protocol_info_t * pi = hdlc_get_protocol_info (pm, p); |
| 84 | vlib_node_t * node = vlib_get_node (pm->vlib_main, pi->node_index); |
| 85 | if (node->format_buffer) |
| 86 | s = format (s, "\n%U%U", |
| 87 | format_white_space, indent, |
| 88 | node->format_buffer, (void *) (h + 1), |
| 89 | max_header_bytes - header_bytes); |
| 90 | } |
| 91 | |
| 92 | return s; |
| 93 | } |
| 94 | |
| 95 | u8 * format_hdlc_header (u8 * s, va_list * args) |
| 96 | { |
| 97 | hdlc_header_t * h = va_arg (*args, hdlc_header_t *); |
| 98 | return format (s, "%U", format_hdlc_header_with_length, h, 0); |
| 99 | } |
| 100 | |
| 101 | /* Returns hdlc protocol as an int in host byte order. */ |
| 102 | uword |
| 103 | unformat_hdlc_protocol_host_byte_order (unformat_input_t * input, |
| 104 | va_list * args) |
| 105 | { |
| 106 | u16 * result = va_arg (*args, u16 *); |
| 107 | hdlc_main_t * pm = &hdlc_main; |
| 108 | int p, i; |
| 109 | |
| 110 | /* Numeric type. */ |
| 111 | if (unformat (input, "0x%x", &p) |
| 112 | || unformat (input, "%d", &p)) |
| 113 | { |
| 114 | if (p >= (1 << 16)) |
| 115 | return 0; |
| 116 | *result = p; |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | /* Named type. */ |
| 121 | if (unformat_user (input, unformat_vlib_number_by_name, |
| 122 | pm->protocol_info_by_name, &i)) |
| 123 | { |
| 124 | hdlc_protocol_info_t * pi = vec_elt_at_index (pm->protocol_infos, i); |
| 125 | *result = pi->protocol; |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | uword |
| 133 | unformat_hdlc_protocol_net_byte_order (unformat_input_t * input, |
| 134 | va_list * args) |
| 135 | { |
| 136 | u16 * result = va_arg (*args, u16 *); |
| 137 | if (! unformat_user (input, unformat_hdlc_protocol_host_byte_order, result)) |
| 138 | return 0; |
| 139 | *result = clib_host_to_net_u16 ((u16) *result); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | uword |
| 144 | unformat_hdlc_header (unformat_input_t * input, va_list * args) |
| 145 | { |
| 146 | u8 ** result = va_arg (*args, u8 **); |
| 147 | hdlc_header_t _h, * h = &_h; |
| 148 | u16 p; |
| 149 | |
| 150 | if (! unformat (input, "%U", |
| 151 | unformat_hdlc_protocol_host_byte_order, &p)) |
| 152 | return 0; |
| 153 | |
| 154 | h->address = 0xff; |
| 155 | h->control = 0x03; |
| 156 | h->protocol = clib_host_to_net_u16 (p); |
| 157 | |
| 158 | /* Add header to result. */ |
| 159 | { |
| 160 | void * p; |
| 161 | u32 n_bytes = sizeof (h[0]); |
| 162 | |
| 163 | vec_add2 (*result, p, n_bytes); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 164 | clib_memcpy (p, h, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return 1; |
| 168 | } |
| 169 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 170 | static u8* |
| 171 | hdlc_build_rewrite (vnet_main_t * vnm, |
| 172 | u32 sw_if_index, |
| 173 | vnet_link_t link_type, |
| 174 | const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 176 | hdlc_header_t * h; |
| 177 | u8* rewrite = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | hdlc_protocol_t protocol; |
| 179 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 180 | switch (link_type) { |
| 181 | #define _(a,b) case VNET_LINK_##a: protocol = HDLC_PROTOCOL_##b; break |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | _ (IP4, ip4); |
| 183 | _ (IP6, ip6); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 184 | _ (MPLS, mpls_unicast); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | #undef _ |
| 186 | default: |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 187 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 190 | vec_validate(rewrite, sizeof(*h)-1); |
| 191 | h = (hdlc_header_t *)rewrite; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | h->address = 0x0f; |
| 193 | h->control = 0x00; |
| 194 | h->protocol = clib_host_to_net_u16 (protocol); |
| 195 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 196 | return (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = { |
| 200 | .name = "HDLC", |
| 201 | .format_header = format_hdlc_header_with_length, |
| 202 | .unformat_header = unformat_hdlc_header, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 203 | .build_rewrite = hdlc_build_rewrite, |
| 204 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static void add_protocol (hdlc_main_t * pm, |
| 208 | hdlc_protocol_t protocol, |
| 209 | char * protocol_name) |
| 210 | { |
| 211 | hdlc_protocol_info_t * pi; |
| 212 | u32 i; |
| 213 | |
| 214 | vec_add2 (pm->protocol_infos, pi, 1); |
| 215 | i = pi - pm->protocol_infos; |
| 216 | |
| 217 | pi->name = protocol_name; |
| 218 | pi->protocol = protocol; |
| 219 | pi->next_index = pi->node_index = ~0; |
| 220 | |
| 221 | hash_set (pm->protocol_info_by_protocol, protocol, i); |
| 222 | hash_set_mem (pm->protocol_info_by_name, pi->name, i); |
| 223 | } |
| 224 | |
| 225 | static clib_error_t * hdlc_init (vlib_main_t * vm) |
| 226 | { |
| 227 | hdlc_main_t * pm = &hdlc_main; |
| 228 | |
| 229 | memset (pm, 0, sizeof (pm[0])); |
| 230 | pm->vlib_main = vm; |
| 231 | |
| 232 | pm->protocol_info_by_name = hash_create_string (0, sizeof (uword)); |
| 233 | pm->protocol_info_by_protocol = hash_create (0, sizeof (uword)); |
| 234 | |
| 235 | #define _(n,s) add_protocol (pm, HDLC_PROTOCOL_##s, #s); |
| 236 | foreach_hdlc_protocol |
| 237 | #undef _ |
| 238 | |
| 239 | return vlib_call_init_function (vm, hdlc_input_init); |
| 240 | } |
| 241 | |
| 242 | VLIB_INIT_FUNCTION (hdlc_init); |
| 243 | |
| 244 | hdlc_main_t * hdlc_get_main (vlib_main_t * vm) |
| 245 | { |
| 246 | vlib_call_init_function (vm, hdlc_init); |
| 247 | return &hdlc_main; |
| 248 | } |
| 249 | |