Line data Source code
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 : * srp_format.c: srp formatting/parsing.
17 : *
18 : * Copyright (c) 2008 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 <vlib/vlib.h>
41 : #include <vnet/srp/srp.h>
42 : #include <vnet/ethernet/ethernet.h>
43 :
44 0 : static u8 * format_srp_mode (u8 * s, va_list * args)
45 : {
46 0 : u32 mode = va_arg (*args, u32);
47 0 : char * t = 0;
48 0 : switch (mode)
49 : {
50 : #define _(f) case SRP_MODE_##f: t = #f; break;
51 0 : foreach_srp_mode
52 : #undef _
53 0 : default: t = 0; break;
54 : }
55 0 : if (t)
56 0 : s = format (s, "%s", t);
57 : else
58 0 : s = format (s, "unknown 0x%x", mode);
59 :
60 0 : return s;
61 : }
62 :
63 0 : u8 * format_srp_header_with_length (u8 * s, va_list * args)
64 : {
65 0 : srp_and_ethernet_header_t * h = va_arg (*args, srp_and_ethernet_header_t *);
66 0 : u32 max_header_bytes = va_arg (*args, u32);
67 0 : ethernet_main_t * em = ðernet_main;
68 : u32 indent, header_bytes;
69 :
70 0 : header_bytes = sizeof (h[0]);
71 0 : if (max_header_bytes != 0 && header_bytes > max_header_bytes)
72 0 : return format (s, "srp header truncated");
73 :
74 0 : indent = format_get_indent (s);
75 :
76 0 : s = format (s, "mode %U, ring %s, priority %d, ttl %d",
77 0 : format_srp_mode, h->srp.mode,
78 0 : h->srp.is_inner_ring ? "inner" : "outer",
79 0 : h->srp.priority, h->srp.ttl);
80 :
81 0 : s = format (s, "\n%U%U: %U -> %U",
82 : format_white_space, indent,
83 0 : format_ethernet_type, clib_net_to_host_u16 (h->ethernet.type),
84 0 : format_ethernet_address, h->ethernet.src_address,
85 0 : format_ethernet_address, h->ethernet.dst_address);
86 :
87 0 : if (max_header_bytes != 0 && header_bytes < max_header_bytes)
88 : {
89 : ethernet_type_info_t * ti;
90 : vlib_node_t * node;
91 :
92 0 : ti = ethernet_get_type_info (em, h->ethernet.type);
93 0 : node = ti ? vlib_get_node (em->vlib_main, ti->node_index) : 0;
94 0 : if (node && node->format_buffer)
95 0 : s = format (s, "\n%U%U",
96 : format_white_space, indent,
97 : node->format_buffer, (void *) h + header_bytes,
98 : max_header_bytes - header_bytes);
99 : }
100 :
101 0 : return s;
102 : }
103 :
104 0 : u8 * format_srp_header (u8 * s, va_list * args)
105 : {
106 0 : srp_header_t * m = va_arg (*args, srp_header_t *);
107 0 : return format (s, "%U", format_srp_header_with_length, m, 0);
108 : }
109 :
110 : uword
111 0 : unformat_srp_header (unformat_input_t * input, va_list * args)
112 : {
113 0 : u8 ** result = va_arg (*args, u8 **);
114 : srp_and_ethernet_header_t * h;
115 :
116 : {
117 : void * p;
118 0 : vec_add2 (*result, p, sizeof (h[0]));
119 0 : h = p;
120 : }
121 :
122 0 : if (! unformat (input, "%U: %U -> %U",
123 : unformat_ethernet_type_net_byte_order, &h->ethernet.type,
124 : unformat_ethernet_address, &h->ethernet.src_address,
125 : unformat_ethernet_address, &h->ethernet.dst_address))
126 0 : return 0;
127 :
128 0 : h->srp.mode = SRP_MODE_data;
129 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
130 : {
131 : u32 x;
132 :
133 0 : if (unformat (input, "control"))
134 0 : h->srp.mode = SRP_MODE_control_pass_to_host;
135 :
136 0 : else if (unformat (input, "pri %d", &x))
137 0 : h->srp.priority = x;
138 :
139 0 : else if (unformat (input, "ttl %d", &x))
140 0 : h->srp.ttl = x;
141 :
142 : else
143 0 : return 0;
144 : }
145 :
146 0 : return 1;
147 : }
|