FD.io VPP  v16.06
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 
44  vnet_rewrite_data_t * rw0,
45  word n_left,
46  uword most_likely_size)
47 {
48  uword n_done = round_pow2 (most_likely_size, sizeof (rw0[0])) / sizeof (rw0[0]);
49 
50  p0 -= n_done;
51  rw0 -= n_done;
52 
53  /* As we enter the cleanup loop, p0 and rw0 point to the last chunk written
54  by the fast path. Hence, the constant 1, which the
55  vnet_rewrite_copy_one macro renders as p0[-1] = rw0[-1]. */
56 
57  while (n_left > 0)
58  {
59  vnet_rewrite_copy_one (p0, rw0, 1);
60  p0--;
61  rw0--;
62  n_left--;
63  }
64 }
65 
66 u8 * format_vnet_rewrite (u8 * s, va_list * args)
67 {
68  vlib_main_t * vm = va_arg (*args, vlib_main_t *);
69  vnet_rewrite_header_t * rw = va_arg (*args, vnet_rewrite_header_t *);
70  u32 max_data_bytes = va_arg (*args, u32);
71  vnet_main_t * vnm = vnet_get_main();
72  vlib_node_t * next;
73  uword indent;
74 
75  next = vlib_get_next_node (vm, rw->node_index, rw->next_index);
76 
77  indent = format_get_indent (s);
78 
79  if (rw->sw_if_index != ~0)
80  {
82  si = vnet_get_sw_interface (vnm, rw->sw_if_index);
83  s = format (s, "%U", format_vnet_sw_interface_name, vnm, si);
84  }
85  else
86  s = format (s, "%v", next->name);
87 
88  /* Format rewrite string. */
89  if (rw->data_bytes > 0)
90  s = format (s, "\n%U%U",
91  format_white_space, indent,
93  rw->data + max_data_bytes - rw->data_bytes,
94  rw->data_bytes);
95 
96  return s;
97 }
98 
99 u8 * format_vnet_rewrite_header (u8 * s, va_list * args)
100 {
101  vlib_main_t * vm = va_arg (*args, vlib_main_t *);
102  vnet_rewrite_header_t * rw = va_arg (*args, vnet_rewrite_header_t *);
103  u8 * packet_data = va_arg (*args, u8 *);
104  u32 packet_data_bytes = va_arg (*args, u32);
105  vlib_node_t * next;
106 
107  next = vlib_get_next_node (vm, rw->node_index, rw->next_index);
108 
109  /* Format rewrite string. */
110  s = format (s, "%U",
112  packet_data, packet_data_bytes);
113 
114  return s;
115 }
116 
118 {
119  vlib_main_t * vm = va_arg (*args, vlib_main_t *);
120  vnet_rewrite_header_t * rw = va_arg (*args, vnet_rewrite_header_t *);
121  u32 max_data_bytes = va_arg (*args, u32);
122  vnet_main_t * vnm = vnet_get_main();
123  vlib_node_t * next;
124  u32 next_index, sw_if_index, max_packet_bytes, error;
125  u8 * rw_data;
126 
127  rw_data = 0;
128  sw_if_index = ~0;
129  max_packet_bytes = ~0;
130  error = 1;
131 
132  /* Parse sw interface. */
133  if (unformat (input, "%U",
134  unformat_vnet_sw_interface, vnm, &sw_if_index))
135  {
137 
138  hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
139 
140  next_index = hi->output_node_index;
141  max_packet_bytes = hi->max_l3_packet_bytes[VLIB_RX];
142  }
143 
144  else if (unformat (input, "%U",
145  unformat_vlib_node, vm, &next_index))
146  ;
147 
148  else
149  goto done;
150 
151  next = vlib_get_node (vm, next_index);
152 
153  if (next->unformat_buffer
154  && unformat_user (input, next->unformat_buffer, &rw_data))
155  ;
156 
157  else if (unformat_user (input, unformat_hex_string, &rw_data)
158  || unformat (input, "0x%U", unformat_hex_string, &rw_data))
159  ;
160 
161  else
162  goto done;
163 
164  /* Re-write does not fit. */
165  if (vec_len (rw_data) >= max_data_bytes)
166  goto done;
167 
168  {
169  u32 tmp;
170 
171  if (unformat (input, "mtu %d", &tmp)
172  && tmp < (1 << BITS (rw->max_l3_packet_bytes)))
173  max_packet_bytes = tmp;
174  }
175 
176  error = 0;
177  rw->sw_if_index = sw_if_index;
178  rw->max_l3_packet_bytes = max_packet_bytes;
179  rw->next_index = vlib_node_add_next (vm, rw->node_index, next_index);
180  vnet_rewrite_set_data_internal (rw, max_data_bytes, rw_data, vec_len (rw_data));
181 
182  done:
183  vec_free (rw_data);
184  return error == 0;
185 }
186 
188  vnet_l3_packet_type_t packet_type,
189  u32 sw_if_index,
190  u32 node_index,
191  void * dst_address,
192  vnet_rewrite_header_t * rw,
193  u32 max_rewrite_bytes)
194 {
195  vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
197  static u8 * rw_tmp = 0;
198  uword n_rw_tmp;
199 
200  rw->sw_if_index = sw_if_index;
201  rw->node_index = node_index;
202  rw->next_index = vlib_node_add_next (vnm->vlib_main, node_index, hw->output_node_index);
203  rw->max_l3_packet_bytes = hw->max_l3_packet_bytes[VLIB_TX];
204 
205  ASSERT (max_rewrite_bytes > 0);
206  vec_reset_length (rw_tmp);
207  vec_validate (rw_tmp, max_rewrite_bytes - 1);
208 
209  ASSERT (hc->set_rewrite);
210  n_rw_tmp = hc->set_rewrite (vnm, sw_if_index, packet_type, dst_address, rw_tmp, max_rewrite_bytes);
211 
212  ASSERT (n_rw_tmp >= 0 && n_rw_tmp < max_rewrite_bytes);
213  vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rw_tmp, n_rw_tmp);
214 }
215 
217  u32 tx_sw_if_index,
218  u32 rewrite_node_index,
219  u32 post_rewrite_node_index,
220  vnet_rewrite_header_t * rw,
221  u8 *rewrite_data,
222  u32 rewrite_length)
223 {
224  ip_adjacency_t * adj = 0;
225  /*
226  * Installed into vnet_buffer(b)->sw_if_index[VLIB_TX] e.g.
227  * by ip4_rewrite_inline. If the post-rewrite node injects into
228  * ipX-forward, this will be interpreted as a FIB number.
229  */
230  rw->sw_if_index = tx_sw_if_index;
231  rw->node_index = rewrite_node_index;
232  rw->next_index = vlib_node_add_next (vnm->vlib_main, rewrite_node_index,
233  post_rewrite_node_index);
234  rw->max_l3_packet_bytes = (u16) ~0; /* we can't know at this point */
235 
236  ASSERT (rewrite_length < sizeof (adj->rewrite_data));
237  /* Leave room for ethernet + VLAN tag */
238  vnet_rewrite_set_data_internal (rw, sizeof(adj->rewrite_data),
239  rewrite_data, rewrite_length);
240 }
241 
242 void serialize_vnet_rewrite (serialize_main_t * m, va_list * va)
243 {
244  vnet_rewrite_header_t * rw = va_arg (*va, vnet_rewrite_header_t *);
245  u32 max_data_bytes = va_arg (*va, u32);
246  u8 * p;
247 
248  serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
249  serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
250  serialize_integer (m, rw->max_l3_packet_bytes, sizeof (rw->max_l3_packet_bytes));
251  p = serialize_get (m, rw->data_bytes);
252  clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes), rw->data_bytes);
253 }
254 
256 {
257  vnet_rewrite_header_t * rw = va_arg (*va, vnet_rewrite_header_t *);
258  u32 max_data_bytes = va_arg (*va, u32);
259  u8 * p;
260 
261  /* It is up to user to fill these in. */
262  rw->node_index = ~0;
263  rw->next_index = ~0;
264 
265  unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
266  unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
267  unserialize_integer (m, &rw->max_l3_packet_bytes, sizeof (rw->max_l3_packet_bytes));
268  p = unserialize_get (m, rw->data_bytes);
269  clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p, rw->data_bytes);
270 }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
vmrglw vmrglh hi
always_inline uword round_pow2(uword x, uword pow2)
Definition: clib.h:255
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
always_inline void * vnet_rewrite_get_data_internal(vnet_rewrite_header_t *rw, int max_size)
Definition: rewrite.h:111
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
always_inline vnet_hw_interface_class_t * vnet_get_hw_interface_class(vnet_main_t *vnm, u32 hw_class_index)
uword vnet_rewrite_data_t
Definition: rewrite.h:47
unformat_function_t unformat_vnet_sw_interface
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
void serialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:242
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
vnet_l3_packet_type_t
Definition: l3_types.h:44
void unserialize_vnet_rewrite(serialize_main_t *m, va_list *va)
Definition: rewrite.c:255
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
unformat_function_t unformat_hex_string
Definition: format.h:285
always_inline void unserialize_integer(serialize_main_t *m, void *x, u32 n_bytes)
Definition: serialize.h:181
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:79
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
u8 * format_vnet_rewrite_header(u8 *s, va_list *args)
Definition: rewrite.c:99
format_function_t format_vnet_sw_interface_name
vlib_main_t * vlib_main
Definition: vnet.h:78
uword unformat_vnet_rewrite(unformat_input_t *input, va_list *args)
Definition: rewrite.c:117
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:43
always_inline void * unserialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:157
format_function_t * format_buffer
Definition: node.h:277
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:216
always_inline vlib_node_t * vlib_get_next_node(vlib_main_t *vm, u32 node_index, u32 next_index)
Definition: node_funcs.h:50
u8 * name
Definition: node.h:187
u8 * format_vnet_rewrite(u8 *s, va_list *args)
Definition: rewrite.c:66
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
void vnet_rewrite_for_sw_interface(vnet_main_t *vnm, vnet_l3_packet_type_t packet_type, u32 sw_if_index, u32 node_index, void *dst_address, vnet_rewrite_header_t *rw, u32 max_rewrite_bytes)
Definition: rewrite.c:187
#define clib_memcpy(a, b, c)
Definition: string.h:63
unformat_function_t * unformat_buffer
Definition: node.h:278
always_inline vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
always_inline void serialize_integer(serialize_main_t *m, u64 x, u32 n_bytes)
Definition: serialize.h:165
u32 max_l3_packet_bytes[VLIB_N_RX_TX]
Definition: interface.h:313
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
u64 uword
Definition: types.h:112
Definition: defs.h:46
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
always_inline vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
always_inline void vnet_rewrite_set_data_internal(vnet_rewrite_header_t *rw, int max_size, void *data, int data_bytes)
Definition: rewrite.h:90
unformat_function_t unformat_vlib_node
Definition: node_funcs.h:967
always_inline void * serialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:161
struct _unformat_input_t unformat_input_t
always_inline void vnet_rewrite_copy_one(vnet_rewrite_data_t *p0, vnet_rewrite_data_t *rw0, int i)
Definition: rewrite.h:121
#define BITS(x)
Definition: clib.h:58
Definition: defs.h:45