FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
format.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  * 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 static u8 * format_srp_mode (u8 * s, va_list * args)
45 {
46  u32 mode = va_arg (*args, u32);
47  char * t = 0;
48  switch (mode)
49  {
50 #define _(f) case SRP_MODE_##f: t = #f; break;
52 #undef _
53  default: t = 0; break;
54  }
55  if (t)
56  s = format (s, "%s", t);
57  else
58  s = format (s, "unknown 0x%x", mode);
59 
60  return s;
61 }
62 
63 u8 * format_srp_header_with_length (u8 * s, va_list * args)
64 {
66  u32 max_header_bytes = va_arg (*args, u32);
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, "srp header truncated");
73 
74  indent = format_get_indent (s);
75 
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);
80 
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);
86 
87  if (max_header_bytes != 0 && header_bytes < max_header_bytes)
88  {
90  vlib_node_t * node;
91 
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);
99  }
100 
101  return s;
102 }
103 
104 u8 * format_srp_header (u8 * s, va_list * args)
105 {
106  srp_header_t * m = va_arg (*args, srp_header_t *);
107  return format (s, "%U", format_srp_header_with_length, m, 0);
108 }
109 
110 uword
111 unformat_srp_header (unformat_input_t * input, va_list * args)
112 {
113  u8 ** result = va_arg (*args, u8 **);
115 
116  {
117  void * p;
118  vec_add2 (*result, p, sizeof (h[0]));
119  h = p;
120  }
121 
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))
126  return 0;
127 
128  h->srp.mode = SRP_MODE_data;
130  {
131  u32 x;
132 
133  if (unformat (input, "control"))
134  h->srp.mode = SRP_MODE_control_pass_to_host;
135 
136  else if (unformat (input, "pri %d", &x))
137  h->srp.priority = x;
138 
139  else if (unformat (input, "ttl %d", &x))
140  h->srp.ttl = x;
141 
142  else
143  return 0;
144  }
145 
146  return 1;
147 }
vlib.h
ethernet_type_info_t
Definition: ethernet.h:182
srp.h
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
srp_header_t
Definition: packet.h:66
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
unformat_input_t
struct _unformat_input_t unformat_input_t
ethernet.h
h
h
Definition: flowhash_template.h:372
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
ti
u32 ti
Definition: interface_output.c:405
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
ethernet_main_t_
Definition: ethernet.h:289
format_srp_header_with_length
u8 * format_srp_header_with_length(u8 *s, va_list *args)
Definition: format.c:63
srp_and_ethernet_header_t
Definition: packet.h:95
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
uword
u64 uword
Definition: types.h:112
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
format
__clib_export u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:428
format_srp_mode
static u8 * format_srp_mode(u8 *s, va_list *args)
Definition: format.c:44
format_srp_header
u8 * format_srp_header(u8 *s, va_list *args)
Definition: format.c:104
unformat_ethernet_type_net_byte_order
uword unformat_ethernet_type_net_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:278
ethernet_main
ethernet_main_t ethernet_main
Definition: init.c:45
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u32
unsigned int u32
Definition: types.h:88
ethernet_get_type_info
static ethernet_type_info_t * ethernet_get_type_info(ethernet_main_t *em, ethernet_type_t type)
Definition: ethernet.h:344
vlib_node_t
Definition: node.h:247
u8
unsigned char u8
Definition: types.h:56
ethernet_main_t_::vlib_main
vlib_main_t * vlib_main
Definition: ethernet.h:291
format_ethernet_address
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
foreach_srp_mode
#define foreach_srp_mode
Definition: packet.h:49
unformat_srp_header
uword unformat_srp_header(unformat_input_t *input, va_list *args)
Definition: format.c:111
format_ethernet_type
u8 * format_ethernet_type(u8 *s, va_list *args)
Definition: format.c:64
unformat_ethernet_address
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137