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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright (c) 2010 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vnet/llc/llc.h>
43 /* Global main structure. */
47 format_llc_protocol (u8 * s, va_list * args)
49 llc_protocol_t p = va_arg (*args, u32);
50 llc_main_t *pm = &llc_main;
51 llc_protocol_info_t *pi = llc_get_protocol_info (pm, p);
54 s = format (s, "%s", pi->name);
56 s = format (s, "0x%02x", p);
62 format_llc_header_with_length (u8 * s, va_list * args)
64 llc_main_t *pm = &llc_main;
65 llc_header_t *h = va_arg (*args, llc_header_t *);
66 u32 max_header_bytes = va_arg (*args, u32);
67 llc_protocol_t p = h->dst_sap;
68 uword indent, header_bytes;
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");
74 indent = format_get_indent (s);
76 s = format (s, "LLC %U -> %U",
77 format_llc_protocol, h->src_sap,
78 format_llc_protocol, h->dst_sap);
80 if (h->control != 0x03)
81 s = format (s, ", control 0x%x", llc_header_get_control (h));
83 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
85 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);
87 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);
98 format_llc_header (u8 * s, va_list * args)
100 llc_header_t *h = va_arg (*args, llc_header_t *);
101 return format (s, "%U", format_llc_header_with_length, h, 0);
104 /* Returns llc protocol as an int in host byte order. */
106 unformat_llc_protocol (unformat_input_t * input, va_list * args)
108 u8 *result = va_arg (*args, u8 *);
109 llc_main_t *pm = &llc_main;
113 if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
122 if (unformat_user (input, unformat_vlib_number_by_name,
123 pm->protocol_info_by_name, &i))
125 llc_protocol_info_t *pi = vec_elt_at_index (pm->protocol_infos, i);
126 *result = pi->protocol;
134 unformat_llc_header (unformat_input_t * input, va_list * args)
136 u8 **result = va_arg (*args, u8 **);
137 llc_header_t _h, *h = &_h;
140 if (!unformat (input, "%U", unformat_llc_protocol, &p))
143 h->src_sap = h->dst_sap = p;
146 /* Add header to result. */
149 u32 n_bytes = sizeof (h[0]);
151 vec_add2 (*result, p, n_bytes);
152 clib_memcpy (p, h, n_bytes);
159 llc_build_rewrite (vnet_main_t * vnm,
161 vnet_link_t link_type, const void *dst_address)
165 llc_protocol_t protocol;
169 #define _(a,b) case VNET_LINK_##a: protocol = LLC_PROTOCOL_##b; break
176 vec_validate (rewrite, sizeof (*h) - 1);
177 h = (llc_header_t *) rewrite;
178 h->src_sap = h->dst_sap = protocol;
185 VNET_HW_INTERFACE_CLASS (llc_hw_interface_class) = {
187 .format_header = format_llc_header_with_length,
188 .unformat_header = unformat_llc_header,
189 .build_rewrite = llc_build_rewrite,
194 add_protocol (llc_main_t * pm, llc_protocol_t protocol, char *protocol_name)
196 llc_protocol_info_t *pi;
199 vec_add2 (pm->protocol_infos, pi, 1);
200 i = pi - pm->protocol_infos;
202 pi->name = protocol_name;
203 pi->protocol = protocol;
204 pi->next_index = pi->node_index = ~0;
206 hash_set (pm->protocol_info_by_protocol, protocol, i);
207 hash_set_mem (pm->protocol_info_by_name, pi->name, i);
210 static clib_error_t *
211 llc_init (vlib_main_t * vm)
214 llc_main_t *pm = &llc_main;
216 memset (pm, 0, sizeof (pm[0]));
219 pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
220 pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
222 #define _(f,n) add_protocol (pm, LLC_PROTOCOL_##f, #f);
223 foreach_llc_protocol;
226 if ((error = vlib_call_init_function (vm, snap_init)))
229 return vlib_call_init_function (vm, llc_input_init);
232 VLIB_INIT_FUNCTION (llc_init);
236 * fd.io coding-style-patch-verification: ON
239 * eval: (c-set-style "gnu")