Skip to content

Commit

Permalink
Merge pull request #291 from qzhuyan/dev/william/lb-server-fixid
Browse files Browse the repository at this point in the history
feat(lb): user netdev ip as lb server fixed id
  • Loading branch information
qzhuyan authored Sep 19, 2024
2 parents 54ceaaa + 5e68c33 commit 8ad53d0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
28 changes: 24 additions & 4 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,11 +1186,31 @@ openLib(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
env, argv[0], ATOM_QUIC_PARAM_GLOBAL_LOAD_BALACING_MODE, &eterm)
&& enif_get_uint(env, eterm, &lb_mode))
{
MsQuic->SetParam(NULL,
QUIC_PARAM_GLOBAL_LOAD_BALACING_MODE,
sizeof(uint16_t),
(uint16_t *)&lb_mode);

if (lb_mode > QUIC_LOAD_BALANCING_COUNT)
{
// quicer specific load balancing settings
// use FixedServerID
QUIC_GLOBAL_SETTINGS global_settings;
global_settings.IsSet.LoadBalancingMode = TRUE;
global_settings.IsSet.FixedServerID = TRUE;
global_settings.LoadBalancingMode
= QUIC_LOAD_BALANCING_SERVER_ID_FIXED;
global_settings.FixedServerID = lb_mode;
status = MsQuic->SetParam(NULL,
QUIC_PARAM_GLOBAL_GLOBAL_SETTINGS,
sizeof(global_settings),
&global_settings);
}
else
{
status = MsQuic->SetParam(NULL,
QUIC_PARAM_GLOBAL_LOAD_BALACING_MODE,
sizeof(uint16_t),
(uint16_t *)&lb_mode);
}
}
assert(QUIC_SUCCEEDED(status));

if (enif_get_map_value(env, argv[0], ATOM_TRACE, &eterm)
&& enif_get_string(env, eterm, lttngPath, PATH_MAX, ERL_NIF_LATIN1))
Expand Down
6 changes: 6 additions & 0 deletions include/quicer.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@
bin :: binary()
}).

-define(QUIC_LOAD_BALANCING_DISABLED, 0).
-define(QUIC_LOAD_BALANCING_SERVER_ID_IP, 1).
-define(QUIC_LOAD_BALANCING_SERVER_ID_FIXED, 2).
-define(QUIC_LOAD_BALANCING_COUNT, 3).
-define(QUICER_LOAD_BALANCING_IFIP_AS_SERVER_ID, 100). %% User Network Interface IP as Server ID

-endif. %% QUICER_HRL
8 changes: 4 additions & 4 deletions src/quicer_local_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

-include("quicer_types.hrl").

-type local_stream_opts() :: stream_opts() | proplists:proplist().
-type cb_ret() :: quicer_stream:cb_ret().
-type cb_state() :: quicer_stream:cb_state().

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local Stream Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -79,10 +83,6 @@
handle_continue/2
]).

-type local_stream_opts() :: stream_opts() | proplists:proplist().
-type cb_ret() :: quicer_stream:cb_ret().
-type cb_state() :: quicer_stream:cb_state().

-spec start_link(module(), connection_handle(), local_stream_opts()) -> gen_server:start_ret().
start_link(CallbackModule, Connection, Opts) ->
start_link(CallbackModule, Connection, Opts, []).
Expand Down
26 changes: 25 additions & 1 deletion src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,15 @@ open_lib() ->
{error, _} ->
priv_dir()
end,
LBMode =
case application:get_env(quicer, lb_mode, 0) of
X when is_integer(X) ->
X;
DevName when is_list(DevName) ->
lb_server_id(ipv4, DevName)
end,
open_lib(#{
load_balacing_mode => application:get_env(quicer, lb_mode, 0),
load_balacing_mode => LBMode,
trace => LibFile
}).

Expand Down Expand Up @@ -424,3 +431,20 @@ priv_dir() ->
Dir ->
Dir
end.

%% @doc Get the load balancing server id from the given device name. ipv4 only.
-spec lb_server_id(ipv4, string()) -> non_neg_integer().
lb_server_id(ipv4, DevName) ->
try
{ok, IfList} = inet:getifaddrs(),
%% @NOTE Be aware of the order of the bytes in the address
lists:foldr(
fun(I, V) -> V bsl 8 bor I end,
0,
tuple_to_list(proplists:get_value(addr, proplists:get_value(DevName, IfList)))
)
catch
_:E ->
logger:error("Failed to set lb mode from ~s, fallback to disabled: ~p", [DevName, E]),
?QUIC_LOAD_BALANCING_DISABLED
end.
8 changes: 4 additions & 4 deletions src/quicer_remote_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
start_link/6
]).

-type remote_stream_opts() :: stream_opts() | proplists:proplist().
-type cb_ret() :: quicer_stream:cb_ret().
-type cb_state() :: quicer_stream:cb_state().

-callback init_handoff(stream_handle(), stream_opts(), connection_handle(), new_stream_props()) ->
cb_ret().
%% Prepare callback state before ownership handoff
Expand Down Expand Up @@ -82,10 +86,6 @@
handle_continue/2
]).

-type remote_stream_opts() :: stream_opts() | proplists:proplist().
-type cb_ret() :: quicer_stream:cb_ret().
-type cb_state() :: quicer_stream:cb_state().

-spec start_link(module(), connection_handle(), remote_stream_opts()) -> gen_server:start_ret().
start_link(CallbackModule, Connection, Opts) ->
start_link(CallbackModule, Connection, Opts#{is_local => false}, []).
Expand Down
20 changes: 20 additions & 0 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
tc_lib_registration_1/1,
tc_lib_re_registration/1,
tc_lib_registration_neg/1,
tc_setopt_global_lb_mode_ifip/1,
tc_open_listener_inval_reg/1,

tc_stream_client_init/1,
Expand Down Expand Up @@ -3081,6 +3082,25 @@ tc_stream_get_owner_remote(Config) ->
SPid ! done,
ensure_server_exit_normal(Ref).

tc_setopt_global_lb_mode_ifip(_Config) ->
{ok, _} = application:ensure_all_started(quicer),
true = code:soft_purge(quicer_nif),
true = code:delete(quicer_nif),
%% If test fail ensure we have this netdev
NetDevName =
case os:type() of
{unix, darwin} -> "lo0";
_ -> "lo"
end,
application:set_env(quicer, lb_mode, NetDevName),
quicer:reg_close(),
quicer:close_lib(),
{ok, _} = quicer:open_lib(),
?assertEqual(
{ok, 2},
quicer:getopt(quic_global, load_balacing_mode)
).

%%% ====================
%%% Internal helpers
%%% ====================
Expand Down

0 comments on commit 8ad53d0

Please sign in to comment.