Initial commit of vpp code.
[vpp.git] / vnet / vnet / llc / llc.c
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. */
44 llc_main_t llc_main;
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;
50   llc_protocol_info_t * pi = llc_get_protocol_info (pm, p);
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",
75               format_llc_protocol, h->src_sap,
76               format_llc_protocol, h->dst_sap);
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     {
83       llc_protocol_info_t * pi = llc_get_protocol_info (pm, p);
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. */
120   if (unformat_user (input, unformat_vlib_number_by_name,
121                      pm->protocol_info_by_name, &i))
122     {
123       llc_protocol_info_t * pi = vec_elt_at_index (pm->protocol_infos, i);
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     memcpy (p, h, n_bytes);
151   }
152   
153   return 1;
154 }
155
156 static uword llc_set_rewrite (vnet_main_t * vnm,
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);
205   hash_set_mem (pm->protocol_info_by_name, pi->name, i);
206 }
207
208 static clib_error_t * llc_init (vlib_main_t * vm)
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);
220   foreach_llc_protocol;
221 #undef _
222
223   if ((error = vlib_call_init_function (vm, snap_init)))
224     return error;
225
226   return vlib_call_init_function (vm, llc_input_init);
227 }
228
229 VLIB_INIT_FUNCTION (llc_init);
230