blob: 53d548bc8ae0cb4e4e46888902a6ec02d46eb90a [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barachba868bb2016-08-08 09:51:21 -040043void
44vnet_rewrite_copy_slow_path (vnet_rewrite_data_t * p0,
45 vnet_rewrite_data_t * rw0,
46 word n_left, uword most_likely_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070047{
Dave Barachba868bb2016-08-08 09:51:21 -040048 uword n_done =
49 round_pow2 (most_likely_size, sizeof (rw0[0])) / sizeof (rw0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
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
Dave Barachba868bb2016-08-08 09:51:21 -040055 by the fast path. Hence, the constant 1, which the
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 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
Dave Barachba868bb2016-08-08 09:51:21 -040067u8 *
68format_vnet_rewrite (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barachba868bb2016-08-08 09:51:21 -040070 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
71 vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 u32 max_data_bytes = va_arg (*args, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 CLIB_UNUSED (uword indent) = va_arg (*args, u32);
Dave Barachba868bb2016-08-08 09:51:21 -040074 vnet_main_t *vnm = vnet_get_main ();
75 vlib_node_t *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 next = vlib_get_next_node (vm, rw->node_index, rw->next_index);
78
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 if (rw->sw_if_index != ~0)
80 {
Dave Barachba868bb2016-08-08 09:51:21 -040081 vnet_sw_interface_t *si;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 si = vnet_get_sw_interface (vnm, rw->sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083 s = format (s, "%U: ", format_vnet_sw_interface_name, vnm, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 }
85 else
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086 s = format (s, "%v: ", next->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
88 /* Format rewrite string. */
89 if (rw->data_bytes > 0)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010090
91 s = format (s, "%U",
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 next->format_buffer ? next->format_buffer : format_hex_bytes,
Dave Barachba868bb2016-08-08 09:51:21 -040093 rw->data + max_data_bytes - rw->data_bytes, rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 return s;
96}
97
Dave Barachba868bb2016-08-08 09:51:21 -040098u8 *
99format_vnet_rewrite_header (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Dave Barachba868bb2016-08-08 09:51:21 -0400101 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 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 u32 packet_data_bytes = va_arg (*args, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400105 vlib_node_t *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 next = vlib_get_next_node (vm, rw->node_index, rw->next_index);
108
109 /* Format rewrite string. */
110 s = format (s, "%U",
111 next->format_buffer ? next->format_buffer : format_hex_bytes,
112 packet_data, packet_data_bytes);
113
114 return s;
115}
116
Dave Barachba868bb2016-08-08 09:51:21 -0400117uword
118unformat_vnet_rewrite (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119{
Dave Barachba868bb2016-08-08 09:51:21 -0400120 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
121 vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 u32 max_data_bytes = va_arg (*args, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400123 vnet_main_t *vnm = vnet_get_main ();
124 vlib_node_t *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 u32 next_index, sw_if_index, max_packet_bytes, error;
Dave Barachba868bb2016-08-08 09:51:21 -0400126 u8 *rw_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
128 rw_data = 0;
129 sw_if_index = ~0;
130 max_packet_bytes = ~0;
131 error = 1;
132
133 /* Parse sw interface. */
Dave Barachba868bb2016-08-08 09:51:21 -0400134 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 {
Dave Barachba868bb2016-08-08 09:51:21 -0400136 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
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
Dave Barachba868bb2016-08-08 09:51:21 -0400144 else if (unformat (input, "%U", unformat_vlib_node, vm, &next_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 ;
146
147 else
148 goto done;
149
150 next = vlib_get_node (vm, next_index);
151
152 if (next->unformat_buffer
153 && unformat_user (input, next->unformat_buffer, &rw_data))
154 ;
155
156 else if (unformat_user (input, unformat_hex_string, &rw_data)
157 || unformat (input, "0x%U", unformat_hex_string, &rw_data))
158 ;
Dave Barachba868bb2016-08-08 09:51:21 -0400159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 else
161 goto done;
162
163 /* Re-write does not fit. */
164 if (vec_len (rw_data) >= max_data_bytes)
165 goto done;
166
167 {
168 u32 tmp;
169
170 if (unformat (input, "mtu %d", &tmp)
171 && tmp < (1 << BITS (rw->max_l3_packet_bytes)))
172 max_packet_bytes = tmp;
173 }
174
175 error = 0;
176 rw->sw_if_index = sw_if_index;
177 rw->max_l3_packet_bytes = max_packet_bytes;
178 rw->next_index = vlib_node_add_next (vm, rw->node_index, next_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400179 vnet_rewrite_set_data_internal (rw, max_data_bytes, rw_data,
180 vec_len (rw_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Dave Barachba868bb2016-08-08 09:51:21 -0400182done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 vec_free (rw_data);
184 return error == 0;
185}
186
Neale Rannsb80c5362016-10-08 13:03:40 +0100187u32
188vnet_tx_node_index_for_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
189{
190 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
191 return (hw->output_node_index);
192}
193
194void
195vnet_rewrite_init (vnet_main_t * vnm,
196 u32 sw_if_index,
197 u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
198{
199 rw->sw_if_index = sw_if_index;
200 rw->node_index = this_node;
201 rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
202 rw->max_l3_packet_bytes =
203 vnet_sw_interface_get_mtu (vnm, sw_if_index, VLIB_TX);
204}
205
Dave Barachba868bb2016-08-08 09:51:21 -0400206void
207vnet_rewrite_for_sw_interface (vnet_main_t * vnm,
Neale Rannsb80c5362016-10-08 13:03:40 +0100208 vnet_link_t link_type,
Dave Barachba868bb2016-08-08 09:51:21 -0400209 u32 sw_if_index,
210 u32 node_index,
211 void *dst_address,
212 vnet_rewrite_header_t * rw,
213 u32 max_rewrite_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214{
Neale Rannsb80c5362016-10-08 13:03:40 +0100215
Dave Barachba868bb2016-08-08 09:51:21 -0400216 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
217 vnet_hw_interface_class_t *hc =
218 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100219 u8 *rewrite = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Neale Rannsb80c5362016-10-08 13:03:40 +0100221 vnet_rewrite_init (vnm, sw_if_index, node_index,
222 vnet_tx_node_index_for_sw_interface (vnm, sw_if_index),
223 rw);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Neale Rannsb80c5362016-10-08 13:03:40 +0100225 ASSERT (hc->build_rewrite);
226 rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Neale Rannsb80c5362016-10-08 13:03:40 +0100228 ASSERT (vec_len (rewrite) < max_rewrite_bytes);
229 vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
230 vec_len (rewrite));
231 vec_free (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232}
233
Dave Barachba868bb2016-08-08 09:51:21 -0400234void
235vnet_rewrite_for_tunnel (vnet_main_t * vnm,
236 u32 tx_sw_if_index,
237 u32 rewrite_node_index,
238 u32 post_rewrite_node_index,
239 vnet_rewrite_header_t * rw,
240 u8 * rewrite_data, u32 rewrite_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241{
Dave Barachba868bb2016-08-08 09:51:21 -0400242 ip_adjacency_t *adj = 0;
243 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 * Installed into vnet_buffer(b)->sw_if_index[VLIB_TX] e.g.
Dave Barachba868bb2016-08-08 09:51:21 -0400245 * by ip4_rewrite_inline. If the post-rewrite node injects into
246 * ipX-forward, this will be interpreted as a FIB number.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 */
Dave Barachba868bb2016-08-08 09:51:21 -0400248 rw->sw_if_index = tx_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 rw->node_index = rewrite_node_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400250 rw->next_index = vlib_node_add_next (vnm->vlib_main, rewrite_node_index,
251 post_rewrite_node_index);
252 rw->max_l3_packet_bytes = (u16) ~ 0; /* we can't know at this point */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
254 ASSERT (rewrite_length < sizeof (adj->rewrite_data));
255 /* Leave room for ethernet + VLAN tag */
Dave Barachba868bb2016-08-08 09:51:21 -0400256 vnet_rewrite_set_data_internal (rw, sizeof (adj->rewrite_data),
257 rewrite_data, rewrite_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258}
259
Dave Barachba868bb2016-08-08 09:51:21 -0400260void
261serialize_vnet_rewrite (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262{
Dave Barachba868bb2016-08-08 09:51:21 -0400263 vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 u32 max_data_bytes = va_arg (*va, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400265 u8 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
267 serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
268 serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
Dave Barachba868bb2016-08-08 09:51:21 -0400269 serialize_integer (m, rw->max_l3_packet_bytes,
270 sizeof (rw->max_l3_packet_bytes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 p = serialize_get (m, rw->data_bytes);
Dave Barachba868bb2016-08-08 09:51:21 -0400272 clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
273 rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274}
275
Dave Barachba868bb2016-08-08 09:51:21 -0400276void
277unserialize_vnet_rewrite (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278{
Dave Barachba868bb2016-08-08 09:51:21 -0400279 vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 u32 max_data_bytes = va_arg (*va, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400281 u8 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
283 /* It is up to user to fill these in. */
284 rw->node_index = ~0;
285 rw->next_index = ~0;
286
287 unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
288 unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
Dave Barachba868bb2016-08-08 09:51:21 -0400289 unserialize_integer (m, &rw->max_l3_packet_bytes,
290 sizeof (rw->max_l3_packet_bytes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 p = unserialize_get (m, rw->data_bytes);
Dave Barachba868bb2016-08-08 09:51:21 -0400292 clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
293 rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294}
Dave Barachba868bb2016-08-08 09:51:21 -0400295
Neale Rannsb80c5362016-10-08 13:03:40 +0100296u8 *
297vnet_build_rewrite_for_sw_interface (vnet_main_t * vnm,
298 u32 sw_if_index,
299 vnet_link_t link_type,
300 const void *dst_address)
301{
302 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
303 vnet_hw_interface_class_t *hc =
304 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
305
306 ASSERT (hc->build_rewrite);
307 return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
308}
309
310
311void
312vnet_update_adjacency_for_sw_interface (vnet_main_t * vnm,
313 u32 sw_if_index, u32 ai)
314{
315 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
316 vnet_hw_interface_class_t *hc =
317 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
318
319 ASSERT (hc->update_adjacency);
320 hc->update_adjacency (vnm, sw_if_index, ai);
321}
322
Dave Barachba868bb2016-08-08 09:51:21 -0400323/*
324 * fd.io coding-style-patch-verification: ON
325 *
326 * Local Variables:
327 * eval: (c-set-style "gnu")
328 * End:
329 */