FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
rewrite.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  * rewrite.c: packet rewrite
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 <vnet/vnet.h>
41 #include <vnet/ip/lookup.h>
42 
43 u8 *
44 format_vnet_rewrite_flags (u8 *s, va_list *ap)
45 {
46  vnet_rewrite_flags_t f = va_arg (*ap, int);
47 
49  s = format (s, "features ");
51  s = format (s, "fixup-ip4o4 ");
53  s = format (s, "fixup-flow-hash ");
54 
55  return (s);
56 }
57 
58 u8 *
59 format_vnet_rewrite (u8 * s, va_list * args)
60 {
61  vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
62  u32 max_data_bytes = va_arg (*args, u32);
63  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
64  vnet_main_t *vnm = vnet_get_main ();
65 
66  ASSERT (rw->data_bytes <= max_data_bytes);
67 
68  if (rw->sw_if_index != ~0)
69  {
72  if (NULL != si)
73  s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si);
74  else
75  s = format (s, "DELETED:%d", rw->sw_if_index);
76  }
77 
78  s = format (s, " mtu:%d next:%d", rw->max_l3_packet_bytes, rw->next_index);
79  s = format (s, " flags:[%U]", format_vnet_rewrite_flags, rw->flags);
80 
81  /* Format rewrite string. */
82  if (rw->data_bytes > 0)
83  s = format (s, " %U", format_hex_bytes, rw->data, rw->data_bytes);
84 
85  return s;
86 }
87 
88 u32
90 {
92  return (hw->output_node_index);
93 }
94 
95 void
98  vnet_link_t linkt,
99  u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
100 {
101  rw->sw_if_index = sw_if_index;
102  rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
103  rw->max_l3_packet_bytes =
105 }
106 
107 void
110 {
111  rw->max_l3_packet_bytes =
113  vnet_link_to_mtu (linkt));
114 }
115 
116 void
118  vnet_link_t link_type,
120  u32 node_index,
121  void *dst_address,
123  u32 max_rewrite_bytes)
124 {
125 
129  u8 *rewrite = NULL;
130 
131  vnet_rewrite_init (vnm, sw_if_index, link_type, node_index,
133  rw);
134 
135  ASSERT (hc->build_rewrite);
136  rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
137 
138  ASSERT (vec_len (rewrite) < max_rewrite_bytes);
139  vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
140  vec_len (rewrite));
141  vec_free (rewrite);
142 }
143 
144 void
146 {
147  vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
148  u32 max_data_bytes = va_arg (*va, u32);
149  u8 *p;
150 
151  serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
152  serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
154  sizeof (rw->max_l3_packet_bytes));
155  p = serialize_get (m, rw->data_bytes);
156  clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
157  rw->data_bytes);
158 }
159 
160 void
162 {
163  vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
164  u32 max_data_bytes = va_arg (*va, u32);
165  u8 *p;
166 
167  /* It is up to user to fill these in. */
168  rw->next_index = ~0;
169 
170  unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
171  unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
173  sizeof (rw->max_l3_packet_bytes));
174  p = unserialize_get (m, rw->data_bytes);
175  clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
176  rw->data_bytes);
177 }
178 
179 u8 *
182  vnet_link_t link_type,
183  const void *dst_address)
184 {
188 
189  ASSERT (hc->build_rewrite);
190  return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
191 }
192 
193 
194 void
196  u32 sw_if_index, u32 ai)
197 {
201 
202  ASSERT (hc->update_adjacency);
203  hc->update_adjacency (vnm, sw_if_index, ai);
204 }
205 
206 /*
207  * fd.io coding-style-patch-verification: ON
208  *
209  * Local Variables:
210  * eval: (c-set-style "gnu")
211  * End:
212  */
vnet_rewrite_header_t_::max_l3_packet_bytes
u16 max_l3_packet_bytes
Definition: rewrite.h:85
serialize_integer
static void serialize_integer(serialize_main_t *m, u64 x, u32 n_bytes)
Definition: serialize.h:185
vnet_sw_interface_t
Definition: interface.h:868
vlib_node_add_next
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1177
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
f
vlib_frame_t * f
Definition: interface_output.c:1080
vnet_rewrite_for_sw_interface
void vnet_rewrite_for_sw_interface(vnet_main_t *vnm, vnet_link_t link_type, u32 sw_if_index, u32 node_index, void *dst_address, vnet_rewrite_header_t *rw, u32 max_rewrite_bytes)
Deprecated.
Definition: rewrite.c:117
vnet_get_sw_interface_or_null
static vnet_sw_interface_t * vnet_get_sw_interface_or_null(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:64
serialize_vnet_rewrite
void serialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:145
format_hex_bytes
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
VNET_REWRITE_FIXUP_FLOW_HASH
@ VNET_REWRITE_FIXUP_FLOW_HASH
this adj performs the flow hash fixup
Definition: rewrite.h:67
vnet_update_adjacency_for_sw_interface
void vnet_update_adjacency_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, u32 ai)
Definition: rewrite.c:195
format_vnet_rewrite_flags
u8 * format_vnet_rewrite_flags(u8 *s, va_list *ap)
Definition: rewrite.c:44
VNET_REWRITE_HAS_FEATURES
@ VNET_REWRITE_HAS_FEATURES
This adjacency/interface has output features configured.
Definition: rewrite.h:57
node_index
node node_index
Definition: interface_output.c:420
unserialize_get
static void * unserialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:171
serialize_main_t
Definition: serialize.h:144
vnet_get_hw_interface_class
static vnet_hw_interface_class_t * vnet_get_hw_interface_class(vnet_main_t *vnm, u32 hw_class_index)
Definition: interface_funcs.h:117
vnet_hw_interface_t::hw_class_index
u32 hw_class_index
Definition: interface.h:663
vnet_build_rewrite_for_sw_interface
u8 * vnet_build_rewrite_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address)
Definition: rewrite.c:180
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
unserialize_integer
static void unserialize_integer(serialize_main_t *m, void *x, u32 n_bytes)
Definition: serialize.h:201
vnet_rewrite_header_t_::next_index
u16 next_index
Definition: rewrite.h:78
unserialize_vnet_rewrite
void unserialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:161
vnet_rewrite_header_t_::data
u8 data[0]
Definition: rewrite.h:97
vnet_rewrite_get_data_internal
static void * vnet_rewrite_get_data_internal(vnet_rewrite_header_t *rw, int max_size)
Definition: rewrite.h:165
vnet_main_t::vlib_main
vlib_main_t * vlib_main
Definition: vnet.h:111
vnet_sw_interface_get_mtu
static u32 vnet_sw_interface_get_mtu(vnet_main_t *vnm, u32 sw_if_index, vnet_mtu_t af)
Definition: interface_funcs.h:313
vnet_rewrite_set_data_internal
static void vnet_rewrite_set_data_internal(vnet_rewrite_header_t *rw, int max_size, void *data, int data_bytes)
Definition: rewrite.h:146
vnet_rewrite_header_t_::flags
vnet_rewrite_flags_t flags
Definition: rewrite.h:88
format_vnet_rewrite
u8 * format_vnet_rewrite(u8 *s, va_list *args)
Definition: rewrite.c:59
VNET_REWRITE_FIXUP_IP4_O_4
@ VNET_REWRITE_FIXUP_IP4_O_4
this adj performs IP4 over IP4 fixup
Definition: rewrite.h:62
vnet_rewrite_header_t_
Definition: rewrite.h:72
vnet_rewrite_init
void vnet_rewrite_init(vnet_main_t *vnm, u32 sw_if_index, vnet_link_t linkt, u32 this_node, u32 next_node, vnet_rewrite_header_t *rw)
Definition: rewrite.c:96
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vnet_tx_node_index_for_sw_interface
u32 vnet_tx_node_index_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: rewrite.c:89
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
vnet_get_sup_hw_interface
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:92
si
vnet_sw_interface_t * si
Definition: interface_output.c:398
vnet_link_t
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
u8
unsigned char u8
Definition: types.h:56
vnet_hw_interface_t::output_node_index
u32 output_node_index
Definition: interface.h:653
lookup.h
vnet_rewrite_flags_t
enum vnet_rewrite_flags_t_ vnet_rewrite_flags_t
Flags associated with the rewrite/adjacency.
vnet_link_to_mtu
vnet_mtu_t vnet_link_to_mtu(vnet_link_t link)
Definition: interface.c:1689
vnet_rewrite_header_t_::data_bytes
u16 data_bytes
Definition: rewrite.h:81
vnet.h
serialize_get
static void * serialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:178
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
rewrite
rewrite
Definition: pnat.api:158
vnet_rewrite_header_t_::sw_if_index
u32 sw_if_index
Definition: rewrite.h:75
vnet_hw_interface_class_t
struct _vnet_hw_interface_class vnet_hw_interface_class_t
format_vnet_sw_interface_name
format_function_t format_vnet_sw_interface_name
Definition: interface_funcs.h:453
vnet_rewrite_update_mtu
void vnet_rewrite_update_mtu(vnet_main_t *vnm, vnet_link_t linkt, vnet_rewrite_header_t *rw)
Definition: rewrite.c:108