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 | u8 * |
| 44 | format_vnet_rewrite (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 46 | vnet_rewrite_header_t *rw = va_arg (*args, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | u32 max_data_bytes = va_arg (*args, u32); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 48 | CLIB_UNUSED (u32 indent) = va_arg (*args, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 49 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Benoît Ganne | 4af1a7f | 2019-03-01 14:14:10 +0100 | [diff] [blame] | 51 | ASSERT (rw->data_bytes <= max_data_bytes); |
| 52 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | if (rw->sw_if_index != ~0) |
| 54 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 55 | vnet_sw_interface_t *si; |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 56 | si = vnet_get_sw_interface_or_null (vnm, rw->sw_if_index); |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 57 | if (NULL != si) |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 58 | s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si); |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 59 | else |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 60 | s = format (s, "DELETED:%d", rw->sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 63 | s = format (s, " mtu:%d", rw->max_l3_packet_bytes); |
| 64 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | /* Format rewrite string. */ |
| 66 | if (rw->data_bytes > 0) |
Benoît Ganne | 4af1a7f | 2019-03-01 14:14:10 +0100 | [diff] [blame] | 67 | s = format (s, " %U", format_hex_bytes, rw->data, rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
| 69 | return s; |
| 70 | } |
| 71 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 72 | u32 |
| 73 | vnet_tx_node_index_for_sw_interface (vnet_main_t * vnm, u32 sw_if_index) |
| 74 | { |
| 75 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 76 | return (hw->output_node_index); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | vnet_rewrite_init (vnet_main_t * vnm, |
| 81 | u32 sw_if_index, |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 82 | vnet_link_t linkt, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 83 | u32 this_node, u32 next_node, vnet_rewrite_header_t * rw) |
| 84 | { |
| 85 | rw->sw_if_index = sw_if_index; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 86 | rw->next_index = vlib_node_add_next (vnm->vlib_main, this_node, next_node); |
| 87 | rw->max_l3_packet_bytes = |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 88 | vnet_sw_interface_get_mtu (vnm, sw_if_index, vnet_link_to_mtu (linkt)); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 91 | void |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 92 | vnet_rewrite_update_mtu (vnet_main_t * vnm, vnet_link_t linkt, |
| 93 | vnet_rewrite_header_t * rw) |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 94 | { |
| 95 | rw->max_l3_packet_bytes = |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 96 | vnet_sw_interface_get_mtu (vnm, rw->sw_if_index, |
| 97 | vnet_link_to_mtu (linkt)); |
Neale Ranns | ffd78d1 | 2018-02-09 06:05:16 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 101 | vnet_rewrite_for_sw_interface (vnet_main_t * vnm, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 102 | vnet_link_t link_type, |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 103 | u32 sw_if_index, |
| 104 | u32 node_index, |
| 105 | void *dst_address, |
| 106 | vnet_rewrite_header_t * rw, |
| 107 | u32 max_rewrite_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 109 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 110 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 111 | vnet_hw_interface_class_t *hc = |
| 112 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 113 | u8 *rewrite = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 115 | vnet_rewrite_init (vnm, sw_if_index, link_type, node_index, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 116 | vnet_tx_node_index_for_sw_interface (vnm, sw_if_index), |
| 117 | rw); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 119 | ASSERT (hc->build_rewrite); |
| 120 | rewrite = hc->build_rewrite (vnm, sw_if_index, link_type, dst_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 122 | ASSERT (vec_len (rewrite) < max_rewrite_bytes); |
| 123 | vnet_rewrite_set_data_internal (rw, max_rewrite_bytes, rewrite, |
| 124 | vec_len (rewrite)); |
| 125 | vec_free (rewrite); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 128 | void |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 129 | serialize_vnet_rewrite (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 131 | vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | u32 max_data_bytes = va_arg (*va, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 133 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
| 135 | serialize_integer (m, rw->sw_if_index, sizeof (rw->sw_if_index)); |
| 136 | serialize_integer (m, rw->data_bytes, sizeof (rw->data_bytes)); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 137 | serialize_integer (m, rw->max_l3_packet_bytes, |
| 138 | sizeof (rw->max_l3_packet_bytes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | p = serialize_get (m, rw->data_bytes); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 140 | clib_memcpy (p, vnet_rewrite_get_data_internal (rw, max_data_bytes), |
| 141 | rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 144 | void |
| 145 | unserialize_vnet_rewrite (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 147 | vnet_rewrite_header_t *rw = va_arg (*va, vnet_rewrite_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | u32 max_data_bytes = va_arg (*va, u32); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 149 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
| 151 | /* It is up to user to fill these in. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | rw->next_index = ~0; |
| 153 | |
| 154 | unserialize_integer (m, &rw->sw_if_index, sizeof (rw->sw_if_index)); |
| 155 | unserialize_integer (m, &rw->data_bytes, sizeof (rw->data_bytes)); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 156 | unserialize_integer (m, &rw->max_l3_packet_bytes, |
| 157 | sizeof (rw->max_l3_packet_bytes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | p = unserialize_get (m, rw->data_bytes); |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 159 | clib_memcpy (vnet_rewrite_get_data_internal (rw, max_data_bytes), p, |
| 160 | rw->data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 162 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 163 | u8 * |
| 164 | vnet_build_rewrite_for_sw_interface (vnet_main_t * vnm, |
| 165 | u32 sw_if_index, |
| 166 | vnet_link_t link_type, |
| 167 | const void *dst_address) |
| 168 | { |
| 169 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 170 | vnet_hw_interface_class_t *hc = |
| 171 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
| 172 | |
| 173 | ASSERT (hc->build_rewrite); |
| 174 | return (hc->build_rewrite (vnm, sw_if_index, link_type, dst_address)); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | void |
| 179 | vnet_update_adjacency_for_sw_interface (vnet_main_t * vnm, |
| 180 | u32 sw_if_index, u32 ai) |
| 181 | { |
| 182 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 183 | vnet_hw_interface_class_t *hc = |
| 184 | vnet_get_hw_interface_class (vnm, hw->hw_class_index); |
| 185 | |
| 186 | ASSERT (hc->update_adjacency); |
| 187 | hc->update_adjacency (vnm, sw_if_index, ai); |
| 188 | } |
| 189 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 190 | /* |
| 191 | * fd.io coding-style-patch-verification: ON |
| 192 | * |
| 193 | * Local Variables: |
| 194 | * eval: (c-set-style "gnu") |
| 195 | * End: |
| 196 | */ |