### :school: TEEP 2024_RT LAB_ORAN DPDK
#### :book: FlexRIC Testing with Free5GC and OAI
:::success
List the essential information of this chapter.
1. Install Free5GC
2. install Open Air Interface (OAI)
3. Install FlexRIC
4. Test E2AP RIC & OAI CU
:::
---
## 1. Install Free5GC
### 1.1 Prerequisites
1. Linux Kernel Version
In order to use the UPF element, you must use the 5.0.0-23-generic or 5.4.x version of the Linux kernel. free5gc uses the gtp5g kernel module, which has been tested and compiled against that kernel versions only. If you installed Ubuntu 20.04, the version should be like 5.4.x. To determine the version of the Linux kernel you are using:
```
$ uname -r
5.4.0-65-generic
```
2. Golang Version
As noted above, free5gc is built and tested with Go 1.18.10
To check the version of Go on your system, from a command prompt:
`go version`
If Go is not installed on your system:
```=
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
```
3. Control-plane Supporting Packages
```=
sudo apt -y update
sudo apt -y install mongodb wget git
sudo systemctl start mongodb
```
4. User-plane Supporting Packages
```=
sudo apt -y update
sudo apt -y install git gcc g++ cmake autoconf libtool pkg-config libmnl-dev libyaml-dev
```
5. Linux Host Network Settings
```=
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o <dn_interface> -j MASQUERADE
sudo iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1400
sudo systemctl stop ufw
sudo systemctl disable ufw # prevents the firewall to wake up after a OS reboot
```
### 1.2 Install Control Plane Elements
1. Clone the free5GC repository
```=
cd ~
git clone --recursive -b v3.3.0 -j `nproc` https://github.com/free5gc/free5gc.git
cd free5gc
```
2. Compile network function services in free5gc
```=
cd ~/free5gc
make
```
### 1.3 Install User Plane Function (UPF)
1. As noted above, the GTP kernel module used by the UPF requires that you use Linux kernel version 5.0.0-23-generic or 5.4.x. To verify your version:
```=
uname -r
```
2. Retrieve the 5G GTP-U kernel module using git and build it
```=
git clone -b v0.8.5 https://github.com/free5gc/gtp5g.git
cd gtp5g
make
sudo make install
```
3. Build the UPF (you may skip this step if you built all network functions above):
```=
cd ~/free5gc
make upf
```
### 1.4 Install WebConsole
1. Before building WebConsole, install nodejs first:
```=
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt update
sudo apt install -y nodejs
corepack enable # setup yarn automatically
```
2. Build WebConsole
```=
cd ~/free5gc
make webconsole
```
## 2. Install Open Air Interface (OAI)
### 2.1 Clone the OAI repository
```=
git clone https://gitlab.eurecom.fr/oai/openairinterface5g oai
cd oai/
git checkout 2023.w48
```
### 2.2 Build OAI with E2 Agent
```=
source oaienv
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:
```=
git submodule init
git submodule update
```
Success build

## 3. Install FlexRIC
i have already make documentation about installation of FlexRIC, you can follow the step using this [link](https://hackmd.io/@RaffieWinata/BJ0bfH2TT)
## 4. Test E2AP RIC & OAI CU
### 4.1 What is E2AP ?
E2 Application Protocol or(E2AP) is a general protocol by which Near RT-RIC and disaggregated E2 Nodes communicate. E2 Nodes are O-DU, O-CU-CP, and O-CU-UP.
For more detail what is E2AP, you can follow this link (Still Working on it) :construction_worker:
### 4.2 Testing Step by Step
#### 4.2.1 Architecture of Testing

**Note:** Because me use different ip segment, you must set IP Forwarding and add route. highly recommended you better use IP with same segment.
#### 4.2.2 Modify OAI File
For config file you can find in this path
```=
sudo nano openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf
```
You won't modify everything, just a few parts that need to be modify such as:
1. Modify PLMN code:

2. Modify AMF and Network Parameters :

You must change the IP, 10.30.1.85 is my Free5GC IP and 10.99.1.221 is my OAI IP. before testing all of this you must add route(IP Forwarding) because Free5GC and OAI is an IP different segment.
3. Add E2
In order to configure E2 agent, please, add the following block in OAI's configuration file:
```=
e2_agent = {
near_ric_ip_addr = "127.0.0.1";
sm_dir = "/usr/local/lib/flexric/"
}
```

#### 4.2.3 Change IP FlexRIC
For change the FlexRIC IP you can use this command:
```=
sudo nano /usr/local/etc/flexric/flexric.conf
```

For this testing i use IP 127.0.0.1 like i explained before in architecture of testing
#### 4.2.4 Execute File
1. Free5GC
```=
./run.sh
```
2. OAI
```=
cd oai/cmake_targets/ran_build/build
sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --rfsim --sa -E
```
3. FlexRIC
```=
./build/examples/ric/nearRT-RIC
```
#### 4.2.5 Result
:::danger
**Error:**
log from OAI : Segmentation fault

:::
:::success
**Troubleshoot**
After i research for troubleshoot the problem. i found solution.
By default, OAI will build the E2 Agent with E2AP v2 and KPM v2. If you want a different version, edit the variable E2AP_VERSION and KPM_VERSION at OAI's CMakeLists.txt file.

**Please note that KPM SM v2.01 is supported only in FlexRIC, but not in OAI.**
Based on this information, we must set the version of E2AP and KPM in OAI and FlexRIC with same version. i choose to swap the version to v3.00
**OAI side:**
```=
sudo nano openairinterface5g/CMakeList.txt
```
and change the version of E2AP and KPM like this

**FlexRIC side**
```=
sudo nano free5gc/CMakeList.txt
```


After you change the script. you must build again the FlexRIC and OAI
**OAI**
```=
./build_oai -I -w SIMU --gNB --nrUE --build-e2 --ninja
```
**FlexRIC**
```=
mkdir build && cd build && cmake .. && make -j8
sudo make install
```
And try to testing again, congratss it will work !
:::
**Log from RIC**

**Log from OAI**

**Testing with Xapp Helloworld**
