Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 43 | void |
| 44 | vnet_rewrite_copy_slow_path (vnet_rewrite_data_t * p0, |
| 45 | vnet_rewrite_data_t * rw0, |
| 46 | word n_left, uword most_likely_size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 48 | uword n_done = |
| 49 | round_pow2 (most_likely_size, sizeof (rw0[0])) / sizeof (rw0[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 55 | by the fast path. Hence, the constant 1, which the |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 67 | u8 * |
| 68 | format_vnet_rewrite (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 70 | vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | u32 max_data_bytes = va_arg (*args, u32); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 72 | CLIB_UNUSED (u32 indent) = va_arg (*args, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 73 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | if (rw->sw_if_index != ~0) |
| 76 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 77 | vnet_sw_interface_t *si; |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 78 | si = vnet_get_sw_interface_safe (vnm, rw->sw_if_index); |
| 79 | if (NULL != si) |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 80 | s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si); |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 81 | else |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 82 | s = format (s, "DELETED:%d", rw->sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 85 | s = format (s, " mtu:%d", rw->max_l3_packet_bytes); |
| 86 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | /* Format rewrite string. */ |
| 88 | if (rw->data_bytes > 0) |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 89 | s = format (s, " %U", |
Neale Ranns | b069a69 | 2017-03-15 12:34:25 -0400 | [diff] [blame] | 90 | format_hex_bytes, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 91 | rw->data + max_data_bytes - rw->data_bytes, rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
| 93 | return s; |
| 94 | } |
| 95 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 96 | u32 |
| 97 | vnet_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 | |
| 103 | void |
| 104 | vnet_rewrite_init (vnet_main_t * vnm, |
| 105 | u32 sw_if_index, |
| 106 | u32 this_node, u32 next_node, vnet_rewrite_header_t * rw) |
| 107 | { |
| 108 | rw->sw_if_index = sw_if_index; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 109 | rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node); |
| 110 | rw->max_l3_packet_bytes = |
| 111 | vnet_sw_interface_get_mtu (vnm, sw_if_index, VLIB_TX); |
| 112 | } |
| 113 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 114 | void |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 115 | vnet_rewrite_update_mtu (vnet_main_t * vnm, vnet_rewrite_header_t * rw) |
| 116 | { |
| 117 | rw->max_l3_packet_bytes = |
| 118 | vnet_sw_interface_get_mtu (vnm, rw->sw_if_index, VLIB_TX); |
| 119 | } |
| 120 | |
| 121 | void |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 122 | vnet_rewrite_for_sw_interface (vnet_main_t * vnm, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 123 | vnet_link_t link_type, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 124 | u32 sw_if_index, |
| 125 | u32 node_index, |
| 126 | void *dst_address, |
| 127 | vnet_rewrite_header_t * rw, |
| 128 | u32 max_rewrite_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 130 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 131 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 132 | vnet_hw_interface_class_t *hc = |
| 133 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 134 | u8 *rewrite = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 136 | vnet_rewrite_init (vnm, sw_if_index, node_index, |
| 137 | vnet_tx_node_index_for_sw_interface (vnm, sw_if_index), |
| 138 | rw); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 140 | ASSERT (hc->build_rewrite); |
| 141 | rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 143 | ASSERT (vec_len (rewrite) < max_rewrite_bytes); |
| 144 | vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite, |
| 145 | vec_len (rewrite)); |
| 146 | vec_free (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 149 | void |
| 150 | vnet_rewrite_for_tunnel (vnet_main_t * vnm, |
| 151 | u32 tx_sw_if_index, |
| 152 | u32 rewrite_node_index, |
| 153 | u32 post_rewrite_node_index, |
| 154 | vnet_rewrite_header_t * rw, |
| 155 | u8 * rewrite_data, u32 rewrite_length) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 157 | ip_adjacency_t *adj = 0; |
| 158 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | * Installed into vnet_buffer(b)->sw_if_index[VLIB_TX] e.g. |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 160 | * by ip4_rewrite_inline. If the post-rewrite node injects into |
| 161 | * ipX-forward, this will be interpreted as a FIB number. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 163 | rw->sw_if_index = tx_sw_if_index; |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 164 | rw->next_index = vlib_node_add_next (vnm->vlib_main, rewrite_node_index, |
| 165 | post_rewrite_node_index); |
| 166 | rw->max_l3_packet_bytes = (u16) ~ 0; /* we can't know at this point */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | |
| 168 | ASSERT (rewrite_length < sizeof (adj->rewrite_data)); |
| 169 | /* Leave room for ethernet + VLAN tag */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 170 | vnet_rewrite_set_data_internal (rw, sizeof (adj->rewrite_data), |
| 171 | rewrite_data, rewrite_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 174 | void |
| 175 | serialize_vnet_rewrite (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 177 | vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | u32 max_data_bytes = va_arg (*va, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 179 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
| 181 | serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index)); |
| 182 | serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes)); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 183 | serialize_integer (m, rw->max_l3_packet_bytes, |
| 184 | sizeof (rw->max_l3_packet_bytes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | p = serialize_get (m, rw->data_bytes); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 186 | clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes), |
| 187 | rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 190 | void |
| 191 | unserialize_vnet_rewrite (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 193 | vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | u32 max_data_bytes = va_arg (*va, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 195 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | |
| 197 | /* It is up to user to fill these in. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | rw->next_index = ~0; |
| 199 | |
| 200 | unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index)); |
| 201 | unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes)); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 202 | unserialize_integer (m, &rw->max_l3_packet_bytes, |
| 203 | sizeof (rw->max_l3_packet_bytes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | p = unserialize_get (m, rw->data_bytes); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 205 | clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p, |
| 206 | rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | } |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 208 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 209 | u8 * |
| 210 | vnet_build_rewrite_for_sw_interface (vnet_main_t * vnm, |
| 211 | u32 sw_if_index, |
| 212 | vnet_link_t link_type, |
| 213 | const void *dst_address) |
| 214 | { |
| 215 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 216 | vnet_hw_interface_class_t *hc = |
| 217 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
| 218 | |
| 219 | ASSERT (hc->build_rewrite); |
| 220 | return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address)); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void |
| 225 | vnet_update_adjacency_for_sw_interface (vnet_main_t * vnm, |
| 226 | u32 sw_if_index, u32 ai) |
| 227 | { |
| 228 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 229 | vnet_hw_interface_class_t *hc = |
| 230 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
| 231 | |
| 232 | ASSERT (hc->update_adjacency); |
| 233 | hc->update_adjacency (vnm, sw_if_index, ai); |
| 234 | } |
| 235 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 236 | /* |
| 237 | * fd.io coding-style-patch-verification: ON |
| 238 | * |
| 239 | * Local Variables: |
| 240 | * eval: (c-set-style "gnu") |
| 241 | * End: |
| 242 | */ |