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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * srp_format.c: srp formatting/parsing.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vnet/srp/srp.h>
42 #include <vnet/ethernet/ethernet.h>
44 static u8 * format_srp_mode (u8 * s, va_list * args)
46 u32 mode = va_arg (*args, u32);
50 #define _(f) case SRP_MODE_##f: t = #f; break;
53 default: t = 0; break;
56 s = format (s, "%s", t);
58 s = format (s, "unknown 0x%x", mode);
63 u8 * format_srp_header_with_length (u8 * s, va_list * args)
65 srp_and_ethernet_header_t * h = va_arg (*args, srp_and_ethernet_header_t *);
66 u32 max_header_bytes = va_arg (*args, u32);
67 ethernet_main_t * em = ðernet_main;
68 u32 indent, header_bytes;
70 header_bytes = sizeof (h[0]);
71 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
72 return format (s, "srp header truncated");
74 indent = format_get_indent (s);
76 s = format (s, "mode %U, ring %s, priority %d, ttl %d",
77 format_srp_mode, h->srp.mode,
78 h->srp.is_inner_ring ? "inner" : "outer",
79 h->srp.priority, h->srp.ttl);
81 s = format (s, "\n%U%U: %U -> %U",
82 format_white_space, indent,
83 format_ethernet_type, clib_net_to_host_u16 (h->ethernet.type),
84 format_ethernet_address, h->ethernet.src_address,
85 format_ethernet_address, h->ethernet.dst_address);
87 if (max_header_bytes != 0 && header_bytes < max_header_bytes)
89 ethernet_type_info_t * ti;
92 ti = ethernet_get_type_info (em, h->ethernet.type);
93 node = ti ? vlib_get_node (em->vlib_main, ti->node_index) : 0;
94 if (node && node->format_buffer)
95 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);
104 u8 * format_srp_header (u8 * s, va_list * args)
106 srp_header_t * m = va_arg (*args, srp_header_t *);
107 return format (s, "%U", format_srp_header_with_length, m, 0);
111 unformat_srp_header (unformat_input_t * input, va_list * args)
113 u8 ** result = va_arg (*args, u8 **);
114 srp_and_ethernet_header_t * h;
118 vec_add2 (*result, p, sizeof (h[0]));
122 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))
128 h->srp.mode = SRP_MODE_data;
129 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
133 if (unformat (input, "control"))
134 h->srp.mode = SRP_MODE_control_pass_to_host;
136 else if (unformat (input, "pri %d", &x))
139 else if (unformat (input, "ttl %d", &x))