# Open5Gs, OAI gNB, and FlexRIC intergration.
>[name=Muhammad Surya Danendra Pabana]
## Table of Contents
>[TOC]
>

We use proxmox for the vms with specifications bellow:
VM1 core network:
- Ubuntu 22.04
- 4 Core
- 4 RAM
- 50 GB storage
VM gNB:
- Ubuntu 22.04
- 4 Core
- 4 RAM
- 50 GB storage
*note:*
- *I already tried using Free5GC as the core network for the OAI gNB, but Free5GC is not compatible with the OAI gNB.*
- ***UERANSIM is not compatible for flexric because it doesn’t have E2 support**.*
## 1. Getting Open5GS
### 1.1 Installing GO
```bash
wget https://dl.google.com/go/go1.18.10.linux-amd64.tar.gz
sudo tar -C /usr/local -zxvf go1.18.10.linux-amd64.tar.gz
mkdir -p ~/go/{bin,pkg,src}
#The following assume that your shell is bash:
echo 'export GOPATH=$HOME/go' >> ~/.bashrc
echo 'export GOROOT=/usr/local/go' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin:$GOROOT/bin' >> ~/.bashrc
echo 'export GO111MODULE=auto' >> ~/.bashrc
source ~/.bashrc
```
### 1.2 Installing MongoDB
```bash
sudo apt -y update
sudo apt -y install mongodb wget git
sudo systemctl start mongodb
```
### 1.3 Installing Open5GS
*Ubuntu* makes it easy to install Open5GS as shown below.
```bash
sudo add-apt-repository ppa:open5gs/latest
sudo apt update
sudo apt install open5gs
```
note: you can see tutorial for other OS [here](https://open5gs.org/open5gs/docs/guide/01-quickstart/).
### 1.4 Installing Open5GS webUI
The WebUI allows you to interactively edit subscriber data. While it is not essential to use this, it makes things easier when you are just starting out on your Open5GS adventure. (A [command line tool](https://github.com/open5gs/open5gs/blob/main/misc/db/open5gs-dbctl) is available for advanced users).
1. Installing Node.js for Ubuntu and Debian:
```bash
# Download and import the Nodesource GPG key
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
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
# Run Update and Install
sudo apt update
sudo apt install nodejs -y
```
2. To install [Node.js](https://nodejs.org/) on *openSUSE*, run the following:
```bash
sudo zypper install nodejs8
```
You can now install WebUI of Open5G
```bash
curl -fsSL https://open5gs.org/open5gs/assets/webui/install | sudo -E bash -
```
## 1.5 Setup 5G Core
Since we are using a private environment, there is no need to modify the PLMN in NRF and AMF.
If you are aiming to connect an external gNB to your core, you will also need to change the NGAP bind address of the AMF **and** the GTPU bind address of the UPF. If you are running an gNB stack locally, you will not need to make these changes.
Modify [/etc/open5gs/amf.yaml](https://github.com/open5gs/open5gs/blob/main/configs/open5gs/amf.yaml.in) to set the NGAP IP address.
```bash
ngap:
server:
- - address: 127.0.0.5
+ - address: 192.168.1.160
metrics:
server:
- address: 127.0.0.5
port: 9090
guami:
```
Modify [/etc/open5gs/upf.yaml](https://github.com/open5gs/open5gs/blob/main/configs/open5gs/upf.yaml.in) to set the GTP-U address.
```bash
gtpu:
server:
- - address: 127.0.0.7
+ - address: 192.168.1.160
session:
- subnet: 10.45.0.1/16
- subnet: 2001:db8:cafe::1/48
```
After changing config files, please restart Open5GS daemons.
```bash
sudo systemctl restart open5gs-nrfd
sudo systemctl restart open5gs-amfd
sudo systemctl restart open5gs-upfd
```
### Register Subscriber information
Connect to `http://localhost:9999` and login with **admin** account.
Username : admin
Password : 1423
**Tip:** You can change the password in *Account* Menu.
To add subscriber information, you can do WebUI operations in the following order:
1. Go to `Subscriber` Menu.
2. Click `+` Button to add a new subscriber.
3. Fill the IMSI.

4. Click `SAVE` Button
### **Adding a route for the UE to have WAN connectivity**
in order to bridge between the PGWU/UPF and WAN (Internet), you must enable IP forwarding and add a NAT rule to your IP Tables.To enable forwarding and add the NAT rule, enter
```bash
### Enable IPv4/IPv6 Forwarding
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1
### Add NAT Rule
sudo iptables -t nat -A POSTROUTING -s 10.45.0.0/16 ! -o ogstun -j MASQUERADE
sudo ip6tables -t nat -A POSTROUTING -s 2001:db8:cafe::/48 ! -o ogstun -j MASQUERADE
```
Configure the firewall correctly. Some operating systems (Ubuntu) by default enable firewall rules to block traffic.
```bash
sudo ufw status
> Status: active
sudo ufw disable
> Firewall stopped and disabled on system startup
sudo ufw status
> Status: inactive
```
### **1.6 Starting and Stopping Open5GS**
When you install the software using the package manager, it is setup to run as a systemd service. You can stop and restart the components and network functions as follows:
```bash
$ sudo systemctl stop open5gs-mmed
$ sudo systemctl stop open5gs-sgwcd
$ sudo systemctl stop open5gs-smfd
$ sudo systemctl stop open5gs-amfd
$ sudo systemctl stop open5gs-sgwud
$ sudo systemctl stop open5gs-upfd
$ sudo systemctl stop open5gs-hssd
$ sudo systemctl stop open5gs-pcrfd
$ sudo systemctl stop open5gs-nrfd
$ sudo systemctl stop open5gs-scpd
$ sudo systemctl stop open5gs-seppd
$ sudo systemctl stop open5gs-ausfd
$ sudo systemctl stop open5gs-udmd
$ sudo systemctl stop open5gs-pcfd
$ sudo systemctl stop open5gs-nssfd
$ sudo systemctl stop open5gs-bsfd
$ sudo systemctl stop open5gs-udrd
$ sudo systemctl stop open5gs-webui
```
```bash
$ sudo systemctl restart open5gs-mmed
$ sudo systemctl restart open5gs-sgwcd
$ sudo systemctl restart open5gs-smfd
$ sudo systemctl restart open5gs-amfd
$ sudo systemctl restart open5gs-sgwud
$ sudo systemctl restart open5gs-upfd
$ sudo systemctl restart open5gs-hssd
$ sudo systemctl restart open5gs-pcrfd
$ sudo systemctl restart open5gs-nrfd
$ sudo systemctl restart open5gs-scpd
$ sudo systemctl restart open5gs-seppd
$ sudo systemctl restart open5gs-ausfd
$ sudo systemctl restart open5gs-udmd
$ sudo systemctl restart open5gs-pcfd
$ sudo systemctl restart open5gs-nssfd
$ sudo systemctl restart open5gs-bsfd
$ sudo systemctl restart open5gs-udrd
$ sudo systemctl restart open5gs-webui
```
## 2. Installing OAI gNB & UE
### 2.1 OAI pre-requisites
- A *recent* CMake (at least v3.15).
```bash
sudo apt-get update
sudo apt-get install ca-certificates gpg wget
test -f /usr/share/doc/kitware-archive-keyring/copyright ||
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
## For Ubuntu Noble Numbat (24.04):
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ noble main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update
## For Ubuntu Jammy Jellyfish (22.04):
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ jammy main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update
## For Ubuntu Focal Fossa (20.04):
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ focal main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
sudo apt-get update
sudo apt-get install kitware-archive-keyring
```
- SWIG (at least v.4.1).
SWIG as an interface generator to enable the multi-language feature (i.e., C/C++ and Python) for the xApps. Please, check your SWIG version (i.e, `swig -version`) and install it from scratch if necessary as described here: [https://swig.org/svn.html](https://swig.org/svn.html) or via the code below:
```bash
git clone https://github.com/swig/swig.git
cd swig
git checkout release-4.1
./autogen.sh
./configure --prefix=/usr/
make -j8make install
```
### 2.2 Download the required dependencies.
```bash
sudo apt install libsctp-dev python3.8 cmake-curses-gui libpcre2-dev python-dev
```
### 2.3 Deployment
```bash
git clone https://gitlab.eurecom.fr/oai/openairinterface5g oai
cd oai/
cd cmake_targets/
./build_oai -I -w SIMU --gNB --nrUE --build-e2 --ninja
```
- `I` option is to install pre-requisites, you only need it the first time you build the softmodem or when some oai dependencies have changed.
- `w` option is to select the radio head support you want to include in your build. Radio head support is provided via a shared library, which is called the "oai device" The build script creates a soft link from `liboai_device.so` to the true device which will be used at run-time (here the USRP one, liboai_usrpdevif.so). The RF simulatorRF simulator is implemented as a specific device replacing RF hardware, it can be specifically built using `w SIMU` option, but is also built during any softmodem build.
- `-gNB` is to build the `nr-softmodem` and `nr-cuup` executables and all required shared libraries
- `-nrUE` is to build the `nr-uesoftmodem` executable and all required shared libraries
- `-ninja` is to use the ninja build tool, which speeds up compilation
- `-build-e2` option is to use the E2 agent, integrated within RAN.
If the openair2/E2AP/flexric folder is empty, try manually the following commands:
```bash
git submodule init
git submodule update
```
><div style="background-color: #5c2b2b; border-left: 4px solid #f87171; padding: 10px; border-radius: 6px; color: #fee2e2;">
>Because I changed the FlexRIC E2AP and KPM version to version 3, I need to do the following:
<div style="background-color: #052e16; border-left: 4px solid #22c55e; padding: 10px; border-radius: 6px; color: #bbf7d0;">
<p><strong style="color: #22c55e;">Solution (This step is for upgrading not for installing, if you want to install it automatically to Version 3 then fo to the step 3 instead):</strong><br>
1.Remove the CMakeCache.txt:</p>
```bash
cd /oai
rm /cmake_targets/ran_build/build/CMakeCache.txt
```
2. Choose the correct version in CmakeLists.txt:
```bash
cd /oai
nano CmakeLists.txt
```

3. Rebuild the OAI:
```bash
./build_oai -I -w SIMU --gNB --nrUE --build-e2 --ninja
```

</div>
### 2.4 Configure OAI gNB and UE
2.4.1 Configure CU
modify oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf. Change the MCC and MNC according to the core network PLMN.

And change the IP address to match the AMF and the IP of the VM.

Add this code to the configuration:
```bash
e2_agent = {
near_ric_ip_addr = "127.0.0.1";
sm_dir = "/usr/local/lib/flexric/"
}
```
2.4.2 Configure DU
modify oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf. Change the MCC and MNC according to the core network PLMN.

Add this code to the configuration:
```bash
e2_agent = {
near_ric_ip_addr = "127.0.0.1";
sm_dir = "/usr/local/lib/flexric/"
}
```
2.4.3 Configure UE
modify oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf. Change the IMSI and key , OPC, dnn, and nssai.

## Installing Flexric

By default, FlexRIC will build the nearRT-RIC with E2AP v2 and KPM v2. If you want a different version, edit the variable E2AP_VERSION and KPM_VERSION at FlexRIC's CMakeLists.txt file. Note that OAI's and FlexRIC's E2AP_VERSION and KPM_VERSION need to match due to O-RAN incompatibilities among versions.
### 3.1 Clone FlexRIC repository
```bash
git clone https://gitlab.eurecom.fr/mosaic5g/flexric flexric
cd flexric/
git checkout f1c08ed2b9b1eceeda7941dd7bf435db0168dd56
```
><div style="background-color: #5c2b2b; border-left: 4px solid #f87171; padding: 10px; border-radius: 6px; color: #fee2e2;">
>I used a specific checkout version, but it was already outdated, which caused many errors.
<div style="background-color: #052e16; border-left: 4px solid #22c55e; padding: 10px; border-radius: 6px; color: #bbf7d0;">
<p><strong style="color: #22c55e;">Solution:</strong><br>
Use the latest commit instead of an outdated one:</p>
```bash
git clone https://gitlab.eurecom.fr/mosaic5g/flexric flexric
cd flexric/
git checkout dev
git pull
```
</div>
### 3.2 Build FlexRIC
```bash
mkdir build && cd build && cmake .. && make -j8
```
I tried using the default version of E2AP V2 and KPM_V2.03, but it caused more errors.
<div style="background-color: #052e16; border-left: 4px solid #22c55e; padding: 10px; border-radius: 6px; color: #bbf7d0;"> <p><strong style="color: #22c55e;">Solution:</strong><br>
so i upgraded it with the following command</p>
```bash
mkdir build && cd build && cmake -DE2AP_VERSION=E2AP_V3_00 -DKPM_VERSION=KPM_V3 .. && make -j8
```

</div>
### 3.3 Installation of Service Model (SMs)
```bash
sudo make install
```
## 4. Demo
### 4.1 Start FlexRIC Near RT RIC
```bash
./flexric/build/examples/ric/nearRT-RIC
```

### 4.2 Start CU, DU, and UE
```bash
sudo ./nr-softmodem -O oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf --rfsim
sudo ./nr-softmodem -O oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf --rfsim -E
cd oai/cmake_targets/ran_build/build
sudo .oai/cmake_targets/ran_build/build/nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --rfsim --rfsimulator.serveraddr 127.0.0.1
```
#### 4.2.1 CU ouput:

Here, it shows that the AMF NG request was successful, as well as the E2 agent setup response, meaning the CU is connected to the RIC.
#### 4.2.2 DU output:

here, it shows the GTP connection successful.

here, it shows that DU is connected to the RIC.
#### 4.2.3 UE output:

the UE is successfully connected to the RU simulation.
#### 4.2.4 RIC output:

#### 4.2.4 UE connectivity test:

```bash
ping -I oaitun_ue1 8.8.8.8
```

#### 4.2.5 xApps
```bash
cd flexric
.build/examples/xApp/c/monitor/xapp_kpm_moni
```

Start different xApps
- start the RC monitor xApp - aperiodic subscription for "UE RRC State Change"
```bash
cd flexric
./build/examples/xApp/c/monitor/xapp_rc_moni
```
- start the RC control xApp - RAN control function "QoS flow mapping configuration" (e.g. creating a new DRB)
```bash
cd flexric
./build/examples/xApp/c/kpm_rc/xapp_kpm_rc
```
- start the (MAC + RLC + PDCP + GTP) monitor xApp
```bash
cd flexric
./build/examples/xApp/c/monitor/xapp_gtp_mac_rlc_pdcp_moni
```
## 5 GTPU Monitoring XApps:
```bash!
cd flexric
./build/examples/xApp/c/monitor/xapp_gtp_mac_rlc_pdcp_moni
```


read the data using sqlitebrowser
```bash!
sudo apt install sqlitebrowser
sqlitebrowser /tmp/xapp_db_1746777138419252
```

### Explanation of GTPU monitoring
GTP-U (GPRS Tunneling Protocol - User Plane) is a protocol used to transport user data (like video, web traffic, etc.) between the gNB and the UPF (User Plane Function) in 5G and LTE networks.
In the RAN (Radio Access Network), monitoring GTP-U helps:
* Track data usage per UE
* Measure uplink/downlink throughput
* Analyze user-plane latency
* Detect performance bottlenecks
FlexRIC uses modular Service Models (SMs).
For GTP-U monitoring:
* The GTP SM (e.g., `gtp_sm.c`) collects user-plane metrics inside the E2 Agent (e.g., gNB/DU)
* The xApp subscribes to these metrics via the E2 interface
* The xApp processes and logs or stores the data

Flow Explanation (in English)
1. Initialization Phase
* The xApp, NEAR-Real-time RIC (NEAR-RIC), and E2 Node (RAN Function) are initialized.
* NEAR-RIC sends an E2 Setup Request to the E2 Node to establish the E2 interface.
* The E2 Node responds with an E2 Setup Response, confirming the interface setup.
* xApp sends an E42 Setup Request to NEAR-RIC (internal FlexRIC-specific step) and receives an E42 Setup Response.
2. Subscription Phase
* The xApp queries the list of connected E2 nodes (e2_nodes_xapp_api()).
* For each node, it checks the RAN function types and sets up monitoring subscriptions for:
* MAC (id 142)
* RLC (id 143)
* PDCP (id 144)
* GTP (id 148)
* The xApp calls report_sm_xapp_api(...) with appropriate callback handlers for each function type.
* This triggers a Subscription Request from the xApp to NEAR-RIC, which forwards it to the E2 Node.
3. Reporting Phase
* After the subscription is active, the E2 Node starts reporting statistics periodically (e.g., every 1_ms) as Indication Messages.
* These messages are handled by specific callback functions like `sm_cb_gtp()`which:
* Calculate latency between message timestamp and current time
* Print the latency for every 1024 messages
4. Unsubscription Phase
* After sleeping for 10 seconds (to allow stats collection), the xApp removes all subscriptions via rm_report_sm_xapp_api(...).
* This triggers a Subscription Delete Request and Response sequence to clean up each subscription.
5. Termination Phase
The xApp then checks for safe shutdown using try_stop_xapp_api() in a loop and terminates after successful de-initialization.
## next ====> multi ue https://hackmd.io/@XyMSKk5ZRU6mrKfDrEH9Vg/ByzQwVjxex