FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
osi.c
Go to the documentation of this file.
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  * 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. */
45 
46 u8 *
47 format_osi_protocol (u8 * s, va_list * args)
48 {
49  osi_protocol_t p = va_arg (*args, u32);
50  osi_main_t *pm = &osi_main;
52 
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 
61 u8 *
62 format_osi_header_with_length (u8 * s, va_list * args)
63 {
64  osi_main_t *pm = &osi_main;
65  osi_header_t *h = va_arg (*args, osi_header_t *);
66  u32 max_header_bytes = va_arg (*args, u32);
67  osi_protocol_t p = h->protocol;
68  u32 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  {
82  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 
92 u8 *
93 format_osi_header (u8 * s, va_list * args)
94 {
95  osi_header_t *h = va_arg (*args, osi_header_t *);
96  return format (s, "%U", format_osi_header_with_length, h, 0);
97 }
98 
99 /* Returns osi protocol as an int in host byte order. */
100 uword
101 unformat_osi_protocol (unformat_input_t * input, va_list * args)
102 {
103  u8 *result = va_arg (*args, u8 *);
104  osi_main_t *pm = &osi_main;
105  int p, i;
106 
107  /* Numeric type. */
108  if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
109  {
110  if (p >= (1 << 8))
111  return 0;
112  *result = p;
113  return 1;
114  }
115 
116  /* Named type. */
118  pm->protocol_info_by_name, &i))
119  {
121  *result = pi->protocol;
122  return 1;
123  }
124 
125  return 0;
126 }
127 
128 uword
129 unformat_osi_header (unformat_input_t * input, va_list * args)
130 {
131  u8 **result = va_arg (*args, u8 **);
132  osi_header_t _h, *h = &_h;
133  u8 p;
134 
135  if (!unformat (input, "%U", unformat_osi_protocol, &p))
136  return 0;
137 
138  h->protocol = p;
139 
140  /* Add header to result. */
141  {
142  void *p;
143  u32 n_bytes = sizeof (h[0]);
144 
145  vec_add2 (*result, p, n_bytes);
146  clib_memcpy (p, h, n_bytes);
147  }
148 
149  return 1;
150 }
151 
152 static void
153 add_protocol (osi_main_t * pm, osi_protocol_t protocol, char *protocol_name)
154 {
156  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 
167 }
168 
169 static clib_error_t *
171 {
172  clib_error_t *error = 0;
173  osi_main_t *pm = &osi_main;
174 
175  /* init order dependency: llc_init -> osi_init */
177  return error;
178 
179  clib_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);
187 #undef _
188 
190 }
191 
193 
194 
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
osi_protocol_info_t::node_index
u32 node_index
Definition: osi.h:91
osi_protocol_t
osi_protocol_t
Definition: osi.h:68
osi_protocol_info_t::next_index
u32 next_index
Definition: osi.h:94
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
osi_main_t::protocol_info_by_name
uword * protocol_info_by_name
Definition: osi.h:116
osi_main_t::protocol_info_by_protocol
uword * protocol_info_by_protocol
Definition: osi.h:116
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
osi_protocol_info_t::protocol
osi_protocol_t protocol
Definition: osi.h:88
osi_protocol_info_t
Definition: osi.h:82
osi_get_protocol_info
static osi_protocol_info_t * osi_get_protocol_info(osi_main_t *m, osi_protocol_t protocol)
Definition: osi.h:123
vlib_call_init_function
#define vlib_call_init_function(vm, x)
Definition: init.h:259
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
hash_create_string
#define hash_create_string(elts, value_bytes)
Definition: hash.h:689
unformat_input_t
struct _unformat_input_t unformat_input_t
hash_create
#define hash_create(elts, value_bytes)
Definition: hash.h:695
osi_protocol_info_t::name
char * name
Definition: osi.h:85
h
h
Definition: flowhash_template.h:372
error
Definition: cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
hash_set
#define hash_set(h, key, value)
Definition: hash.h:255
osi.h
unformat_osi_header
uword unformat_osi_header(unformat_input_t *input, va_list *args)
Definition: osi.c:129
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
osi_main_t::vlib_main
vlib_main_t * vlib_main
Definition: osi.h:111
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
osi_header_t
Definition: osi.h:75
osi_main_t::protocol_infos
osi_protocol_info_t * protocol_infos
Definition: osi.h:113
format_osi_header_with_length
u8 * format_osi_header_with_length(u8 *s, va_list *args)
Definition: osi.c:62
uword
u64 uword
Definition: types.h:112
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
unformat_osi_protocol
uword unformat_osi_protocol(unformat_input_t *input, va_list *args)
Definition: osi.c:101
osi_main_t
Definition: osi.h:109
format
description fragment has unexpected format
Definition: map.api:433
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
osi_main
osi_main_t osi_main
Definition: osi.c:44
llc_init
static clib_error_t * llc_init(vlib_main_t *vm)
Definition: llc.c:211
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
n_bytes
u32 n_bytes
Definition: interface_output.c:401
osi_init
static clib_error_t * osi_init(vlib_main_t *vm)
Definition: osi.c:170
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
foreach_osi_protocol
@ foreach_osi_protocol
Definition: osi.h:71
osi_input_init
static clib_error_t * osi_input_init(vlib_main_t *vm)
Definition: node.c:266
vnet.h
unformat_vlib_number_by_name
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
format_osi_header
u8 * format_osi_header(u8 *s, va_list *args)
Definition: osi.c:93
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
add_protocol
static void add_protocol(osi_main_t *pm, osi_protocol_t protocol, char *protocol_name)
Definition: osi.c:153
format_osi_protocol
u8 * format_osi_protocol(u8 *s, va_list *args)
Definition: osi.c:47