Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix path maint down #230

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ggsn_gn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ handle_request(ReqKey,
DAF = proplists:get_bool('Dual Address Bearer Flag', gtp_v1_c:get_common_flags(IEs)),

Context1 = update_context_from_gtp_req(Request, Context0),
ContextPreAuth = gtp_path:bind(Request, false, Context1),
ContextPreAuth = gtp_path:bind(Request, Context1),

gtp_context:terminate_colliding_context(ContextPreAuth),

Expand Down Expand Up @@ -426,7 +426,7 @@ handle_request(ReqKey,
'Session' := Session} = Data0) ->

Context0 = update_context_from_gtp_req(Request, OldContext),
Context = gtp_path:bind(Request, false, Context0),
Context = gtp_path:bind(Request, Context0),
URRActions = update_session_from_gtp_req(IEs, Session, Context),

Data1 = if Context /= OldContext ->
Expand Down Expand Up @@ -490,7 +490,7 @@ handle_response({From, TermCause},
ie = #{?'Cause' := #cause{value = Cause}}} = Response,
_Request, _State,
#{context := Context0} = Data) ->
Context = gtp_path:bind(Response, false, Context0),
Context = gtp_path:bind(Response, Context0),
close_pdp_context(TermCause, Data),
if is_tuple(From) -> gen_statem:reply(From, {ok, Cause});
true -> ok
Expand Down Expand Up @@ -678,7 +678,7 @@ defer_usage_report(URRActions, UsageReport) ->

apply_context_change(NewContext0, OldContext, URRActions,
#{pfcp := PCtx0, pcc := PCC} = Data) ->
NewContext = gtp_path:bind(false, NewContext0),
NewContext = gtp_path:bind(NewContext0),
{PCtx, UsageReport} =
ergw_gsn_lib:modify_sgi_session(PCC, URRActions,
#{}, NewContext, PCtx0),
Expand Down
21 changes: 11 additions & 10 deletions src/ggsn_gn_proxy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ handle_request(ReqKey,
'Session' := Session} = Data) ->

Context1 = update_context_from_gtp_req(Request, Context0#context{state = #context_state{}}),
Context2 = gtp_path:bind(Request, false, Context1),
Context2 = gtp_path:bind(Request, Context1),

gtp_context:terminate_colliding_context(Context2),
gtp_context:remote_context_register_new(Context2),
Expand All @@ -297,7 +297,7 @@ handle_request(ReqKey,
{ok, _} = ergw_aaa_session:invoke(Session, SessionOpts, start, #{async => true}),

ProxyContext0 = init_proxy_context(ProxyGtpPort, Context2, ProxyInfo, ProxyGGSN),
ProxyContext1 = gtp_path:bind(true, ProxyContext0),
ProxyContext1 = gtp_path:bind(ProxyContext0),

ergw_sx_node:wait_connect(SxConnectId),
{Context, ProxyContext, PCtx} =
Expand All @@ -316,7 +316,7 @@ handle_request(ReqKey,
when ?IS_REQUEST_CONTEXT(ReqKey, Request, OldContext) ->

Context0 = update_context_from_gtp_req(Request, OldContext),
Context1 = gtp_path:bind(Request, false, Context0),
Context1 = gtp_path:bind(Request, Context0),

gtp_context:remote_context_update(OldContext, Context1),

Expand All @@ -340,8 +340,8 @@ handle_request(ReqKey,
proxy_context := ProxyContext0} = Data)
when ?IS_REQUEST_CONTEXT(ReqKey, Request, ProxyContext0) ->

Context = gtp_path:bind(false, Context0),
ProxyContext = gtp_path:bind(Request, true, ProxyContext0),
Context = gtp_path:bind(Context0),
ProxyContext = gtp_path:bind(Request, ProxyContext0),

DataNew = Data#{context => Context, proxy_context => ProxyContext},
forward_request(ggsn2sgsn, ReqKey, Request, DataNew, Data),
Expand All @@ -354,8 +354,8 @@ handle_request(ReqKey,
#{context := Context0,
proxy_context := ProxyContext0} = Data)
when ?IS_REQUEST_CONTEXT_OPTIONAL_TEI(ReqKey, Request, Context0) ->
Context = gtp_path:bind(Request, false, Context0),
ProxyContext = gtp_path:bind(true, ProxyContext0),
Context = gtp_path:bind(Request, Context0),
ProxyContext = gtp_path:bind(ProxyContext0),

DataNew = Data#{context => Context, proxy_context => ProxyContext},
forward_request(sgsn2ggsn, ReqKey, Request, DataNew, Data),
Expand Down Expand Up @@ -403,7 +403,7 @@ handle_response(#proxy_request{direction = sgsn2ggsn} = ProxyRequest,
?LOG(debug, "OK Proxy Response ~p", [Response]),

ProxyContext1 = update_context_from_gtp_req(Response, PrevProxyCtx),
ProxyContext = gtp_path:bind(Response, true, ProxyContext1),
ProxyContext = gtp_path:bind(Response, ProxyContext1),
gtp_context:remote_context_register(ProxyContext),

Return =
Expand Down Expand Up @@ -431,7 +431,8 @@ handle_response(#proxy_request{direction = sgsn2ggsn,
pfcp := PCtx0} = Data) ->
?LOG(debug, "OK Proxy Response ~p", [Response]),

ProxyContext = update_context_from_gtp_req(Response, OldProxyContext),
ProxyContext1 = update_context_from_gtp_req(Response, OldProxyContext),
ProxyContext = gtp_path:bind(Response, ProxyContext1),
gtp_context:remote_context_update(OldProxyContext, ProxyContext),

PCtx = ergw_proxy_lib:modify_forward_session(PrevContext, Context,
Expand Down Expand Up @@ -531,7 +532,7 @@ handle_sgsn_change(_, _, ProxyContext) ->

update_path_bind(NewContext0, OldContext)
when NewContext0 /= OldContext ->
NewContext = gtp_path:bind(false, NewContext0),
NewContext = gtp_path:bind(NewContext0),
gtp_path:unbind(OldContext),
NewContext;
update_path_bind(NewContext, _OldContext) ->
Expand Down
97 changes: 67 additions & 30 deletions src/gtp_path.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-export([start_link/4, all/1,
maybe_new_path/3,
handle_request/2, handle_response/4,
bind/2, bind/3, unbind/1, down/2,
bind/1, bind/2, unbind/1, down/2,
get_handler/2, info/1]).

%% Validate environment Variables
Expand Down Expand Up @@ -69,19 +69,19 @@ handle_request(#request{gtp_port = GtpPort, ip = IP} = ReqKey, #gtp{version = Ve
handle_response(Path, Request, Ref, Response) ->
gen_statem:cast(Path, {handle_response, Request, Ref, Response}).

bind(Active, #context{remote_restart_counter = RestartCounter} = Context) ->
bind_path_recovery(RestartCounter, Active, bind_path(Context)).
bind(Context) ->
monitor_path_recovery(bind_path(Context)).

bind(#gtp{ie = #{{recovery, 0} :=
#recovery{restart_counter = RestartCounter}}
} = Request, Active, Context) ->
bind_path_recovery(RestartCounter, Active, bind_path(Request, Context));
} = Request, Context) ->
bind_path_recovery(RestartCounter, bind_path(Request, Context));
bind(#gtp{ie = #{{v2_recovery, 0} :=
#v2_recovery{restart_counter = RestartCounter}}
} = Request, Active, Context) ->
bind_path_recovery(RestartCounter, Active, bind_path(Request, Context));
bind(Request, Active, Context) ->
bind_path_recovery(undefined, Active, bind_path(Request, Context)).
} = Request, Context) ->
bind_path_recovery(RestartCounter, bind_path(Request, Context));
bind(Request, Context) ->
bind_path_recovery(undefined, bind_path(Request, Context)).

unbind(#context{version = Version, control_port = GtpPort,
remote_control_teid = #fq_teid{ip = RemoteIP}}) ->
Expand Down Expand Up @@ -146,8 +146,7 @@ stop(Path) ->

%% Timer value: echo = echo interval when peer is up.

-define(Defaults, [{active, false}, % keep probing path even when idle/down
{t3, 10 * 1000}, % echo retry interval
-define(Defaults, [{t3, 10 * 1000}, % echo retry interval
{n3, 5}, % echo retry count
{echo, 60 * 1000}, % echo ping interval
{idle_timeout, 1800 * 1000}, % time to keep the path entry when idle
Expand All @@ -172,8 +171,6 @@ validate_timeout(_Opt, infinity = Value) ->
validate_timeout(Opt, Value) ->
throw({error, {options, {Opt, Value}}}).

validate_option(active, Value) when is_boolean(Value) ->
Value;
validate_option(t3, Value)
when is_integer(Value) andalso Value > 0 ->
Value;
Expand Down Expand Up @@ -206,7 +203,7 @@ init([#gtp_port{name = PortName} = GtpPort, Version, RemoteIP, Args]) ->
State = #state{peer = #peer{state = up, contexts = 0},
echo = stopped},

Data0 = maps:with([active, t3, n3, echo,
Data0 = maps:with([t3, n3, echo,
idle_timeout, idle_echo,
down_timeout, down_echo], Args),
Data = Data0#{
Expand All @@ -217,7 +214,8 @@ init([#gtp_port{name = PortName} = GtpPort, Version, RemoteIP, Args]) ->
ip => RemoteIP,
reg_key => RegKey,

contexts => #{}
contexts => #{},
monitors => #{}
},

?LOG(debug, "State: ~p Data: ~p", [State, Data]),
Expand Down Expand Up @@ -257,18 +255,27 @@ handle_event({call, From}, all, _State, #{contexts := CtxS} = _Data) ->
Reply = maps:keys(CtxS),
{keep_state_and_data, [{reply, From, Reply}]};

handle_event({call, From}, {bind, Pid, Active}, #state{recovery = RstCnt} = State, Data) ->
register(Pid, Active, State, Data, [{reply, From, {ok, RstCnt}}]);
handle_event({call, From}, {MonOrBind, Pid}, #state{peer = #peer{state = down}}, _Data)
when MonOrBind == monitor; MonOrBind == bind ->
Path = self(),
proc_lib:spawn(fun() -> gtp_context:path_restart(Pid, Path) end),
{keep_state_and_data, [{reply, From, {ok, undefined}}]};

handle_event({call, From}, {monitor, Pid}, #state{recovery = RstCnt} = State, Data) ->
register_monitor(Pid, State, Data, [{reply, From, {ok, RstCnt}}]);

handle_event({call, From}, {bind, Pid}, #state{recovery = RstCnt} = State, Data) ->
register_bind(Pid, State, Data, [{reply, From, {ok, RstCnt}}]);

handle_event({call, From}, {bind, Pid, Active, RstCnt}, State, Data) ->
handle_event({call, From}, {bind, Pid, RstCnt}, State, Data) ->
case update_restart_counter(RstCnt, State, Data) of
initial ->
register(Pid, Active, State#state{recovery = RstCnt}, Data, [{reply, From, ok}]);
register_bind(Pid, State#state{recovery = RstCnt}, Data, [{reply, From, ok}]);
peer_restart ->
%% try again after state change
path_restart(RstCnt, State, Data, [postpone]);
no ->
register(Pid, Active, State, Data, [{reply, From, ok}])
register_bind(Pid, State, Data, [{reply, From, ok}])
end;

handle_event({call, From}, {unbind, Pid}, State, Data) ->
Expand Down Expand Up @@ -386,10 +393,10 @@ enter_state_timeout_action(_State, _Data) ->

enter_state_echo_action(busy, #{echo := EchoInterval}) when is_integer(EchoInterval) ->
{{timeout, echo}, EchoInterval, start_echo};
enter_state_echo_action(idle, #{active := true, idle_echo := EchoInterval})
enter_state_echo_action(idle, #{idle_echo := EchoInterval})
when is_integer(EchoInterval) ->
{{timeout, echo}, EchoInterval, start_echo};
enter_state_echo_action(down, #{active := true, down_echo := EchoInterval})
enter_state_echo_action(down, #{down_echo := EchoInterval})
when is_integer(EchoInterval) ->
{{timeout, echo}, EchoInterval, start_echo};
enter_state_echo_action(_, _) ->
Expand Down Expand Up @@ -478,17 +485,43 @@ update_contexts(State0, #{gtp_port := GtpPort, version := Version, ip := IP} = D
Data = Data0#{contexts => CtxS},
{next_state, State, Data, Actions}.

register(Pid, Active, State, #{contexts := CtxS} = Data0, Actions) ->
?LOG(debug, "~s: register(~p)", [?MODULE, Pid]),
register_monitor(Pid, State, #{contexts := CtxS, monitors := Mons} = Data, Actions)
when is_map_key(Pid, CtxS), is_map_key(Pid, Mons) ->
?LOG(debug, "~s: monitor(~p)", [?MODULE, Pid]),
{next_state, State, Data, Actions};
register_monitor(Pid, State, #{monitors := Mons} = Data, Actions) ->
?LOG(debug, "~s: monitor(~p)", [?MODULE, Pid]),
MRef = erlang:monitor(process, Pid),
Data = maps:update_with(active, fun(V) -> V or Active end, Data0),
{next_state, State, Data#{monitors => maps:put(Pid, MRef, Mons)}, Actions}.

%% register_bind/5
register_bind(Pid, MRef, State, #{contexts := CtxS} = Data, Actions) ->
update_contexts(State, Data, maps:put(Pid, MRef, CtxS), Actions).

%% register_bind/4
register_bind(Pid, State, #{monitors := Mons} = Data, Actions)
when is_map_key(Pid, Mons) ->
?LOG(debug, "~s: register(~p)", [?MODULE, Pid]),
MRef = maps:get(Pid, Mons),
register_bind(Pid, MRef, State, Data#{monitors => maps:remove(Pid, Mons)}, Actions);
register_bind(Pid, State, #{contexts := CtxS} = Data, Actions)
when is_map_key(Pid, CtxS) ->
{next_state, State, Data, Actions};
register_bind(Pid, State, Data, Actions) ->
?LOG(debug, "~s: register(~p)", [?MODULE, Pid]),
MRef = erlang:monitor(process, Pid),
register_bind(Pid, MRef, State, Data, Actions).

unregister(Pid, State, #{contexts := CtxS} = Data, Actions)
when is_map_key(Pid, CtxS) ->
MRef = maps:get(Pid, CtxS),
demonitor(MRef, [flush]),
erlang:demonitor(MRef, [flush]),
update_contexts(State, Data, maps:remove(Pid, CtxS), Actions);
unregister(Pid, State, #{monitors := Mons} = Data, Actions)
when is_map_key(Pid, Mons) ->
MRef = maps:get(Pid, Mons),
erlang:demonitor(MRef, [flush]),
{next_state, State, Data#{monitors => maps:remove(Pid, Mons)}, Actions};
unregister(_Pid, _, _Data, Actions) ->
{keep_state_and_data, Actions}.

Expand All @@ -500,12 +533,16 @@ bind_path(#context{version = Version, control_port = CntlGtpPort,
Path = maybe_new_path(CntlGtpPort, Version, RemoteCntlIP),
Context#context{path = Path}.

bind_path_recovery(RestartCounter, Active, #context{path = Path} = Context)
monitor_path_recovery(#context{path = Path} = Context) ->
{ok, PathRestartCounter} = gen_statem:call(Path, {monitor, self()}),
Context#context{remote_restart_counter = PathRestartCounter}.

bind_path_recovery(RestartCounter, #context{path = Path} = Context)
when is_integer(RestartCounter) ->
ok = gen_statem:call(Path, {bind, self(), Active, RestartCounter}),
ok = gen_statem:call(Path, {bind, self(), RestartCounter}),
Context#context{remote_restart_counter = RestartCounter};
bind_path_recovery(_RestartCounter, Active, #context{path = Path} = Context) ->
{ok, PathRestartCounter} = gen_statem:call(Path, {bind, self(), Active}),
bind_path_recovery(_RestartCounter, #context{path = Path} = Context) ->
{ok, PathRestartCounter} = gen_statem:call(Path, {bind, self()}),
Context#context{remote_restart_counter = PathRestartCounter}.

send_echo_request(State, #{gtp_port := GtpPort, handler := Handler, ip := DstIP,
Expand Down
10 changes: 5 additions & 5 deletions src/pgw_s5s8.erl
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ handle_request(ReqKey,

Context1 = update_context_tunnel_ids(FqCntlTEID, FqDataTEID, Context0),
Context2 = update_context_from_gtp_req(Request, Context1),
ContextPreAuth = gtp_path:bind(Request, false, Context2),
ContextPreAuth = gtp_path:bind(Request, Context2),

gtp_context:terminate_colliding_context(ContextPreAuth),

Expand Down Expand Up @@ -473,7 +473,7 @@ handle_request(ReqKey,

Context0 = update_context_tunnel_ids(FqCntlTEID, FqDataTEID, OldContext),
Context1 = update_context_from_gtp_req(Request, Context0),
Context = gtp_path:bind(Request, false, Context1),
Context = gtp_path:bind(Request, Context1),
URRActions = update_session_from_gtp_req(IEs, Session, Context),

Data1 = if Context /= OldContext ->
Expand Down Expand Up @@ -642,7 +642,7 @@ handle_response(CommandReqKey,
#{context := Context0, pfcp := PCtx,
'Session' := Session} = Data0) ->
gtp_context:request_finished(CommandReqKey),
Context = gtp_path:bind(Response, false, Context0),
Context = gtp_path:bind(Response, Context0),
Data = Data0#{context => Context},

if Cause =:= request_accepted andalso BearerCause =:= request_accepted ->
Expand Down Expand Up @@ -672,7 +672,7 @@ handle_response({From, TermCause},
ie = #{?'Cause' := #v2_cause{v2_cause = RespCause}} = IEs} = Response,
_Request, _State,
#{context := Context0, 'Session' := Session} = Data) ->
Context = gtp_path:bind(Response, false, Context0),
Context = gtp_path:bind(Response, Context0),
process_secondary_rat_usage_data_reports(IEs, Context, Session),
close_pdn_context(TermCause, Data),
if is_tuple(From) -> gen_statem:reply(From, {ok, RespCause});
Expand Down Expand Up @@ -868,7 +868,7 @@ apply_context_change(NewContext0, OldContext, URRActions,
_ ->
#{}
end,
NewContext = gtp_path:bind(false, NewContext0),
NewContext = gtp_path:bind(NewContext0),
{PCtx, UsageReport} =
ergw_gsn_lib:modify_sgi_session(PCC, URRActions,
ModifyOpts, NewContext, PCtx0),
Expand Down
Loading