From eac3ee26c10d4b518c88d2d6b2caa85e7cbd8801 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Fri, 17 Nov 2017 16:04:42 +0000 Subject: [PATCH] Starting to add TUN if for SGi interface --- srsepc/hdr/spgw/spgw.h | 4 +++ srsepc/src/spgw/spgw.cc | 58 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/srsepc/hdr/spgw/spgw.h b/srsepc/hdr/spgw/spgw.h index 121daeee2..6d781cf61 100644 --- a/srsepc/hdr/spgw/spgw.h +++ b/srsepc/hdr/spgw/spgw.h @@ -63,9 +63,13 @@ private: virtual ~spgw(); static spgw *m_instance; + srslte::error_t init_sgi_if(); + bool m_running; srslte::byte_buffer_pool *m_pool; + int m_sgi_if; + int m_sgi_sock; /*Logs*/ srslte::log_filter *m_spgw_log; diff --git a/srsepc/src/spgw/spgw.cc b/srsepc/src/spgw/spgw.cc index 3497afadb..016188ec0 100644 --- a/srsepc/src/spgw/spgw.cc +++ b/srsepc/src/spgw/spgw.cc @@ -26,6 +26,12 @@ #include #include +#include +#include +#include +#include +#include +#include #include "spgw/spgw.h" namespace srsepc{ @@ -68,8 +74,11 @@ spgw::cleanup(void) int spgw::init(spgw_args_t* args, srslte::log_filter *spgw_log) { - + //Init log m_spgw_log = spgw_log; + + //Init Si interface + init_sgi_if(); m_spgw_log->info("SP-GW Initialized.\n"); m_spgw_log->console("SP-GW Initialized.\n"); return 0; @@ -99,4 +108,51 @@ spgw::run_thread() return; } +srslte::error_t +spgw::init_sgi_if() +{ + char dev[IFNAMSIZ] = "srs_spgw_sgi"; + struct ifreq ifr; + + // Construct the TUN device + m_sgi_if = open("/dev/net/tun", O_RDWR); + m_spgw_log->info("TUN file descriptor = %d\n", m_sgi_if); + if(m_sgi_if < 0) + { + m_spgw_log->debug("Failed to open TUN device: %s\n", strerror(errno)); + return(srslte::ERROR_CANT_START); + } + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TUN | IFF_NO_PI; + strncpy(ifr.ifr_ifrn.ifrn_name, dev, IFNAMSIZ); + if(ioctl(m_sgi_if, TUNSETIFF, &ifr) < 0) + { + m_spgw_log->debug("Failed to set TUN device name: %s\n", strerror(errno)); + close(m_sgi_if); + return(srslte::ERROR_CANT_START); + } + + // Bring up the interface + m_sgi_sock = socket(AF_INET, SOCK_DGRAM, 0); + if(ioctl(m_sgi_sock, SIOCGIFFLAGS, &ifr) < 0) + { + m_spgw_log->debug("Failed to bring up socket: %s\n", strerror(errno)); + close(m_sgi_if); + return(srslte::ERROR_CANT_START); + } + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; + if(ioctl(m_sgi_sock, SIOCSIFFLAGS, &ifr) < 0) + { + m_spgw_log->debug("Failed to set socket flags: %s\n", strerror(errno)); + close(m_sgi_if); + return(srslte::ERROR_CANT_START); + } + + //if_up = true; + + return(srslte::ERROR_NONE); + +} + } //namespace srsepc