From ebefec595dac54cf6c8fe83ebe580dcc2014cb63 Mon Sep 17 00:00:00 2001 From: Shashank D Date: Thu, 11 Nov 2021 20:11:17 +0530 Subject: [PATCH 1/2] config: Create required interfaces in BNG Signed-off-by: Shashank D --- config_util/config.yaml | 8 +++++ config_util/config_utility.py | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 config_util/config.yaml create mode 100644 config_util/config_utility.py diff --git a/config_util/config.yaml b/config_util/config.yaml new file mode 100644 index 0000000..540ed26 --- /dev/null +++ b/config_util/config.yaml @@ -0,0 +1,8 @@ +interfaces: + - ifname: enp2s0 + customers: + - id: 100 + outer_vlan: 1000 + inner_vlan: 400 + ip_addresses: + - 10.0.2.1/32 \ No newline at end of file diff --git a/config_util/config_utility.py b/config_util/config_utility.py new file mode 100644 index 0000000..f074a3b --- /dev/null +++ b/config_util/config_utility.py @@ -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 From c244deda57c3201ace884c0c00090c0aa85b6972 Mon Sep 17 00:00:00 2001 From: Bhaskar Kataria Date: Thu, 11 Nov 2021 23:29:00 +0530 Subject: [PATCH 2/2] config: Added IPv4 and IPv6 address support Signed-off-by: Bhaskar Kataria --- config_util/config.yaml | 5 +++-- config_util/config_utility.py | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) mode change 100644 => 100755 config_util/config_utility.py diff --git a/config_util/config.yaml b/config_util/config.yaml index 540ed26..5402901 100644 --- a/config_util/config.yaml +++ b/config_util/config.yaml @@ -1,8 +1,9 @@ interfaces: - - ifname: enp2s0 + - ifname: enp6s0 customers: - id: 100 outer_vlan: 1000 inner_vlan: 400 ip_addresses: - - 10.0.2.1/32 \ No newline at end of file + - 10.0.2.1/32 + - 2a00::1/64 \ No newline at end of file diff --git a/config_util/config_utility.py b/config_util/config_utility.py old mode 100644 new mode 100755 index f074a3b..337cf17 --- a/config_util/config_utility.py +++ b/config_util/config_utility.py @@ -1,3 +1,4 @@ +#! /bin/python # SPDX-License-Identifier: GPL-2.0-or-later import yaml @@ -32,9 +33,8 @@ link=base_iface, vlan_id=inner_vlan_tag, ) - except pr2modules.netlink.exceptions.NetlinkError: - pass - + 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}" @@ -51,14 +51,22 @@ 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(outer_vlan_iface)[0], + index=ip.link_lookup(ifname=outer_vlan_iface)[0], address=ip_str, mask=mask, ) -# TODO: Handle ipv6 # TODO: Existing addresses