From cf5d5968a4fc3bf93a883b7d26a7c55fb5d765c7 Mon Sep 17 00:00:00 2001 From: wonkr Date: Tue, 12 Jul 2022 18:43:54 -0500 Subject: [PATCH] add dhcp example --- examples/B10-dhcp/README.md | 68 +++++++++++++++++++++++++++++++++++++ examples/B10-dhcp/dhcp.py | 55 ++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 examples/B10-dhcp/README.md create mode 100755 examples/B10-dhcp/dhcp.py diff --git a/examples/B10-dhcp/README.md b/examples/B10-dhcp/README.md new file mode 100644 index 000000000..ee8134792 --- /dev/null +++ b/examples/B10-dhcp/README.md @@ -0,0 +1,68 @@ +# DHCP + +In this example, we show how to deploy a dhcp server inside the +SEED Emulator and how to set a host's ip with the installed dhcp server. +We first create dhcp servers on `AS151` and `AS152` controller. +We then 2 hosts in each AS to get ip address from the installed dhcp server. + +See the comments in the code for detailed explanation. + +We can utilize DHCP on `C03-bring-your-own-internet`. When we connect external +devices such as computer, smartphone, and IoT device to a internet emulator, +the installed dhcp will assign ip address to the newly attached devices so that +they can communicate with the nodes inside the emulator and use internet service. + +## Step 1) Deploy a dhcp + +```python +# Create a DHCP server (virtual node). +dhcp = DHCPService() + +# Default DhcpIpRange : x.x.x.101 ~ x.x.x.120 +# Set DhcpIpRange : x.x.x.125 ~ x.x.x.140 +dhcp.install('dhcp-01').setIpRange(125, 140) +dhcp.install('dhcp-02') + + +# Customize the display name (for visualization purpose) +emu.getVirtualNode('dhcp-01').setDisplayName('DHCP Server 1') +emu.getVirtualNode('dhcp-02').setDisplayName('DHCP Server 2') + + +# Create new host in AS-151 and AS-161, use them to host the DHCP servers. +# We can also host it on an existing node. +as151 = base.getAutonomousSystem(151) +as151.createHost('dhcp-server-01').joinNetwork('net0') + +as161 = base.getAutonomousSystem(161) +as161.createHost('dhcp-server-02').joinNetwork('net0') + +# Bind the DHCP virtual node to the physical node. +emu.addBinding(Binding('dhcp-01', filter = Filter(asn=151, nodeName='dhcp-server-01'))) +emu.addBinding(Binding('dhcp-02', filter = Filter(asn=161, nodeName='dhcp-server-02'))) +``` + +Use method `DHCPServer:setIpRange` to set the ip range to assign. +The default IP range of Emulator is as below. +- host ip range : 71-99 +- dhcp ip range : 101-120 +- router ip range : 254-200 + +`DHCPServer:setIpRange` can change dhcp ip range. To change entire ip range, +we can use `Network:setHostIpRange()`, `Network:setDhcpIpRange()`, and +`Network:setRouterIpRange()`. + + +### Step 2) Set host to use dhcp + +```python +# Create new hosts in AS-151, use it to host the Host which use dhcp instead of static ip +as151.createHost('dhcp-client-01').joinNetwork('net0', address = "dhcp") +as151.createHost('dhcp-client-02').joinNetwork('net0', address = "dhcp") + +# Create new hosts in AS-161, use it to host the Host which use dhcp instead of static ip +as161.createHost('dhcp-client-01').joinNetwork('net0', address = "dhcp") +as161.createHost('dhcp-client-01').joinNetwork('net0', address = "dhcp") + +``` + diff --git a/examples/B10-dhcp/dhcp.py b/examples/B10-dhcp/dhcp.py new file mode 100755 index 000000000..6baa9fdf4 --- /dev/null +++ b/examples/B10-dhcp/dhcp.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# encoding: utf-8 + +from seedemu import * + +emu = Emulator() + +# Load the pre-built component +emu.load('../B00-mini-internet/base-component.bin') + +base:Base = emu.getLayer('Base') + +# Create a DHCP server (virtual node). +dhcp = DHCPService() + +# Default DhcpIpRange : x.x.x.101 ~ x.x.x.120 +# Set DhcpIpRange : x.x.x.125 ~ x.x.x.140 +dhcp.install('dhcp-01').setIpRange(125, 140) +dhcp.install('dhcp-02') + + +# Customize the display name (for visualization purpose) +emu.getVirtualNode('dhcp-01').setDisplayName('DHCP Server 1') +emu.getVirtualNode('dhcp-02').setDisplayName('DHCP Server 2') + + +# Create new hosts in AS-151 and AS-161, use them to host the DHCP servers. +# We can also host it on an existing node. +as151 = base.getAutonomousSystem(151) +as151.createHost('dhcp-server-01').joinNetwork('net0') + +as161 = base.getAutonomousSystem(161) +as161.createHost('dhcp-server-02').joinNetwork('net0') + +# Bind the DHCP virtual node to the physical node. +emu.addBinding(Binding('dhcp-01', filter = Filter(asn=151, nodeName='dhcp-server-01'))) +emu.addBinding(Binding('dhcp-02', filter = Filter(asn=161, nodeName='dhcp-server-02'))) + + +# Create new hosts in AS-151 and AS-161 +# Make them to use dhcp instead of static ip +as151.createHost('dhcp-client-01').joinNetwork('net0', address = "dhcp") +as151.createHost('dhcp-client-02').joinNetwork('net0', address = "dhcp") + +as161.createHost('dhcp-client-03').joinNetwork('net0', address = "dhcp") +as161.createHost('dhcp-client-04').joinNetwork('net0', address = "dhcp") + +# Add the dhcp layer +emu.addLayer(dhcp) + +# Render the emulation +emu.render() + +# Compil the emulation +emu.compile(Docker(), './output', override=True) \ No newline at end of file