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

WIP: Program to configure vlan interfaces, routes from a config file #13

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions config_util/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interfaces:
- ifname: enp6s0
customers:
- id: 100
outer_vlan: 1000
inner_vlan: 400
ip_addresses:
- 10.0.2.1/32
- 2a00::1/64
73 changes: 73 additions & 0 deletions config_util/config_utility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#! /bin/python
# SPDX-License-Identifier: GPL-2.0-or-later
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you should include a shebang for ease of use?


import yaml
import ipaddress
import pr2modules
from pyroute2 import IPRoute


CONFIG_FILE = "./config.yaml"

with open(CONFIG_FILE) as f:
config = yaml.load(f, Loader=yaml.SafeLoader)


print(config)

# Create the base interface
ip = IPRoute()

for iface in config["interfaces"]:
if_name = iface["ifname"]
print(if_name)
for customer in iface["customers"]:
inner_vlan_tag = customer["inner_vlan"]
outer_vlan_tag = customer["outer_vlan"]
base_iface = ip.link_lookup(ifname=if_name)[0]
try:
ip.link(
"add",
ifname=f"{if_name}.{inner_vlan_tag}",
kind="vlan",
link=base_iface,
vlan_id=inner_vlan_tag,
)
except pr2modules.netlink.exceptions.NetlinkError as error:
print (error)
# TODO: Check for existing inner tagged iface
# TODO: Check if the base iface exists
outer_vlan_iface = f"{if_name}.{inner_vlan_tag}.{outer_vlan_tag}"
ip.link(
"add",
ifname=outer_vlan_iface,
kind="vlan",
link=ip.link_lookup(ifname=f"{if_name}.{inner_vlan_tag}")[0],
vlan_id=outer_vlan_tag,
)
# TODO: Check for existing outer tagged iface
for ip_addr_str in customer["ip_addresses"]:
ip_addr = ipaddress.ip_interface(ip_addr_str)
if ip_addr.version == 4:
mask = ip_addr.network.prefixlen
ip_str = ip_addr.ip.compressed
ip.addr(
"add",
index=ip.link_lookup(ifname=outer_vlan_iface)[0],
address=ip_str,
mask=mask,
)
if ip_addr.version == 6:
mask = ip_addr.network.prefixlen
ip_str = ip_addr.ip.compressed

ip.addr(
"add",
index=ip.link_lookup(ifname=outer_vlan_iface)[0],
address=ip_str,
mask=mask,
)
# TODO: Existing addresses


# TODO: Routing (with vlan interface), Attaching ebpf program