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