Skip to content

Commit

Permalink
feat(lb): user netdev ip as lb server fixed id
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Sep 19, 2024
1 parent 6766470 commit 942356f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
27 changes: 23 additions & 4 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,29 @@ 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;
MsQuic->SetParam(NULL,
QUIC_PARAM_GLOBAL_GLOBAL_SETTINGS,
sizeof(global_settings),
&global_settings);
}
else
{
MsQuic->SetParam(NULL,
QUIC_PARAM_GLOBAL_LOAD_BALACING_MODE,
sizeof(uint16_t),
(uint16_t *)&lb_mode);
}
}

if (enif_get_map_value(env, argv[0], ATOM_TRACE, &eterm)
Expand Down
8 changes: 8 additions & 0 deletions include/quicer.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
-ifndef(QUICER_HRL).
-define(QUICER_HRL, true).

-define(DEFAULT_LB_DEV, "eth0").

%%% ========================================
%%% mirror macro from NIF code
%%% ========================================
Expand Down Expand Up @@ -140,4 +142,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
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
?QUICER_LOAD_BALANCING_IFIP_AS_SERVER_ID ->
lb_server_id(ipv4, application:get_env(quicer, lb_dev, ?DEFAULT_LB_DEV));
X ->
X
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.

0 comments on commit 942356f

Please sign in to comment.