FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
tcp_packet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #ifndef included_tcp_packet_h
17 #define included_tcp_packet_h
18 
19 #include <vnet/vnet.h>
20 
21 /* TCP flags bit 0 first. */
22 #define foreach_tcp_flag \
23  _ (FIN) /**< No more data from sender. */ \
24  _ (SYN) /**< Synchronize sequence numbers. */ \
25  _ (RST) /**< Reset the connection. */ \
26  _ (PSH) /**< Push function. */ \
27  _ (ACK) /**< Ack field significant. */ \
28  _ (URG) /**< Urgent pointer field significant. */ \
29  _ (ECE) /**< ECN-echo. Receiver got CE packet */ \
30  _ (CWR) /**< Sender reduced congestion window */
31 
32 enum
33 {
34 #define _(f) TCP_FLAG_BIT_##f,
36 #undef _
38 };
39 
40 enum
41 {
42 #define _(f) TCP_FLAG_##f = 1 << TCP_FLAG_BIT_##f,
44 #undef _
45 };
46 
47 typedef struct _tcp_header
48 {
49  union
50  {
51  struct
52  {
53  u16 src_port; /**< Source port. */
54  u16 dst_port; /**< Destination port. */
55  };
56  struct
57  {
58  u16 src, dst;
59  };
60  };
61 
62  u32 seq_number; /**< Sequence number of the first data octet in this
63  * segment, except when SYN is present. If SYN
64  * is present the seq number is is the ISN and the
65  * first data octet is ISN+1 */
66  u32 ack_number; /**< Acknowledgement number if ACK is set. It contains
67  * the value of the next sequence number the sender
68  * of the segment is expecting to receive. */
69  u8 data_offset_and_reserved;
70  u8 flags; /**< Flags: see the macro above */
71  u16 window; /**< Number of bytes sender is willing to receive. */
72 
73  u16 checksum; /**< Checksum of TCP pseudo header and data. */
74  u16 urgent_pointer; /**< Seq number of the byte after the urgent data. */
75 } __attribute__ ((packed)) tcp_header_t;
76 
77 /* Flag tests that return 0 or !0 */
78 #define tcp_doff(_th) ((_th)->data_offset_and_reserved >> 4)
79 #define tcp_fin(_th) ((_th)->flags & TCP_FLAG_FIN)
80 #define tcp_syn(_th) ((_th)->flags & TCP_FLAG_SYN)
81 #define tcp_rst(_th) ((_th)->flags & TCP_FLAG_RST)
82 #define tcp_psh(_th) ((_th)->flags & TCP_FLAG_PSH)
83 #define tcp_ack(_th) ((_th)->flags & TCP_FLAG_ACK)
84 #define tcp_urg(_th) ((_th)->flags & TCP_FLAG_URG)
85 #define tcp_ece(_th) ((_th)->flags & TCP_FLAG_ECE)
86 #define tcp_cwr(_th) ((_th)->flags & TCP_FLAG_CWR)
87 
88 /* Flag tests that return 0 or 1 */
89 #define tcp_is_syn(_th) !!((_th)->flags & TCP_FLAG_SYN)
90 #define tcp_is_fin(_th) !!((_th)->flags & TCP_FLAG_FIN)
91 
92 always_inline int
94 {
95  return tcp_doff (t) * sizeof (u32);
96 }
97 
98 /*
99  * TCP options.
100  */
101 
102 typedef enum tcp_option_type
103 {
104  TCP_OPTION_EOL = 0, /**< End of options. */
105  TCP_OPTION_NOOP = 1, /**< No operation. */
106  TCP_OPTION_MSS = 2, /**< Limit MSS. */
107  TCP_OPTION_WINDOW_SCALE = 3, /**< Window scale. */
108  TCP_OPTION_SACK_PERMITTED = 4, /**< Selective Ack permitted. */
109  TCP_OPTION_SACK_BLOCK = 5, /**< Selective Ack block. */
110  TCP_OPTION_TIMESTAMP = 8, /**< Timestamps. */
111  TCP_OPTION_UTO = 28, /**< User timeout. */
112  TCP_OPTION_AO = 29, /**< Authentication Option. */
114 
115 #define foreach_tcp_options_flag \
116  _ (MSS) /**< MSS advertised in SYN */ \
117  _ (TSTAMP) /**< Timestamp capability advertised in SYN */ \
118  _ (WSCALE) /**< Wnd scale capability advertised in SYN */ \
119  _ (SACK_PERMITTED) /**< SACK capability advertised in SYN */ \
120  _ (SACK) /**< SACK present */
121 
122 enum
123 {
124 #define _(f) TCP_OPTS_FLAG_BIT_##f,
126 #undef _
128 };
129 
130 enum
131 {
132 #define _(f) TCP_OPTS_FLAG_##f = 1 << TCP_OPTS_FLAG_BIT_##f,
134 #undef _
135 };
136 
137 typedef struct _sack_block
138 {
139  u32 start; /**< Start sequence number */
140  u32 end; /**< End sequence number (first outside) */
141 } sack_block_t;
142 
143 typedef struct
144 {
145  u8 flags; /** Option flags, see above */
146 
147  u16 mss; /**< Maximum segment size advertised */
148  u8 wscale; /**< Window scale advertised */
149  u32 tsval; /**< Timestamp value */
150  u32 tsecr; /**< Echoed/reflected time stamp */
151  sack_block_t *sacks; /**< SACK blocks */
152  u8 n_sack_blocks; /**< Number of SACKs blocks */
153 } tcp_options_t;
154 
155 /* Flag tests that return 0 or !0 */
156 #define tcp_opts_mss(_to) ((_to)->flags & TCP_OPTS_FLAG_MSS)
157 #define tcp_opts_tstamp(_to) ((_to)->flags & TCP_OPTS_FLAG_TSTAMP)
158 #define tcp_opts_wscale(_to) ((_to)->flags & TCP_OPTS_FLAG_WSCALE)
159 #define tcp_opts_sack(_to) ((_to)->flags & TCP_OPTS_FLAG_SACK)
160 #define tcp_opts_sack_permitted(_to) ((_to)->flags & TCP_OPTS_FLAG_SACK_PERMITTED)
161 
162 /* TCP option lengths */
163 #define TCP_OPTION_LEN_EOL 1
164 #define TCP_OPTION_LEN_NOOP 1
165 #define TCP_OPTION_LEN_MSS 4
166 #define TCP_OPTION_LEN_WINDOW_SCALE 3
167 #define TCP_OPTION_LEN_SACK_PERMITTED 2
168 #define TCP_OPTION_LEN_TIMESTAMP 10
169 #define TCP_OPTION_LEN_SACK_BLOCK 8
170 
171 #define TCP_HDR_LEN_MAX 60
172 #define TCP_WND_MAX 65535U
173 #define TCP_MAX_WND_SCALE 14 /* See RFC 1323 */
174 #define TCP_OPTS_ALIGN 4
175 #define TCP_OPTS_MAX_SACK_BLOCKS 3
176 #endif /* included_tcp_packet_h */
177 
178 /*
179  * fd.io coding-style-patch-verification: ON
180  *
181  * Local Variables:
182  * eval: (c-set-style "gnu")
183  * End:
184  */
End of options.
Definition: tcp_packet.h:104
struct _sack_block sack_block_t
Selective Ack permitted.
Definition: tcp_packet.h:108
#define tcp_doff(_th)
Definition: tcp_packet.h:78
No operation.
Definition: tcp_packet.h:105
u8 n_sack_blocks
Number of SACKs blocks.
Definition: tcp_packet.h:152
struct _tcp_header tcp_header_t
unsigned char u8
Definition: types.h:56
u8 wscale
Window scale advertised.
Definition: tcp_packet.h:148
User timeout.
Definition: tcp_packet.h:111
Limit MSS.
Definition: tcp_packet.h:106
#define always_inline
Definition: clib.h:92
enum tcp_option_type tcp_option_type_t
sack_block_t * sacks
SACK blocks.
Definition: tcp_packet.h:151
unsigned int u32
Definition: types.h:88
#define foreach_tcp_options_flag
SACK present.
Definition: tcp_packet.h:115
unsigned short u16
Definition: types.h:57
Selective Ack block.
Definition: tcp_packet.h:109
u16 mss
Option flags, see above.
Definition: tcp_packet.h:147
#define foreach_tcp_flag
Sender reduced congestion window.
Definition: tcp_packet.h:22
u32 tsval
Timestamp value.
Definition: tcp_packet.h:149
u32 tsecr
Echoed/reflected time stamp.
Definition: tcp_packet.h:150
Authentication Option.
Definition: tcp_packet.h:112
static int tcp_header_bytes(tcp_header_t *t)
Definition: tcp_packet.h:93
Window scale.
Definition: tcp_packet.h:107
Timestamps.
Definition: tcp_packet.h:110
u32 flags
Definition: vhost-user.h:77
tcp_option_type
Definition: tcp_packet.h:102