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