SatCat5
switch_cfg.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
13 
14 #pragma once
15 
16 #include <satcat5/cfgbus_core.h>
17 #include <satcat5/ethernet.h>
18 
19 namespace satcat5 {
20  namespace eth {
35  constexpr u32 VTAG_ADMIT_ALL = 0x00000000u;
36  constexpr u32 VTAG_RESTRICT = 0x00010000u;
37  constexpr u32 VTAG_PRIORITY = 0x00110000u;
38  constexpr u32 VTAG_MANDATORY = 0x00220000u;
40 
49  constexpr u32 VPOL_UNLIMITED = 0x80000000u;
50  constexpr u32 VPOL_DEMOTE = 0x90000000u;
51  constexpr u32 VPOL_STRICT = 0xA0000000u;
52  constexpr u32 VPOL_AUTO = 0xB0000000u;
54 
55  // Define unit scaling for VLAN rate-limiter configuration.
56  constexpr u32 VRATE_SCALE_1X = 0x00000000u; // 1 LSB = 8 kbps
57  constexpr u32 VRATE_SCALE_256X = 0x08000000u; // 1 LSB = 2 Mbps
58  constexpr u64 VRATE_THRESHOLD = 100000000;
59 
63  constexpr u32 VLAN_CONNECT_ALL = (u32)(-1);
64  constexpr u32 VLAN_CONNECT_NONE = 0;
66 
68  struct VtagPolicy {
71  u32 value;
72 
75  inline u32 policy() const
76  { return u32(value & 0x00FF0000); }
77  inline unsigned port() const
78  { return unsigned(value >> 24); }
79  inline satcat5::eth::VlanTag vtag() const
80  { return VlanTag{u16(value & 0x0000FFFF)}; }
82 
88  constexpr VtagPolicy(u32 port, u32 policy, VlanTag vtag = VTAG_DEFAULT)
89  : value(policy | vtag.value | ((port & 0xFF) << 24)) {}
90  constexpr VtagPolicy()
91  : value(0) {}
92  explicit constexpr VtagPolicy(u32 other)
93  : value(other) {}
94  constexpr VtagPolicy(const VtagPolicy& other)
95  : value(other.value) {}
97  { value = other.value; return *this; }
99  };
100 
102  constexpr satcat5::eth::VtagPolicy
103  VCFG_DEFAULT(0, VTAG_ADMIT_ALL, VTAG_DEFAULT);
104 
107  struct VlanRate {
109  u32 tok_rate;
110  u32 tok_max;
111 
113  static constexpr u32 bps2rate(u64 rate_bps) {
114  return (rate_bps < VRATE_THRESHOLD)
115  ? u32(rate_bps / 8000)
116  : u32(rate_bps / 2000000);
117  }
118 
120  constexpr VlanRate() // Default constructor
122  , tok_rate(0)
123  , tok_max(0) {}
125  constexpr VlanRate(
126  u32 policy, // Policy (e.g., VPOL_STRICT)
127  u64 rate_bps, // Rate limit (bits per second)
128  u32 burst_msec=1) // Burst duration in milliseconds
129  : tok_policy(
130  rate_bps < VRATE_THRESHOLD
131  ? (policy | VRATE_SCALE_1X)
132  : (policy | VRATE_SCALE_256X))
133  , tok_rate(bps2rate(rate_bps))
134  , tok_max(burst_msec * bps2rate(rate_bps))
135  {
136  // Nothing else to initialize.
137  }
138  };
139 
144  constexpr satcat5::eth::VlanRate
145  VRATE_ZERO (VPOL_STRICT, 0, 0),
146  VRATE_8KBPS (VPOL_STRICT, 8000ull, 4096),
147  VRATE_16KBPS (VPOL_STRICT, 16000ull, 2048),
148  VRATE_32KBPS (VPOL_STRICT, 32000ull, 1024),
149  VRATE_64KBPS (VPOL_STRICT, 64000ull, 512),
150  VRATE_128KBPS (VPOL_STRICT, 128000ull, 256),
151  VRATE_256KBPS (VPOL_STRICT, 256000ull, 128),
152  VRATE_512KBPS (VPOL_STRICT, 512000ull, 64),
153  VRATE_1MBPS (VPOL_STRICT, 1000000ull, 32),
154  VRATE_2MBPS (VPOL_STRICT, 2000000ull, 16),
155  VRATE_4MBPS (VPOL_STRICT, 4000000ull, 8),
156  VRATE_8MBPS (VPOL_STRICT, 8000000ull, 4),
157  VRATE_10MBPS (VPOL_STRICT, 10000000ull, 3),
158  VRATE_16MBPS (VPOL_STRICT, 16000000ull, 2),
159  VRATE_100MBPS (VPOL_STRICT, 100000000ull),
160  VRATE_1GBPS (VPOL_STRICT, 1000000000ull),
161  VRATE_10GBPS (VPOL_STRICT, 10000000000ull),
162  VRATE_UNLIMITED (VPOL_UNLIMITED, 0, 0);
164 
166  class SwitchConfig {
167  public:
169  SwitchConfig(satcat5::cfg::ConfigBus* cfg, unsigned devaddr);
170 
172  void log_info(const char* label);
173 
175  u32 port_count();
176 
178  void priority_reset();
179 
184  bool priority_set(u16 etype, unsigned plen = 16);
185 
189  void set_miss_bcast(unsigned port_idx, bool enable);
190 
192  void set_miss_mask(u32 mask);
193 
195  u32 get_miss_mask();
196 
200  void set_promiscuous(unsigned port_idx, bool enable);
201 
203  void set_promiscuous_mask(u32 mask);
204 
206  u32 get_promiscuous_mask();
207 
209  void set_traffic_filter(u16 etype = 0);
211  inline u16 get_traffic_filter() const {return m_stats_filter;}
212 
214  u32 get_traffic_count();
215 
217  u16 get_frame_min();
219  u16 get_frame_max();
220 
224 
228  s32 ptp_get_offset_rx(unsigned port_idx);
229  s32 ptp_get_offset_tx(unsigned port_idx);
230  u32 ptp_get_2step_mask();
231  void ptp_set_offset_rx(unsigned port_idx, s32 subns);
232  void ptp_set_offset_tx(unsigned port_idx, s32 subns);
233  void ptp_set_2step(unsigned port_idx, bool enable);
235 
237  void vlan_reset(bool lockdown = false);
239  u32 vlan_get_mask(u16 vid);
241  void vlan_set_mask(u16 vid, u32 mask);
243  void vlan_set_port(const VtagPolicy& cfg);
245  void vlan_join(u16 vid, unsigned port);
247  void vlan_leave(u16 vid, unsigned port);
249  void vlan_set_rate(u16 vid, const satcat5::eth::VlanRate& cfg);
250 
252  unsigned mactbl_size();
258  bool mactbl_read(
259  unsigned tbl_idx,
260  unsigned& port_idx,
261  satcat5::eth::MacAddr& mac_addr);
268  bool mactbl_write(
269  unsigned port_idx,
270  const satcat5::eth::MacAddr& mac_addr);
272  bool mactbl_clear();
274  bool mactbl_learn(bool enable);
276  void mactbl_log(const char* label);
277 
278  // Define ConfigBus register map (see also: switch_types.vhd)
279  static constexpr unsigned
297  REG_LOGGING = 17;
298 
299  // Additional ConfigBus registers for each port.
300  static constexpr unsigned REG_PORT(unsigned idx)
301  {return 512 + 16*idx;}
302  static constexpr unsigned REG_PTP_RX(unsigned idx)
303  {return REG_PORT(idx) + 8;}
304  static constexpr unsigned REG_PTP_TX(unsigned idx)
305  {return REG_PORT(idx) + 9;}
306 
307  protected:
308  bool mactbl_wait_idle();
309 
313  };
314  }
315 }
ConfigBus core definitions.
Generic ConfigBus API.
Definition: cfgbus_core.h:124
Pointer-like wrapper for one or more ConfigBus registers.
Definition: cfgbus_core.h:76
Management functions for a SatCat5 Ethernet switch.
Definition: switch_cfg.h:166
void mactbl_log(const char *label)
Log the contents of the MAC-address table.
Definition: switch_cfg.cc:296
bool mactbl_write(unsigned port_idx, const satcat5::eth::MacAddr &mac_addr)
Write a new entry to the MAC-address table.
Definition: switch_cfg.cc:257
static constexpr unsigned REG_MACTBL_CTRL
MAC-table control (read-write)
Definition: switch_cfg.h:293
static constexpr unsigned REG_MACTBL_LSB
MAC-table control (read-write)
Definition: switch_cfg.h:291
static constexpr unsigned REG_VLAN_VID
VLAN connections: set VID (read-write)
Definition: switch_cfg.h:289
void vlan_reset(bool lockdown=false)
Revert all VLAN settings to default.
Definition: switch_cfg.cc:165
u32 get_miss_mask()
Identify which ports are currently in "miss-as-broadcast" mode.
Definition: switch_cfg.cc:91
bool priority_set(u16 etype, unsigned plen=16)
Designate specific EtherType range(s) as high-priority.
Definition: switch_cfg.cc:59
u16 get_frame_max()
Get the maximum allowed frame size, in bytes.
Definition: switch_cfg.cc:129
static constexpr unsigned REG_PORT(unsigned idx)
Base address for per-port registers.
Definition: switch_cfg.h:300
void vlan_set_mask(u16 vid, u32 mask)
Set connected port-mask for the designated VLAN.
Definition: switch_cfg.cc:196
static constexpr unsigned REG_MACCOUNT
MAC-address table size (read-only)
Definition: switch_cfg.h:283
void ptp_set_2step(unsigned port_idx, bool enable)
PTP configuration for each port.
Definition: switch_cfg.cc:158
static constexpr unsigned REG_VLAN_PORT
VLAN port configuration (write-only)
Definition: switch_cfg.h:288
void vlan_leave(u16 vid, unsigned port)
Remote a given port from the designated VLAN.
Definition: switch_cfg.cc:222
SwitchConfig(satcat5::cfg::ConfigBus *cfg, unsigned devaddr)
Attach to the designated ConfigBus address.
Definition: switch_cfg.cc:26
void set_miss_bcast(unsigned port_idx, bool enable)
Enable or disable "miss-as-broadcast" flag on the specified port.
Definition: switch_cfg.cc:80
static constexpr unsigned REG_FRAMESIZE
Frame size limits (read-only)
Definition: switch_cfg.h:287
bool mactbl_clear()
Clear MAC-address table contents.
Definition: switch_cfg.cc:278
void vlan_join(u16 vid, unsigned port)
Join a given port to the designated VLAN.
Definition: switch_cfg.cc:212
unsigned mactbl_size()
Read the maximum size of the MAC-address table.
Definition: switch_cfg.cc:273
void set_miss_mask(u32 mask)
Enable or disable all "miss-as-broadcast" flags at once.
Definition: switch_cfg.cc:87
static constexpr unsigned REG_CORECLOCK
Core clock frequency, in Hz (read-only)
Definition: switch_cfg.h:282
static constexpr unsigned REG_DATAPATH
Datapath width, in bits (read-only)
Definition: switch_cfg.h:281
static constexpr unsigned REG_PROMISC
Promisicuous port mask (read-write)
Definition: switch_cfg.h:284
void ptp_set_offset_tx(unsigned port_idx, s32 subns)
PTP configuration for each port.
Definition: switch_cfg.cc:154
bool mactbl_wait_idle()
Wait for MAC-table access.
Definition: switch_cfg.cc:314
static constexpr unsigned REG_PRIORITY
Packet prioritization (read-write, optional)
Definition: switch_cfg.h:285
void vlan_set_port(const VtagPolicy &cfg)
Set tag policy and other per-port settings.
Definition: switch_cfg.cc:201
static constexpr unsigned REG_MISS_BCAST
Miss-as-broadcast port mask (read-write)
Definition: switch_cfg.h:294
u32 get_promiscuous_mask()
Identify which ports are currently promiscuous.
Definition: switch_cfg.cc:106
static constexpr unsigned REG_MACTBL_MSB
MAC-table control (read-write)
Definition: switch_cfg.h:292
u32 vlan_get_mask(u16 vid)
Get connected port-mask for the designated VLAN.
Definition: switch_cfg.cc:191
u32 m_pri_wridx
Next index in priority table.
Definition: switch_cfg.h:311
static constexpr unsigned REG_PTP_TX(unsigned idx)
Tx timestamp offset for Nth port.
Definition: switch_cfg.h:304
static constexpr unsigned REG_VLAN_RATE
VLAN rate-control configuration (write-only)
Definition: switch_cfg.h:296
u16 get_traffic_filter() const
Query the current traffic filter setting.
Definition: switch_cfg.h:211
u32 ptp_get_2step_mask()
PTP configuration for each port.
Definition: switch_cfg.cc:146
u32 get_traffic_count()
Report matching frames since last call to get_traffic_count().
Definition: switch_cfg.cc:115
satcat5::cfg::Register m_reg
ConfigBus register space.
Definition: switch_cfg.h:310
static constexpr unsigned REG_PTP_2STEP
PTP "twoStep" mode flag (read-write)
Definition: switch_cfg.h:295
void priority_reset()
Clear all EtherType-based priority settings.
Definition: switch_cfg.cc:48
void set_traffic_filter(u16 etype=0)
Set EtherType filter for traffic reporting. (0 = Any type)
Definition: switch_cfg.cc:110
s32 ptp_get_offset_rx(unsigned port_idx)
PTP configuration for each port.
Definition: switch_cfg.cc:138
void set_promiscuous_mask(u32 mask)
Set the "promiscuous" flag for all ports at once.
Definition: switch_cfg.cc:102
u32 port_count()
Number of ports on this switch.
Definition: switch_cfg.cc:44
static constexpr unsigned REG_PTP_RX(unsigned idx)
Rx timestamp offset for Nth port.
Definition: switch_cfg.h:302
static constexpr unsigned REG_PORTCOUNT
Number of ports (read-only)
Definition: switch_cfg.h:280
void set_promiscuous(unsigned port_idx, bool enable)
Enable or disable "promiscuous" flag on the specified port index.
Definition: switch_cfg.cc:95
satcat5::cfg::Register get_log_register()
Get packet-logging register.
Definition: switch_cfg.cc:134
static constexpr unsigned REG_LOGGING
Packet logging diagnostics (read-only)
Definition: switch_cfg.h:297
bool mactbl_read(unsigned tbl_idx, unsigned &port_idx, satcat5::eth::MacAddr &mac_addr)
Read the Nth entry from the MAC-address table.
Definition: switch_cfg.cc:232
void log_info(const char *label)
Log some basic info about this switch.
Definition: switch_cfg.cc:34
static constexpr unsigned REG_VLAN_MASK
VLAN connections: set mask (read-write)
Definition: switch_cfg.h:290
void ptp_set_offset_rx(unsigned port_idx, s32 subns)
PTP configuration for each port.
Definition: switch_cfg.cc:150
s32 ptp_get_offset_tx(unsigned port_idx)
PTP configuration for each port.
Definition: switch_cfg.cc:142
u16 get_frame_min()
Get the minimum allowed frame size, in bytes.
Definition: switch_cfg.cc:124
void vlan_set_rate(u16 vid, const satcat5::eth::VlanRate &cfg)
Set the maximum aggregated throughput for a given VID.
Definition: switch_cfg.cc:205
u16 m_stats_filter
Filter stats by EtherType.
Definition: switch_cfg.h:312
bool mactbl_learn(bool enable)
Enable automatic learning of new MAC addresses?
Definition: switch_cfg.cc:287
static constexpr unsigned REG_PKTCOUNT
Packet-counting w/ filter (read-write)
Definition: switch_cfg.h:286
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
Data structure for configuring VLAN rate-limiter parameters.
Definition: switch_cfg.h:107
u32 tok_rate
Tokens per millisecond.
Definition: switch_cfg.h:109
u32 tok_max
Maximum accumulated tokens.
Definition: switch_cfg.h:110
constexpr VlanRate(u32 policy, u64 rate_bps, u32 burst_msec=1)
Constructor for any other policy.
Definition: switch_cfg.h:125
u32 tok_policy
Policy and scaling.
Definition: switch_cfg.h:108
constexpr VlanRate()
Constructof for the default unlimited policy.
Definition: switch_cfg.h:120
static constexpr u32 bps2rate(u64 rate_bps)
Convert bits-per-second to internal configuration word.
Definition: switch_cfg.h:113
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
Data structure for configuring VLAN tagging policy of each port.
Definition: switch_cfg.h:68
VtagPolicy & operator=(const VtagPolicy &other)
Each constructors creates a packed configuration word.
Definition: switch_cfg.h:96
u32 value
Packed value holds the tag policy, port number, and default VID.
Definition: switch_cfg.h:71
constexpr VtagPolicy(u32 port, u32 policy, VlanTag vtag=VTAG_DEFAULT)
Each constructors creates a packed configuration word.
Definition: switch_cfg.h:88
u32 policy() const
Accessors for each individual field.
Definition: switch_cfg.h:75
constexpr VtagPolicy(const VtagPolicy &other)
Each constructors creates a packed configuration word.
Definition: switch_cfg.h:94
constexpr VtagPolicy(u32 other)
Each constructors creates a packed configuration word.
Definition: switch_cfg.h:92
unsigned port() const
Accessors for each individual field.
Definition: switch_cfg.h:77
satcat5::eth::VlanTag vtag() const
Accessors for each individual field.
Definition: switch_cfg.h:79
constexpr VtagPolicy()
Each constructors creates a packed configuration word.
Definition: switch_cfg.h:90
constexpr satcat5::eth::VtagPolicy VCFG_DEFAULT(0, VTAG_ADMIT_ALL, VTAG_DEFAULT)
Default VLAN tagging policy.
constexpr satcat5::eth::VlanRate VRATE_32KBPS(VPOL_STRICT, 32000ull, 1024)
Define some commonly-used rate-limiter configurations.
constexpr u32 VTAG_RESTRICT
Define VLAN policy modes for a given switch port.
Definition: switch_cfg.h:36
constexpr satcat5::eth::VlanRate VRATE_16MBPS(VPOL_STRICT, 16000000ull, 2)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_16KBPS(VPOL_STRICT, 16000ull, 2048)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_100MBPS(VPOL_STRICT, 100000000ull)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_1GBPS(VPOL_STRICT, 1000000000ull)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_10GBPS(VPOL_STRICT, 10000000000ull)
Define some commonly-used rate-limiter configurations.
constexpr u32 VTAG_ADMIT_ALL
Define VLAN policy modes for a given switch port.
Definition: switch_cfg.h:35
constexpr u32 VPOL_UNLIMITED
Define VLAN rate-limiter modes for each VID.
Definition: switch_cfg.h:49
constexpr satcat5::eth::VlanRate VRATE_256KBPS(VPOL_STRICT, 256000ull, 128)
Define some commonly-used rate-limiter configurations.
constexpr u32 VLAN_CONNECT_ALL
Common port-connection masks.
Definition: switch_cfg.h:63
constexpr satcat5::eth::VlanRate VRATE_ZERO(VPOL_STRICT, 0, 0)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_64KBPS(VPOL_STRICT, 64000ull, 512)
Define some commonly-used rate-limiter configurations.
constexpr u32 VLAN_CONNECT_NONE
Common port-connection masks.
Definition: switch_cfg.h:64
constexpr satcat5::eth::VlanRate VRATE_1MBPS(VPOL_STRICT, 1000000ull, 32)
Define some commonly-used rate-limiter configurations.
constexpr u32 VPOL_STRICT
Define VLAN rate-limiter modes for each VID.
Definition: switch_cfg.h:51
constexpr satcat5::eth::VlanRate VRATE_UNLIMITED(VPOL_UNLIMITED, 0, 0)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_8KBPS(VPOL_STRICT, 8000ull, 4096)
Define some commonly-used rate-limiter configurations.
constexpr u32 VTAG_MANDATORY
Define VLAN policy modes for a given switch port.
Definition: switch_cfg.h:38
constexpr u32 VPOL_AUTO
Define VLAN rate-limiter modes for each VID.
Definition: switch_cfg.h:52
constexpr satcat5::eth::VlanRate VRATE_2MBPS(VPOL_STRICT, 2000000ull, 16)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_8MBPS(VPOL_STRICT, 8000000ull, 4)
Define some commonly-used rate-limiter configurations.
constexpr u32 VTAG_PRIORITY
Define VLAN policy modes for a given switch port.
Definition: switch_cfg.h:37
constexpr satcat5::eth::VlanRate VRATE_10MBPS(VPOL_STRICT, 10000000ull, 3)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_128KBPS(VPOL_STRICT, 128000ull, 256)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_512KBPS(VPOL_STRICT, 512000ull, 64)
Define some commonly-used rate-limiter configurations.
constexpr satcat5::eth::VlanRate VRATE_4MBPS(VPOL_STRICT, 4000000ull, 8)
Define some commonly-used rate-limiter configurations.
constexpr u32 VPOL_DEMOTE
Define VLAN rate-limiter modes for each VID.
Definition: switch_cfg.h:50