FD.io VPP  v18.11-rc0-18-g2a3fb1a
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),
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",
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_main_t * vlib_main
Definition: ethernet.h:265
u8 src_address[6]
Definition: packet.h:56
uword unformat_ethernet_type_net_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:265
#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
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define foreach_srp_mode
Definition: packet.h:49
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unsigned char u8
Definition: types.h:56
ethernet_header_t ethernet
Definition: packet.h:97
ethernet_main_t ethernet_main
Definition: init.c:45
u8 dst_address[6]
Definition: packet.h:55
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
srp_header_t srp
Definition: packet.h:96
unsigned int u32
Definition: types.h:88
static u8 * format_srp_mode(u8 *s, va_list *args)
Definition: format.c:44
static ethernet_type_info_t * ethernet_get_type_info(ethernet_main_t *em, ethernet_type_t type)
Definition: ethernet.h:311
struct _unformat_input_t unformat_input_t
format_function_t * format_buffer
Definition: node.h:347
u8 * format_srp_header(u8 *s, va_list *args)
Definition: format.c:104
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
uword unformat_srp_header(unformat_input_t *input, va_list *args)
Definition: format.c:111
u8 * format_srp_header_with_length(u8 *s, va_list *args)
Definition: format.c:63
u64 uword
Definition: types.h:112
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:227
u8 * format_ethernet_type(u8 *s, va_list *args)
Definition: format.c:58
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170