09abedd5e747bcb207c92b13f7971a7ff4bfeff2
[vpp.git] / src / vnet / buffer.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  * vnet/buffer.h: vnet buffer flags
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_buffer_h
41 #define included_vnet_buffer_h
42
43 #include <vlib/vlib.h>
44
45 /**
46  * Flags that are set in the high order bits of ((vlib_buffer*)b)->flags
47  *
48  */
49 #define foreach_vnet_buffer_flag                                              \
50   _ (1, L4_CHECKSUM_COMPUTED, "l4-cksum-computed", 1)                         \
51   _ (2, L4_CHECKSUM_CORRECT, "l4-cksum-correct", 1)                           \
52   _ (3, VLAN_2_DEEP, "vlan-2-deep", 1)                                        \
53   _ (4, VLAN_1_DEEP, "vlan-1-deep", 1)                                        \
54   _ (5, SPAN_CLONE, "span-clone", 1)                                          \
55   _ (6, LOOP_COUNTER_VALID, "loop-counter-valid", 0)                          \
56   _ (7, LOCALLY_ORIGINATED, "local", 1)                                       \
57   _ (8, IS_IP4, "ip4", 1)                                                     \
58   _ (9, IS_IP6, "ip6", 1)                                                     \
59   _ (10, OFFLOAD, "offload", 0)                                               \
60   _ (11, IS_NATED, "natted", 1)                                               \
61   _ (12, L2_HDR_OFFSET_VALID, "l2_hdr_offset_valid", 0)                       \
62   _ (13, L3_HDR_OFFSET_VALID, "l3_hdr_offset_valid", 0)                       \
63   _ (14, L4_HDR_OFFSET_VALID, "l4_hdr_offset_valid", 0)                       \
64   _ (15, FLOW_REPORT, "flow-report", 1)                                       \
65   _ (16, IS_DVR, "dvr", 1)                                                    \
66   _ (17, QOS_DATA_VALID, "qos-data-valid", 0)                                 \
67   _ (18, GSO, "gso", 0)                                                       \
68   _ (19, AVAIL1, "avail1", 1)                                                 \
69   _ (20, AVAIL2, "avail2", 1)                                                 \
70   _ (21, AVAIL3, "avail3", 1)                                                 \
71   _ (22, AVAIL4, "avail4", 1)                                                 \
72   _ (23, AVAIL5, "avail5", 1)                                                 \
73   _ (24, AVAIL6, "avail6", 1)                                                 \
74   _ (25, AVAIL7, "avail7", 1)                                                 \
75   _ (26, AVAIL8, "avail8", 1)                                                 \
76   _ (27, AVAIL9, "avail9", 1)
77
78 /*
79  * Please allocate the FIRST available bit, redefine
80  * AVAIL 1 ... AVAILn-1, and remove AVAILn. Please maintain the
81  * VNET_BUFFER_FLAGS_ALL_AVAIL definition.
82  */
83
84 #define VNET_BUFFER_FLAGS_ALL_AVAIL                                           \
85   (VNET_BUFFER_F_AVAIL1 | VNET_BUFFER_F_AVAIL2 | VNET_BUFFER_F_AVAIL3 |       \
86    VNET_BUFFER_F_AVAIL4 | VNET_BUFFER_F_AVAIL5 | VNET_BUFFER_F_AVAIL6 |       \
87    VNET_BUFFER_F_AVAIL7 | VNET_BUFFER_F_AVAIL8 | VNET_BUFFER_F_AVAIL9)
88
89 #define VNET_BUFFER_FLAGS_VLAN_BITS \
90   (VNET_BUFFER_F_VLAN_1_DEEP | VNET_BUFFER_F_VLAN_2_DEEP)
91
92 enum
93 {
94 #define _(bit, name, s, v) VNET_BUFFER_F_##name  = (1 << LOG2_VLIB_BUFFER_FLAG_USER(bit)),
95   foreach_vnet_buffer_flag
96 #undef _
97 };
98
99 enum
100 {
101 #define _(bit, name, s, v) VNET_BUFFER_F_LOG2_##name  = LOG2_VLIB_BUFFER_FLAG_USER(bit),
102   foreach_vnet_buffer_flag
103 #undef _
104 };
105
106 /* Make sure that the vnet and vlib bits are disjoint */
107 STATIC_ASSERT (((VNET_BUFFER_FLAGS_ALL_AVAIL & VLIB_BUFFER_FLAGS_ALL) == 0),
108                "VLIB / VNET buffer flags overlap");
109
110 #define foreach_vnet_buffer_offload_flag                                      \
111   _ (0, IP_CKSUM, "offload-ip-cksum", 1)                                      \
112   _ (1, TCP_CKSUM, "offload-tcp-cksum", 1)                                    \
113   _ (2, UDP_CKSUM, "offload-udp-cksum", 1)                                    \
114   _ (3, OUTER_IP_CKSUM, "offload-outer-ip-cksum", 1)                          \
115   _ (4, OUTER_UDP_CKSUM, "offload-outer-udp-cksum", 1)                        \
116   _ (5, TNL_VXLAN, "offload-vxlan-tunnel", 1)                                 \
117   _ (6, TNL_IPIP, "offload-ipip-tunnel", 1)
118
119 typedef enum
120 {
121 #define _(bit, name, s, v) VNET_BUFFER_OFFLOAD_F_##name = (1 << bit),
122   foreach_vnet_buffer_offload_flag
123 #undef _
124 } vnet_buffer_oflags_t;
125
126 #define VNET_BUFFER_OFFLOAD_F_TNL_MASK                                        \
127   (VNET_BUFFER_OFFLOAD_F_TNL_VXLAN | VNET_BUFFER_OFFLOAD_F_TNL_IPIP)
128
129 #define foreach_buffer_opaque_union_subtype     \
130 _(ip)                                           \
131 _(l2)                                           \
132 _(l2t)                                          \
133 _(l2_classify)                                  \
134 _(policer)                                      \
135 _(ipsec)                                        \
136 _(map)                                          \
137 _(map_t)                                        \
138 _(ip_frag)                                      \
139 _(mpls)                                         \
140 _(tcp)
141
142 /*
143  * vnet stack buffer opaque array overlay structure.
144  * The vnet_buffer_opaque_t *must* be the same size as the
145  * vlib_buffer_t "opaque" structure member, 32 bytes.
146  *
147  * When adding a union type, please add a stanza to
148  * foreach_buffer_opaque_union_subtype (directly above).
149  * Code in vnet_interface_init(...) verifies the size
150  * of the union, and will announce any deviations in an
151  * impossible-to-miss manner.
152  */
153 typedef struct
154 {
155   u32 sw_if_index[VLIB_N_RX_TX];
156   i16 l2_hdr_offset;
157   i16 l3_hdr_offset;
158   i16 l4_hdr_offset;
159   u8 feature_arc_index;
160   /* offload flags */
161   vnet_buffer_oflags_t oflags : 8;
162
163   union
164   {
165     /* IP4/6 buffer opaque. */
166     struct
167     {
168       /* Adjacency from destination IP address lookup [VLIB_TX].
169          Adjacency from source IP address lookup [VLIB_RX].
170          This gets set to ~0 until source lookup is performed. */
171       u32 adj_index[VLIB_N_RX_TX];
172
173       union
174       {
175         struct
176         {
177           /* Flow hash value for this packet computed from IP src/dst address
178              protocol and ports. */
179           u32 flow_hash;
180
181           union
182           {
183             /* next protocol */
184             u32 save_protocol;
185
186             /* Hint for transport protocols */
187             u32 fib_index;
188           };
189
190           /* Rewrite length */
191           u8 save_rewrite_length;
192
193           union
194           {
195             /* sw_if_index of the local interface the buffer was received on
196              * - if hitting a DPO_RECEIVE - it is set in ip[46]-receive.
197              * This is ~0 if the dpo is not a receive dpo, or if the
198              * interface is not specified (e.g. route add via local) */
199             u32 rx_sw_if_index;
200
201             /* MFIB RPF ID */
202             u32 rpf_id;
203           };
204         };
205
206         /* ICMP */
207         struct
208         {
209           u8 type;
210           u8 code;
211           u32 data;
212         } icmp;
213
214         /* reassembly */
215         union
216         {
217           /* group input/output to simplify the code, this way
218            * we can handoff while keeping input variables intact */
219           struct
220           {
221             /* input variables */
222             struct
223             {
224               u32 next_index;   /* index of next node - used by custom apps */
225               u32 error_next_index;     /* index of next node if error - used by custom apps */
226             };
227             /* handoff variables */
228             struct
229             {
230               u16 owner_thread_index;
231             };
232           };
233           /* output variables */
234           struct
235           {
236             union
237             {
238               /* shallow virtual reassembly output variables */
239               struct
240               {
241                 u16 l4_src_port;        /* tcp/udp/icmp src port */
242                 u16 l4_dst_port;        /* tcp/udp/icmp dst port */
243                 u32 tcp_ack_number;
244                 u8 save_rewrite_length;
245                 u8 ip_proto;    /* protocol in ip header */
246                 u8 icmp_type_or_tcp_flags;
247                 u8 is_non_first_fragment;
248                 u32 tcp_seq_number;
249               };
250               /* full reassembly output variables */
251               struct
252               {
253                 u16 estimated_mtu;      /* estimated MTU calculated during reassembly */
254               };
255             };
256           };
257           /* internal variables used during reassembly */
258           struct
259           {
260             u16 fragment_first;
261             u16 fragment_last;
262             u16 range_first;
263             u16 range_last;
264             u32 next_range_bi;
265             u16 ip6_frag_hdr_offset;
266           };
267         } reass;
268       };
269     } ip;
270
271     /*
272      * MPLS:
273      * data copied from the MPLS header that was popped from the packet
274      * during the look-up.
275      */
276     struct
277     {
278       /* do not overlay w/ ip.adj_index[0,1] nor flow hash */
279       u32 pad[VLIB_N_RX_TX + 1];
280       u8 ttl;
281       u8 exp;
282       u8 first;
283       u8 pyld_proto:3;          /* dpo_proto_t */
284       u8 rsvd:5;
285       /* Rewrite length */
286       u8 save_rewrite_length;
287       /* Save the mpls header length including all label stack */
288       u8 mpls_hdr_length;
289       /*
290        * BIER - the number of bytes in the header.
291        *  the len field in the header is not authoritative. It's the
292        * value in the table that counts.
293        */
294       struct
295       {
296         u8 n_bytes;
297       } bier;
298     } mpls;
299
300     /* l2 bridging path, only valid there */
301     struct opaque_l2
302     {
303       u32 feature_bitmap;
304       u16 bd_index;             /* bridge-domain index */
305       u16 l2fib_sn;             /* l2fib bd/int seq_num */
306       u8 l2_len;                /* ethernet header length */
307       u8 shg;                   /* split-horizon group */
308       u8 bd_age;                /* aging enabled */
309     } l2;
310
311     /* l2tpv3 softwire encap, only valid there */
312     struct
313     {
314       u32 pad[4];               /* do not overlay w/ ip.adj_index[0,1] */
315       u8 next_index;
316       u32 session_index;
317     } l2t;
318
319     /* L2 classify */
320     struct
321     {
322       struct opaque_l2 pad;
323       union
324       {
325         u32 table_index;
326         u32 opaque_index;
327       };
328       u64 hash;
329     } l2_classify;
330
331     /* vnet policer */
332     struct
333     {
334       u32 pad[8 - VLIB_N_RX_TX - 1];    /* to end of opaque */
335       u32 index;
336     } policer;
337
338     /* interface output features */
339     struct
340     {
341       /* don't overlap the adjcencies nor flow-hash */
342       u32 __pad[3];
343       u32 sad_index;
344       u32 protect_index;
345       u16 thread_index;
346     } ipsec;
347
348     /* MAP */
349     struct
350     {
351       u16 mtu;
352     } map;
353
354     /* MAP-T */
355     struct
356     {
357       u32 map_domain_index;
358       struct
359       {
360         u32 saddr, daddr;
361         u16 frag_offset;        //Fragmentation header offset
362         u16 l4_offset;          //L4 header overall offset
363         u8 l4_protocol;         //The final protocol number
364       } v6;                     //Used by ip6_map_t only
365       u16 checksum_offset;      //L4 checksum overall offset
366       u16 mtu;                  //Exit MTU
367     } map_t;
368
369     /* IP Fragmentation */
370     struct
371     {
372       u32 pad[2];               /* do not overlay w/ ip.adj_index[0,1] */
373       u16 mtu;
374       u8 next_index;
375       u8 flags;                 //See ip_frag.h
376     } ip_frag;
377
378     /* COP - configurable junk filter(s) */
379     struct
380     {
381       /* Current configuration index. */
382       u32 current_config_index;
383     } cop;
384
385     /* LISP */
386     struct
387     {
388       /* overlay address family */
389       u16 overlay_afi;
390     } lisp;
391
392     /* TCP */
393     struct
394     {
395       u32 connection_index;
396       union
397       {
398         u32 seq_number;
399         u32 next_node_opaque;
400       };
401       u32 seq_end;
402       u32 ack_number;
403       u16 hdr_offset;           /**< offset relative to ip hdr */
404       u16 data_offset;          /**< offset relative to ip hdr */
405       u16 data_len;             /**< data len */
406       u8 flags;
407     } tcp;
408
409     /* SNAT */
410     struct
411     {
412       u32 flags;
413       u32 required_thread_index;
414     } snat;
415
416     u32 unused[6];
417   };
418 } vnet_buffer_opaque_t;
419
420 #define VNET_REWRITE_TOTAL_BYTES 128
421 STATIC_ASSERT (VNET_REWRITE_TOTAL_BYTES <= VLIB_BUFFER_PRE_DATA_SIZE,
422                "VNET_REWRITE_TOTAL_BYTES too big");
423
424 STATIC_ASSERT (STRUCT_SIZE_OF (vnet_buffer_opaque_t, ip.save_rewrite_length)
425                == STRUCT_SIZE_OF (vnet_buffer_opaque_t,
426                                   ip.reass.save_rewrite_length)
427                && STRUCT_SIZE_OF (vnet_buffer_opaque_t,
428                                   ip.reass.save_rewrite_length) ==
429                STRUCT_SIZE_OF (vnet_buffer_opaque_t, mpls.save_rewrite_length)
430                && STRUCT_SIZE_OF (vnet_buffer_opaque_t,
431                                   mpls.save_rewrite_length) == 1
432                && VNET_REWRITE_TOTAL_BYTES < UINT8_MAX,
433                "save_rewrite_length member must be able to hold the max value of rewrite length");
434
435 STATIC_ASSERT (STRUCT_OFFSET_OF (vnet_buffer_opaque_t, ip.save_rewrite_length)
436                == STRUCT_OFFSET_OF (vnet_buffer_opaque_t,
437                                     ip.reass.save_rewrite_length)
438                && STRUCT_OFFSET_OF (vnet_buffer_opaque_t,
439                                     mpls.save_rewrite_length) ==
440                STRUCT_OFFSET_OF (vnet_buffer_opaque_t,
441                                  ip.reass.save_rewrite_length),
442                "save_rewrite_length must be aligned so that reass doesn't overwrite it");
443
444 /*
445  * The opaque field of the vlib_buffer_t is interpreted as a
446  * vnet_buffer_opaque_t. Hence it should be big enough to accommodate one.
447  */
448 STATIC_ASSERT (sizeof (vnet_buffer_opaque_t) <=
449                STRUCT_SIZE_OF (vlib_buffer_t, opaque),
450                "VNET buffer meta-data too large for vlib_buffer");
451
452 #define vnet_buffer(b) ((vnet_buffer_opaque_t *) (b)->opaque)
453
454 /* Full cache line (64 bytes) of additional space */
455 typedef struct
456 {
457   /**
458    * QoS marking data that needs to persist from the recording nodes
459    * (nominally in the ingress path) to the marking node (in the
460    * egress path)
461    */
462   struct
463   {
464     u8 bits;
465     u8 source;
466   } qos;
467
468   u8 loop_counter;
469   u8 __unused[1];
470
471   /* Group Based Policy */
472   struct
473   {
474     u8 __unused;
475     u8 flags;
476     u16 sclass;
477   } gbp;
478
479   /**
480    * The L4 payload size set on input on GSO enabled interfaces
481    * when we receive a GSO packet (a chain of buffers with the first one
482    * having GSO bit set), and needs to persist all the way to the interface-output,
483    * in case the egress interface is not GSO-enabled - then we need to perform
484    * the segmentation, and use this value to cut the payload appropriately.
485    */
486   struct
487   {
488     u16 gso_size;
489     /* size of L4 prototol header */
490     u16 gso_l4_hdr_sz;
491     i16 outer_l3_hdr_offset;
492     i16 outer_l4_hdr_offset;
493   };
494
495   struct
496   {
497     u32 arc_next;
498     union
499     {
500       u32 cached_session_index;
501       u32 cached_dst_nat_session_index;
502     };
503   } nat;
504
505   union
506   {
507     struct
508     {
509       u64 pad[1];
510       u64 pg_replay_timestamp;
511     };
512     u32 unused[8];
513   };
514 } vnet_buffer_opaque2_t;
515
516 #define vnet_buffer2(b) ((vnet_buffer_opaque2_t *) (b)->opaque2)
517
518 /*
519  * The opaque2 field of the vlib_buffer_t is interpreted as a
520  * vnet_buffer_opaque2_t. Hence it should be big enough to accommodate one.
521  */
522 STATIC_ASSERT (sizeof (vnet_buffer_opaque2_t) <=
523                STRUCT_SIZE_OF (vlib_buffer_t, opaque2),
524                "VNET buffer opaque2 meta-data too large for vlib_buffer");
525
526 #define gso_mtu_sz(b) (vnet_buffer2(b)->gso_size + \
527                        vnet_buffer2(b)->gso_l4_hdr_sz + \
528                        vnet_buffer(b)->l4_hdr_offset - \
529                        vnet_buffer (b)->l3_hdr_offset)
530
531 format_function_t format_vnet_buffer_no_chain;
532 format_function_t format_vnet_buffer;
533 format_function_t format_vnet_buffer_offload;
534 format_function_t format_vnet_buffer_flags;
535 format_function_t format_vnet_buffer_opaque;
536 format_function_t format_vnet_buffer_opaque2;
537
538 static_always_inline void
539 vnet_buffer_offload_flags_set (vlib_buffer_t *b, vnet_buffer_oflags_t oflags)
540 {
541   if (b->flags & VNET_BUFFER_F_OFFLOAD)
542     {
543       /* add a flag to existing offload */
544       vnet_buffer (b)->oflags |= oflags;
545     }
546   else
547     {
548       /* no offload yet: reset offload flags to new value */
549       vnet_buffer (b)->oflags = oflags;
550       b->flags |= VNET_BUFFER_F_OFFLOAD;
551     }
552 }
553
554 static_always_inline void
555 vnet_buffer_offload_flags_clear (vlib_buffer_t *b, vnet_buffer_oflags_t oflags)
556 {
557   vnet_buffer (b)->oflags &= ~oflags;
558   if (0 == vnet_buffer (b)->oflags)
559     b->flags &= ~VNET_BUFFER_F_OFFLOAD;
560 }
561
562 #endif /* included_vnet_buffer_h */
563
564 /*
565  * fd.io coding-style-patch-verification: ON
566  *
567  * Local Variables:
568  * eval: (c-set-style "gnu")
569  * End:
570  */