mininet
Spin up and interact with virtual networks using
Mininet and Node.js
npm install mininet
Usage
const mininet = require('mininet')
const mn = new Mininet()
const s1 = mn.createSwitch()
const h1 = mn.createHost()
const h2 = mn.createHost()
h1.link(s1)
h2.link(s1)
mn.start()
const proc = h1.spawn('node server.js')
proc.on('message:listening', function () {
const proc2 = h2.spawn('curl --silent ' + h1.ip + ':10000')
proc2.on('stdout', function (data) {
process.stdout.write('h2 ' + data)
mn.stop()
})
})
proc.on('stdout', function (data) {
process.stdout.write('h1 ' + data)
})
Assuming server.js looks like this
const http = require('http')
const mn = require('mininet/host')
const server = http.createServer(function (req, res) {
console.log('Server responding')
res.end('hello from server\n')
})
server.listen(10000, function () {
console.log('Server listening on', this.address().port)
mn.send('listening')
})
API
const mn = new Mininet([options])
Create a new mininet instance. Options include
{
clean: false,
sudo: true,
sock: '/tmp/mn.sock',
debug: false,
stdio: null,
prefixStdio: false
}
If for some reason your mininet instance stops working
you probably wanna try using clean: true.
mn.start([callback])
Start the mininet network. Usually you call this
after defining your hosts, switches and links.
After the network has fully started start is emitted.
mn.stop([callback])
Stop the mininet network. You should not call
any other methods after this.
After the network has fully stopped stop is emitted.
mn.switches
Array of all created switches.
mn.hosts
Array of all created hosts.
const sw = mn.createSwitch()
Create a new switch
sw.link(other, [options])
Link the switch with another switch or host.
Options include:
{
bandwidth: 10,
delay: '100ms',
loss: 10,
htb: true
}
const host = mn.createHost()
Create an new host
host.ip
The IP address of the host. Populated after the network is started.
host.mac
The MAC address of the host. Populated after the network is started.
host.link(other, [options])
Link the host with another host or switch.
Takes the same options as sw.link.
host.exec(cmd, [callback])
Execute a command and buffer the output and return it in the callback.
const proc = host.spawn(cmd, [options])
Spawn a new process to run the in background of the host.
Options include:
{
stdio: 'inherit',
prefixStdio: 'some-prefix'
}
If you set prefixStdio: true it will be converted to {host.id}.{process.id}.
When debugging it can be useful to set both {stdio: 'inherit', prefixStdio: true}.
const proc = host.spawnNode(programSource, [options])
Helper that spawns a Node.js source inside the host. Useful when using multiline strings
host.spawnNode(
`
console.log('starting timer...')
setInterval(() => console.log('Time is', Date.now()))
`,
{
stdio: 'inherit',
prefixStdio: true
}
)
proc.id
Unique string id of the process
proc.kill([signal])
Kill the process.
proc.send(type, data)
Send a message to the process.
proc.on('stdout', data)
Emitted when the process has output.
proc.on('message', type, data)
Emitted when the process received a message.
proc.on('message:{type}', data)
Same as above but with the type as part of the event name
for convenience.
proc.on('exit')
Emitted when the process exits.
Messaging
If you are spawning a node process you can require mininet/host
to communicate with the host.
const host = require('mininet/host')
Require this in a spawned process.
host.send(type, data)
Send a message to the host.
host.sendTo(processId, type, data)
Send a message to another process.
host.broadcast(type, data)
Send a message to all processes.
host.on('message', type, data, metadata)
Emitted when a message is received from the host.
The metadata argument contains the following data
{
from: 'some-process-id-or-host'
}
host.on('message:{type}', data, metadata)
Same as above but with the type as part of the event name
for convenience.
License
MIT