blob: 706d478853e62ada04fff22da389c70779814de3 [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.c: llc 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/llc/llc.h>
42
43/* Global main structure. */
44llc_main_t llc_main;
45
Calvin151fb722016-08-24 10:35:59 -040046u8 *
47format_llc_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
49 llc_protocol_t p = va_arg (*args, u32);
Calvin151fb722016-08-24 10:35:59 -040050 llc_main_t *pm = &llc_main;
51 llc_protocol_info_t *pi = llc_get_protocol_info (pm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
53 if (pi)
54 s = format (s, "%s", pi->name);
55 else
56 s = format (s, "0x%02x", p);
57
58 return s;
59}
60
Calvin151fb722016-08-24 10:35:59 -040061u8 *
62format_llc_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Calvin151fb722016-08-24 10:35:59 -040064 llc_main_t *pm = &llc_main;
65 llc_header_t *h = va_arg (*args, llc_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u32 max_header_bytes = va_arg (*args, u32);
67 llc_protocol_t p = h->dst_sap;
Christophe Fontained3c008d2017-10-02 18:10:54 +020068 u32 indent, header_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
70 header_bytes = llc_header_length (h);
71 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
72 return format (s, "llc header truncated");
73
74 indent = format_get_indent (s);
75
76 s = format (s, "LLC %U -> %U",
77 format_llc_protocol, h->src_sap,
78 format_llc_protocol, h->dst_sap);
79
80 if (h->control != 0x03)
81 s = format (s, ", control 0x%x", llc_header_get_control (h));
82
83 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
84 {
Calvin151fb722016-08-24 10:35:59 -040085 llc_protocol_info_t *pi = llc_get_protocol_info (pm, p);
86 vlib_node_t *node = vlib_get_node (pm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 if (node->format_buffer)
88 s = format (s, "\n%U%U",
89 format_white_space, indent,
90 node->format_buffer, (void *) (h + 1),
91 max_header_bytes - header_bytes);
92 }
93
94 return s;
95}
96
Calvin151fb722016-08-24 10:35:59 -040097u8 *
98format_llc_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Calvin151fb722016-08-24 10:35:59 -0400100 llc_header_t *h = va_arg (*args, llc_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 return format (s, "%U", format_llc_header_with_length, h, 0);
102}
103
104/* Returns llc protocol as an int in host byte order. */
105uword
106unformat_llc_protocol (unformat_input_t * input, va_list * args)
107{
Calvin151fb722016-08-24 10:35:59 -0400108 u8 *result = va_arg (*args, u8 *);
109 llc_main_t *pm = &llc_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 int p, i;
111
112 /* Numeric type. */
Calvin151fb722016-08-24 10:35:59 -0400113 if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 {
115 if (p >= (1 << 8))
116 return 0;
117 *result = p;
118 return 1;
119 }
120
121 /* Named type. */
122 if (unformat_user (input, unformat_vlib_number_by_name,
123 pm->protocol_info_by_name, &i))
124 {
Calvin151fb722016-08-24 10:35:59 -0400125 llc_protocol_info_t *pi = vec_elt_at_index (pm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 *result = pi->protocol;
127 return 1;
128 }
129
130 return 0;
131}
132
133uword
134unformat_llc_header (unformat_input_t * input, va_list * args)
135{
Calvin151fb722016-08-24 10:35:59 -0400136 u8 **result = va_arg (*args, u8 **);
137 llc_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 u8 p;
139
Calvin151fb722016-08-24 10:35:59 -0400140 if (!unformat (input, "%U", unformat_llc_protocol, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 return 0;
142
143 h->src_sap = h->dst_sap = p;
144 h->control = 0x3;
145
146 /* Add header to result. */
147 {
Calvin151fb722016-08-24 10:35:59 -0400148 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 u32 n_bytes = sizeof (h[0]);
150
151 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100152 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 }
Calvin151fb722016-08-24 10:35:59 -0400154
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 return 1;
156}
157
Neale Rannsb80c5362016-10-08 13:03:40 +0100158static u8 *
159llc_build_rewrite (vnet_main_t * vnm,
160 u32 sw_if_index,
161 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
Neale Rannsb80c5362016-10-08 13:03:40 +0100163 llc_header_t *h;
164 u8 *rewrite = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 llc_protocol_t protocol;
166
Neale Rannsb80c5362016-10-08 13:03:40 +0100167 switch (link_type)
Calvin151fb722016-08-24 10:35:59 -0400168 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100169#define _(a,b) case VNET_LINK_##a: protocol = LLC_PROTOCOL_##b; break
Calvin151fb722016-08-24 10:35:59 -0400170 _(IP4, ip4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171#undef _
Calvin151fb722016-08-24 10:35:59 -0400172 default:
Neale Rannsb80c5362016-10-08 13:03:40 +0100173 return (NULL);
Calvin151fb722016-08-24 10:35:59 -0400174 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Neale Rannsb80c5362016-10-08 13:03:40 +0100176 vec_validate (rewrite, sizeof (*h) - 1);
177 h = (llc_header_t *) rewrite;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 h->src_sap = h->dst_sap = protocol;
179 h->control = 0x3;
Calvin151fb722016-08-24 10:35:59 -0400180
Neale Rannsb80c5362016-10-08 13:03:40 +0100181 return (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182}
183
Calvin151fb722016-08-24 10:35:59 -0400184/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185VNET_HW_INTERFACE_CLASS (llc_hw_interface_class) = {
186 .name = "LLC",
187 .format_header = format_llc_header_with_length,
188 .unformat_header = unformat_llc_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100189 .build_rewrite = llc_build_rewrite,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190};
Calvin151fb722016-08-24 10:35:59 -0400191/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Calvin151fb722016-08-24 10:35:59 -0400193static void
194add_protocol (llc_main_t * pm, llc_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195{
Calvin151fb722016-08-24 10:35:59 -0400196 llc_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 u32 i;
198
199 vec_add2 (pm->protocol_infos, pi, 1);
200 i = pi - pm->protocol_infos;
201
202 pi->name = protocol_name;
203 pi->protocol = protocol;
204 pi->next_index = pi->node_index = ~0;
205
206 hash_set (pm->protocol_info_by_protocol, protocol, i);
207 hash_set_mem (pm->protocol_info_by_name, pi->name, i);
208}
209
Calvin151fb722016-08-24 10:35:59 -0400210static clib_error_t *
211llc_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212{
Calvin151fb722016-08-24 10:35:59 -0400213 clib_error_t *error;
214 llc_main_t *pm = &llc_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
216 memset (pm, 0, sizeof (pm[0]));
217 pm->vlib_main = vm;
218
219 pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
220 pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
221
222#define _(f,n) add_protocol (pm, LLC_PROTOCOL_##f, #f);
223 foreach_llc_protocol;
224#undef _
225
226 if ((error = vlib_call_init_function (vm, snap_init)))
227 return error;
228
229 return vlib_call_init_function (vm, llc_input_init);
230}
231
232VLIB_INIT_FUNCTION (llc_init);
233
Calvin151fb722016-08-24 10:35:59 -0400234
235/*
236 * fd.io coding-style-patch-verification: ON
237 *
238 * Local Variables:
239 * eval: (c-set-style "gnu")
240 * End:
241 */