SatCat5
eth_header.h
Go to the documentation of this file.
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
15 
16 #pragma once
17 
18 #include <satcat5/io_core.h>
19 #include <satcat5/types.h>
20 
21 // Enable 802.1Q VLAN tagging?
22 #ifndef SATCAT5_VLAN_ENABLE
23 #define SATCAT5_VLAN_ENABLE 1
24 #endif
25 
26 namespace satcat5 {
27  namespace eth {
29  struct MacAddr {
31  u8 addr[6];
32 
33  MacAddr() = default;
34  MacAddr(const MacAddr& t) = default;
35  MacAddr& operator=(const MacAddr& t) = default;
36 
38  static constexpr satcat5::eth::MacAddr from_u64(u64 x) {
39  return MacAddr {
40  (u8)(x >> 40), (u8)(x >> 32), (u8)(x >> 24),
41  (u8)(x >> 16), (u8)(x >> 8), (u8)(x >> 0)};
42  }
44  constexpr u64 to_u64() const {
45  return 1099511627776ULL * addr[0]
46  + 4294967296ULL * addr[1]
47  + 16777216ULL * addr[2]
48  + 65536ULL * addr[3]
49  + 256ULL * addr[4]
50  + 1ULL * addr[5];
51  }
52 
55  constexpr u64 to_ptp_clockid() const {
56  return 72057594037927936ull * addr[0]
57  + 281474976710656ull * addr[1]
58  + 1099511627776ull * addr[2]
59  + 4294967296ull * 0xFF
60  + 16777216ull * 0xFE
61  + 65536ull * addr[3]
62  + 256ull * addr[4]
63  + 1ull * addr[5];
64  }
65 
68  bool operator==(const satcat5::eth::MacAddr& other) const;
69  bool operator<(const satcat5::eth::MacAddr& other) const;
70  inline bool operator!=(const satcat5::eth::MacAddr& other) const
71  {return !operator==(other);}
73 
74  // Tests for various reserved address ranges:
75  bool is_broadcast() const;
76  bool is_l2multicast() const;
77  bool is_l3multicast() const;
78  bool is_multicast() const;
79  bool is_swcontrol() const;
80  bool is_unicast() const;
81  bool is_valid() const;
82 
84  void log_to(satcat5::log::LogBuffer& wr) const;
86  inline void write_to(satcat5::io::Writeable* wr) const
87  {wr->write_bytes(6, addr);}
90  {return rd->read_bytes(6, addr);}
91  };
92 
96  struct MacType {
98  u16 value;
99 
100  MacType() = default;
101  MacType(const MacType& t) = default;
102  MacType& operator=(const MacType& t) = default;
103 
106  inline bool operator==(const satcat5::eth::MacType& other) const
107  {return value == other.value;}
108  inline bool operator!=(const satcat5::eth::MacType& other) const
109  {return value != other.value;}
111 
113  void log_to(satcat5::log::LogBuffer& wr) const;
115  inline void write_to(satcat5::io::Writeable* wr) const
116  {wr->write_u16(value);}
119  {value = rd->read_u16(); return true;}
120  };
121 
124  struct VlanTag {
126  u16 value;
127 
128  VlanTag() = default;
129  VlanTag(const VlanTag& t) = default;
130  VlanTag& operator=(const VlanTag& t) = default;
131 
134  inline bool any() const {return !!value;}
135  inline u16 vid() const {return ((value >> 0) & 0xFFF);}
136  inline u16 dei() const {return ((value >> 12) & 0x001);}
137  inline u16 pcp() const {return ((value >> 13) & 0x007);}
138  inline void set(u16 vid, u16 dei, u16 pcp) {
139  value = ((vid & 0xFFF) << 0)
140  | ((dei & 0x001) << 12)
141  | ((pcp & 0x007) << 13);
142  }
144 
147  inline bool operator==(const satcat5::eth::VlanTag& other) const
148  {return value == other.value;}
149  inline bool operator!=(const satcat5::eth::VlanTag& other) const
150  {return value != other.value;}
152 
154  void log_to(satcat5::log::LogBuffer& wr) const;
156  inline void write_to(satcat5::io::Writeable* wr) const
157  {wr->write_u16(value);}
160  {value = rd->read_u16(); return true;}
161  };
162 
167  struct Header {
175 
176  Header() = default;
177  Header(const Header& t) = default;
178  Header& operator=(const Header& t) = default;
179 
181  void log_to(satcat5::log::LogBuffer& wr) const;
182 
184  void write_to(satcat5::io::Writeable* wr) const;
185 
189  };
190 
191  // Commonly used MAC addresses and EtherTypes.
192  constexpr satcat5::eth::MacAddr MACADDR_NONE =
193  {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
194  constexpr satcat5::eth::MacAddr MACADDR_BROADCAST =
195  {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
196  constexpr satcat5::eth::MacAddr MACADDR_FLOWCTRL =
197  {{0x01, 0x80, 0xC2, 0x00, 0x00, 0x01}};
198  constexpr satcat5::eth::MacAddr BASEADDR_LINKLOCAL =
199  {{0x01, 0x80, 0xC2, 0x00, 0x00, 0x00}};
200  constexpr satcat5::eth::MacAddr BASEADDR_L2MULTICAST =
201  {{0x01, 0x80, 0xC2, 0x00, 0x01, 0x00}};
202  constexpr satcat5::eth::MacAddr BASEADDR_L3MULTICAST =
203  {{0x01, 0x00, 0x5E, 0x00, 0x00, 0x00}};
204 
205  constexpr satcat5::eth::MacType
206  ETYPE_NONE = {0x0000},
207  ETYPE_IPV4 = {0x0800},
208  ETYPE_ARP = {0x0806},
209  ETYPE_RECOVERY = {0x5252},
210  ETYPE_CFGBUS_CMD = {0x5C01},
211  ETYPE_CFGBUS_ACK = {0x5C02},
212  ETYPE_SLINGSHOT_LOG = {0x5C03},
213  ETYPE_CBOR_TLM = {0x5C04},
214  ETYPE_SWITCH_LOG = {0x5C05},
215  ETYPE_VTAG = {0x8100},
216  ETYPE_FLOWCTRL = {0x8808},
217  ETYPE_MACSEC = {0x88E5},
218  ETYPE_PTP = {0x88F7};
219 
220  constexpr satcat5::eth::VlanTag
221  VTAG_NONE = {0x0000}, // Untagged frame
222  VTAG_DEFAULT = {0x0001}, // Default ports use VID = 1
223  VTAG_PRIORITY1 = {0x2000}, // Set priority only (1-7)
224  VTAG_PRIORITY2 = {0x4000},
225  VTAG_PRIORITY3 = {0x6000},
226  VTAG_PRIORITY4 = {0x8000},
227  VTAG_PRIORITY5 = {0xA000},
228  VTAG_PRIORITY6 = {0xC000},
229  VTAG_PRIORITY7 = {0xE000};
230 
231  // Min/max range for VLAN identifier (VID)
232  constexpr u16 VID_NONE = 0; // Default / placeholder
233  constexpr u16 VID_MIN = 1; // Start of user VID range
234  constexpr u16 VID_MAX = 4094; // End of user VID range
235  constexpr u16 VID_RSVD = 4095; // Reserved
236 
237  // Null Ethernet header.
238  constexpr satcat5::eth::Header HEADER_NULL =
239  {MACADDR_NONE, MACADDR_NONE, ETYPE_NONE, VTAG_NONE};
240  }
241 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual bool read_bytes(unsigned nbytes, void *dst)
Read 0 or more bytes into a buffer.
Definition: io_readable.cc:190
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
virtual void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
Internal buffer used by the Log class.
Definition: log.h:134
I/O interface core definitions.
An Ethernet frame header.
Definition: eth_header.h:167
void write_to(satcat5::io::Writeable *wr) const
Write Ethernet header to the designated stream.
Definition: eth_header.cc:117
satcat5::eth::MacType type
Ethernet header fields.
Definition: eth_header.h:172
satcat5::eth::VlanTag vtag
Ethernet header fields.
Definition: eth_header.h:173
satcat5::eth::MacAddr src
Ethernet header fields.
Definition: eth_header.h:171
satcat5::eth::MacAddr dst
Ethernet header fields.
Definition: eth_header.h:170
bool read_from(satcat5::io::Readable *rd)
Read Ethernet header from the designated stream.
Definition: eth_header.cc:128
void log_to(satcat5::log::LogBuffer &wr) const
Human-readable report of all fields.
Definition: eth_header.cc:109
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
constexpr u64 to_ptp_clockid() const
Convert to PTP Clock-ID (IEEE 1588-2008 7.5.2.2.2 Note 2).
Definition: eth_header.h:55
static constexpr satcat5::eth::MacAddr from_u64(u64 x)
Convert from u64 to MacAddr.
Definition: eth_header.h:38
void write_to(satcat5::io::Writeable *wr) const
Write this field to a byte-stream.
Definition: eth_header.h:86
bool operator<(const satcat5::eth::MacAddr &other) const
Basic comparisons.
Definition: eth_header.cc:24
bool is_l2multicast() const
L2 multicast (01:80:C2:*:*:* except link-local)
Definition: eth_header.cc:39
bool is_valid() const
Any nonzero address.
Definition: eth_header.cc:80
bool is_l3multicast() const
L3 multicast (01:00:5E:*:*:*)
Definition: eth_header.cc:49
bool operator!=(const satcat5::eth::MacAddr &other) const
Basic comparisons.
Definition: eth_header.h:70
bool read_from(satcat5::io::Readable *rd)
Read this field from a byte-stream.
Definition: eth_header.h:89
bool is_swcontrol() const
Link-local control (01:80:C2:00:00:*)
Definition: eth_header.cc:63
u8 addr[6]
Byte array in network order (Index 0 = MSB)
Definition: eth_header.h:31
bool is_multicast() const
Broadcast, L2 multicast, or L3 multicast.
Definition: eth_header.cc:57
bool operator==(const satcat5::eth::MacAddr &other) const
Basic comparisons.
Definition: eth_header.cc:14
bool is_broadcast() const
Broadcast (FF:FF:FF:FF:FF:FF)
Definition: eth_header.cc:33
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
Definition: eth_header.cc:86
bool is_unicast() const
Any normal single-destination address.
Definition: eth_header.cc:74
constexpr u64 to_u64() const
Convert from MacAddr to u64.
Definition: eth_header.h:44
EtherType field (uint16) is used a protocol-ID [1536..65535].
Definition: eth_header.h:96
u16 value
The 16-bit value is stored in processor-native order.
Definition: eth_header.h:98
void write_to(satcat5::io::Writeable *wr) const
Write this field to a byte-stream.
Definition: eth_header.h:115
bool operator!=(const satcat5::eth::MacType &other) const
Basic comparisons.
Definition: eth_header.h:108
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
Definition: eth_header.cc:96
bool read_from(satcat5::io::Readable *rd)
Read this field from a byte-stream.
Definition: eth_header.h:118
bool operator==(const satcat5::eth::MacType &other) const
Basic comparisons.
Definition: eth_header.h:106
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
Definition: eth_header.cc:102
bool read_from(satcat5::io::Readable *rd)
Read this field from a byte-stream.
Definition: eth_header.h:159
u16 pcp() const
Accessors for each individual field.
Definition: eth_header.h:137
u16 value
The 16-bit value holds VID, DEI, and PCP fields.
Definition: eth_header.h:126
bool any() const
Accessors for each individual field.
Definition: eth_header.h:134
void write_to(satcat5::io::Writeable *wr) const
Write this field to a byte-stream.
Definition: eth_header.h:156
bool operator==(const satcat5::eth::VlanTag &other) const
Basic comparisons.
Definition: eth_header.h:147
u16 vid() const
Accessors for each individual field.
Definition: eth_header.h:135
void set(u16 vid, u16 dei, u16 pcp)
Accessors for each individual field.
Definition: eth_header.h:138
bool operator!=(const satcat5::eth::VlanTag &other) const
Basic comparisons.
Definition: eth_header.h:149
u16 dei() const
Accessors for each individual field.
Definition: eth_header.h:136
Basic type aliases and prototypes used throughout SatCat5.