osmith has submitted this change. ( https://gerrit.osmocom.org/c/osmo-s1gw/+/37042?usp=email )
Change subject: Add a global supervisor and env parameters ...................................................................... Add a global supervisor and env parameters Change-Id: I2da6bccf728bdbe615aaf853b33077ceb4481176 --- M src/osmo_s1gw_app.erl A src/osmo_s1gw_sup.erl M src/sctp_proxy.erl M src/sctp_server.erl 4 files changed, 78 insertions(+), 19 deletions(-) Approvals: osmith: Looks good to me, approved; Verified diff --git a/src/osmo_s1gw_app.erl b/src/osmo_s1gw_app.erl index 596b3e8..7e1bc56 100644 --- a/src/osmo_s1gw_app.erl +++ b/src/osmo_s1gw_app.erl @@ -4,8 +4,9 @@ -export([start/2, stop/1]). start(_StartType, _StartArgs) -> - sctp_server:start_link(). - %% TODO: osmo_s1gw_sup:start_link(). + osmo_s1gw_sup:start_link(). stop(_State) -> - ok. + ok. + +%% vim:set ts=4 sw=4 et: diff --git a/src/osmo_s1gw_sup.erl b/src/osmo_s1gw_sup.erl new file mode 100644 index 0000000..a62f266 --- /dev/null +++ b/src/osmo_s1gw_sup.erl @@ -0,0 +1,46 @@ +-module(osmo_s1gw_sup). +-behaviour(supervisor). + +-export([init/1, + start_link/0]). + +-include("s1ap.hrl"). + +-define(SERVER, ?MODULE). +-define(ENV_APP_NAME, osmo_s1gw). +-define(ENV_DEFAULT_S1GW_BIND_ADDR, "0.0.0.0"). +-define(ENV_DEFAULT_S1GW_BIND_PORT, ?S1AP_PORT). +-define(ENV_DEFAULT_MME_ADDR, "127.0.0.1"). +-define(ENV_DEFAULT_MME_PORT, ?S1AP_PORT). + +%% ------------------------------------------------------------------ +%% supervisor API +%% ------------------------------------------------------------------ + +start_link() -> + supervisor:start_link({local, ?SERVER}, ?MODULE, []). + + +init([]) -> + S1GWBindAddr = get_env(s1gw_bind_addr, ?ENV_DEFAULT_S1GW_BIND_ADDR), + S1GWBindPort = get_env(s1gw_bind_port, ?ENV_DEFAULT_S1GW_BIND_PORT), + MmeAddr = get_env(mme_addr, ?ENV_DEFAULT_MME_ADDR), + MmePort = get_env(mme_port, ?ENV_DEFAULT_MME_PORT), + SctpServer = {sctp_server, {sctp_server, start_link, + [S1GWBindAddr, S1GWBindPort, + {MmeAddr, MmePort}]}, + permanent, + 5000, + worker, + [sctp_server]}, + {ok, {{one_for_all, 5, 10}, [SctpServer]}}. + + +%% ------------------------------------------------------------------ +%% private API +%% ------------------------------------------------------------------ + +get_env(Param, Default) -> + application:get_env(?ENV_APP_NAME, Param, Default). + +%% vim:set ts=4 sw=4 et: diff --git a/src/sctp_proxy.erl b/src/sctp_proxy.erl index 2cc6165..43cb081 100644 --- a/src/sctp_proxy.erl +++ b/src/sctp_proxy.erl @@ -34,6 +34,10 @@ %% gen_statem API %% ------------------------------------------------------------------ +init([Aid, MmeAddrStr, MmePort]) when is_list(MmeAddrStr) -> + {ok, MmeAddr} = inet:parse_address(MmeAddrStr), + init([Aid, MmeAddr, MmePort]); + init([Aid, MmeAddr, MmePort]) -> {ok, connecting, #{enb_aid => Aid, diff --git a/src/sctp_server.erl b/src/sctp_server.erl index 48583c5..249cd8f 100644 --- a/src/sctp_server.erl +++ b/src/sctp_server.erl @@ -6,28 +6,25 @@ handle_call/3, handle_cast/2, terminate/2]). --export([start_link/0, - start_link/2]). --export([send_data/2]). +-export([start_link/3, + send_data/2]). -include_lib("kernel/include/inet.hrl"). -include_lib("kernel/include/inet_sctp.hrl"). -include("s1ap.hrl"). --record(server_state, {sock, clients}). +-record(server_state, {sock, clients, mme_addr_port}). -record(client_state, {addr_port, pid}). %% ------------------------------------------------------------------ %% public API %% ------------------------------------------------------------------ -start_link() -> - start_link("127.0.0.1", ?S1AP_PORT). - -start_link(BindAddr, BindPort) -> +start_link(BindAddr, BindPort, MmeAddrPort) -> gen_server:start_link({local, ?MODULE}, ?MODULE, - [BindAddr, BindPort], []). + [BindAddr, BindPort, MmeAddrPort], + []). send_data(Aid, Data) -> @@ -38,11 +35,11 @@ %% gen_server API %% ------------------------------------------------------------------ -init([BindAddrStr, BindPort]) when is_list(BindAddrStr) -> +init([BindAddrStr, BindPort, MmeAddrPort]) when is_list(BindAddrStr) -> {ok, BindAddr} = inet:parse_address(BindAddrStr), - init([BindAddr, BindPort]); + init([BindAddr, BindPort, MmeAddrPort]); -init([BindAddr, BindPort]) -> +init([BindAddr, BindPort, MmeAddrPort]) -> process_flag(trap_exit, true), {ok, Sock} = gen_sctp:open([{ip, BindAddr}, {port, BindPort}, @@ -52,7 +49,8 @@ logger:info("SCTP server listening on ~w:~w", [BindAddr, BindPort]), ok = gen_sctp:listen(Sock, true), {ok, #server_state{sock = Sock, - clients = dict:new()}}. + clients = dict:new(), + mme_addr_port = MmeAddrPort}}. handle_call(Info, _From, State) -> @@ -116,7 +114,8 @@ case ConnState of comm_up -> logger:notice("Connection (id=~p, ~p:~p) established", [Aid, FromAddr, FromPort]), - Clients = client_add(State#server_state.clients, Aid, FromAddr, FromPort); + Clients = client_add(State#server_state.clients, Aid, FromAddr, FromPort, + State#server_state.mme_addr_port); shutdown_comp -> logger:notice("Connection (id=~p, ~p:~p) closed", [Aid, FromAddr, FromPort]), Clients = client_del(State#server_state.clients, Aid); @@ -151,8 +150,8 @@ %% Add a new client to the list, spawning a proxy process -client_add(Clients, Aid, FromAddr, FromPort) -> - {ok, Pid} = sctp_proxy:start_link(Aid, {127,0,1,1}, ?S1AP_PORT), %% XXX! +client_add(Clients, Aid, FromAddr, FromPort, {MmeAddr, MmePort}) -> + {ok, Pid} = sctp_proxy:start_link(Aid, MmeAddr, MmePort), NewClient = #client_state{addr_port = {FromAddr, FromPort}, pid = Pid}, dict:store(Aid, NewClient, Clients). -- To view, visit https://gerrit.osmocom.org/c/osmo-s1gw/+/37042?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmo-s1gw Gerrit-Branch: master Gerrit-Change-Id: I2da6bccf728bdbe615aaf853b33077ceb4481176 Gerrit-Change-Number: 37042 Gerrit-PatchSet: 1 Gerrit-Owner: osmith <osm...@sysmocom.de> Gerrit-Reviewer: osmith <osm...@sysmocom.de> Gerrit-CC: fixeria <vyanits...@sysmocom.de> Gerrit-MessageType: merged