FD.io VPP  v16.06
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 * format_llc_protocol (u8 * s, va_list * args)
47 {
48  llc_protocol_t p = va_arg (*args, u32);
49  llc_main_t * pm = &llc_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_llc_header_with_length (u8 * s, va_list * args)
61 {
62  llc_main_t * pm = &llc_main;
63  llc_header_t * h = va_arg (*args, llc_header_t *);
64  u32 max_header_bytes = va_arg (*args, u32);
65  llc_protocol_t p = h->dst_sap;
66  uword indent, header_bytes;
67 
68  header_bytes = llc_header_length (h);
69  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
70  return format (s, "llc header truncated");
71 
72  indent = format_get_indent (s);
73 
74  s = format (s, "LLC %U -> %U",
77 
78  if (h->control != 0x03)
79  s = format (s, ", control 0x%x", llc_header_get_control (h));
80 
81  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
82  {
84  vlib_node_t * node = vlib_get_node (pm->vlib_main, pi->node_index);
85  if (node->format_buffer)
86  s = format (s, "\n%U%U",
87  format_white_space, indent,
88  node->format_buffer, (void *) (h + 1),
89  max_header_bytes - header_bytes);
90  }
91 
92  return s;
93 }
94 
95 u8 * format_llc_header (u8 * s, va_list * args)
96 {
97  llc_header_t * h = va_arg (*args, llc_header_t *);
98  return format (s, "%U", format_llc_header_with_length, h, 0);
99 }
100 
101 /* Returns llc protocol as an int in host byte order. */
102 uword
103 unformat_llc_protocol (unformat_input_t * input, va_list * args)
104 {
105  u8 * result = va_arg (*args, u8 *);
106  llc_main_t * pm = &llc_main;
107  int p, i;
108 
109  /* Numeric type. */
110  if (unformat (input, "0x%x", &p)
111  || unformat (input, "%d", &p))
112  {
113  if (p >= (1 << 8))
114  return 0;
115  *result = p;
116  return 1;
117  }
118 
119  /* Named type. */
121  pm->protocol_info_by_name, &i))
122  {
124  *result = pi->protocol;
125  return 1;
126  }
127 
128  return 0;
129 }
130 
131 uword
132 unformat_llc_header (unformat_input_t * input, va_list * args)
133 {
134  u8 ** result = va_arg (*args, u8 **);
135  llc_header_t _h, * h = &_h;
136  u8 p;
137 
138  if (! unformat (input, "%U", unformat_llc_protocol, &p))
139  return 0;
140 
141  h->src_sap = h->dst_sap = p;
142  h->control = 0x3;
143 
144  /* Add header to result. */
145  {
146  void * p;
147  u32 n_bytes = sizeof (h[0]);
148 
149  vec_add2 (*result, p, n_bytes);
150  clib_memcpy (p, h, n_bytes);
151  }
152 
153  return 1;
154 }
155 
157  u32 sw_if_index,
158  u32 l3_type,
159  void * dst_address,
160  void * rewrite,
161  uword max_rewrite_bytes)
162 {
163  llc_header_t * h = rewrite;
164  llc_protocol_t protocol;
165 
166  if (max_rewrite_bytes < sizeof (h[0]))
167  return 0;
168 
169  switch (l3_type) {
170 #define _(a,b) case VNET_L3_PACKET_TYPE_##a: protocol = LLC_PROTOCOL_##b; break
171  _ (IP4, ip4);
172 #undef _
173  default:
174  return 0;
175  }
176 
177  h->src_sap = h->dst_sap = protocol;
178  h->control = 0x3;
179 
180  return sizeof (h[0]);
181 }
182 
183 VNET_HW_INTERFACE_CLASS (llc_hw_interface_class) = {
184  .name = "LLC",
185  .format_header = format_llc_header_with_length,
186  .unformat_header = unformat_llc_header,
187  .set_rewrite = llc_set_rewrite,
188 };
189 
190 static void add_protocol (llc_main_t * pm,
191  llc_protocol_t protocol,
192  char * protocol_name)
193 {
194  llc_protocol_info_t * pi;
195  u32 i;
196 
197  vec_add2 (pm->protocol_infos, pi, 1);
198  i = pi - pm->protocol_infos;
199 
200  pi->name = protocol_name;
201  pi->protocol = protocol;
202  pi->next_index = pi->node_index = ~0;
203 
204  hash_set (pm->protocol_info_by_protocol, protocol, i);
206 }
207 
209 {
210  clib_error_t * error;
211  llc_main_t * pm = &llc_main;
212 
213  memset (pm, 0, sizeof (pm[0]));
214  pm->vlib_main = vm;
215 
216  pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
217  pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
218 
219 #define _(f,n) add_protocol (pm, LLC_PROTOCOL_##f, #f);
221 #undef _
222 
223  if ((error = vlib_call_init_function (vm, snap_init)))
224  return error;
225 
227 }
228 
230 
always_inline u16 llc_header_get_control(llc_header_t *h)
Definition: llc.h:92
u8 * format_llc_header_with_length(u8 *s, va_list *args)
Definition: llc.c:60
#define hash_set(h, key, value)
Definition: hash.h:237
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
llc_protocol_info_t * protocol_infos
Definition: llc.h:133
uword * protocol_info_by_protocol
Definition: llc.h:136
always_inline llc_protocol_info_t * llc_get_protocol_info(llc_main_t *m, llc_protocol_t protocol)
Definition: llc.h:143
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
always_inline u8 llc_header_length(llc_header_t *h)
Definition: llc.h:99
VNET_HW_INTERFACE_CLASS(llc_hw_interface_class)
static clib_error_t * llc_input_init(vlib_main_t *vm)
Definition: node.c:264
u8 src_sap
Definition: llc.h:80
uword unformat_llc_header(unformat_input_t *input, va_list *args)
Definition: llc.c:132
#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
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
llc_protocol_t
Definition: llc.h:71
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
#define vlib_call_init_function(vm, x)
Definition: init.h:159
#define hash_create_string(elts, value_bytes)
Definition: hash.h:609
uword * protocol_info_by_name
Definition: llc.h:136
u8 * format_llc_protocol(u8 *s, va_list *args)
Definition: llc.c:46
format_function_t * format_buffer
Definition: node.h:277
static void add_protocol(llc_main_t *pm, llc_protocol_t protocol, char *protocol_name)
Definition: llc.c:190
#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
llc_protocol_t protocol
Definition: llc.h:109
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
vlib_main_t * vlib_main
Definition: llc.h:131
u8 control
Definition: llc.h:85
static clib_error_t * llc_init(vlib_main_t *vm)
Definition: llc.c:208
uword unformat_llc_protocol(unformat_input_t *input, va_list *args)
Definition: llc.c:103
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
u64 uword
Definition: types.h:112
static uword llc_set_rewrite(vnet_main_t *vnm, u32 sw_if_index, u32 l3_type, void *dst_address, void *rewrite, uword max_rewrite_bytes)
Definition: llc.c:156
unsigned char u8
Definition: types.h:56
llc_main_t llc_main
Definition: llc.c:44
Definition: lisp_types.h:24
struct _unformat_input_t unformat_input_t
static clib_error_t * snap_init(vlib_main_t *vm)
Definition: snap.c:173
u8 dst_sap
Definition: llc.h:80
u8 * format_llc_header(u8 *s, va_list *args)
Definition: llc.c:95
char * name
Definition: llc.h:106