open5gs: Pre-configure tun device for UPF

open5gs requires the tun device to be pre-created and pre-configured
before using it. A ticket was opened in order to allow upfd itself
configuring it in [1]

[1] https://github.com/open5gs/open5gs/issues/899

Change-Id: I3b1857f09017cacee1c86f41dae1463c11b50490
diff --git a/src/osmo_gsm_tester/obj/epc_open5gs.py b/src/osmo_gsm_tester/obj/epc_open5gs.py
index 6db513b..64546fd 100644
--- a/src/osmo_gsm_tester/obj/epc_open5gs.py
+++ b/src/osmo_gsm_tester/obj/epc_open5gs.py
@@ -61,7 +61,10 @@
         db_host = values['epc']['db_host']
         db_uri = 'mongodb://'+db_host+'/open5gs'
         config.overlay(values, dict(epc=dict(db_uri=db_uri,
+                                             apn_name=self.apn_name(),
+                                             tun_name=self.tun_name(),
                                              tun_addr=self.tun_addr(),
+                                             tun_netmask=self.tun_netmask(),
                                              addr_smf=self.priv_addr_smf(),
                                              addr_upf=self.priv_addr_upf(),
                                              addr_sgwc=self.priv_addr_sgwc(),
@@ -121,6 +124,15 @@
     def priv_addr_sgwu(self):
         return self.gen_priv_addr(4)
 
+    def tun_name(self):
+        return "ogstun"
+
+    def tun_netmask(self):
+        return "24"
+
+    def apn_name(self):
+        return "internet"
+
 ###################
 # PUBLIC (test API included)
 ###################
@@ -170,7 +182,7 @@
                 "default_indicator": True, \
                 "session": [ \
                     { \
-                    "name": "internet", \
+                    "name": self.apn_name(), \
                     "type": 3, "pcc_rule": [], "ambr": {"uplink": {"value": 1, "unit": 0}, "downlink": {"value": 1, "unit": 0}}, \
                     "qos": { "index": 9, "arp": {"priority_level": 8, "pre_emption_capability": 1, "pre_emption_vulnerability": 1} } \
                     } \
diff --git a/src/osmo_gsm_tester/obj/upf_open5gs.py b/src/osmo_gsm_tester/obj/upf_open5gs.py
index 78a520f..344c177 100644
--- a/src/osmo_gsm_tester/obj/upf_open5gs.py
+++ b/src/osmo_gsm_tester/obj/upf_open5gs.py
@@ -34,6 +34,7 @@
     CFGFILE = 'open5gs-upfd.yaml'
     LOGFILE = 'open5gs-upfd.log'
     DIAMETERFILE = 'open5gs-freediameter.conf'
+    O5GS_TUN_SETUP_BIN = 'osmo-gsm-tester_open5gs_tun_setup.sh'
 
     def __init__(self, testenv, o5gs_epc):
         super().__init__(log.C_RUN, 'open5gs-upfd')
@@ -137,6 +138,9 @@
             f.write(r)
 
         if not self._run_node.is_local():
+            self.rem_host.run_remote_sync('tun-setup', ('sudo', Open5gsUPF.O5GS_TUN_SETUP_BIN,
+                                          self.o5gs_epc.tun_name(), self.o5gs_epc.tun_addr(),
+                                          self.o5gs_epc.tun_netmask()))
             self.rem_host.recreate_remote_dir(self.remote_inst)
             self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
             self.rem_host.recreate_remote_dir(remote_run_dir)
diff --git a/src/osmo_gsm_tester/templates/open5gs-smfd.yaml.tmpl b/src/osmo_gsm_tester/templates/open5gs-smfd.yaml.tmpl
index f2bd807..00a0eae 100644
--- a/src/osmo_gsm_tester/templates/open5gs-smfd.yaml.tmpl
+++ b/src/osmo_gsm_tester/templates/open5gs-smfd.yaml.tmpl
@@ -318,7 +318,7 @@
       - addr: ${epc.run_addr}
         port: 2153
     subnet:
-      - addr: ${epc.tun_addr}/16
+      - addr: ${epc.tun_addr}/${epc.tun_netmask}
     dns:
       - 8.8.8.8
       - 8.8.4.4
diff --git a/src/osmo_gsm_tester/templates/open5gs-upfd.yaml.tmpl b/src/osmo_gsm_tester/templates/open5gs-upfd.yaml.tmpl
index 87066d6..757c0ec 100644
--- a/src/osmo_gsm_tester/templates/open5gs-upfd.yaml.tmpl
+++ b/src/osmo_gsm_tester/templates/open5gs-upfd.yaml.tmpl
@@ -110,7 +110,9 @@
       - addr: ${epc.run_addr}
         port: 2154
     subnet:
-      - addr: ${epc.tun_addr}/16
+      - addr: ${epc.tun_addr}/${epc.tun_netmask}
+        dnn: ${epc.apn_name}
+        dev: ${epc.tun_name}
 
 #
 # smf: