-
Notifications
You must be signed in to change notification settings - Fork 8
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
shashank68
wants to merge
2
commits into
xdp-project:main
Choose a base branch
from
shashank68:config_util
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interfaces: | ||
- ifname: enp2s0 | ||
customers: | ||
- id: 100 | ||
outer_vlan: 1000 | ||
inner_vlan: 400 | ||
ip_addresses: | ||
- 10.0.2.1/32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
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: | ||
pass | ||
|
||
# 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(outer_vlan_iface)[0], | ||
address=ip_str, | ||
mask=mask, | ||
) | ||
# TODO: Handle ipv6 | ||
# TODO: Existing addresses | ||
|
||
|
||
# TODO: Routing (with vlan interface), Attaching ebpf program |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?