Skip to content

Commit

Permalink
Script to switch edge
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Sep 25, 2024
1 parent 343e134 commit 711bf5e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ ue/ping/%:
@# pings from ue1 to ue2
@TARGET=$(shell docker exec -it ue$(@F)-debug bash -c "ip --brief address show uesimtun0|awk '{print \$$3; exit}'|cut -d"/" -f 1");\
docker exec -it ue$(*D)-debug bash -c "ping $$TARGET"
.PHONY: ue/switch-edge
ue/switch-edge/%:
@# swich edge for ue
@UE_IP=$(shell docker exec -it ue$(@F)-debug bash -c "ip --brief address show uesimtun0|awk '{print \$$3; exit}'|cut -d"/" -f 1");\
scripts/switch.py $(BCONFIG) $$UE_IP
1 change: 1 addition & 0 deletions doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ When you update the git repository, please ensure to update Docker images as wel
## Using UEs
- `make ue/ip/<ue-number>`: show IP Address of the UE within the Mobile Network
- `make ue/ping/<ue-source-number>/<ue-target-number>`: ping from `ue-source` to `ue-target`
- `make ue/switch-edge/<ue-number>`: switch edge enabled for this ue (only for NextMN-SRv6)
3 changes: 1 addition & 2 deletions doc/how-to-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
3. `make u`
4. `make t/ue1` and `ping 10.4.0.1`
5. `make t/s0`/`make t/s1` and `tshark -i any -f icmp`
6. `make ctrl` ; In the web browser, look for uplink `srgw` rule `UUID` for your UE
7. Enable new uplink rule & disable old uplink rule using `./script/switch.sh {UUID_TO_ENABLE} {UUID_TO_DISABLE}` (replace `{UUID_TO_ENABLE}`/`{UUID_TO_DISABLE}` with rule's UUIDs).
6. `make ue/switch-edge/1` to swich between edges
8. When done with the test, `make d`
34 changes: 34 additions & 0 deletions scripts/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
'''Switch UE edge'''
# Copyright 2024 Louis Royer. All rights reserved.
# Use of this source code is governed by a MIT-style license that can be
# found in the LICENSE file.
# SPDX-License-Identifier: MIT
import argparse
import ipaddress
import requests
import yaml

if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='switch',
description='Switch UE edge'
)
parser.add_argument('config')
parser.add_argument('ue_addr')
args = parser.parse_args()
with open(args.config, 'r', encoding='utf-8') as f:
c = yaml.safe_load(f)
srgw = f'http://[{c["subnets"]["control"]["srgw0"]["ipv6_address"]}]:8080'
r = requests.get(f'{srgw}/rules', timeout=1)
rules = [None, None]
for ruleid, rule in r.json().items():
if ipaddress.ip_address(args.ue_addr) in ipaddress.ip_network(
rule['Match']['ue-ip-prefix']):
if not rule['Enabled']:
rules[0] = ruleid
else:
rules[1] = ruleid
if (rules[0] is None) or (rules[1] is None):
raise ValueError('Could not found 1 enabled and 1 disabled rule for this IP Address')
requests.patch(f'{srgw}/rules/switch/{rules[0]}/{rules[1]}', timeout=1)
3 changes: 0 additions & 3 deletions scripts/switch.sh

This file was deleted.

0 comments on commit 711bf5e

Please sign in to comment.