Repository navigation
Ethernet Driver
Ethernet network driver with MAC address handling and IEEE 802.3 frame processing.
JNode's Ethernet driver subsystem provides the link layer for network communication. It handles 48-bit MAC addresses, IEEE 802.3 frame parsing/construction, and serves as the base for hardware-specific network drivers (RTL8139, ViaRhine, EEPRO100, etc.).
| Class/File | Path | Purpose |
|---|---|---|
EthernetAddress |
net/src/net/org/jnode/net/ethernet/EthernetAddress.java |
48-bit MAC address wrapper |
EthernetHeader |
net/src/net/org/jnode/net/ethernet/EthernetHeader.java |
IEEE 802.3 frame header |
EthernetConstants |
net/src/net/org/jnode/net/ethernet/EthernetConstants.java |
Protocol IDs and frame sizes |
EthernetUtils |
net/src/net/org/jnode/net/ethernet/EthernetUtils.java |
Protocol extraction helper |
AbstractEthernetDriver |
net/src/driver/org/jnode/driver/net/ethernet/spi/AbstractEthernetDriver.java |
Base driver class |
BasicEthernetDriver |
net/src/driver/org/jnode/driver/net/ethernet/spi/BasicEthernetDriver.java |
Extended driver with transmit |
LanceCore |
net/src/driver/org/jnode/driver/net/lance/LanceCore.java |
AMD PCnet/LANCE register and initialization handling |
TxDescriptorRing |
net/src/driver/org/jnode/driver/net/lance/TxDescriptorRing.java |
Transmit descriptor ring; scans from currentDescriptor for a host-owned slot |
EthernetAddress wraps a 6-byte MAC address with parsing, formatting, and broadcast detection:
// Create from string (colon or hyphen separated)
EthernetAddress addr = new EthernetAddress("00:11:22:33:44:55");
// Broadcast address
EthernetAddress broadcast = EthernetAddress.BROADCAST;
// Check if broadcast
boolean isBc = addr.isBroadcast();
// Format to string
String formatted = addr.toString(); // "00:11:22:33:44:55"EthernetHeader parses the 14-byte IEEE 802.3 header:
+0 Destination MAC (6 bytes)
+6 Source MAC (6 bytes)
+12 Length/Type (2 bytes) — >1500 = protocol ID, <=1500 = length
+14 Payload starts here
// Parse from socket buffer
EthernetHeader hdr = new EthernetHeader(skbuf);
// Access fields
EthernetAddress dst = hdr.getDestination();
EthernetAddress src = hdr.getSource();
int lengthType = hdr.getLengthType(); // Protocol ID or length
// Construct and prepend header
EthernetHeader newHdr = new EthernetHeader(dst, src, protocolID);
newHdr.prefixTo(skbuf);EthernetUtils.getProtocol() distinguishes between 802.3 length fields and Ethernet II type values:
int protocol = EthernetUtils.getProtocol(hdr);
// Returns ETH_P_802_2 if length <= 1500
// Returns the type value if length > 1500AbstractNetDriver (abstract base)
└── AbstractEthernetDriver
└── BasicEthernetDriver
├── RTL8139Driver (Realtek)
├── ViaRhineDriver
├── EEPRO100Driver (Intel)
├── LanceDriver
└── BCM570xDriver (Broadcom)
When a packet arrives, AbstractEthernetDriver.onReceive():
- Extracts the Ethernet header from the buffer
- Sets the link layer header on the socket buffer
- Determines protocol ID using
EthernetUtils.getProtocol() - Pulls the header and passes to upper layers
AbstractEthernetDriver.doTransmit() handles outgoing packets:
- Pre-allocates space for 14-byte header
- Uses broadcast as default destination if none specified
- Writes destination and source MAC addresses
- Writes protocol ID from socket buffer's protocol field
- Loops back to self if destination matches our address
- Otherwise calls
doTransmitEthernet()for hardware-specific transmission
BasicEthernetDriver adds frame padding to meet minimum 60-byte frame size (ETH_ZLEN).
The AMD LANCE (PCnet) path has two hardware-ordering rules that are easy to break:
-
Scan the ring from the current descriptor.
TxDescriptorRing.transmit()starts atcurrentDescriptorand walks the whole ring looking for the first descriptor whoseisOwnerSelf()is true. If the ring head is still owned by the card, an older slot further round may already be free. After handing a buffer to the card, it advancescurrentDescriptorpast that slot. The previous implementation looked only at a fixed slot and could logNo free transmit descriptorwhile the ring still had space. -
Acknowledge
CSR0_IDONwhen polling initialization.LanceCore.initialize()writesCSR0_INIT | CSR0_IENA, then pollsCSR0_IDONfor up toINIT_POLL_RETRIES * INIT_POLL_DELAY(1000 ms). On success it starts the chip withCSR0_STRT | CSR0_IENA | CSR0_IDON; theCSR0_IDONbit acknowledges the initialization-done condition. The interrupt handler applies the same start/ack sequence, so a polled initialization and an IDON interrupt cannot start the chip twice. - If polling times out, the code stops the chip (
CSR0_STOP) and relies on the initialization-done interrupt path instead of leaving a half-started device.
Key values from EthernetConstants:
| Constant | Value | Meaning |
|---|---|---|
ETH_ALEN |
6 | MAC address length |
ETH_HLEN |
14 | Header length |
ETH_ZLEN |
60 | Minimum frame size (without FCS) |
ETH_DATA_LEN |
1500 | Maximum payload |
ETH_FRAME_LEN |
1514 | Maximum frame (without FCS) |
ETH_P_IP |
0x0800 | IPv4 |
ETH_P_ARP |
0x0806 | ARP |
ETH_P_IPV6 |
0x86DD | IPv6 |
ETH_P_802_1Q |
0x8100 | VLAN tagging |
ETH_P_PPP_SES |
0x8864 | PPPoE |
-
MAC address byte order: Ethernet uses network byte order (big-endian) in the frame, but
EthernetAddressstores bytes in the natural array order. - Length vs Type ambiguity: Values 0x0500-0xFFFF are treated as protocol types; 0x0000-0x0500 as 802.3 length (with ETH_P_802_2 as default protocol).
-
Broadcast loopback:
doTransmit()checks if destination equals our address and callsonReceive()instead of transmitting — prevents duplicate processing for addressed-to-self traffic. -
Frame padding:
BasicEthernetDriverpads frames under 60 bytes before transmission; this happens in the driver layer, not the protocol layer. - No CRC/FCS handling: The frame check sequence (CRC) is handled by hardware; JNode's driver operates on the header + payload only.
-
A full transmit ring does not imply a full first slot. LANCE must scan from
currentDescriptor; checking one slot can fail while another is free. -
CSR0_IDONis write-one-to-acknowledge. Starting the chip without including it in the sameCSR0write can re-enter initialization. -
Initialization can complete by poll or by interrupt, not both.
initialize()disables interrupts while polling and then starts the chip; the interrupt path checks IDON before starting.
- Network-Stack — Full network architecture
- Network-Layers — Protocol layering with SocketBuffer
- ARP — MAC address resolution (uses EthernetAddress)
- IPv4-Protocol — Higher layer IP protocol
- IPv4-Routing — Route selection over these interfaces
- Network Device Plugin — Network device plugin system
- Driver-Framework — Device driver framework