FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
hdlc.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  * hdlc.c: hdlc
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/hdlc/hdlc.h>
42 
43 /* Global main structure. */
45 
46 u8 *
47 format_hdlc_protocol (u8 * s, va_list * args)
48 {
49  hdlc_protocol_t p = va_arg (*args, u32);
50  hdlc_main_t *pm = &hdlc_main;
52 
53  if (pi)
54  s = format (s, "%s", pi->name);
55  else
56  s = format (s, "0x%04x", p);
57 
58  return s;
59 }
60 
61 u8 *
62 format_hdlc_header_with_length (u8 * s, va_list * args)
63 {
64  hdlc_main_t *pm = &hdlc_main;
65  hdlc_header_t *h = va_arg (*args, hdlc_header_t *);
66  u32 max_header_bytes = va_arg (*args, u32);
67  hdlc_protocol_t p = clib_net_to_host_u16 (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, "hdlc header truncated");
73 
74  indent = format_get_indent (s);
75 
76  s = format (s, "HDLC %U", format_hdlc_protocol, p);
77 
78  if (h->address != 0xff)
79  s = format (s, ", address 0x%02x", h->address);
80  if (h->control != 0x03)
81  s = format (s, ", control 0x%02x", h->control);
82 
83  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
84  {
86  vlib_node_t *node = vlib_get_node (pm->vlib_main, pi->node_index);
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_hdlc_header (u8 * s, va_list * args)
99 {
100  hdlc_header_t *h = va_arg (*args, hdlc_header_t *);
101  return format (s, "%U", format_hdlc_header_with_length, h, 0);
102 }
103 
104 /* Returns hdlc protocol as an int in host byte order. */
105 uword
107  va_list * args)
108 {
109  u16 *result = va_arg (*args, u16 *);
110  hdlc_main_t *pm = &hdlc_main;
111  int p, i;
112 
113  /* Numeric type. */
114  if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
115  {
116  if (p >= (1 << 16))
117  return 0;
118  *result = p;
119  return 1;
120  }
121 
122  /* Named type. */
124  pm->protocol_info_by_name, &i))
125  {
127  *result = pi->protocol;
128  return 1;
129  }
130 
131  return 0;
132 }
133 
134 uword
136  va_list * args)
137 {
138  u16 *result = va_arg (*args, u16 *);
140  return 0;
141  *result = clib_host_to_net_u16 ((u16) * result);
142  return 1;
143 }
144 
145 uword
146 unformat_hdlc_header (unformat_input_t * input, va_list * args)
147 {
148  u8 **result = va_arg (*args, u8 **);
149  hdlc_header_t _h, *h = &_h;
150  u16 p;
151 
152  if (!unformat (input, "%U", unformat_hdlc_protocol_host_byte_order, &p))
153  return 0;
154 
155  h->address = 0xff;
156  h->control = 0x03;
157  h->protocol = clib_host_to_net_u16 (p);
158 
159  /* Add header to result. */
160  {
161  void *p;
162  u32 n_bytes = sizeof (h[0]);
163 
164  vec_add2 (*result, p, n_bytes);
165  clib_memcpy (p, h, n_bytes);
166  }
167 
168  return 1;
169 }
170 
171 static u8 *
173  u32 sw_if_index,
174  vnet_link_t link_type, const void *dst_address)
175 {
176  hdlc_header_t *h;
177  u8 *rewrite = NULL;
178  hdlc_protocol_t protocol;
179 
180  switch (link_type)
181  {
182 #define _(a,b) case VNET_LINK_##a: protocol = HDLC_PROTOCOL_##b; break
183  _(IP4, ip4);
184  _(IP6, ip6);
185  _(MPLS, mpls_unicast);
186 #undef _
187  default:
188  return (NULL);
189  }
190 
191  vec_validate (rewrite, sizeof (*h) - 1);
192  h = (hdlc_header_t *) rewrite;
193  h->address = 0x0f;
194  h->control = 0x00;
195  h->protocol = clib_host_to_net_u16 (protocol);
196 
197  return (rewrite);
198 }
199 
200 /* *INDENT-OFF* */
202  .name = "HDLC",
203  .format_header = format_hdlc_header_with_length,
204  .unformat_header = unformat_hdlc_header,
205  .build_rewrite = hdlc_build_rewrite,
207 };
208 /* *INDENT-ON* */
209 
210 static void
211 add_protocol (hdlc_main_t * pm, hdlc_protocol_t protocol, char *protocol_name)
212 {
214  u32 i;
215 
216  vec_add2 (pm->protocol_infos, pi, 1);
217  i = pi - pm->protocol_infos;
218 
219  pi->name = protocol_name;
220  pi->protocol = protocol;
221  pi->next_index = pi->node_index = ~0;
222 
223  hash_set (pm->protocol_info_by_protocol, protocol, i);
225 }
226 
227 static clib_error_t *
229 {
230  hdlc_main_t *pm = &hdlc_main;
231 
232  memset (pm, 0, sizeof (pm[0]));
233  pm->vlib_main = vm;
234 
235  pm->protocol_info_by_name = hash_create_string (0, sizeof (uword));
236  pm->protocol_info_by_protocol = hash_create (0, sizeof (uword));
237 
238 #define _(n,s) add_protocol (pm, HDLC_PROTOCOL_##s, #s);
240 #undef _
242 }
243 
245 
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
vnet_hw_interface_class_t hdlc_hw_interface_class
#define hash_set(h, key, value)
Definition: hash.h:255
static hdlc_protocol_info_t * hdlc_get_protocol_info(hdlc_main_t *em, hdlc_protocol_t protocol)
Definition: hdlc.h:83
u8 * format_hdlc_protocol(u8 *s, va_list *args)
Definition: hdlc.c:47
u8 control
Definition: packet.h:65
#define NULL
Definition: clib.h:55
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:562
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define hash_set_mem(h, key, value)
Definition: hash.h:275
static clib_error_t * hdlc_input_init(vlib_main_t *vm)
Definition: node.c:332
u8 * format_hdlc_header(u8 *s, va_list *args)
Definition: hdlc.c:98
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unsigned char u8
Definition: types.h:56
uword unformat_hdlc_protocol_net_byte_order(unformat_input_t *input, va_list *args)
Definition: hdlc.c:135
hdlc_protocol_t
Definition: packet.h:52
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static clib_error_t * hdlc_init(vlib_main_t *vm)
Definition: hdlc.c:228
hdlc_main_t hdlc_main
Definition: hdlc.c:44
unsigned int u32
Definition: types.h:88
#define vlib_call_init_function(vm, x)
Definition: init.h:227
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
uword unformat_hdlc_header(unformat_input_t *input, va_list *args)
Definition: hdlc.c:146
format_function_t * format_buffer
Definition: node.h:347
uword * protocol_info_by_protocol
Definition: hdlc.h:79
u8 * format_hdlc_header_with_length(u8 *s, va_list *args)
Definition: hdlc.c:62
vlib_main_t * vm
Definition: buffer.c:294
vlib_main_t * vlib_main
Definition: hdlc.h:74
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define hash_create(elts, value_bytes)
Definition: hash.h:696
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
uword unformat_vlib_number_by_name(unformat_input_t *input, va_list *args)
Definition: format.c:157
u8 address
Definition: packet.h:62
uword * protocol_info_by_name
Definition: hdlc.h:79
VNET_HW_INTERFACE_CLASS(hdlc_hw_interface_class)
u64 uword
Definition: types.h:112
static void add_protocol(hdlc_main_t *pm, hdlc_protocol_t protocol, char *protocol_name)
Definition: hdlc.c:211
a point 2 point interface
Definition: interface.h:345
Definition: lisp_types.h:37
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
hdlc_protocol_info_t * protocol_infos
Definition: hdlc.h:76
hdlc_protocol_t protocol
Definition: hdlc.h:63
uword unformat_hdlc_protocol_host_byte_order(unformat_input_t *input, va_list *args)
Definition: hdlc.c:106
Definition: lisp_types.h:38
static u8 * hdlc_build_rewrite(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Definition: hdlc.c:172
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u16 protocol
Definition: packet.h:68