8435a726553166d3c01488d72913bcfb61a6c62e
[vpp.git] / src / vnet / rewrite.h
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.h: 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 #ifndef included_vnet_rewrite_h
41 #define included_vnet_rewrite_h
42
43 #include <vlib/vlib.h>
44 #include <vnet/l3_types.h>
45
46 /* Consider using vector types for speed? */
47 typedef uword vnet_rewrite_data_t;
48
49 /**
50  * Flags associated with the rewrite/adjacency
51  */
52 typedef enum vnet_rewrite_flags_t_
53 {
54   /**
55    * This adjacency/interface has output features configured
56    */
57   VNET_REWRITE_HAS_FEATURES = (1 << 0),
58 } __attribute__ ((packed)) vnet_rewrite_flags_t;
59
60 /* *INDENT-OFF* */
61 typedef CLIB_PACKED (struct {
62   /* Interface to mark re-written packets with. */
63   u32 sw_if_index;
64
65   /* Next node to feed after packet rewrite is done. */
66   u16 next_index;
67
68   /* Number of bytes in rewrite data. */
69   u16 data_bytes;
70
71   /* Max packet size layer 3 (MTU) for output interface.
72      Used for MTU check after packet rewrite. */
73   u16 max_l3_packet_bytes;
74
75   u16 unused1;
76   u8  unused2;
77
78   vnet_rewrite_flags_t flags;
79
80   /* When dynamically writing a multicast destination L2 addresss
81    * this is the offset within the address to start writing n
82    * bytes of the IP mcast address */
83   u8 dst_mcast_offset;
84
85   /* When dynamically writing a multicast destination L2 addresss
86    * this is the number of bytes of the dest IP address to write into
87    * the MAC rewrite */
88   u8 dst_mcast_n_bytes;
89
90   /* Rewrite string starting at end and going backwards. */
91   u8 data[0];
92 }) vnet_rewrite_header_t;
93 /* *INDENT-ON* */
94
95 /**
96  * At 16 bytes of rewrite herader we have enought space left for a IPv6
97  * (40 bytes) + LISP-GPE (8 bytes) in the cache line
98  */
99 STATIC_ASSERT (sizeof (vnet_rewrite_header_t) <= 16,
100                "Rewrite header too big");
101
102 /*
103   Helper macro for declaring rewrite string w/ given max-size.
104
105   Typical usage:
106     typedef struct {
107       //
108       int a, b;
109
110       // Total adjacency is 64 bytes.
111       vnet_rewrite_declare(64 - 2*sizeof(int)) rw;
112     } my_adjacency_t;
113 */
114 #define vnet_declare_rewrite(total_bytes)                               \
115 struct {                                                                \
116   vnet_rewrite_header_t rewrite_header;                                 \
117                                                                         \
118   u8 rewrite_data[(total_bytes) - sizeof (vnet_rewrite_header_t)];      \
119 }
120
121 always_inline void
122 vnet_rewrite_clear_data_internal (vnet_rewrite_header_t * rw, int max_size)
123 {
124   /* Sanity check values carefully for this memset operation */
125   ASSERT ((max_size > 0) && (max_size < VLIB_BUFFER_PRE_DATA_SIZE));
126
127   rw->data_bytes = 0;
128   memset (rw->data, 0xfe, max_size);
129 }
130
131 always_inline void
132 vnet_rewrite_set_data_internal (vnet_rewrite_header_t * rw,
133                                 int max_size, void *data, int data_bytes)
134 {
135   /* Sanity check values carefully for this memset operation */
136   ASSERT ((max_size > 0) && (max_size < VLIB_BUFFER_PRE_DATA_SIZE));
137   ASSERT ((data_bytes >= 0) && (data_bytes < max_size));
138
139   rw->data_bytes = data_bytes;
140   clib_memcpy (rw->data + max_size - data_bytes, data, data_bytes);
141   memset (rw->data, 0xfe, max_size - data_bytes);
142 }
143
144 #define vnet_rewrite_set_data(rw,data,data_bytes)               \
145   vnet_rewrite_set_data_internal (&((rw).rewrite_header),       \
146                                   sizeof ((rw).rewrite_data),   \
147                                   (data),                       \
148                                   (data_bytes))
149
150 always_inline void *
151 vnet_rewrite_get_data_internal (vnet_rewrite_header_t * rw, int max_size)
152 {
153   ASSERT (rw->data_bytes <= max_size);
154   return rw->data + max_size - rw->data_bytes;
155 }
156
157 #define vnet_rewrite_get_data(rw) \
158   vnet_rewrite_get_data_internal (&((rw).rewrite_header), sizeof ((rw).rewrite_data))
159
160 always_inline void
161 vnet_rewrite_copy_one (vnet_rewrite_data_t * p0, vnet_rewrite_data_t * rw0,
162                        int i)
163 {
164   p0[-i] = rw0[-i];
165 }
166
167 void vnet_rewrite_copy_slow_path (vnet_rewrite_data_t * p0,
168                                   vnet_rewrite_data_t * rw0,
169                                   word n_left, uword most_likely_size);
170
171 /* *INDENT-OFF* */
172 typedef CLIB_PACKED (struct {
173   u64 a;
174   u32 b;
175   u16 c;
176 }) eh_copy_t;
177 /* *INDENT-ON* */
178
179 always_inline void
180 _vnet_rewrite_one_header (vnet_rewrite_header_t * h0,
181                           void *packet0, int max_size, int most_likely_size)
182 {
183   vnet_rewrite_data_t *p0 = packet0;
184   vnet_rewrite_data_t *rw0 = (vnet_rewrite_data_t *) (h0->data + max_size);
185   word n_left0;
186
187   /* 0xfefe => poisoned adjacency => crash */
188   ASSERT (h0->data_bytes != 0xfefe);
189
190   if (PREDICT_TRUE (h0->data_bytes == sizeof (eh_copy_t)))
191     {
192       eh_copy_t *s, *d;
193       s = (eh_copy_t *) (h0->data + max_size - sizeof (eh_copy_t));
194       d = (eh_copy_t *) (((u8 *) packet0) - sizeof (eh_copy_t));
195       clib_memcpy (d, s, sizeof (eh_copy_t));
196       return;
197     }
198
199
200 #define _(i)                                                            \
201   do {                                                                  \
202     if (most_likely_size > ((i)-1)*sizeof (vnet_rewrite_data_t))        \
203       vnet_rewrite_copy_one (p0, rw0, (i));                             \
204   } while (0)
205
206   _(4);
207   _(3);
208   _(2);
209   _(1);
210
211 #undef _
212
213   n_left0 = (int)
214     (((int) h0->data_bytes - most_likely_size) + (sizeof (rw0[0]) - 1))
215     / (int) sizeof (rw0[0]);
216   if (PREDICT_FALSE (n_left0 > 0))
217     vnet_rewrite_copy_slow_path (p0, rw0, n_left0, most_likely_size);
218 }
219
220 always_inline void
221 _vnet_rewrite_two_headers (vnet_rewrite_header_t * h0,
222                            vnet_rewrite_header_t * h1,
223                            void *packet0,
224                            void *packet1, int max_size, int most_likely_size)
225 {
226   vnet_rewrite_data_t *p0 = packet0;
227   vnet_rewrite_data_t *p1 = packet1;
228   vnet_rewrite_data_t *rw0 = (vnet_rewrite_data_t *) (h0->data + max_size);
229   vnet_rewrite_data_t *rw1 = (vnet_rewrite_data_t *) (h1->data + max_size);
230   word n_left0, n_left1;
231   int slow_path;
232
233   /* 0xfefe => poisoned adjacency => crash */
234   ASSERT (h0->data_bytes != 0xfefe);
235   ASSERT (h1->data_bytes != 0xfefe);
236
237   /* Arithmetic calculation: bytes0 == bytes1 == 14 */
238   slow_path = h0->data_bytes ^ h1->data_bytes;
239   slow_path += h0->data_bytes ^ sizeof (eh_copy_t);
240
241   if (PREDICT_TRUE (slow_path == 0))
242     {
243       eh_copy_t *s0, *d0, *s1, *d1;
244       s0 = (eh_copy_t *) (h0->data + max_size - sizeof (eh_copy_t));
245       d0 = (eh_copy_t *) (((u8 *) packet0) - sizeof (eh_copy_t));
246       clib_memcpy (d0, s0, sizeof (eh_copy_t));
247       s1 = (eh_copy_t *) (h1->data + max_size - sizeof (eh_copy_t));
248       d1 = (eh_copy_t *) (((u8 *) packet1) - sizeof (eh_copy_t));
249       clib_memcpy (d1, s1, sizeof (eh_copy_t));
250       return;
251     }
252
253 #define _(i)                                                            \
254   do {                                                                  \
255     if (most_likely_size > ((i)-1)*sizeof (vnet_rewrite_data_t))        \
256       {                                                                 \
257         vnet_rewrite_copy_one (p0, rw0, (i));                           \
258         vnet_rewrite_copy_one (p1, rw1, (i));                           \
259       }                                                                 \
260   } while (0)
261
262   _(4);
263   _(3);
264   _(2);
265   _(1);
266
267 #undef _
268
269   n_left0 = (int)
270     (((int) h0->data_bytes - most_likely_size) + (sizeof (rw0[0]) - 1))
271     / (int) sizeof (rw0[0]);
272   n_left1 = (int)
273     (((int) h1->data_bytes - most_likely_size) + (sizeof (rw1[0]) - 1))
274     / (int) sizeof (rw1[0]);
275
276   if (PREDICT_FALSE (n_left0 > 0 || n_left1 > 0))
277     {
278       vnet_rewrite_copy_slow_path (p0, rw0, n_left0, most_likely_size);
279       vnet_rewrite_copy_slow_path (p1, rw1, n_left1, most_likely_size);
280     }
281 }
282
283 #define vnet_rewrite_one_header(rw0,p0,most_likely_size)        \
284   _vnet_rewrite_one_header (&((rw0).rewrite_header), (p0),      \
285                             sizeof ((rw0).rewrite_data),        \
286                             (most_likely_size))
287
288 #define vnet_rewrite_two_headers(rw0,rw1,p0,p1,most_likely_size)        \
289   _vnet_rewrite_two_headers (&((rw0).rewrite_header), &((rw1).rewrite_header), \
290                              (p0), (p1),                                \
291                              sizeof ((rw0).rewrite_data),               \
292                              (most_likely_size))
293
294 always_inline void
295 _vnet_fixup_one_header (vnet_rewrite_header_t * h0,
296                         u8 * addr, u32 addr_len,
297                         u8 * packet0, int clear_first_bit)
298 {
299   /* location to write to in the packet */
300   u8 *p0 = packet0 - h0->dst_mcast_offset;
301   u8 *p1 = p0;
302   /* location to write from in the L3 dest address */
303   u8 *a0 = addr + addr_len - h0->dst_mcast_n_bytes;
304
305   clib_memcpy (p0, a0, h0->dst_mcast_n_bytes);
306   if (clear_first_bit)
307     *p1 &= 0x7f;
308 }
309
310 #define vnet_fixup_one_header(rw0,addr,p0,clear_first_bit)              \
311   _vnet_fixup_one_header (&((rw0).rewrite_header),                      \
312                           (u8*)(addr), sizeof((*addr)),                 \
313                           (u8*)(p0), (clear_first_bit))
314
315 #define VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST ((void *) 0)
316 /** Deprecated */
317 void vnet_rewrite_for_sw_interface (struct vnet_main_t *vnm,
318                                     vnet_link_t packet_type,
319                                     u32 sw_if_index,
320                                     u32 node_index,
321                                     void *dst_address,
322                                     vnet_rewrite_header_t * rw,
323                                     u32 max_rewrite_bytes);
324
325 u32 vnet_tx_node_index_for_sw_interface (struct vnet_main_t *vnm,
326                                          u32 sw_if_index);
327
328 void vnet_rewrite_init (struct vnet_main_t *vnm,
329                         u32 sw_if_index,
330                         u32 this_node,
331                         u32 next_node, vnet_rewrite_header_t * rw);
332
333 u8 *vnet_build_rewrite_for_sw_interface (struct vnet_main_t *vnm,
334                                          u32 sw_if_index,
335                                          vnet_link_t packet_type,
336                                          const void *dst_address);
337 void vnet_update_adjacency_for_sw_interface (struct vnet_main_t *vnm,
338                                              u32 sw_if_index, u32 ai);
339
340 format_function_t format_vnet_rewrite;
341
342 serialize_function_t serialize_vnet_rewrite, unserialize_vnet_rewrite;
343
344 #endif /* included_vnet_rewrite_h */
345
346 /*
347  * fd.io coding-style-patch-verification: ON
348  *
349  * Local Variables:
350  * eval: (c-set-style "gnu")
351  * End:
352  */