FD.io VPP  v20.05.1-6-gf53edbc3b
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 (u8 * s, va_list * args)
45 {
46  vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
47  u32 max_data_bytes = va_arg (*args, u32);
48  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
49  vnet_main_t *vnm = vnet_get_main ();
50 
51  ASSERT (rw->data_bytes <= max_data_bytes);
52 
53  if (rw->sw_if_index != ~0)
54  {
57  if (NULL != si)
58  s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si);
59  else
60  s = format (s, "DELETED:%d", rw->sw_if_index);
61  }
62 
63  s = format (s, " mtu:%d next:%d", rw->max_l3_packet_bytes, rw->next_index);
64 
65  /* Format rewrite string. */
66  if (rw->data_bytes > 0)
67  s = format (s, " %U", format_hex_bytes, rw->data, rw->data_bytes);
68 
69  return s;
70 }
71 
72 u32
74 {
75  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
76  return (hw->output_node_index);
77 }
78 
79 void
82  vnet_link_t linkt,
83  u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
84 {
86  rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
88  vnet_sw_interface_get_mtu (vnm, sw_if_index, vnet_link_to_mtu (linkt));
89 }
90 
91 void
94 {
97  vnet_link_to_mtu (linkt));
98 }
99 
100 void
102  vnet_link_t link_type,
104  u32 node_index,
105  void *dst_address,
107  u32 max_rewrite_bytes)
108 {
109 
110  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
113  u8 *rewrite = NULL;
114 
115  vnet_rewrite_init (vnm, sw_if_index, link_type, node_index,
116  vnet_tx_node_index_for_sw_interface (vnm, sw_if_index),
117  rw);
118 
119  ASSERT (hc->build_rewrite);
120  rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
121 
122  ASSERT (vec_len (rewrite) < max_rewrite_bytes);
123  vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
124  vec_len (rewrite));
125  vec_free (rewrite);
126 }
127 
128 void
130 {
131  vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
132  u32 max_data_bytes = va_arg (*va, u32);
133  u8 *p;
134 
135  serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
136  serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
138  sizeof (rw->max_l3_packet_bytes));
139  p = serialize_get (m, rw->data_bytes);
140  clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
141  rw->data_bytes);
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  /* It is up to user to fill these in. */
152  rw->next_index = ~0;
153 
154  unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
155  unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
157  sizeof (rw->max_l3_packet_bytes));
158  p = unserialize_get (m, rw->data_bytes);
159  clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
160  rw->data_bytes);
161 }
162 
163 u8 *
166  vnet_link_t link_type,
167  const void *dst_address)
168 {
169  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
172 
173  ASSERT (hc->build_rewrite);
174  return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
175 }
176 
177 
178 void
180  u32 sw_if_index, u32 ai)
181 {
182  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
185 
186  ASSERT (hc->update_adjacency);
187  hc->update_adjacency (vnm, sw_if_index, ai);
188 }
189 
190 /*
191  * fd.io coding-style-patch-verification: ON
192  *
193  * Local Variables:
194  * eval: (c-set-style "gnu")
195  * End:
196  */
#define CLIB_UNUSED(x)
Definition: clib.h:86
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definitions for all things IP (v4|v6) unicast and multicast lookup related.
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:101
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static u32 vnet_sw_interface_get_mtu(vnet_main_t *vnm, u32 sw_if_index, vnet_mtu_t af)
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
void serialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:129
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
void unserialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:145
static void * serialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:178
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
void vnet_update_adjacency_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, u32 ai)
Definition: rewrite.c:179
unsigned int u32
Definition: types.h:88
format_function_t format_vnet_sw_interface_name
vlib_main_t * vlib_main
Definition: vnet.h:80
static void * vnet_rewrite_get_data_internal(vnet_rewrite_header_t *rw, int max_size)
Definition: rewrite.h:158
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:164
static void * unserialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:171
u8 * format_vnet_rewrite(u8 *s, va_list *args)
Definition: rewrite.c:44
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:80
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
u32 vnet_tx_node_index_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: rewrite.c:73
static void vnet_rewrite_set_data_internal(vnet_rewrite_header_t *rw, int max_size, void *data, int data_bytes)
Definition: rewrite.h:139
static void unserialize_integer(serialize_main_t *m, void *x, u32 n_bytes)
Definition: serialize.h:201
static void serialize_integer(serialize_main_t *m, u64 x, u32 n_bytes)
Definition: serialize.h:185
#define ASSERT(truth)
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
static vnet_hw_interface_class_t * vnet_get_hw_interface_class(vnet_main_t *vnm, u32 hw_class_index)
struct _vnet_hw_interface_class vnet_hw_interface_class_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vnet_mtu_t vnet_link_to_mtu(vnet_link_t link)
Definition: interface.c:1619
u8 si
Definition: lisp_types.api:47
void vnet_rewrite_update_mtu(vnet_main_t *vnm, vnet_link_t linkt, vnet_rewrite_header_t *rw)
Definition: rewrite.c:92
static vnet_sw_interface_t * vnet_get_sw_interface_or_null(vnet_main_t *vnm, u32 sw_if_index)