FD.io VPP  v16.06
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 * format_osi_protocol (u8 * s, va_list * args)
47 {
48  osi_protocol_t p = va_arg (*args, u32);
49  osi_main_t * pm = &osi_main;
51 
52  if (pi)
53  s = format (s, "%s", pi->name);
54  else
55  s = format (s, "0x%02x", p);
56 
57  return s;
58 }
59 
60 u8 * format_osi_header_with_length (u8 * s, va_list * args)
61 {
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;
67 
68  header_bytes = sizeof (h[0]);
69  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
70  return format (s, "osi header truncated");
71 
72  indent = format_get_indent (s);
73 
74  s = format (s, "OSI %U", format_osi_protocol, p);
75 
76  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
77  {
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);
85  }
86 
87  return s;
88 }
89 
90 u8 * format_osi_header (u8 * s, va_list * args)
91 {
92  osi_header_t * h = va_arg (*args, osi_header_t *);
93  return format (s, "%U", format_osi_header_with_length, h, 0);
94 }
95 
96 /* Returns osi protocol as an int in host byte order. */
97 uword
98 unformat_osi_protocol (unformat_input_t * input, va_list * args)
99 {
100  u8 * result = va_arg (*args, u8 *);
101  osi_main_t * pm = &osi_main;
102  int p, i;
103 
104  /* Numeric type. */
105  if (unformat (input, "0x%x", &p)
106  || unformat (input, "%d", &p))
107  {
108  if (p >= (1 << 8))
109  return 0;
110  *result = p;
111  return 1;
112  }
113 
114  /* Named type. */
116  pm->protocol_info_by_name, &i))
117  {
119  *result = pi->protocol;
120  return 1;
121  }
122 
123  return 0;
124 }
125 
126 uword
127 unformat_osi_header (unformat_input_t * input, va_list * args)
128 {
129  u8 ** result = va_arg (*args, u8 **);
130  osi_header_t _h, * h = &_h;
131  u8 p;
132 
133  if (! unformat (input, "%U", unformat_osi_protocol, &p))
134  return 0;
135 
136  h->protocol = p;
137 
138  /* Add header to result. */
139  {
140  void * p;
141  u32 n_bytes = sizeof (h[0]);
142 
143  vec_add2 (*result, p, n_bytes);
144  clib_memcpy (p, h, n_bytes);
145  }
146 
147  return 1;
148 }
149 
150 static void add_protocol (osi_main_t * pm,
151  osi_protocol_t protocol,
152  char * protocol_name)
153 {
154  osi_protocol_info_t * pi;
155  u32 i;
156 
157  vec_add2 (pm->protocol_infos, pi, 1);
158  i = pi - pm->protocol_infos;
159 
160  pi->name = protocol_name;
161  pi->protocol = protocol;
162  pi->next_index = pi->node_index = ~0;
163 
164  hash_set (pm->protocol_info_by_protocol, protocol, i);
166 }
167 
169 {
170  clib_error_t * error = 0;
171  osi_main_t * pm = &osi_main;
172 
173  /* init order dependency: llc_init -> osi_init */
174  if ((error = vlib_call_init_function(vm, llc_init)))
175  return error;
176 
177  memset (pm, 0, sizeof (pm[0]));
178  pm->vlib_main = vm;
179 
180  pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
181  pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
182 
183 #define _(f,n) add_protocol (pm, OSI_PROTOCOL_##f, #f);
185 #undef _
186 
188 }
189 
191 
u32 next_index
Definition: osi.h:91
u32 node_index
Definition: osi.h:88
#define hash_set(h, key, value)
Definition: hash.h:237
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
u8 protocol
Definition: osi.h:75
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
#define hash_set_mem(h, key, value)
Definition: hash.h:257
osi_protocol_t protocol
Definition: osi.h:85
always_inline osi_protocol_info_t * osi_get_protocol_info(osi_main_t *m, osi_protocol_t protocol)
Definition: osi.h:118
u8 * format_osi_header_with_length(u8 *s, va_list *args)
Definition: osi.c:60
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
char * name
Definition: osi.h:82
u8 * format_osi_protocol(u8 *s, va_list *args)
Definition: osi.c:46
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
uword unformat_osi_protocol(unformat_input_t *input, va_list *args)
Definition: osi.c:98
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
osi_protocol_t
Definition: osi.h:68
#define vlib_call_init_function(vm, x)
Definition: init.h:159
#define hash_create_string(elts, value_bytes)
Definition: hash.h:609
vlib_main_t * vlib_main
Definition: osi.h:106
uword * protocol_info_by_name
Definition: osi.h:111
osi_main_t osi_main
Definition: osi.c:44
osi_protocol_info_t * protocol_infos
Definition: osi.h:108
format_function_t * format_buffer
Definition: node.h:277
uword unformat_osi_header(unformat_input_t *input, va_list *args)
Definition: osi.c:127
static clib_error_t * osi_init(vlib_main_t *vm)
Definition: osi.c:168
#define clib_memcpy(a, b, c)
Definition: string.h:63
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:139
#define hash_create(elts, value_bytes)
Definition: hash.h:615
unsigned int u32
Definition: types.h:88
static clib_error_t * osi_input_init(vlib_main_t *vm)
Definition: node.c:252
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
u8 * format_osi_header(u8 *s, va_list *args)
Definition: osi.c:90
static clib_error_t * llc_init(vlib_main_t *vm)
Definition: llc.c:208
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
struct _unformat_input_t unformat_input_t
static void add_protocol(osi_main_t *pm, osi_protocol_t protocol, char *protocol_name)
Definition: osi.c:150
uword * protocol_info_by_protocol
Definition: osi.h:111