Skip to content

Commit

Permalink
f-93: Corrects naming from dhcpcore to coredhcp
Browse files Browse the repository at this point in the history
Signed-off-by: Aleix Ramírez <[email protected]>
  • Loading branch information
aleixrm committed Nov 27, 2024
1 parent eb37b6f commit dd4822f
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Ignore the dhcpcore-onelease binary
dhcpcore-onelease
# Ignore the coredhcp-onelease binary
coredhcp-onelease

# Ignore client binary
client/dhcpcore_client
client/coredhcp_client
client/client

# Ignore the leases file
Expand Down
84 changes: 84 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# coredhcp-onelease VRouter plugin for OpenNebula

This go module contains a wrapper for [coredhcp](https://github.com/coredhcp/coredhcp), that instantiates a coredhcp server for each interface indicated in the configuation file, allowing specifying configurations for requests coming from different interfaces. Those services could include our custom `onelease` plugin, which implements the OpenNebula IP Address lease based on the client's MAC address last four bytes (by default, the MAC address should start with the `02:00` prefix). We can also exclude some IPs from the lease adding them to the corresponding parameter.

# Execution

In order to run the server, you should execute the following commands:
```
go build .
sudo ./coredhcp-onelease
```

The coredhcp server will look for a configuration YAML file, for instance located in this same directory (config.yml). See the section below in order to see how to configure it.

# Configuration

In order to load the plugin on the server, you should have a `config.yml` configuration file in one of the following places:
```
* ./onelease-config.yml
* /coredhcp/onelease-config.yml
* /root/.coredhcp/onelease-config.yml
* /etc/coredhcp/onelease-config.yml
```

or you can pass the file explicitly when running the server with the `-c` option:

```
sudo ./coredhcp-onelease -c myconfig.yml
```


The config file content should contain the list of plugins and their arguments for each protocol version (DHCPv6 and DHCPv4), e.g.

```
eth0:
server4:
listen:
- "%eth0"
plugins:
- lease_time: 3600s
- server_id: 192.168.100.1
- dns: 8.8.8.8 8.8.4.4
- router: 192.168.100.1
- netmask: 255.255.255.0
- onelease: leases-eth0.sqlite3 192.168.100.20 192.168.100.30 3600s --excluded-ips 192.168.100.22,192.168.100.25
--mac2ip --mac2ip-prefix 04:00
eth1:
server4:
listen:
- "%eth1"
plugins:
- lease_time: 3600s
- server_id: 172.100.10.1
- dns: 8.8.8.8 8.8.4.4
- router: 172.100.10.1
- netmask: 255.255.255.0
- onelease: leases-eth0.sqlite3 172.100.10.2 172.100.10.100 3600s --excluded-ips 172.100.10.50,172.100.10.60
--mac2ip
```

The plugin parameters are
```
onelease: <lease_database_file> <lease_range_start_ip> <lease_range_end_ip> <lease_time> <optional_parameters>
```
Where optional parameters are:
* `--excluded-ips`: A comma-separated list of IPs from the range to be excluded from the allocation.
* `--mac2ip`: Enables MAC2IP address translation, i.e. it will allocate the specified IP from the least 4 bytes of the provided client MAC address (the IP should be in the lease range).
* `--mac2ip-prefix`: The MAC address 2-byte prefix for using in the MAC2IP feature (all the requests with this client MAC address 2-byte prefix will allocate the specified IP in the last 4 bytes). Defaults to "02:00".

[There](https://github.com/coredhcp/coredhcp/blob/master/cmds/coredhcp/config.yml.example) you have an example of each interface configuration in case you want to take it as reference, but as we are using a wrapper,
remember to nest the configuration on each interface tag.

# Testing

You can test the server features using the [client](./client/README.md) included in this module or any dhcp client tool like `dhclient` or `dhcping`.

# Maintenance

This module `main.go` file is a wrapper of the Coredhcp module, based on the source code generated by the [coredhcp-generator](https://github.com/coredhcp/coredhcp/tree/master/cmds/coredhcp-generator) and adapted for spawning multiple Coredhcp servers on each specified interface. In order to add or remove any plugin you should modify the `main.go` file from this module directly, as if substantially differs from the one generated by the `coredhcp-generator`.

# Licensing

The original work from the Coredhcp team and all the OpenNebula Systems modifications are licensed under the MIT License included in this directory.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ This is a simple dhcpv4 client for use as a debugging tool with coredhcp
The client allows to specify a mac address as argument in order to include it in its requests, e.g.

```
go build -o dhcpcore_client
sudo ./dhcpcore_client "02:00:aa:bb:cc:dd"
go build -o coredhcp_client
sudo ./coredhcp_client "02:00:aa:bb:cc:dd"
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/dhcpcore-onelease
module github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/coredhcp-onelease

go 1.20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"os/signal"
"sync"

oneleaseconfig "github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/dhcpcore-onelease/pkg/config"
oneleaseconfig "github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/coredhcp-onelease/pkg/config"

"github.com/coredhcp/coredhcp/logger"
"github.com/coredhcp/coredhcp/server"

dhcpcoreconfig "github.com/coredhcp/coredhcp/config"
coredhcpconfig "github.com/coredhcp/coredhcp/config"

pl_onelease "github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/dhcpcore-onelease/plugins/onelease"
pl_onelease "github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/coredhcp-onelease/plugins/onelease"

"github.com/coredhcp/coredhcp/plugins"
pl_autoconfigure "github.com/coredhcp/coredhcp/plugins/autoconfigure"
Expand Down Expand Up @@ -136,7 +136,7 @@ func main() {
wg.Add(1)
go func(iface string, configFile string) {
defer wg.Done()
cfg, err := dhcpcoreconfig.Load(configFile)
cfg, err := coredhcpconfig.Load(configFile)
if err != nil {
errChan <- fmt.Errorf("failed to load configuration for interface %s: %v", iface, err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)
ipToAllocate = net.IPNet{IP: ipFromMAC}
log.Infof("MAC %s matches the prefix %x, trying to allocate IP %s...", macAddress.String(), p.MACPrefix, ipToAllocate.IP.String())
} else {
log.Infof("MAC %s does not match the prefix %x, providing conventional lease", macAddress.String(), p.MACPrefix)
log.Infof("MAC %s does not match the prefix %x, providing conventional lease...", macAddress.String(), p.MACPrefix)
}
}
// The allocator will try to allocate the given IP address, but if it exits, it will return a different one (an available one)
// The allocator will try to allocate the given IP address, but if it is already allocated, it will return a different one (an available one)
ip, err := p.allocator.Allocate(ipToAllocate)
if err != nil {
log.Errorf("Could not allocate IP for MAC %s: %v", req.ClientHWAddr.String(), err)
Expand All @@ -97,9 +97,8 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)

// if the MAC address is mapped to an IP address, check if the allocated IP address matches the requested one, if not, revert the allocation and return
if p.enableMAC2IP && macPrefixMatches && !ip.IP.Equal(ipToAllocate.IP) {
log.Warnf("Allocated IP %s for MAC %s does not match the requested IP %s", ip.IP.String(), req.ClientHWAddr.String(), ipToAllocate.IP.String())
//revert the allocation of the undesired IP
p.allocator.Free(ip)
log.Errorf("MAC2IP: Could not allocate IP %s for MAC %s: IP already allocated", req.ClientHWAddr.String(), err)
return resp, true
}

Expand Down
75 changes: 0 additions & 75 deletions appliances/VRouter/DHCP4v2/dhcpcore-onelease/README.md

This file was deleted.

4 changes: 2 additions & 2 deletions appliances/VRouter/DHCP4v2/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module DHCP4v2

ONEAPP_VNF_DHCP4_INTERFACES = env :ONEAPP_VNF_DHCP4_INTERFACES, '' # nil -> none, empty -> all

SERVICE_DIR = '/etc/one-appliance/service.d/VRouter/DHCP4v2/dhcpcore-onelease'
SERVICE_DIR = '/etc/one-appliance/service.d/VRouter/DHCP4v2/coredhcp-onelease'
CONFIG_FILE_NAME = 'onelease-config.yml'

def parse_env
Expand Down Expand Up @@ -127,7 +127,7 @@ def install(initdir: '/etc/init.d')
BASE_DIR="#{SERVICE_DIR}"
CONFIG_FILE="$BASE_DIR/#{CONFIG_FILE_NAME}"
SERVICE_EXEC="$BASE_DIR/dhcpcore-onelease"
SERVICE_EXEC="$BASE_DIR/coredhcp-onelease"
PIDFILE="/run/$RC_SVCNAME.pid"
LOG_DIR="#{SERVICE_LOGDIR}"
LOG_FILE="$LOG_DIR/$RC_SVCNAME.log"
Expand Down
2 changes: 1 addition & 1 deletion packer/service_VRouter/VRouter.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ build {
provisioner "shell" {
inline_shebang = "/bin/bash -e"
inline = [
"cd /etc/one-appliance/service.d/VRouter/DHCP4v2/dhcpcore-onelease",
"cd /etc/one-appliance/service.d/VRouter/DHCP4v2/coredhcp-onelease",
"CGO_ENABLED=1 GCC=musl-gcc go build",
]
}
Expand Down

0 comments on commit dd4822f

Please sign in to comment.