TAS
TCP Acceleration as an OS Service
packet_defs.h
1 /*
2  * Copyright 2019 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef PACKET_DEFS_H_
26 #define PACKET_DEFS_H_
27 
28 #include <stdint.h>
29 
30 #include <utils.h>
31 
32 /******************************************************************************/
33 /* Ethernet */
34 
35 #define ETH_ADDR_LEN 6
36 
37 #define ETH_TYPE_IP 0x0800
38 #define ETH_TYPE_ARP 0x0806
39 
40 struct eth_addr {
41  uint8_t addr[ETH_ADDR_LEN];
42 } __attribute__ ((packed));
43 
44 struct eth_hdr {
45  struct eth_addr dest;
46  struct eth_addr src;
47  beui16_t type;
48 } __attribute__ ((packed));
49 
50 /******************************************************************************/
51 /* IPv4 */
52 
53 #define IPH_V(hdr) ((hdr)->_v_hl >> 4)
54 #define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f)
55 #define IPH_TOS(hdr) ((hdr)->_tos)
56 #define IPH_ECN(hdr) ((hdr)->_tos & 0x3)
57 
58 #define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = (((v) << 4) | (hl))
59 #define IPH_TOS_SET(hdr, tos) (hdr)->_tos = (tos)
60 #define IPH_ECN_SET(hdr, e) (hdr)->_tos = ((hdr)->_tos & 0xffc) | (e)
61 
62 #define IP_HLEN 20
63 
64 #define IP_PROTO_IP 0
65 #define IP_PROTO_ICMP 1
66 #define IP_PROTO_IGMP 2
67 #define IP_PROTO_IPENCAP 4
68 #define IP_PROTO_UDP 17
69 #define IP_PROTO_UDPLITE 136
70 #define IP_PROTO_TCP 6
71 #define IP_PROTO_DCCP 33
72 
73 #define IP_ECN_NONE 0x0
74 #define IP_ECN_ECT0 0x2
75 #define IP_ECN_ECT1 0x1
76 #define IP_ECN_CE 0x3
77 
78 typedef beui32_t ip_addr_t;
79 
80 struct ip_hdr {
81  /* version / header length */
82  uint8_t _v_hl;
83  /* type of service */
84  uint8_t _tos;
85  /* total length */
86  beui16_t len;
87  /* identification */
88  beui16_t id;
89  /* fragment offset field */
90  beui16_t offset;
91  /* time to live */
92  uint8_t ttl;
93  /* protocol*/
94  uint8_t proto;
95  /* checksum */
96  uint16_t chksum;
97  /* source and destination IP addresses */
98  ip_addr_t src;
99  ip_addr_t dest;
100 } __attribute__ ((packed));
101 
102 /******************************************************************************/
103 /* ARP */
104 
105 #define ARP_OPER_REQUEST 1
106 #define ARP_OPER_REPLY 2
107 #define ARP_HTYPE_ETHERNET 1
108 #define ARP_PTYPE_IPV4 0x0800
109 
110 struct arp_hdr {
111  beui16_t htype;
112  beui16_t ptype;
113  uint8_t hlen;
114  uint8_t plen;
115  beui16_t oper;
116  struct eth_addr sha;
117  ip_addr_t spa;
118  struct eth_addr tha;
119  ip_addr_t tpa;
120 } __attribute__((packed));
121 
122 
123 /******************************************************************************/
124 /* TCP */
125 
126 #define TCP_FIN 0x01U
127 #define TCP_SYN 0x02U
128 #define TCP_RST 0x04U
129 #define TCP_PSH 0x08U
130 #define TCP_ACK 0x10U
131 #define TCP_URG 0x20U
132 #define TCP_ECE 0x40U
133 #define TCP_CWR 0x80U
134 #define TCP_NS 0x100U
135 
136 #define TCP_FLAGS 0x1ffU
137 
138 /* Length of the TCP header, excluding options. */
139 #define TCP_HLEN 20
140 
141 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
142 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
143 
144 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
145 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & PP_HTONS((uint16_t)(~(uint16_t)(TCP_FLAGS)))) | htons(flags))
146 #define TCPH_HDRLEN_FLAGS_SET(phdr, len, flags) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | (flags))
147 
148 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags))
149 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
150 
151 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0))
152 
154 #define TCP_BUILD_MSS_OPTION(mss) htonl(0x02040000 | ((mss) & 0xFFFF))
155 
156 #define TCP_BUILD_SACK_OPTION htonl(0x04020101)
157 
158 struct tcp_hdr {
159  beui16_t src;
160  beui16_t dest;
161  beui32_t seqno;
162  beui32_t ackno;
163  uint16_t _hdrlen_rsvd_flags;
164  beui16_t wnd;
165  uint16_t chksum;
166  beui16_t urgp;
167 } __attribute__((packed));
168 
169 #define TCP_OPT_END_OF_OPTIONS 0
170 #define TCP_OPT_NO_OP 1
171 #define TCP_OPT_MSS 2
172 #define TCP_OPT_TIMESTAMP 8
173 struct tcp_mss_opt {
174  uint8_t kind;
175  uint8_t length;
176  beui16_t mss;
177 } __attribute__((packed));
178 
179 
181  uint8_t kind;
182  uint8_t length;
183  beui32_t ts_val;
184  beui32_t ts_ecr;
185 } __attribute__((packed));
186 
187 
188 /******************************************************************************/
189 /* Object framing */
190 
191 #define OBJ_MAGIC 0xb805
192 
193 struct obj_hdr {
194  beui32_t len;
195  beui16_t magic;
196  uint8_t src;
197  uint8_t dstlen;
198  uint8_t dst[];
199 } __attribute__((packed));
200 
201 
202 /******************************************************************************/
203 /* TCP packets */
204 
205 struct pkt_arp {
206  struct eth_hdr eth;
207  struct arp_hdr arp;
208 } __attribute__ ((packed));
209 
210 struct pkt_ip {
211  struct eth_hdr eth;
212  struct ip_hdr ip;
213 } __attribute__ ((packed));
214 
215 struct pkt_tcp {
216  struct eth_hdr eth;
217  struct ip_hdr ip;
218  struct tcp_hdr tcp;
219 } __attribute__ ((packed));
220 
221 #endif
Definition: utils.h:44
Definition: utils.h:45