FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
llc.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  * llc.c: llc 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/llc/llc.h>
42 
43 /* Global main structure. */
45 
46 u8 *
47 format_llc_protocol (u8 * s, va_list * args)
48 {
49  llc_protocol_t p = va_arg (*args, u32);
50  llc_main_t *pm = &llc_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_llc_header_with_length (u8 * s, va_list * args)
63 {
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  u32 indent, header_bytes;
69 
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");
73 
74  indent = format_get_indent (s);
75 
76  s = format (s, "LLC %U -> %U",
77  format_llc_protocol, h->src_sap,
78  format_llc_protocol, h->dst_sap);
79 
80  if (h->control != 0x03)
81  s = format (s, ", control 0x%x", llc_header_get_control (h));
82 
83  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
84  {
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);
92  }
93 
94  return s;
95 }
96 
97 u8 *
98 format_llc_header (u8 * s, va_list * args)
99 {
100  llc_header_t *h = va_arg (*args, llc_header_t *);
101  return format (s, "%U", format_llc_header_with_length, h, 0);
102 }
103 
104 /* Returns llc protocol as an int in host byte order. */
105 uword
106 unformat_llc_protocol (unformat_input_t * input, va_list * args)
107 {
108  u8 *result = va_arg (*args, u8 *);
109  llc_main_t *pm = &llc_main;
110  int p, i;
111 
112  /* Numeric type. */
113  if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
114  {
115  if (p >= (1 << 8))
116  return 0;
117  *result = p;
118  return 1;
119  }
120 
121  /* Named type. */
123  pm->protocol_info_by_name, &i))
124  {
126  *result = pi->protocol;
127  return 1;
128  }
129 
130  return 0;
131 }
132 
133 uword
134 unformat_llc_header (unformat_input_t * input, va_list * args)
135 {
136  u8 **result = va_arg (*args, u8 **);
137  llc_header_t _h, *h = &_h;
138  u8 p;
139 
140  if (!unformat (input, "%U", unformat_llc_protocol, &p))
141  return 0;
142 
143  h->src_sap = h->dst_sap = p;
144  h->control = 0x3;
145 
146  /* Add header to result. */
147  {
148  void *p;
149  u32 n_bytes = sizeof (h[0]);
150 
151  vec_add2 (*result, p, n_bytes);
152  clib_memcpy (p, h, n_bytes);
153  }
154 
155  return 1;
156 }
157 
158 static u8 *
161  vnet_link_t link_type, const void *dst_address)
162 {
163  llc_header_t *h;
164  u8 *rewrite = NULL;
166 
167  switch (link_type)
168  {
169 #define _(a,b) case VNET_LINK_##a: protocol = LLC_PROTOCOL_##b; break
170  _(IP4, ip4);
171 #undef _
172  default:
173  return (NULL);
174  }
175 
176  vec_validate (rewrite, sizeof (*h) - 1);
177  h = (llc_header_t *) rewrite;
178  h->src_sap = h->dst_sap = protocol;
179  h->control = 0x3;
180 
181  return (rewrite);
182 }
183 
184 /* *INDENT-OFF* */
185 VNET_HW_INTERFACE_CLASS (llc_hw_interface_class) = {
186  .name = "LLC",
187  .format_header = format_llc_header_with_length,
188  .unformat_header = unformat_llc_header,
189  .build_rewrite = llc_build_rewrite,
190 };
191 /* *INDENT-ON* */
192 
193 static void
194 add_protocol (llc_main_t * pm, llc_protocol_t protocol, char *protocol_name)
195 {
197  u32 i;
198 
199  vec_add2 (pm->protocol_infos, pi, 1);
200  i = pi - pm->protocol_infos;
201 
202  pi->name = protocol_name;
203  pi->protocol = protocol;
204  pi->next_index = pi->node_index = ~0;
205 
208 }
209 
210 static clib_error_t *
212 {
214  llc_main_t *pm = &llc_main;
215 
216  clib_memset (pm, 0, sizeof (pm[0]));
217  pm->vlib_main = vm;
218 
219  pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
220  pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
221 
222 #define _(f,n) add_protocol (pm, LLC_PROTOCOL_##f, #f);
224 #undef _
225 
227  return error;
228 
230 }
231 
233 
234 
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */
llc_header_length
static u8 llc_header_length(llc_header_t *h)
Definition: llc.h:101
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
llc_main_t
Definition: llc.h:134
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
llc_header_get_control
static u16 llc_header_get_control(llc_header_t *h)
Definition: llc.h:94
llc_main_t::protocol_infos
llc_protocol_info_t * protocol_infos
Definition: llc.h:138
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
llc_get_protocol_info
static llc_protocol_info_t * llc_get_protocol_info(llc_main_t *m, llc_protocol_t protocol)
Definition: llc.h:148
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
snap_init
static clib_error_t * snap_init(vlib_main_t *vm)
Definition: snap.c:179
unformat_input_t
struct _unformat_input_t unformat_input_t
format_llc_header_with_length
u8 * format_llc_header_with_length(u8 *s, va_list *args)
Definition: llc.c:62
hash_create
#define hash_create(elts, value_bytes)
Definition: hash.h:695
VNET_HW_INTERFACE_CLASS
VNET_HW_INTERFACE_CLASS(llc_hw_interface_class)
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
llc_protocol_info_t
Definition: llc.h:106
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
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
llc_protocol_info_t::node_index
u32 node_index
Definition: llc.h:115
llc_header_t
Definition: llc.h:78
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
foreach_llc_protocol
@ foreach_llc_protocol
Definition: llc.h:74
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
format_llc_protocol
u8 * format_llc_protocol(u8 *s, va_list *args)
Definition: llc.c:47
llc_input_init
static clib_error_t * llc_input_init(vlib_main_t *vm)
Definition: node.c:272
vnet_main_t
Definition: vnet.h:76
format_llc_header
u8 * format_llc_header(u8 *s, va_list *args)
Definition: llc.c:98
format
description fragment has unexpected format
Definition: map.api:433
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
llc_main_t::vlib_main
vlib_main_t * vlib_main
Definition: llc.h:136
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
unformat_llc_protocol
uword unformat_llc_protocol(unformat_input_t *input, va_list *args)
Definition: llc.c:106
add_protocol
static void add_protocol(llc_main_t *pm, llc_protocol_t protocol, char *protocol_name)
Definition: llc.c:194
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
n_bytes
u32 n_bytes
Definition: interface_output.c:401
llc_main_t::protocol_info_by_protocol
uword * protocol_info_by_protocol
Definition: llc.h:141
llc_main
llc_main_t llc_main
Definition: llc.c:44
llc_protocol_info_t::protocol
llc_protocol_t protocol
Definition: llc.h:112
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vnet_link_t
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
vlib_node_t
Definition: node.h:247
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
llc.h
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
llc_build_rewrite
static u8 * llc_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Definition: llc.c:159
vnet.h
llc_main_t::protocol_info_by_name
uword * protocol_info_by_name
Definition: llc.h:141
llc_protocol_info_t::name
char * name
Definition: llc.h:109
unformat_vlib_number_by_name
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
rewrite
rewrite
Definition: pnat.api:158
unformat_llc_header
uword unformat_llc_header(unformat_input_t *input, va_list *args)
Definition: llc.c:134
llc_protocol_info_t::next_index
u32 next_index
Definition: llc.h:118
llc_protocol_t
llc_protocol_t
Definition: llc.h:71
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129