# Lab4 srsRAN gNB with COTS UEs
## Outline
- [1. Lab Content](#1.-Lab-Content)
- [2. Background Knowledge](#2.-Background-Knowledge)
- [3. Environment Setup / Equipment Specifications](#3.-Environment-Setup-/-Equipment-Specifications)
- [4. Lab Architecture](#4.-Lab-Architecture)
- [5. Lab Procedure](#5.-Lab-Procedure)
-----
## 1. Lab Content
- Set up a gNB using srsRAN.
- Set up the Open5GS Core Network using a physical host.
- Connect commercial UEs (User Equipment) to the self-hosted gNB.
----------
## 2. Background Knowledge
- srsRAN: srsRAN is an open-source 4G/5G Radio Access Network (RAN) software suite developed by SRS (Software Radio Systems). Its goal is to allow researchers, telecom engineers, and developers to quickly build complete mobile network components like eNB, gNB, and UE on general-purpose computers using SDR (Software Defined Radio), without the need for expensive carrier-grade equipment. Key features include:
- Fully compliant with 3GPP standards, supporting LTE and 5G NR L1/L2/L3 protocol stacks.
- 5G architecture adopts CU/DU split, supports NG-RAN and F1 interfaces, facilitating O-RAN research.
- Fully compatible with SDRs like USRP, BladeRF, and LimeSDR for direct wireless signal transmission and reception.
- UHD: The USRP Hardware Driver is the official universal driver and API provided by Ettus Research / NI for the USRP series of SDR devices. Any software that wants to control a USRP, such as GNU Radio, srsRAN, MATLAB, or Python scripts, must operate through UHD.
- USIM: UMTS Subscriber Identity Module. The USIM is a user identity module used in UMTS. It can store user data, phone numbers, authentication data, and provide storage space for SMS.
## 3. Environment Setup / Equipment Specifications
### 3-1 Hardware
| Item Name | Specifications | Quantity |
|:----------- | ------------------------ |:----:|
| PC | Any host capable of installing Ubuntu | 1 |
| SDR | USRP B210 | 1 |
| UE | Google Pixel 8 | 1 |
| USIM | Sysmocom SJA5-S17 | 1 |
| Card Reader | HID OMNIKEY | 1 |
### 3-2
| USIM ID | Student |
|:--------------- | ---- |
| 999700000123080 | 蘇翰庭、曹莛宥、Farhan |
| 999700000123081 | 李明銳 |
| 999700000123082 | 王祈皓 |
| 999700000123083 | 吳昕叡 |
| 999700000123084 | 謝東諺 |
| 999700000123085 | 周承誌、吳亞倫 |
| 999700000123086 | 蘇洋銘 |
| 999700000123087 | 吳晉毅 |
| 999700000123088 | 郭峻里 |
| 999700000123089 | 蘇仕偉 |
### 3-3 Software
| Software Name | Version |
|:-------- |:-------------- |
| Ubuntu | 22.04 LTS |
| Open5gs | v2.7.1-jammy |
| srsRAN | srsRAN Project |
| Android | 16 |
-----
### 3-4 [Ubuntu Installation](https://github.com/TTU-CWBT/Ubuntu-install-tutorial)
:warning: This lab requires a physical Ubuntu host installation. Please prepare a USB drive for burning an Ubuntu image onto it. Any content in the USB drive will be formatted, so please check what's inside before use.
:warning: This lab requires Ubuntu Desktop installed. After installing Ubuntu, please enter the command to install the desktop environment in the terminal. This command may take some time, please wait.
``` shell=1
sudo apt install ubuntu-desktop
```
# 4. Lab Architecture

# 5. Lab Procedure
## Questions & Discussion
1. Screenshot the output of `lsusb` to confirm the USRP B210 is there.
2. Screenshot the output of `uhd_usrp_probe` to confirm the B210 is functioning correctly.
3. Screenshot the screen after modifying the gNB configuration.
4. Explain the purpose of modifying the AMF addr & tac and the cell_cfg tac.
5. Screenshot to verify the successful installation of Open5GS.
6. Screenshot subscriber registration in the WebUI
7. Screenshot the screen showing the gNB starting up normally.
8. What antenna configuration does this gNB use? What frequency band? What are the UL/DL frequencies? Provide a screenshot with labels as proof.
9. What are your USIM IMSI and ICCID?
10. Explain the difference between IMSI and ICCID.
11. Screenshot the success output for `./pySim-read.py -p0`.
12. Screenshot the phone information screen in engineering mode.
13. Perform a self-defined network functional test and document it with photos.
14. Measure the maximum DL/UL throughput of the UE.
15. Measure the distance from the UE to the gNB at three different distances and observe the effect of distance on bitrate. (Please document with photos).
## 5-0. Before we start
1. Run the following commands to update Ubuntu to the latest state:
``` shell=1
sudo apt update
sudo apt upgrade
```
2. Install the necessary dependencies. Enter the following command in the terminal. When asked (y/n), press `y`.
``` shell=1
sudo apt-get install cmake make gcc g++ pkg-config libfftw3-dev libmbedtls-dev libsctp-dev libyaml-cpp-dev libgtest-dev
```
3. Plug the USRP B210 into the computer and confirm it is plugged into a USB 3.0 port.
## 5-1. Install UHD
- Execute the following commands to install UHD:
``` shell=1
sudo add-apt-repository ppa:ettusresearch/uhd
sudo apt-get update
sudo apt-get install libuhd-dev uhd-host
sudo /usr/lib/uhd/utils/uhd_images_downloader.py
```
> :warning:On Ubuntu 22.04, the uhd_images_downloader.py is located in the /usr/lib/uhd/utils folder, but on other Ubuntu versions it may be located in the /usr/libexec/uhd/utils folde.
- Check List
1. Use the `lsusb` command to ensure the USRP B210 device is recognized.

2. Use the `uhd_usrp_probe` command to test if the B210 is plugged into a USB 3.0 port and operating normally.

----
### 5-1. Questions & Discussion
1. Screenshot the output of `lsusb` to confirm the USRP B210 is there.
2. Screenshot the output of `uhd_usrp_probe` to confirm the B210 is functioning correctly.
----
## 5-2. Install srsRAN-Project
- Clone srsRAN source code:
``` shell=1
sudo apt install git
git clone https://github.com/srsran/srsRAN_Project.git
```
- Check CMakeLists.txt:
``` shell=1
cd srsRAN_Project
vim CMakeLists.txt
```
Ensure UHD is enabled. If you also need to use ZEROMQ, you can set it to ON as well.

> vim command tips: Press i to enter edit mode.
> Press Esc after finishing edits.
> Then type `:wq` (Enter) to save and exit.
- Build Makefile:
``` shell=1
mkdir build
cd build
cmake ..
```

- Compile srsRAN_Project:
``` shell=1
make -j $(nproc)
```

- Install srsRAN_Project:
``` shell=1
sudo make install
```

- Modify gNB configuration file:
Copy gnb_rf_b200_tdd_n78_20mhz.yml to the home directory for easier execution later.
``` shell=1
cp ~/srsRAN_Project/configs/gnb_rf_b200_tdd_n78_20mhz.yml ~/
```
- Edit gnb_rf_b200_tdd_n78_20mhz.yml:
``` shell=1
cd ~
vim gnb_rf_b200_tdd_n78_20mhz.yml
```
The config that needs to be modified includes the amf addr & tac, and the cell_cfg tac.

---
### 5-2. Questions & Discussion
1. Screenshot the screen after modifying the gNB configuration.
2. Explain the purpose of modifying the AMF addr & tac and the cell_cfg tac.
---
## 5-3. Install Open5GS
- Import public key: Enter the following commands in the terminal.
``` shell=1
sudo apt update
sudo apt install gnupg
sudo apt install curlcurl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
```
- Create deb repository:
``` shell=1
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
```
- Install MongoDB package:
``` shell=1
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
```
- Check List
Check MongoDB version; if it appears, the installation is basically successful.
``` shell=1
mongosh --version
```

- Install Open5G Project:
``` shell=1
sudo add-apt-repository ppa:open5gs/latest
sudo apt update
sudo apt install open5gs
```
### Verify Open5GS Installation Success
If Open5GS is installed successfully, it will run automatically in the background every time you reboot. Enter `ps aux | grep open5gs` to confirm if the Open5GS core functions are working properly. If successful, the screen will look like the following (please confirm that these functions are normal; do not proceed if they are not).

> Core functions include:
> AMF, SMF, UPF, NRF, UDM, UDR, PCF,AUSF, NSSF
After confirming that the core functions are enabled, you can start configuring the core network's AMF and NRF functions.
- Configure Core Network AMF:
Enter `sudo vim /etc/open5gs/amf.yaml` to enter the AMF configuration file. Modify the AMF config as the image.

After completion, enter `sudo systemctl restart open5gs-amfd.service`to restart the AMF.
- Configure Core Network NRF:
Enter `sudo vim /etc/open5gs/nrf.yaml` to enter the NRF configuration file. Modify the NRF config as the image.

After completion, enter `sudo systemctl restart open5gs-nrfd.service`
---
### 5-3. Questions & Discussion
1. Screenshot to verify the successful installation of Open5GS.
---
## 5-4. Install WebUI
- Install Nodesource GPG key:
``` shell=1
sudo apt update
sudo apt install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
```
- Create deb repository:
``` shell=1
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
```
- Install WebUI:
``` shell=1
sudo apt update
sudo apt install nodejs -y
curl -fsSL https://open5gs.org/open5gs/assets/webui/install | sudo -E bash -
```
- Login to WebUI:
Enter `http://127.0.0.1:9999` in the FireFox address bar.
>Default Account: admin
Default Password: 1423

After entering, click `+` to add a new subscriber.

- Register subscriber:
>IMSI = 001010123456789
K = 465B5CE8B199B49FAA5F0A2EE238A6BC
OPc = E8ED289DEBA952E4283B54E88E6183CA
APN = internet
Type = IPv4


Click SAVE after completion.
- Configure Network Rules:
:warning: These two commands must be re-entered every time you reboot.
>Enable IPv4 Forwarding
``` shell=1
$sudo sysctl -w net.ipv4.ip_forward=1
```
>Add NAT Rule
``` shell=1
sudo iptables -t nat -A POSTROUTING -s 10.45.0.0/16 ! -o ogstun -j MASQUERADE
```
### Start gNB
``` shell=1
cd ~
sudo gnb -c gnb_rf_b200_tdd_n78_20mhz.yml
```

---
### 5-4. Questions & Discussion
1. Screenshot subscriber registration in the WebUI
2. Screenshot the screen showing the gNB starting up normally.
3. What antenna configuration does this gNB use? What frequency band? What are the UL/DL frequencies? Provide a screenshot with labels as proof.
---
## 5-5. Program USIM
### USIM User Table
| IMSI | ICCID | ACC | PIN1 | PUK1 | PIN2 | PUK2 | Ki | OPC | ADM1 |
|-------------------|-----------------------|------|------|----------|------|----------|----------------------------------------|-------------------------------------------|----------|
| 999700000123080 | 89494400000001230802 | 0001 | 0000 | 08703177 | 0000 | 45260138 | 8C9961848DAA585D050FAB591F4276E3 | D7F0CEC46CCB75C37A759CDD98606D67 | 21864569 |
| 999700000123081 | 89494400000001230810 | 0002 | 0000 | 95973474 | 0000 | 08950111 | 61358AB9C491FFFB59AE50D2EB9A6C2C | 904CFF670336D92F634EA2ECBC7C00C3 | 64731141 |
| 999700000123082 | 89494400000001230828 | 0004 | 0000 | 86355500 | 0000 | 56026461 | 23E90A58C1812FBCBCF825E0F17848A8 | D261CF29BEC37F9688B3EF25D7536992 | 45438380 |
| 999700000123083 | 89494400000001230836 | 0008 | 0000 | 89728335 | 0000 | 46256102 | EF753BA4140CD93F885E3A09FBAC7293 | 50626393F8BCA1FF1B11CF9A7868C50B | 81820960 |
| 999700000123084 | 89494400000001230844 | 0010 | 0000 | 13098206 | 0000 | 20635957 | 3306B7A38F2261AB15653DCC14AED52E | 8109AA55B350F3164F8421E13BEA0390 | 04279297 |
| 999700000123085 | 89494400000001230851 | 0020 | 0000 | 93314241 | 0000 | 11509590 | 52055049E55A843C37B2EC4F28AC539E | A5E7EB5A30AC6469A0D5794A78F4921C | 44440693 |
| 999700000123086 | 89494400000001230869 | 0040 | 0000 | 70631814 | 0000 | 20238703 | CEA2AFF2E4B4606CF3B4A9F726875EC7 | D94B0AC1A503EF22C8C1B022F8A4166C | 94255361 |
| 999700000123087 | 89494400000001230877 | 0080 | 0000 | 18908969 | 0000 | 22132097 | 92398FE1F2F3B6BDA41F38C66FA70A5 | 7896A00BD749FDC9D5E952B20115A82B | 36137799 |
| 999700000123088 | 89494400000001230885 | 0100 | 0000 | 19476117 | 0000 | 37149093 | 9522DA9224F0CC3277FCDFEA12074A9F | EBC6A13393A08DBBCD20C4B0051CEE40 | 52599472 |
| 999700000123089 | 89494400000001230893 | 0200 | 0000 | 64917072 | 0000 | 56246574 | 792A857D82996D7A1BEFEBB07DFC80D | 50EC29E8983BFE58FC8849A7077CCA6D | 67514285 |
- Install pySim
``` shell=1
git clone https://github.com/osmocom/pysim
cd pysim
sudo apt-get install --no-install-recommends pcscd libpcsclite-dev python3 python3-setuptools python3-pyscard python3-pip
pip3 install -r requirements.txt
```
- Program data to USIM
This part requires paying attention to detail. You will need to check the corresponding table based on the USIM number you received.
```shell=
./pySim-prog.py –p0 –s [ICCID] --mcc=[MCC] --mnc=[MNC] -a [ADM-KEY] --imsi=[IMSI] -k [KI] --opc [OPC]
```
In this part, only [**ICCID**] and [**ADM-KEY**] need to be changed; copy and paste the rest!
``` shell=1
mcc=001
mnc=01
imsi=001010123456789
key= 465B5CE8B199B49FAA5F0A2EE238A6BC
opc=E8ED289DEBA952E4283B54E88E6183CA
```
Below is an example for reference:
:warning:Please be careful with the difference between『`-`』and『`--`』

>`./pySim-prog.py -p0 -s 8949440000001230802 --mcc=001 --mnc=01 -a 21864569 --imsi=001010123456789 -k 465B5CE8B199B49FAA5F0A2EE238A6BC --opc=E8ED289DEBA952E4283B54E88E6183CA`

- SUCI Configuration
Enter the pySim shell via the following command, and enter the desired configuration through subsequent commands.
``` shell=1
./pySim-shell.py -p0
```

In the pySim-Shell, enter the following commands to complete the setup:
``` shell=1
pySIM-shell (MF)> select MF
pySIM-shell (MF)> select ADF.USIM
pySIM-shell (MF/ADF.USIM)> select EF.UST
pySIM-shell (MF)> verify_adm <ADM-KEY>
pySIM-shell (MF/ADF.USIM/EF.UST)> ust_service_deactivate 124
pySIM-shell (MF/ADF.USIM/EF.UST)> ust_service_deactivate 125
pySIM-shell (MF/ADF.USIM/EF.UST)> qui
```

- Check Point
Check if the USIM programming result meets expectations via the `./pySim-read.py -p0` command.

---
### 5-5. Questions & Discussion
1. What are your USIM IMSI and ICCID?
2. Explain the difference between IMSI and ICCID.
3. Screenshot the success output for `./pySim-read.py -p0`.
---
## 5-6. Setup COTS UE
- UE Password:0000
- Configure COTS UE (Google Pixel 8)
Insert the USIM into the phone and edit the access point. The settings here must match the settings on the WebUI!
>Settings > Network & internet > Internet (Mobile Data Settings)

>Scroll down to『Access Point Names』> Tap『+』in the top right to add > Set Name, APN, APN Protocol, APN Roaming Protocol, and Toggle APN Enable/Disable.


### Connect to gNB
- After configuration is complete, simply toggle Airplane mode to connect to the gNB. The first connection might take a while, please wait patiently. Once connected, you will see "Open5GS-Magic" displayed in the status bar.

:warning: If everything is set up successfully or if there is no internet access after connecting, simply toggle Airplane mode on and off a few more times :)
- View Phone System Information:
Enter『`*#*#4636#*#*`』in the phone dialer to enter engineering mode and view phone status.

---
### 5-6. Questions & Discussion
1. Screenshot the phone information screen in engineering mode.
---
## 5-7. UE Testing
- iperf3 test
Download the iperf3 app on the phone to use iperf3 functions.

Coordinate with the terminal command`ipref3 -s`

- Arbitrary network function test:

---
### 5-7. Questions & Discussion
1. Perform a self-defined network functional test and document it with photos.
2. Measure the maximum DL/UL throughput of the UE.
3. Measure the distance from the UE to the gNB at three different distances and observe the effect of distance on bitrate. (Please document with photos).
---