Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f-93: DHCP4v2 onelease plugin code for VRouter #139

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dffed0b
f-93: DHCP4v2 onelease plugin code for VRouter
aleixrm Nov 20, 2024
74debb6
f-93: Corrects plugin return variables
aleixrm Nov 20, 2024
39abbfc
f-93: Wraps server creation for each interface
aleixrm Nov 21, 2024
5afcd98
F93: Alpine 3.19 and compile dhcp server
jaimecb Nov 21, 2024
a1b7ca5
f-93: Implements config steps and tests
aleixrm Nov 22, 2024
4aa3eee
F93: Update install function and add iptables
jaimecb Nov 22, 2024
0c6b9e0
f-93: corrects config file name
aleixrm Nov 25, 2024
10e1f7e
f-93: creates constants for service and config dirs
aleixrm Nov 25, 2024
a7f8207
f-93: Excludes old VNF dhcp4 from the VRouter package
aleixrm Nov 26, 2024
92f34d6
f-93: corrects VRouter dhcp4v2 reference in failover
aleixrm Nov 26, 2024
1e8ec97
f-93: Adds custom leases plugin with excluded ips
aleixrm Nov 26, 2024
a1de3d1
F-93: custom mac prefix support
jaimecb Nov 26, 2024
259cfb5
f-93: Corrects tests
aleixrm Nov 26, 2024
6cf6ccc
f-93: Merges onelease behavior in onerange
aleixrm Nov 26, 2024
b598a84
f-93: Corrects test and config file generation
aleixrm Nov 27, 2024
eb37b6f
F-93: onerange plugin to onelease
jaimecb Nov 27, 2024
dd4822f
f-93: Corrects naming from dhcpcore to coredhcp
aleixrm Nov 27, 2024
ab3940b
f-93: Changes plugin name from range to onelease
aleixrm Nov 27, 2024
4f74bcf
F-93: add coredhcp-onelease tests
jaimecb Nov 28, 2024
367f499
f-93: fix check excluded IP is in range
aleixrm Nov 29, 2024
5bff324
F-93: Packer script rm source code and go test case
jaimecb Nov 29, 2024
f50daba
F-93: Fix log output and improve test coverage
jaimecb Dec 2, 2024
b8e4137
f-93: Fixes returned response when error
aleixrm Dec 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ packer-service_Wordpress: packer-alma8 ${DIR_EXPORT}/service_Wordpress.qcow2
packer-service_example: packer-alma8 ${DIR_EXPORT}/service_example.qcow2
@${INFO} "Packer service_example done"

packer-service_VRouter: packer-alpine318 ${DIR_EXPORT}/service_VRouter.qcow2
packer-service_VRouter: packer-alpine319 ${DIR_EXPORT}/service_VRouter.qcow2
@${INFO} "Packer service_VRouter done"

packer-service_Harbor: packer-ubuntu2204 ${DIR_EXPORT}/service_Harbor.qcow2
Expand Down
12 changes: 12 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Ignore the coredhcp-onelease binary
coredhcp-onelease

# Ignore client binary
client/coredhcp_client
client/client

# Ignore the leases file
*.sqlite3

# Ignore config files
onelease-config.yml
21 changes: 21 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 coredhcp, 2024 OpenNebula Systems

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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.

14 changes: 14 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# DHCPv4 debug client

This is a simple dhcpv4 client for use as a debugging tool with coredhcp

***This is not a general-purpose DHCP client. This is only a testing/debugging tool for developing CoreDHCP***

# Execution

The client allows to specify a mac address as argument in order to include it in its requests, e.g.

```
go build -o coredhcp_client
sudo ./coredhcp_client "02:00:aa:bb:cc:dd"
```
54 changes: 54 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

/*
* Sample DHCPv4 client to test on the local interface
*/

import (
"flag"
"net"
"time"

"github.com/coredhcp/coredhcp/logger"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
)

var log = logger.GetLogger("main")

func main() {
flag.Parse()

var macString string
if len(flag.Args()) > 0 {
macString = flag.Arg(0)
} else {
macString = "00:11:22:33:44:55"
}

c := client4.NewClient()
c.LocalAddr = &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 68,
}
c.RemoteAddr = &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 67,
}
c.ReadTimeout = 10 * time.Second
log.Printf("%+v", c)

mac, err := net.ParseMAC(macString)
if err != nil {
log.Fatal(err)
}

conv, err := c.Exchange("lo",
dhcpv4.WithHwAddr(mac))
for _, p := range conv {
log.Print(p.Summary())
}
if err != nil {
log.Fatal(err)
}
}
49 changes: 49 additions & 0 deletions appliances/VRouter/DHCP4v2/coredhcp-onelease/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module github.com/OpenNebula/one-apps/appliances/VRouterd/DHCP4v2/coredhcp-onelease

go 1.20

require (
github.com/coredhcp/coredhcp v0.0.0-20240908184240-576af8676ffa
github.com/insomniacslk/dhcp v0.0.0-20240829085014-a3a4c1f04475
github.com/mattn/go-sqlite3 v1.14.22
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bits-and-blooms/bitset v1.14.2 // indirect
github.com/chappjc/logrus-prefix v0.0.0-20180227015900-3a1d64819adb // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/u-root/uio v0.0.0-20230305220412-3e8cd9d6bf63 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
Loading