feat(core): Java's IP and ICMP threads, measured with Java FILIUS
Every packet that comes in goes through its node's IP thread (UDP, TCP) or ICMP thread (ICMP), as Java's IPThread and ICMPThread: deliver, answer an echo request or forward, one after the other; while one waits for ARP, the next wait behind it. A packet a node sends to itself is delivered at once, as Java's IP.senden does. The transport send threads and these are one mechanism now (handToProtocolThread). ArpRun has three stories through the router (routedUdpBehind, routedPingBesideUdp, routedPingBehindPing); the oracle writes ICMP frames with type, code, addresses and sequence number. All seven ArpRun stories match Java FILIUS. Measured and not taken: Java's ARP wakes one waiter with notify, not the one whose answer came; the core wakes all that wait for the address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,7 @@ import { readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { IPv4DeliveryResult, NetworkRuntimeEngine } from '../src/index.js';
|
||||
import { icmpMessage, type NetworkRuntimeEngine } from '../src/index.js';
|
||||
import { GAP_TOLERANCE_MILLISECONDS, gaps, javaFrames, watchedEngine, type JavaFrame } from './javaRun.js';
|
||||
import type { Scenario } from './scenario.js';
|
||||
|
||||
@@ -21,9 +21,13 @@ interface JavaRun {
|
||||
events: [number, string][];
|
||||
}
|
||||
|
||||
/** A ping's result in Java's words: the ICMP answer's type and code, or null when none came. */
|
||||
function pingResult(result: IPv4DeliveryResult, answered: boolean): string {
|
||||
/** A ping from HOST in Java's words: the ICMP answer's type and code, or null when none came in time. */
|
||||
function ping(engine: NetworkRuntimeEngine, target: string, sequenceNumber = 1): string {
|
||||
const result = engine.sendIcmpEcho(HOST, target, { sequenceNumber });
|
||||
if (result.type === 'icmpError' && result.kind === 'destinationHostUnreachable') return 'ICMP 3/1';
|
||||
const answered = (engine.state.icmpObservationsByNodeId.get(HOST) ?? []).some(
|
||||
(observation) => observation.message.kind === 'echoReply' && observation.message.sequenceNumber === sequenceNumber,
|
||||
);
|
||||
return answered ? 'ICMP 0/0' : 'null';
|
||||
}
|
||||
|
||||
@@ -37,12 +41,8 @@ function udpSend(engine: NetworkRuntimeEngine, target: string, payload: string):
|
||||
}
|
||||
|
||||
const STORIES: Record<string, (engine: NetworkRuntimeEngine, call: Call) => void> = {
|
||||
pingLan: (engine, call) => call('sendProbe 10.0.0.9', () => pingResult(engine.sendIcmpEcho(HOST, '10.0.0.9'), false)),
|
||||
pingRouted: (engine, call) =>
|
||||
call('sendProbe 20.0.0.9', () => {
|
||||
const result = engine.sendIcmpEcho(HOST, '20.0.0.9');
|
||||
return pingResult(result, result.type === 'delivered' && result.nodeId !== HOST);
|
||||
}),
|
||||
pingLan: (engine, call) => call('sendProbe 10.0.0.9', () => ping(engine, '10.0.0.9')),
|
||||
pingRouted: (engine, call) => call('sendProbe 20.0.0.9', () => ping(engine, '20.0.0.9')),
|
||||
udpTwice: (engine, call) => {
|
||||
call('senden eins', () => udpSend(engine, '10.0.0.9', 'eins'));
|
||||
call('senden zwei', () => udpSend(engine, '10.0.0.9', 'zwei'));
|
||||
@@ -51,6 +51,32 @@ const STORIES: Record<string, (engine: NetworkRuntimeEngine, call: Call) => void
|
||||
call('senden verloren', () => udpSend(engine, '10.0.0.9', 'verloren'));
|
||||
call('senden dahinter', () => udpSend(engine, '20.0.0.2', 'dahinter'));
|
||||
},
|
||||
routedUdpBehind: (engine, call) => {
|
||||
call('senden verloren', () => udpSend(engine, '20.0.0.9', 'verloren'));
|
||||
call('senden dahinter', () => udpSend(engine, '20.0.0.2', 'dahinter'));
|
||||
},
|
||||
routedPingBesideUdp: (engine, call) => {
|
||||
call('sendProbe 20.0.0.2', () => ping(engine, '20.0.0.2'));
|
||||
call('senden verloren', () => udpSend(engine, '20.0.0.9', 'verloren'));
|
||||
engine.handle({ type: 'advance', toMilliseconds: engine.state.currentTimeMilliseconds + 100 });
|
||||
call('sendProbe 20.0.0.2', () => ping(engine, '20.0.0.2', 2));
|
||||
},
|
||||
routedPingBehindPing: (engine, call) => {
|
||||
// Java's sendeICMP sends in the caller's thread, which waits for ARP.
|
||||
call('sendeICMP 20.0.0.9', () => {
|
||||
engine.resolveMacAddress(HOST, '10.0.0.1');
|
||||
engine.sendIPv4Packet(HOST, {
|
||||
identity: engine.allocatePacketIdentity(),
|
||||
senderIPAddress: '10.0.0.2',
|
||||
receiverIPAddress: '20.0.0.9',
|
||||
timeToLive: 64,
|
||||
protocol: 'icmp',
|
||||
payload: { kind: 'icmp', message: icmpMessage('echoRequest', { identifier: 2, sequenceNumber: 2, data: new Uint8Array() }) },
|
||||
});
|
||||
return '';
|
||||
});
|
||||
call('sendProbe 20.0.0.2', () => ping(engine, '20.0.0.2'));
|
||||
},
|
||||
};
|
||||
|
||||
/** The client's free port as "client": Java draws it at random, the engine from its seed. */
|
||||
|
||||
Reference in New Issue
Block a user