FD.io VPP  v18.01.2-1-g9b554f3
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 void
45  vnet_rewrite_data_t * rw0,
46  word n_left, uword most_likely_size)
47 {
48  uword n_done =
49  round_pow2 (most_likely_size, sizeof (rw0[0])) / sizeof (rw0[0]);
50 
51  p0 -= n_done;
52  rw0 -= n_done;
53 
54  /* As we enter the cleanup loop, p0 and rw0 point to the last chunk written
55  by the fast path. Hence, the constant 1, which the
56  vnet_rewrite_copy_one macro renders as p0[-1] = rw0[-1]. */
57 
58  while (n_left > 0)
59  {
60  vnet_rewrite_copy_one (p0, rw0, 1);
61  p0--;
62  rw0--;
63  n_left--;
64  }
65 }
66 
67 u8 *
68 format_vnet_rewrite (u8 * s, va_list * args)
69 {
70  vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
71  u32 max_data_bytes = va_arg (*args, u32);
72  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
73  vnet_main_t *vnm = vnet_get_main ();
74 
75  if (rw->sw_if_index != ~0)
76  {
78  si = vnet_get_sw_interface_safe (vnm, rw->sw_if_index);
79  if (NULL != si)
80  s = format (s, "%U: ", format_vnet_sw_interface_name, vnm, si);
81  else
82  s = format (s, "DELETED:%d", rw->sw_if_index);
83  }
84 
85  /* Format rewrite string. */
86  if (rw->data_bytes > 0)
87 
88  s = format (s, "%U",
90  rw->data + max_data_bytes - rw->data_bytes, rw->data_bytes);
91 
92  return s;
93 }
94 
95 u32
97 {
98  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
99  return (hw->output_node_index);
100 }
101 
102 void
104  u32 sw_if_index,
105  u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
106 {
107  rw->sw_if_index = sw_if_index;
108  rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
109  rw->max_l3_packet_bytes =
110  vnet_sw_interface_get_mtu (vnm, sw_if_index, VLIB_TX);
111 }
112 
113 void
115  vnet_link_t link_type,
116  u32 sw_if_index,
117  u32 node_index,
118  void *dst_address,
119  vnet_rewrite_header_t * rw,
120  u32 max_rewrite_bytes)
121 {
122 
123  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
126  u8 *rewrite = NULL;
127 
128  vnet_rewrite_init (vnm, sw_if_index, node_index,
129  vnet_tx_node_index_for_sw_interface (vnm, sw_if_index),
130  rw);
131 
132  ASSERT (hc->build_rewrite);
133  rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
134 
135  ASSERT (vec_len (rewrite) < max_rewrite_bytes);
136  vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
137  vec_len (rewrite));
138  vec_free (rewrite);
139 }
140 
141 void
143  u32 tx_sw_if_index,
144  u32 rewrite_node_index,
145  u32 post_rewrite_node_index,
146  vnet_rewrite_header_t * rw,
147  u8 * rewrite_data, u32 rewrite_length)
148 {
149  ip_adjacency_t *adj = 0;
150  /*
151  * Installed into vnet_buffer(b)->sw_if_index[VLIB_TX] e.g.
152  * by ip4_rewrite_inline. If the post-rewrite node injects into
153  * ipX-forward, this will be interpreted as a FIB number.
154  */
155  rw->sw_if_index = tx_sw_if_index;
156  rw->next_index = vlib_node_add_next (vnm->vlib_main, rewrite_node_index,
157  post_rewrite_node_index);
158  rw->max_l3_packet_bytes = (u16) ~ 0; /* we can't know at this point */
159 
160  ASSERT (rewrite_length < sizeof (adj->rewrite_data));
161  /* Leave room for ethernet + VLAN tag */
162  vnet_rewrite_set_data_internal (rw, sizeof (adj->rewrite_data),
163  rewrite_data, rewrite_length);
164 }
165 
166 void
168 {
169  vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
170  u32 max_data_bytes = va_arg (*va, u32);
171  u8 *p;
172 
173  serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
174  serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
175  serialize_integer (m, rw->max_l3_packet_bytes,
176  sizeof (rw->max_l3_packet_bytes));
177  p = serialize_get (m, rw->data_bytes);
178  clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
179  rw->data_bytes);
180 }
181 
182 void
184 {
185  vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
186  u32 max_data_bytes = va_arg (*va, u32);
187  u8 *p;
188 
189  /* It is up to user to fill these in. */
190  rw->next_index = ~0;
191 
192  unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
193  unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
194  unserialize_integer (m, &rw->max_l3_packet_bytes,
195  sizeof (rw->max_l3_packet_bytes));
196  p = unserialize_get (m, rw->data_bytes);
197  clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
198  rw->data_bytes);
199 }
200 
201 u8 *
203  u32 sw_if_index,
204  vnet_link_t link_type,
205  const void *dst_address)
206 {
207  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
210 
211  ASSERT (hc->build_rewrite);
212  return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
213 }
214 
215 
216 void
218  u32 sw_if_index, u32 ai)
219 {
220  vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
223 
224  ASSERT (hc->update_adjacency);
225  hc->update_adjacency (vnm, sw_if_index, ai);
226 }
227 
228 /*
229  * fd.io coding-style-patch-verification: ON
230  *
231  * Local Variables:
232  * eval: (c-set-style "gnu")
233  * End:
234  */
void vnet_rewrite_init(vnet_main_t *vnm, u32 sw_if_index, u32 this_node, u32 next_node, vnet_rewrite_header_t *rw)
Definition: rewrite.c:103
#define CLIB_UNUSED(x)
Definition: clib.h:79
static uword vnet_sw_interface_get_mtu(vnet_main_t *vnm, u32 sw_if_index, vlib_rx_or_tx_t dir)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
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.
uword vnet_rewrite_data_t
Definition: rewrite.h:47
#define NULL
Definition: clib.h:55
IP unicast adjacency.
Definition: adj.h:174
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:114
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1108
void serialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:167
void unserialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:183
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
static void vnet_rewrite_copy_one(vnet_rewrite_data_t *p0, vnet_rewrite_data_t *rw0, int i)
Definition: rewrite.h:159
void vnet_update_adjacency_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index, u32 ai)
Definition: rewrite.c:217
format_function_t format_vnet_sw_interface_name
vlib_main_t * vlib_main
Definition: vnet.h:78
void vnet_rewrite_copy_slow_path(vnet_rewrite_data_t *p0, vnet_rewrite_data_t *rw0, word n_left, uword most_likely_size)
Definition: rewrite.c:44
static void * vnet_rewrite_get_data_internal(vnet_rewrite_header_t *rw, int max_size)
Definition: rewrite.h:149
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:202
static void * unserialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:171
void vnet_rewrite_for_tunnel(vnet_main_t *vnm, u32 tx_sw_if_index, u32 rewrite_node_index, u32 post_rewrite_node_index, vnet_rewrite_header_t *rw, u8 *rewrite_data, u32 rewrite_length)
Definition: rewrite.c:142
u8 * format_vnet_rewrite(u8 *s, va_list *args)
Definition: rewrite.c:68
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
u32 vnet_tx_node_index_for_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: rewrite.c:96
static void vnet_rewrite_set_data_internal(vnet_rewrite_header_t *rw, int max_size, void *data, int data_bytes)
Definition: rewrite.h:130
#define clib_memcpy(a, b, c)
Definition: string.h:75
static vnet_sw_interface_t * vnet_get_sw_interface_safe(vnet_main_t *vnm, u32 sw_if_index)
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
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:286
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
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)
u64 uword
Definition: types.h:112
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
i64 word
Definition: types.h:111
struct _vnet_hw_interface_class vnet_hw_interface_class_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56