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/osi/osi.h>
43 /* Global main structure. */
46 u8 * format_osi_protocol (u8 * s, va_list * args)
48 osi_protocol_t p = va_arg (*args, u32);
49 osi_main_t * pm = &osi_main;
50 osi_protocol_info_t * pi = osi_get_protocol_info (pm, p);
53 s = format (s, "%s", pi->name);
55 s = format (s, "0x%02x", p);
60 u8 * format_osi_header_with_length (u8 * s, va_list * args)
62 osi_main_t * pm = &osi_main;
63 osi_header_t * h = va_arg (*args, osi_header_t *);
64 u32 max_header_bytes = va_arg (*args, u32);
65 osi_protocol_t p = h->protocol;
66 uword indent, header_bytes;
68 header_bytes = sizeof (h[0]);
69 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
70 return format (s, "osi header truncated");
72 indent = format_get_indent (s);
74 s = format (s, "OSI %U", format_osi_protocol, p);
76 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
78 osi_protocol_info_t * pi = osi_get_protocol_info (pm, p);
79 vlib_node_t * node = vlib_get_node (pm->vlib_main, pi->node_index);
80 if (node->format_buffer)
81 s = format (s, "\n%U%U",
82 format_white_space, indent,
83 node->format_buffer, (void *) (h + 1),
84 max_header_bytes - header_bytes);
90 u8 * format_osi_header (u8 * s, va_list * args)
92 osi_header_t * h = va_arg (*args, osi_header_t *);
93 return format (s, "%U", format_osi_header_with_length, h, 0);
96 /* Returns osi protocol as an int in host byte order. */
98 unformat_osi_protocol (unformat_input_t * input, va_list * args)
100 u8 * result = va_arg (*args, u8 *);
101 osi_main_t * pm = &osi_main;
105 if (unformat (input, "0x%x", &p)
106 || unformat (input, "%d", &p))
115 if (unformat_user (input, unformat_vlib_number_by_name,
116 pm->protocol_info_by_name, &i))
118 osi_protocol_info_t * pi = vec_elt_at_index (pm->protocol_infos, i);
119 *result = pi->protocol;
127 unformat_osi_header (unformat_input_t * input, va_list * args)
129 u8 ** result = va_arg (*args, u8 **);
130 osi_header_t _h, * h = &_h;
133 if (! unformat (input, "%U", unformat_osi_protocol, &p))
138 /* Add header to result. */
141 u32 n_bytes = sizeof (h[0]);
143 vec_add2 (*result, p, n_bytes);
144 memcpy (p, h, n_bytes);
150 static void add_protocol (osi_main_t * pm,
151 osi_protocol_t protocol,
152 char * protocol_name)
154 osi_protocol_info_t * pi;
157 vec_add2 (pm->protocol_infos, pi, 1);
158 i = pi - pm->protocol_infos;
160 pi->name = protocol_name;
161 pi->protocol = protocol;
162 pi->next_index = pi->node_index = ~0;
164 hash_set (pm->protocol_info_by_protocol, protocol, i);
165 hash_set_mem (pm->protocol_info_by_name, pi->name, i);
168 static clib_error_t * osi_init (vlib_main_t * vm)
170 osi_main_t * pm = &osi_main;
172 memset (pm, 0, sizeof (pm[0]));
175 pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
176 pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
178 #define _(f,n) add_protocol (pm, OSI_PROTOCOL_##f, #f);
179 foreach_osi_protocol;
182 return vlib_call_init_function (vm, osi_input_init);
185 VLIB_INIT_FUNCTION (osi_init);