blob: 975dc4ab817ff70d907b5ed9b3748cbbdbefcf47 [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 vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 u32 max_data_bytes = va_arg (*args, u32);
Christophe Fontained3c008d2017-10-02 18:10:54 +020072 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
Dave Barachba868bb2016-08-08 09:51:21 -040073 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 if (rw->sw_if_index != ~0)
76 {
Dave Barachba868bb2016-08-08 09:51:21 -040077 vnet_sw_interface_t *si;
Neale Ranns75152282017-01-09 01:00:45 -080078 si = vnet_get_sw_interface_safe (vnm, rw->sw_if_index);
79 if (NULL != si)
Neale Rannsffd78d12018-02-09 06:05:16 -080080 s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si);
Neale Ranns75152282017-01-09 01:00:45 -080081 else
Neale Ranns1bd01092017-03-15 15:41:17 -040082 s = format (s, "DELETED:%d", rw->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
Ole Troan6ee40512018-02-12 18:14:39 +010085 s = format (s, " mtu:%d", rw->max_l3_packet_bytes);
86
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 /* Format rewrite string. */
88 if (rw->data_bytes > 0)
Neale Rannsffd78d12018-02-09 06:05:16 -080089 s = format (s, " %U",
Neale Rannsb069a692017-03-15 12:34:25 -040090 format_hex_bytes,
Dave Barachba868bb2016-08-08 09:51:21 -040091 rw->data + max_data_bytes - rw->data_bytes, rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
93 return s;
94}
95
Neale Rannsb80c5362016-10-08 13:03:40 +010096u32
97vnet_tx_node_index_for_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
98{
99 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
100 return (hw->output_node_index);
101}
102
103void
104vnet_rewrite_init (vnet_main_t * vnm,
105 u32 sw_if_index,
Ole Troand7231612018-06-07 10:17:57 +0200106 vnet_link_t linkt,
Neale Rannsb80c5362016-10-08 13:03:40 +0100107 u32 this_node, u32 next_node, vnet_rewrite_header_t * rw)
108{
109 rw->sw_if_index = sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100110 rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node);
111 rw->max_l3_packet_bytes =
Ole Troand7231612018-06-07 10:17:57 +0200112 vnet_sw_interface_get_mtu (vnm, sw_if_index, vnet_link_to_mtu (linkt));
Neale Rannsb80c5362016-10-08 13:03:40 +0100113}
114
Dave Barachba868bb2016-08-08 09:51:21 -0400115void
Ole Troand7231612018-06-07 10:17:57 +0200116vnet_rewrite_update_mtu (vnet_main_t * vnm, vnet_link_t linkt,
117 vnet_rewrite_header_t * rw)
Neale Rannsffd78d12018-02-09 06:05:16 -0800118{
119 rw->max_l3_packet_bytes =
Ole Troand7231612018-06-07 10:17:57 +0200120 vnet_sw_interface_get_mtu (vnm, rw->sw_if_index,
121 vnet_link_to_mtu (linkt));
Neale Rannsffd78d12018-02-09 06:05:16 -0800122}
123
124void
Dave Barachba868bb2016-08-08 09:51:21 -0400125vnet_rewrite_for_sw_interface (vnet_main_t * vnm,
Neale Rannsb80c5362016-10-08 13:03:40 +0100126 vnet_link_t link_type,
Dave Barachba868bb2016-08-08 09:51:21 -0400127 u32 sw_if_index,
128 u32 node_index,
129 void *dst_address,
130 vnet_rewrite_header_t * rw,
131 u32 max_rewrite_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132{
Neale Rannsb80c5362016-10-08 13:03:40 +0100133
Dave Barachba868bb2016-08-08 09:51:21 -0400134 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
135 vnet_hw_interface_class_t *hc =
136 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100137 u8 *rewrite = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
Ole Troand7231612018-06-07 10:17:57 +0200139 vnet_rewrite_init (vnm, sw_if_index, link_type, node_index,
Neale Rannsb80c5362016-10-08 13:03:40 +0100140 vnet_tx_node_index_for_sw_interface (vnm, sw_if_index),
141 rw);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
Neale Rannsb80c5362016-10-08 13:03:40 +0100143 ASSERT (hc->build_rewrite);
144 rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Neale Rannsb80c5362016-10-08 13:03:40 +0100146 ASSERT (vec_len (rewrite) < max_rewrite_bytes);
147 vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite,
148 vec_len (rewrite));
149 vec_free (rewrite);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150}
151
Dave Barachba868bb2016-08-08 09:51:21 -0400152void
153vnet_rewrite_for_tunnel (vnet_main_t * vnm,
154 u32 tx_sw_if_index,
155 u32 rewrite_node_index,
156 u32 post_rewrite_node_index,
157 vnet_rewrite_header_t * rw,
158 u8 * rewrite_data, u32 rewrite_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159{
Dave Barachba868bb2016-08-08 09:51:21 -0400160 ip_adjacency_t *adj = 0;
161 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 * Installed into vnet_buffer(b)->sw_if_index[VLIB_TX] e.g.
Dave Barachba868bb2016-08-08 09:51:21 -0400163 * by ip4_rewrite_inline. If the post-rewrite node injects into
164 * ipX-forward, this will be interpreted as a FIB number.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 */
Dave Barachba868bb2016-08-08 09:51:21 -0400166 rw->sw_if_index = tx_sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400167 rw->next_index = vlib_node_add_next (vnm->vlib_main, rewrite_node_index,
168 post_rewrite_node_index);
Neale Ranns1bce5a92018-10-30 06:34:25 -0700169 /* we can't know at this point */
170 rw->max_l3_packet_bytes = (u16) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 ASSERT (rewrite_length < sizeof (adj->rewrite_data));
173 /* Leave room for ethernet + VLAN tag */
Dave Barachba868bb2016-08-08 09:51:21 -0400174 vnet_rewrite_set_data_internal (rw, sizeof (adj->rewrite_data),
175 rewrite_data, rewrite_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176}
177
Dave Barachba868bb2016-08-08 09:51:21 -0400178void
179serialize_vnet_rewrite (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180{
Dave Barachba868bb2016-08-08 09:51:21 -0400181 vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 u32 max_data_bytes = va_arg (*va, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400183 u8 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
185 serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index));
186 serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes));
Dave Barachba868bb2016-08-08 09:51:21 -0400187 serialize_integer (m, rw->max_l3_packet_bytes,
188 sizeof (rw->max_l3_packet_bytes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 p = serialize_get (m, rw->data_bytes);
Dave Barachba868bb2016-08-08 09:51:21 -0400190 clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes),
191 rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192}
193
Dave Barachba868bb2016-08-08 09:51:21 -0400194void
195unserialize_vnet_rewrite (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196{
Dave Barachba868bb2016-08-08 09:51:21 -0400197 vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 u32 max_data_bytes = va_arg (*va, u32);
Dave Barachba868bb2016-08-08 09:51:21 -0400199 u8 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
201 /* It is up to user to fill these in. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 rw->next_index = ~0;
203
204 unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index));
205 unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes));
Dave Barachba868bb2016-08-08 09:51:21 -0400206 unserialize_integer (m, &rw->max_l3_packet_bytes,
207 sizeof (rw->max_l3_packet_bytes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 p = unserialize_get (m, rw->data_bytes);
Dave Barachba868bb2016-08-08 09:51:21 -0400209 clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p,
210 rw->data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211}
Dave Barachba868bb2016-08-08 09:51:21 -0400212
Neale Rannsb80c5362016-10-08 13:03:40 +0100213u8 *
214vnet_build_rewrite_for_sw_interface (vnet_main_t * vnm,
215 u32 sw_if_index,
216 vnet_link_t link_type,
217 const void *dst_address)
218{
219 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
220 vnet_hw_interface_class_t *hc =
221 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
222
223 ASSERT (hc->build_rewrite);
224 return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address));
225}
226
227
228void
229vnet_update_adjacency_for_sw_interface (vnet_main_t * vnm,
230 u32 sw_if_index, u32 ai)
231{
232 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
233 vnet_hw_interface_class_t *hc =
234 vnet_get_hw_interface_class (vnm, hw->hw_class_index);
235
236 ASSERT (hc->update_adjacency);
237 hc->update_adjacency (vnm, sw_if_index, ai);
238}
239
Dave Barachba868bb2016-08-08 09:51:21 -0400240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables:
244 * eval: (c-set-style "gnu")
245 * End:
246 */