blob: 34c867f1bf59edea2ab14e12b7ea3bedf9609b38 [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 * osi.c: osi 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/osi/osi.h>
42
43/* Global main structure. */
44osi_main_t osi_main;
45
Calvin81b52c52016-08-11 14:10:51 -040046u8 *
47format_osi_protocol (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
49 osi_protocol_t p = va_arg (*args, u32);
Calvin81b52c52016-08-11 14:10:51 -040050 osi_main_t *pm = &osi_main;
51 osi_protocol_info_t *pi = osi_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
Calvin81b52c52016-08-11 14:10:51 -040061u8 *
62format_osi_header_with_length (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Calvin81b52c52016-08-11 14:10:51 -040064 osi_main_t *pm = &osi_main;
65 osi_header_t *h = va_arg (*args, osi_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u32 max_header_bytes = va_arg (*args, u32);
67 osi_protocol_t p = h->protocol;
68 uword indent, header_bytes;
69
70 header_bytes = sizeof (h[0]);
71 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
72 return format (s, "osi header truncated");
73
74 indent = format_get_indent (s);
75
76 s = format (s, "OSI %U", format_osi_protocol, p);
77
78 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
79 {
Calvin81b52c52016-08-11 14:10:51 -040080 osi_protocol_info_t *pi = osi_get_protocol_info (pm, p);
81 vlib_node_t *node = vlib_get_node (pm->vlib_main, pi->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 if (node->format_buffer)
83 s = format (s, "\n%U%U",
84 format_white_space, indent,
85 node->format_buffer, (void *) (h + 1),
86 max_header_bytes - header_bytes);
87 }
88
89 return s;
90}
91
Calvin81b52c52016-08-11 14:10:51 -040092u8 *
93format_osi_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Calvin81b52c52016-08-11 14:10:51 -040095 osi_header_t *h = va_arg (*args, osi_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 return format (s, "%U", format_osi_header_with_length, h, 0);
97}
98
99/* Returns osi protocol as an int in host byte order. */
100uword
101unformat_osi_protocol (unformat_input_t * input, va_list * args)
102{
Calvin81b52c52016-08-11 14:10:51 -0400103 u8 *result = va_arg (*args, u8 *);
104 osi_main_t *pm = &osi_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 int p, i;
106
107 /* Numeric type. */
Calvin81b52c52016-08-11 14:10:51 -0400108 if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 {
110 if (p >= (1 << 8))
111 return 0;
112 *result = p;
113 return 1;
114 }
115
116 /* Named type. */
117 if (unformat_user (input, unformat_vlib_number_by_name,
118 pm->protocol_info_by_name, &i))
119 {
Calvin81b52c52016-08-11 14:10:51 -0400120 osi_protocol_info_t *pi = vec_elt_at_index (pm->protocol_infos, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 *result = pi->protocol;
122 return 1;
123 }
124
125 return 0;
126}
127
128uword
129unformat_osi_header (unformat_input_t * input, va_list * args)
130{
Calvin81b52c52016-08-11 14:10:51 -0400131 u8 **result = va_arg (*args, u8 **);
132 osi_header_t _h, *h = &_h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 u8 p;
134
Calvin81b52c52016-08-11 14:10:51 -0400135 if (!unformat (input, "%U", unformat_osi_protocol, &p))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 return 0;
137
138 h->protocol = p;
139
140 /* Add header to result. */
141 {
Calvin81b52c52016-08-11 14:10:51 -0400142 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 u32 n_bytes = sizeof (h[0]);
144
145 vec_add2 (*result, p, n_bytes);
Damjan Marionf1213b82016-03-13 02:22:06 +0100146 clib_memcpy (p, h, n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 }
Calvin81b52c52016-08-11 14:10:51 -0400148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 return 1;
150}
151
Calvin81b52c52016-08-11 14:10:51 -0400152static void
153add_protocol (osi_main_t * pm, osi_protocol_t protocol, char *protocol_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154{
Calvin81b52c52016-08-11 14:10:51 -0400155 osi_protocol_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 u32 i;
157
158 vec_add2 (pm->protocol_infos, pi, 1);
159 i = pi - pm->protocol_infos;
160
161 pi->name = protocol_name;
162 pi->protocol = protocol;
163 pi->next_index = pi->node_index = ~0;
164
165 hash_set (pm->protocol_info_by_protocol, protocol, i);
166 hash_set_mem (pm->protocol_info_by_name, pi->name, i);
167}
168
Calvin81b52c52016-08-11 14:10:51 -0400169static clib_error_t *
170osi_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171{
Calvin81b52c52016-08-11 14:10:51 -0400172 clib_error_t *error = 0;
173 osi_main_t *pm = &osi_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Nagaprabhanjan Bellaria0005dc2016-03-18 19:10:21 +0530175 /* init order dependency: llc_init -> osi_init */
Calvin81b52c52016-08-11 14:10:51 -0400176 if ((error = vlib_call_init_function (vm, llc_init)))
Dave Barachacf08722016-03-19 08:53:58 -0400177 return error;
Nagaprabhanjan Bellaria0005dc2016-03-18 19:10:21 +0530178
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 memset (pm, 0, sizeof (pm[0]));
180 pm->vlib_main = vm;
181
182 pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
183 pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
184
185#define _(f,n) add_protocol (pm, OSI_PROTOCOL_##f, #f);
186 foreach_osi_protocol;
187#undef _
188
189 return vlib_call_init_function (vm, osi_input_init);
190}
191
192VLIB_INIT_FUNCTION (osi_init);
193
Calvin81b52c52016-08-11 14:10:51 -0400194
195/*
196 * fd.io coding-style-patch-verification: ON
197 *
198 * Local Variables:
199 * eval: (c-set-style "gnu")
200 * End:
201 */