Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 1 | /* |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 2 | * Copyright (c) 2013 ARM Limited |
| 3 | * All rights reserved |
| 4 | * |
| 5 | * The license below extends only to copyright in the software and shall |
| 6 | * not be construed as granting a license to any other intellectual |
| 7 | * property including but not limited to intellectual property relating |
| 8 | * to a hardware implementation of the functionality of the software |
| 9 | * licensed hereunder. You may use the software subject to the license |
| 10 | * terms below provided that you ensure that this notice is replicated |
| 11 | * unmodified and in its entirety in all distributions of the software, |
| 12 | * modified or unmodified, in source code or in binary form. |
| 13 | * |
Steve Reinhardt | ad8b963 | 2005-06-05 05:16:00 -0400 | [diff] [blame] | 14 | * Copyright (c) 2002-2005 The Regents of The University of Michigan |
Gabe Black | 7f6ca09 | 2010-11-23 17:08:41 -0500 | [diff] [blame] | 15 | * Copyright (c) 2010 Advanced Micro Devices, Inc. |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 16 | * All rights reserved. |
| 17 | * |
| 18 | * Redistribution and use in source and binary forms, with or without |
| 19 | * modification, are permitted provided that the following conditions are |
| 20 | * met: redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer; |
| 22 | * redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution; |
| 25 | * neither the name of the copyright holders nor the names of its |
| 26 | * contributors may be used to endorse or promote products derived from |
| 27 | * this software without specific prior written permission. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 33 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 40 | */ |
| 41 | |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 42 | #ifndef __BASE_INET_HH__ |
| 43 | #define __BASE_INET_HH__ |
| 44 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 45 | #include <iosfwd> |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 46 | #include <string> |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 47 | #include <utility> |
| 48 | #include <vector> |
| 49 | |
Daniel R. Carvalho | 9b675eb | 2021-05-13 12:02:34 -0300 | [diff] [blame] | 50 | #include "base/compiler.hh" |
Nathan Binkert | eef3a2e | 2009-05-17 14:34:50 -0700 | [diff] [blame] | 51 | #include "base/types.hh" |
Andreas Sandberg | 23c961a | 2015-12-10 10:35:18 +0000 | [diff] [blame] | 52 | #include "dev/net/etherpkt.hh" |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 53 | #include "dnet/os.h" |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 54 | #include "dnet/eth.h" |
| 55 | #include "dnet/ip.h" |
| 56 | #include "dnet/ip6.h" |
| 57 | #include "dnet/addr.h" |
| 58 | #include "dnet/arp.h" |
| 59 | #include "dnet/icmp.h" |
| 60 | #include "dnet/tcp.h" |
| 61 | #include "dnet/udp.h" |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 62 | #include "dnet/intf.h" |
| 63 | #include "dnet/route.h" |
| 64 | #include "dnet/fw.h" |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 65 | #include "dnet/blob.h" |
| 66 | #include "dnet/rand.h" |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 67 | |
Daniel R. Carvalho | 974a47d | 2021-05-09 12:32:07 -0300 | [diff] [blame] | 68 | namespace gem5 |
| 69 | { |
| 70 | |
Daniel R. Carvalho | 06fb075 | 2021-05-06 15:50:55 -0300 | [diff] [blame] | 71 | GEM5_DEPRECATED_NAMESPACE(Net, networking); |
| 72 | namespace networking |
| 73 | { |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 74 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 75 | /* |
| 76 | * Ethernet Stuff |
| 77 | */ |
| 78 | struct EthAddr : protected eth_addr |
| 79 | { |
| 80 | protected: |
| 81 | void parse(const std::string &addr); |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 82 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 83 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 84 | /** |
| 85 | * @ingroup api_inet |
| 86 | * @{ |
| 87 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 88 | EthAddr(); |
| 89 | EthAddr(const uint8_t ea[ETH_ADDR_LEN]); |
| 90 | EthAddr(const eth_addr &ea); |
| 91 | EthAddr(const std::string &addr); |
| 92 | const EthAddr &operator=(const eth_addr &ea); |
| 93 | const EthAddr &operator=(const std::string &addr); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 94 | /** @} */ // end of api_inet |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 95 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @ingroup api_inet |
| 98 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 99 | int size() const { return sizeof(eth_addr); } |
| 100 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * @ingroup api_inet |
| 104 | * @{ |
| 105 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 106 | const uint8_t *bytes() const { return &data[0]; } |
| 107 | uint8_t *bytes() { return &data[0]; } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 108 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 109 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 110 | /** |
| 111 | * @ingroup api_inet |
| 112 | * @{ |
| 113 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 114 | const uint8_t *addr() const { return &data[0]; } |
Anthony Gutierrez | 3956ec0 | 2014-07-02 13:19:13 -0400 | [diff] [blame] | 115 | bool unicast() const { return !(data[0] & 0x01); } |
| 116 | bool multicast() const { return !unicast() && !broadcast(); } |
| 117 | bool broadcast() const |
| 118 | { |
| 119 | bool isBroadcast = true; |
| 120 | for (int i = 0; i < ETH_ADDR_LEN; ++i) { |
| 121 | isBroadcast = isBroadcast && data[i] == 0xff; |
| 122 | } |
| 123 | |
| 124 | return isBroadcast; |
| 125 | } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 126 | /** @} */ // end of api_inet |
Anthony Gutierrez | 3956ec0 | 2014-07-02 13:19:13 -0400 | [diff] [blame] | 127 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 128 | /** |
| 129 | * @ingroup api_inet |
| 130 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 131 | std::string string() const; |
Nathan Binkert | b64619d | 2004-11-10 18:49:55 -0500 | [diff] [blame] | 132 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 133 | /** |
| 134 | * @ingroup api_inet |
| 135 | */ |
Nathan Binkert | b64619d | 2004-11-10 18:49:55 -0500 | [diff] [blame] | 136 | operator uint64_t() const |
| 137 | { |
| 138 | uint64_t reg = 0; |
| 139 | reg |= ((uint64_t)data[0]) << 40; |
| 140 | reg |= ((uint64_t)data[1]) << 32; |
| 141 | reg |= ((uint64_t)data[2]) << 24; |
| 142 | reg |= ((uint64_t)data[3]) << 16; |
| 143 | reg |= ((uint64_t)data[4]) << 8; |
| 144 | reg |= ((uint64_t)data[5]) << 0; |
| 145 | return reg; |
| 146 | } |
| 147 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 148 | }; |
| 149 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 150 | /** |
| 151 | * @ingroup api_inet |
| 152 | * @{ |
| 153 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 154 | std::ostream &operator<<(std::ostream &stream, const EthAddr &ea); |
| 155 | bool operator==(const EthAddr &left, const EthAddr &right); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 156 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 157 | |
| 158 | struct EthHdr : public eth_hdr |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 159 | { |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 160 | bool isVlan() const { return (ntohs(eth_type) == ETH_TYPE_8021Q); } |
| 161 | uint16_t type() const { |
| 162 | if (!isVlan()) |
| 163 | return ntohs(eth_type); |
| 164 | else |
| 165 | // L3 type is now 16 bytes into the hdr with 802.1Q |
| 166 | // instead of 12. dnet/eth.h only supports 802.1 |
| 167 | return ntohs(*((uint16_t*)(((uint8_t *)this) + 16))); |
| 168 | } |
| 169 | uint16_t vlanId() const { |
| 170 | if (isVlan()) |
| 171 | return ntohs(*((uint16_t*)(((uint8_t *)this) + 14))); |
| 172 | else |
| 173 | return 0x0000; |
| 174 | } |
| 175 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 176 | const EthAddr &src() const { return *(EthAddr *)ð_src; } |
| 177 | const EthAddr &dst() const { return *(EthAddr *)ð_dst; } |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 178 | |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 179 | int size() const { |
| 180 | if (!isVlan()) |
| 181 | return sizeof(eth_hdr); |
| 182 | else |
| 183 | return (sizeof(eth_hdr)+4); |
| 184 | } |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 185 | |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 186 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 187 | const uint8_t *payload() const { return bytes() + size(); } |
| 188 | uint8_t *bytes() { return (uint8_t *)this; } |
| 189 | uint8_t *payload() { return bytes() + size(); } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 190 | }; |
| 191 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 192 | class EthPtr |
| 193 | { |
| 194 | protected: |
| 195 | friend class IpPtr; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 196 | friend class Ip6Ptr; |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 197 | EthPacketPtr p; |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 198 | |
| 199 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 200 | /** |
| 201 | * @ingroup api_inet |
| 202 | * @{ |
| 203 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 204 | EthPtr() {} |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 205 | EthPtr(const EthPacketPtr &ptr) : p(ptr) { } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 206 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 207 | |
| 208 | EthHdr *operator->() { return (EthHdr *)p->data; } |
| 209 | EthHdr &operator*() { return *(EthHdr *)p->data; } |
| 210 | operator EthHdr *() { return (EthHdr *)p->data; } |
| 211 | |
| 212 | const EthHdr *operator->() const { return (const EthHdr *)p->data; } |
| 213 | const EthHdr &operator*() const { return *(const EthHdr *)p->data; } |
| 214 | operator const EthHdr *() const { return (const EthHdr *)p->data; } |
| 215 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 216 | /** |
| 217 | * @ingroup api_inet |
| 218 | */ |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 219 | const EthPtr &operator=(const EthPacketPtr &ptr) { p = ptr; return *this; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 220 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 221 | /** |
| 222 | * @ingroup api_inet |
| 223 | * @{ |
| 224 | */ |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 225 | const EthPacketPtr packet() const { return p; } |
| 226 | EthPacketPtr packet() { return p; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 227 | bool operator!() const { return !p; } |
Andreas Hansson | ad3f75d | 2014-10-16 05:49:46 -0400 | [diff] [blame] | 228 | operator bool() const { return (p != nullptr); } |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 229 | int off() const { return 0; } |
| 230 | int pstart() const { return off() + ((const EthHdr*)p->data)->size(); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 231 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /* |
| 235 | * IP Stuff |
| 236 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 237 | struct IpAddress |
| 238 | { |
| 239 | protected: |
| 240 | uint32_t _ip; |
| 241 | |
| 242 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 243 | /** |
| 244 | * @ingroup api_inet |
| 245 | * @{ |
| 246 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 247 | IpAddress() : _ip(0) |
| 248 | {} |
| 249 | IpAddress(const uint32_t __ip) : _ip(__ip) |
| 250 | {} |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 251 | /** @} */ // end of api_net |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 252 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 253 | /** |
| 254 | * @ingroup api_inet |
| 255 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 256 | uint32_t ip() const { return _ip; } |
| 257 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 258 | /** |
| 259 | * @ingroup api_inet |
| 260 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 261 | std::string string() const; |
| 262 | }; |
| 263 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 264 | /** |
| 265 | * @ingroup api_inet |
| 266 | * @{ |
| 267 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 268 | std::ostream &operator<<(std::ostream &stream, const IpAddress &ia); |
| 269 | bool operator==(const IpAddress &left, const IpAddress &right); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 270 | /** @} */ // end of api_inet |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 271 | |
| 272 | struct IpNetmask : public IpAddress |
| 273 | { |
| 274 | protected: |
| 275 | uint8_t _netmask; |
| 276 | |
| 277 | public: |
| 278 | IpNetmask() : IpAddress(), _netmask(0) |
| 279 | {} |
| 280 | IpNetmask(const uint32_t __ip, const uint8_t __netmask) : |
| 281 | IpAddress(__ip), _netmask(__netmask) |
| 282 | {} |
| 283 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 284 | /** |
| 285 | * @ingroup api_inet |
| 286 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 287 | uint8_t netmask() const { return _netmask; } |
| 288 | |
| 289 | std::string string() const; |
| 290 | }; |
| 291 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 292 | /** |
| 293 | * @ingroup api_inet |
| 294 | * @{ |
| 295 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 296 | std::ostream &operator<<(std::ostream &stream, const IpNetmask &in); |
| 297 | bool operator==(const IpNetmask &left, const IpNetmask &right); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 298 | /** @} */ // end of api_inet |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 299 | |
| 300 | struct IpWithPort : public IpAddress |
| 301 | { |
| 302 | protected: |
| 303 | uint16_t _port; |
| 304 | |
| 305 | public: |
| 306 | IpWithPort() : IpAddress(), _port(0) |
| 307 | {} |
| 308 | IpWithPort(const uint32_t __ip, const uint16_t __port) : |
| 309 | IpAddress(__ip), _port(__port) |
| 310 | {} |
| 311 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 312 | /** |
| 313 | * @ingroup api_inet |
| 314 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 315 | uint8_t port() const { return _port; } |
| 316 | |
| 317 | std::string string() const; |
| 318 | }; |
| 319 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 320 | /** |
| 321 | * @ingroup api_inet |
| 322 | * @{ |
| 323 | */ |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 324 | std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp); |
| 325 | bool operator==(const IpWithPort &left, const IpWithPort &right); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 326 | /** @} */ // end of api_inet |
Gabe Black | b3de485 | 2010-11-23 15:54:43 -0500 | [diff] [blame] | 327 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 328 | struct IpOpt; |
| 329 | struct IpHdr : public ip_hdr |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 330 | { |
| 331 | uint8_t version() const { return ip_v; } |
Nathan Binkert | eaf66f4 | 2004-10-01 19:48:33 -0400 | [diff] [blame] | 332 | uint8_t hlen() const { return ip_hl * 4; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 333 | uint8_t tos() const { return ip_tos; } |
| 334 | uint16_t len() const { return ntohs(ip_len); } |
| 335 | uint16_t id() const { return ntohs(ip_id); } |
| 336 | uint16_t frag_flags() const { return ntohs(ip_off) >> 13; } |
| 337 | uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; } |
| 338 | uint8_t ttl() const { return ip_ttl; } |
| 339 | uint8_t proto() const { return ip_p; } |
Nathan Binkert | eaf66f4 | 2004-10-01 19:48:33 -0400 | [diff] [blame] | 340 | uint16_t sum() const { return ip_sum; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 341 | uint32_t src() const { return ntohl(ip_src); } |
| 342 | uint32_t dst() const { return ntohl(ip_dst); } |
| 343 | |
Nathan Binkert | eaf66f4 | 2004-10-01 19:48:33 -0400 | [diff] [blame] | 344 | void sum(uint16_t sum) { ip_sum = sum; } |
Ali Saidi | aab595a | 2008-12-05 13:58:21 -0500 | [diff] [blame] | 345 | void id(uint16_t _id) { ip_id = htons(_id); } |
| 346 | void len(uint16_t _len) { ip_len = htons(_len); } |
| 347 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 348 | bool options(std::vector<const IpOpt *> &vec) const; |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 349 | |
| 350 | int size() const { return hlen(); } |
| 351 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 352 | const uint8_t *payload() const { return bytes() + size(); } |
| 353 | uint8_t *bytes() { return (uint8_t *)this; } |
| 354 | uint8_t *payload() { return bytes() + size(); } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 355 | }; |
| 356 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 357 | class IpPtr |
| 358 | { |
| 359 | protected: |
| 360 | friend class TcpPtr; |
| 361 | friend class UdpPtr; |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 362 | EthPacketPtr p; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 363 | bool eth_hdr_vlan; |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 364 | |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 365 | void set(const EthPacketPtr &ptr) |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 366 | { |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 367 | p = 0; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 368 | eth_hdr_vlan = false; |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 369 | |
| 370 | if (ptr) { |
| 371 | EthHdr *eth = (EthHdr *)ptr->data; |
| 372 | if (eth->type() == ETH_TYPE_IP) |
| 373 | p = ptr; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 374 | if (eth->isVlan()) |
| 375 | eth_hdr_vlan = true; |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 376 | } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 380 | /** |
| 381 | * @ingroup api_inet |
| 382 | * @{ |
| 383 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 384 | IpPtr() : p(0), eth_hdr_vlan(false) {} |
| 385 | IpPtr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); } |
| 386 | IpPtr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); } |
| 387 | IpPtr(const IpPtr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 388 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 389 | |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 390 | IpHdr *get() { return (IpHdr *)(p->data + sizeof(eth_hdr) + |
| 391 | ((eth_hdr_vlan) ? 4 : 0)); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 392 | IpHdr *operator->() { return get(); } |
| 393 | IpHdr &operator*() { return *get(); } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 394 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 395 | /** |
| 396 | * @ingroup api_inet |
| 397 | * @{ |
| 398 | */ |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 399 | const IpHdr *get() const |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 400 | { return (const IpHdr *)(p->data + sizeof(eth_hdr) + |
| 401 | ((eth_hdr_vlan) ? 4 : 0)); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 402 | const IpHdr *operator->() const { return get(); } |
| 403 | const IpHdr &operator*() const { return *get(); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 404 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 405 | |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 406 | const IpPtr &operator=(const EthPacketPtr &ptr) { set(ptr); return *this; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 407 | const IpPtr &operator=(const EthPtr &ptr) { set(ptr.p); return *this; } |
| 408 | const IpPtr &operator=(const IpPtr &ptr) { p = ptr.p; return *this; } |
| 409 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 410 | /** |
| 411 | * @ingroup api_inet |
| 412 | * @{ |
| 413 | */ |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 414 | const EthPacketPtr packet() const { return p; } |
| 415 | EthPacketPtr packet() { return p; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 416 | bool operator!() const { return !p; } |
Andreas Hansson | ad3f75d | 2014-10-16 05:49:46 -0400 | [diff] [blame] | 417 | operator bool() const { return (p != nullptr); } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 418 | int off() const { return (sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0)); } |
| 419 | int pstart() const { return (off() + get()->size()); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 420 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 421 | }; |
| 422 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 423 | /** |
| 424 | * @ingroup api_inet |
| 425 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 426 | uint16_t cksum(const IpPtr &ptr); |
| 427 | |
| 428 | struct IpOpt : public ip_opt |
| 429 | { |
| 430 | uint8_t type() const { return opt_type; } |
| 431 | uint8_t typeNumber() const { return IP_OPT_NUMBER(opt_type); } |
| 432 | uint8_t typeClass() const { return IP_OPT_CLASS(opt_type); } |
| 433 | uint8_t typeCopied() const { return IP_OPT_COPIED(opt_type); } |
| 434 | uint8_t len() const { return IP_OPT_TYPEONLY(type()) ? 1 : opt_len; } |
| 435 | |
| 436 | bool isNumber(int num) const { return typeNumber() == IP_OPT_NUMBER(num); } |
| 437 | bool isClass(int cls) const { return typeClass() == IP_OPT_CLASS(cls); } |
| 438 | bool isCopied(int cpy) const { return typeCopied() == IP_OPT_COPIED(cpy); } |
| 439 | |
| 440 | const uint8_t *data() const { return opt_data.data8; } |
| 441 | void sec(ip_opt_data_sec &sec) const; |
| 442 | void lsrr(ip_opt_data_rr &rr) const; |
| 443 | void ssrr(ip_opt_data_rr &rr) const; |
| 444 | void ts(ip_opt_data_ts &ts) const; |
| 445 | uint16_t satid() const { return ntohs(opt_data.satid); } |
| 446 | uint16_t mtup() const { return ntohs(opt_data.mtu); } |
| 447 | uint16_t mtur() const { return ntohs(opt_data.mtu); } |
| 448 | void tr(ip_opt_data_tr &tr) const; |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 449 | uint16_t rtralt() const { return ntohs(opt_data.rtralt); } |
| 450 | void sdb(std::vector<uint32_t> &vec) const; |
| 451 | }; |
| 452 | |
| 453 | /* |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 454 | * Ip6 Classes |
| 455 | */ |
| 456 | struct Ip6Opt; |
| 457 | struct Ip6Hdr : public ip6_hdr |
| 458 | { |
| 459 | uint8_t version() const { return ip6_vfc; } |
| 460 | uint32_t flow() const { return ntohl(ip6_flow); } |
| 461 | uint16_t plen() const { return ntohs(ip6_plen); } |
| 462 | uint16_t hlen() const { return IP6_HDR_LEN; } |
| 463 | uint8_t nxt() const { return ip6_nxt; } |
| 464 | uint8_t hlim() const { return ip6_hlim; } |
| 465 | |
| 466 | const uint8_t* src() const { return ip6_src.data; } |
| 467 | const uint8_t* dst() const { return ip6_dst.data; } |
| 468 | |
| 469 | int extensionLength() const; |
| 470 | const Ip6Opt* getExt(uint8_t ext) const; |
| 471 | const Ip6Opt* fragmentExt() const { return getExt(IP_PROTO_FRAGMENT); } |
| 472 | const Ip6Opt* rtTypeExt() const { return getExt(IP_PROTO_ROUTING); } |
| 473 | const Ip6Opt* dstOptExt() const { return getExt(IP_PROTO_DSTOPTS); } |
| 474 | uint8_t proto() const; |
| 475 | |
| 476 | void plen(uint16_t _plen) { ip6_plen = htons(_plen); } |
| 477 | |
| 478 | int size() const { return IP6_HDR_LEN + extensionLength(); } |
| 479 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 480 | const uint8_t *payload() const { return bytes() + IP6_HDR_LEN |
| 481 | + extensionLength(); } |
| 482 | uint8_t *bytes() { return (uint8_t *)this; } |
| 483 | uint8_t *payload() { return bytes() + IP6_HDR_LEN |
| 484 | + extensionLength(); } |
| 485 | }; |
| 486 | |
| 487 | class Ip6Ptr |
| 488 | { |
| 489 | protected: |
| 490 | friend class TcpPtr; |
| 491 | friend class UdpPtr; |
| 492 | EthPacketPtr p; |
| 493 | bool eth_hdr_vlan; |
| 494 | |
| 495 | void set(const EthPacketPtr &ptr) |
| 496 | { |
| 497 | p = 0; |
| 498 | eth_hdr_vlan = false; |
| 499 | |
| 500 | if (ptr) { |
| 501 | EthHdr *eth = (EthHdr *)ptr->data; |
| 502 | if (eth->type() == ETH_TYPE_IPV6) |
| 503 | p = ptr; |
| 504 | if (eth->isVlan()) |
| 505 | eth_hdr_vlan = true; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 510 | /** |
| 511 | * @ingroup api_inet |
| 512 | * @{ |
| 513 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 514 | Ip6Ptr() : p(0), eth_hdr_vlan(false) {} |
| 515 | Ip6Ptr(const EthPacketPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr); } |
| 516 | Ip6Ptr(const EthPtr &ptr) : p(0), eth_hdr_vlan(false) { set(ptr.p); } |
| 517 | Ip6Ptr(const Ip6Ptr &ptr) : p(ptr.p), eth_hdr_vlan(ptr.eth_hdr_vlan) { } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 518 | /** @} */ // end of api_inet |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 519 | |
| 520 | Ip6Hdr *get() { return (Ip6Hdr *)(p->data + sizeof(eth_hdr) |
| 521 | + ((eth_hdr_vlan) ? 4 : 0)); } |
| 522 | Ip6Hdr *operator->() { return get(); } |
| 523 | Ip6Hdr &operator*() { return *get(); } |
| 524 | |
| 525 | const Ip6Hdr *get() const |
| 526 | { return (const Ip6Hdr *)(p->data + sizeof(eth_hdr) |
| 527 | + ((eth_hdr_vlan) ? 4 : 0)); } |
| 528 | const Ip6Hdr *operator->() const { return get(); } |
| 529 | const Ip6Hdr &operator*() const { return *get(); } |
| 530 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 531 | /** |
| 532 | * @ingroup api_inet |
| 533 | * @{ |
| 534 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 535 | const Ip6Ptr &operator=(const EthPacketPtr &ptr) |
| 536 | { set(ptr); return *this; } |
| 537 | const Ip6Ptr &operator=(const EthPtr &ptr) |
| 538 | { set(ptr.p); return *this; } |
| 539 | const Ip6Ptr &operator=(const Ip6Ptr &ptr) |
| 540 | { p = ptr.p; return *this; } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 541 | /** @} */ // end of api_inet |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 542 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 543 | /** |
| 544 | * @ingroup api_inet |
| 545 | * @{ |
| 546 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 547 | const EthPacketPtr packet() const { return p; } |
| 548 | EthPacketPtr packet() { return p; } |
| 549 | bool operator!() const { return !p; } |
Andreas Hansson | ad3f75d | 2014-10-16 05:49:46 -0400 | [diff] [blame] | 550 | operator bool() const { return (p != nullptr); } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 551 | int off() const { return sizeof(eth_hdr) + ((eth_hdr_vlan) ? 4 : 0); } |
| 552 | int pstart() const { return off() + get()->size(); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 553 | /** @} */ // end of api_inet |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | // Dnet supplied ipv6 opt header is incomplete and |
| 557 | // newer NIC card filters expect a more robust |
| 558 | // ipv6 header option declaration. |
Daniel R. Carvalho | 2922f76 | 2021-01-09 12:28:03 -0300 | [diff] [blame] | 559 | struct ip6_opt_fragment |
| 560 | { |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 561 | uint16_t offlg; |
| 562 | uint32_t ident; |
| 563 | }; |
| 564 | |
Daniel R. Carvalho | 2922f76 | 2021-01-09 12:28:03 -0300 | [diff] [blame] | 565 | struct ip6_opt_routing_type2 |
| 566 | { |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 567 | uint8_t type; |
| 568 | uint8_t segleft; |
| 569 | uint32_t reserved; |
| 570 | ip6_addr_t addr; |
| 571 | }; |
| 572 | |
Gabe Black | ce6b5e7 | 2021-05-08 20:35:12 -0700 | [diff] [blame] | 573 | struct GEM5_PACKED ip6_opt_dstopts |
Daniel R. Carvalho | 2922f76 | 2021-01-09 12:28:03 -0300 | [diff] [blame] | 574 | { |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 575 | uint8_t type; |
| 576 | uint8_t length; |
| 577 | ip6_addr_t addr; |
Gabe Black | 463cb28 | 2020-10-14 00:38:42 -0700 | [diff] [blame] | 578 | }; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 579 | |
Gabe Black | ce6b5e7 | 2021-05-08 20:35:12 -0700 | [diff] [blame] | 580 | struct GEM5_PACKED ip6_opt_hdr |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 581 | { |
| 582 | uint8_t ext_nxt; |
| 583 | uint8_t ext_len; |
Daniel R. Carvalho | 5c8983f | 2021-03-23 13:29:40 -0300 | [diff] [blame] | 584 | union |
| 585 | { |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 586 | struct ip6_opt_fragment fragment; |
| 587 | struct ip6_opt_routing_type2 rtType2; |
| 588 | struct ip6_opt_dstopts dstOpts; |
| 589 | } ext_data; |
Gabe Black | 463cb28 | 2020-10-14 00:38:42 -0700 | [diff] [blame] | 590 | }; |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 591 | |
| 592 | struct Ip6Opt : public ip6_opt_hdr |
| 593 | { |
| 594 | uint8_t nxt() const { return ext_nxt; } |
| 595 | uint8_t extlen() const { return ext_len; } |
| 596 | uint8_t len() const { return extlen() + 8; } |
| 597 | |
| 598 | // Supporting the types of header extensions likely to be encountered: |
| 599 | // fragment, routing type 2 and dstopts. |
| 600 | |
| 601 | // Routing type 2 |
| 602 | uint8_t rtType2Type() const { return ext_data.rtType2.type; } |
| 603 | uint8_t rtType2SegLft() const { return ext_data.rtType2.segleft; } |
| 604 | const uint8_t* rtType2Addr() const { return ext_data.rtType2.addr.data; } |
| 605 | |
| 606 | // Fragment |
| 607 | uint16_t fragmentOfflg() const { return ntohs(ext_data.fragment.offlg); } |
| 608 | uint32_t fragmentIdent() const { return ntohl(ext_data.fragment.ident); } |
| 609 | |
| 610 | // Dst Options/Home Address Option |
| 611 | uint8_t dstOptType() const { return ext_data.dstOpts.type; } |
| 612 | uint8_t dstOptLength() const { return ext_data.dstOpts.length; } |
| 613 | const uint8_t* dstOptAddr() const { return ext_data.dstOpts.addr.data; } |
| 614 | }; |
| 615 | |
| 616 | |
| 617 | /* |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 618 | * TCP Stuff |
| 619 | */ |
| 620 | struct TcpOpt; |
| 621 | struct TcpHdr : public tcp_hdr |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 622 | { |
| 623 | uint16_t sport() const { return ntohs(th_sport); } |
| 624 | uint16_t dport() const { return ntohs(th_dport); } |
| 625 | uint32_t seq() const { return ntohl(th_seq); } |
| 626 | uint32_t ack() const { return ntohl(th_ack); } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 627 | uint8_t off() const { return th_off*4; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 628 | uint8_t flags() const { return th_flags & 0x3f; } |
| 629 | uint16_t win() const { return ntohs(th_win); } |
Nathan Binkert | eaf66f4 | 2004-10-01 19:48:33 -0400 | [diff] [blame] | 630 | uint16_t sum() const { return th_sum; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 631 | uint16_t urp() const { return ntohs(th_urp); } |
| 632 | |
Nathan Binkert | eaf66f4 | 2004-10-01 19:48:33 -0400 | [diff] [blame] | 633 | void sum(uint16_t sum) { th_sum = sum; } |
Ali Saidi | aab595a | 2008-12-05 13:58:21 -0500 | [diff] [blame] | 634 | void seq(uint32_t _seq) { th_seq = htonl(_seq); } |
Steve Reinhardt | dc8018a | 2016-02-06 17:21:18 -0800 | [diff] [blame] | 635 | void flags(uint8_t _flags) { th_flags = _flags; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 636 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 637 | bool options(std::vector<const TcpOpt *> &vec) const; |
| 638 | |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 639 | int size() const { return off(); } |
| 640 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 641 | const uint8_t *payload() const { return bytes() + size(); } |
| 642 | uint8_t *bytes() { return (uint8_t *)this; } |
| 643 | uint8_t *payload() { return bytes() + size(); } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 644 | }; |
| 645 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 646 | class TcpPtr |
| 647 | { |
| 648 | protected: |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 649 | EthPacketPtr p; |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 650 | int _off; |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 651 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 652 | void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 653 | void set(const IpPtr &ptr) |
| 654 | { |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 655 | if (ptr && ptr->proto() == IP_PROTO_TCP) |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 656 | set(ptr.p, ptr.pstart()); |
| 657 | else |
| 658 | set(0, 0); |
| 659 | } |
| 660 | void set(const Ip6Ptr &ptr) |
| 661 | { |
| 662 | if (ptr && ptr->proto() == IP_PROTO_TCP) |
| 663 | set(ptr.p, ptr.pstart()); |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 664 | else |
| 665 | set(0, 0); |
| 666 | } |
| 667 | |
| 668 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 669 | /** |
| 670 | * @ingroup api_inet |
| 671 | * @{ |
| 672 | */ |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 673 | TcpPtr() : p(0), _off(0) {} |
| 674 | TcpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 675 | TcpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); } |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 676 | TcpPtr(const TcpPtr &ptr) : p(ptr.p), _off(ptr._off) {} |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 677 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 678 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 679 | TcpHdr *get() { return (TcpHdr *)(p->data + _off); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 680 | TcpHdr *operator->() { return get(); } |
| 681 | TcpHdr &operator*() { return *get(); } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 682 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 683 | const TcpHdr *get() const { return (const TcpHdr *)(p->data + _off); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 684 | const TcpHdr *operator->() const { return get(); } |
| 685 | const TcpHdr &operator*() const { return *get(); } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 686 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 687 | /** |
| 688 | * @ingroup api_inet |
| 689 | * @{ |
| 690 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 691 | const TcpPtr &operator=(const IpPtr &i) |
| 692 | { set(i); return *this; } |
| 693 | const TcpPtr &operator=(const TcpPtr &t) |
| 694 | { set(t.p, t._off); return *this; } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 695 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 696 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 697 | /** |
| 698 | * @ingroup api_inet |
| 699 | * @{ |
| 700 | */ |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 701 | const EthPacketPtr packet() const { return p; } |
| 702 | EthPacketPtr packet() { return p; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 703 | bool operator!() const { return !p; } |
Andreas Hansson | ad3f75d | 2014-10-16 05:49:46 -0400 | [diff] [blame] | 704 | operator bool() const { return (p != nullptr); } |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 705 | int off() const { return _off; } |
| 706 | int pstart() const { return off() + get()->size(); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 707 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 708 | }; |
| 709 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 710 | /** |
| 711 | * @ingroup api_inet |
| 712 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 713 | uint16_t cksum(const TcpPtr &ptr); |
| 714 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 715 | struct TcpOpt : public tcp_opt |
| 716 | { |
| 717 | uint8_t type() const { return opt_type; } |
| 718 | uint8_t len() const { return TCP_OPT_TYPEONLY(type()) ? 1 : opt_len; } |
| 719 | |
| 720 | bool isopt(int opt) const { return type() == opt; } |
| 721 | |
| 722 | const uint8_t *data() const { return opt_data.data8; } |
| 723 | |
| 724 | uint16_t mss() const { return ntohs(opt_data.mss); } |
| 725 | uint8_t wscale() const { return opt_data.wscale; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 726 | uint32_t echo() const { return ntohl(opt_data.echo); } |
| 727 | uint32_t tsval() const { return ntohl(opt_data.timestamp[0]); } |
| 728 | uint32_t tsecr() const { return ntohl(opt_data.timestamp[1]); } |
| 729 | uint32_t cc() const { return ntohl(opt_data.cc); } |
| 730 | uint8_t cksum() const{ return opt_data.cksum; } |
| 731 | const uint8_t *md5() const { return opt_data.md5; } |
| 732 | |
| 733 | int size() const { return len(); } |
| 734 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 735 | const uint8_t *payload() const { return bytes() + size(); } |
| 736 | uint8_t *bytes() { return (uint8_t *)this; } |
| 737 | uint8_t *payload() { return bytes() + size(); } |
| 738 | }; |
| 739 | |
| 740 | /* |
| 741 | * UDP Stuff |
| 742 | */ |
| 743 | struct UdpHdr : public udp_hdr |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 744 | { |
| 745 | uint16_t sport() const { return ntohs(uh_sport); } |
| 746 | uint16_t dport() const { return ntohs(uh_dport); } |
| 747 | uint16_t len() const { return ntohs(uh_ulen); } |
Nathan Binkert | f7323ae | 2004-11-15 15:40:35 -0500 | [diff] [blame] | 748 | uint16_t sum() const { return uh_sum; } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 749 | |
Nathan Binkert | f7323ae | 2004-11-15 15:40:35 -0500 | [diff] [blame] | 750 | void sum(uint16_t sum) { uh_sum = sum; } |
Ali Saidi | aab595a | 2008-12-05 13:58:21 -0500 | [diff] [blame] | 751 | void len(uint16_t _len) { uh_ulen = htons(_len); } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 752 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 753 | int size() const { return sizeof(udp_hdr); } |
Nathan Binkert | 1d02345 | 2004-09-21 01:41:55 -0400 | [diff] [blame] | 754 | const uint8_t *bytes() const { return (const uint8_t *)this; } |
| 755 | const uint8_t *payload() const { return bytes() + size(); } |
| 756 | uint8_t *bytes() { return (uint8_t *)this; } |
| 757 | uint8_t *payload() { return bytes() + size(); } |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 758 | }; |
| 759 | |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 760 | class UdpPtr |
| 761 | { |
| 762 | protected: |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 763 | EthPacketPtr p; |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 764 | int _off; |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 765 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 766 | void set(const EthPacketPtr &ptr, int offset) { p = ptr; _off = offset; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 767 | void set(const IpPtr &ptr) |
| 768 | { |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 769 | if (ptr && ptr->proto() == IP_PROTO_UDP) |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 770 | set(ptr.p, ptr.pstart()); |
| 771 | else |
| 772 | set(0, 0); |
| 773 | } |
| 774 | void set(const Ip6Ptr &ptr) |
| 775 | { |
| 776 | if (ptr && ptr->proto() == IP_PROTO_UDP) |
| 777 | set(ptr.p, ptr.pstart()); |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 778 | else |
| 779 | set(0, 0); |
| 780 | } |
| 781 | |
| 782 | public: |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 783 | /** |
| 784 | * @ingroup api_inet |
| 785 | */ |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 786 | UdpPtr() : p(0), _off(0) {} |
| 787 | UdpPtr(const IpPtr &ptr) : p(0), _off(0) { set(ptr); } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 788 | UdpPtr(const Ip6Ptr &ptr) : p(0), _off(0) { set(ptr); } |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 789 | UdpPtr(const UdpPtr &ptr) : p(ptr.p), _off(ptr._off) {} |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 790 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 791 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 792 | UdpHdr *get() { return (UdpHdr *)(p->data + _off); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 793 | UdpHdr *operator->() { return get(); } |
| 794 | UdpHdr &operator*() { return *get(); } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 795 | |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 796 | const UdpHdr *get() const { return (const UdpHdr *)(p->data + _off); } |
Nathan Binkert | 87d03d0 | 2008-06-17 22:14:12 -0700 | [diff] [blame] | 797 | const UdpHdr *operator->() const { return get(); } |
| 798 | const UdpHdr &operator*() const { return *get(); } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 799 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 800 | /** |
| 801 | * @ingroup api_inet |
| 802 | * @{ |
| 803 | */ |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 804 | const UdpPtr &operator=(const IpPtr &i) { set(i); return *this; } |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 805 | const UdpPtr &operator=(const UdpPtr &t) |
| 806 | { set(t.p, t._off); return *this; } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 807 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 808 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 809 | /** |
| 810 | * @ingroup api_inet |
| 811 | * @{ |
| 812 | */ |
Ali Saidi | 8f8d095 | 2006-04-24 19:31:50 -0400 | [diff] [blame] | 813 | const EthPacketPtr packet() const { return p; } |
| 814 | EthPacketPtr packet() { return p; } |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 815 | bool operator!() const { return !p; } |
Andreas Hansson | ad3f75d | 2014-10-16 05:49:46 -0400 | [diff] [blame] | 816 | operator bool() const { return (p != nullptr); } |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 817 | int off() const { return _off; } |
| 818 | int pstart() const { return off() + get()->size(); } |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 819 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 820 | }; |
| 821 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 822 | /** |
| 823 | * @ingroup api_inet |
| 824 | * @{ |
| 825 | */ |
Geoffrey Blake | fb04964 | 2013-10-31 13:41:13 -0500 | [diff] [blame] | 826 | uint16_t __tu_cksum6(const Ip6Ptr &ip6); |
Andreas Hansson | 319443d | 2013-02-19 05:56:07 -0500 | [diff] [blame] | 827 | uint16_t __tu_cksum(const IpPtr &ip); |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 828 | uint16_t cksum(const UdpPtr &ptr); |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 829 | /** @} */ // end of api_inet |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 830 | |
Hoa Nguyen | 197deeb | 2020-08-23 21:41:21 -0700 | [diff] [blame] | 831 | /** |
| 832 | * @ingroup api_inet |
| 833 | */ |
Ali Saidi | 11ac0c7 | 2009-01-06 10:36:56 -0500 | [diff] [blame] | 834 | int hsplit(const EthPacketPtr &ptr); |
| 835 | |
Daniel R. Carvalho | 06fb075 | 2021-05-06 15:50:55 -0300 | [diff] [blame] | 836 | } // namespace networking |
Daniel R. Carvalho | 974a47d | 2021-05-09 12:32:07 -0300 | [diff] [blame] | 837 | } // namespace gem5 |
Nathan Binkert | a7fd772 | 2004-10-23 16:18:44 -0400 | [diff] [blame] | 838 | |
Nathan Binkert | a58b834 | 2004-09-20 10:43:53 -0400 | [diff] [blame] | 839 | #endif // __BASE_INET_HH__ |