Skip to content

Commit

Permalink
Add support of sndbuf
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed Oct 21, 2022
1 parent 767ee2d commit 35adaca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Different handlers are used for the sources.
]},
{servers, [
{root, {"127.0.0.1", [1812, 1813], [{socket_opts, [{recbuf, 8192},
{sndbuf, 131072},
{netns, "/var/run/netns/myns"}]}]}}
]},
{root, [
Expand Down Expand Up @@ -256,13 +257,13 @@ Example of full configuration with keys which can use in `eradius`:
{acct, {"127.0.0.1", [1813]}}
]},
{counter_aggregator, false},
%% List of histogram buckets for RADIUS servers metrics
%% List of histogram buckets for RADIUS servers metrics
{histogram_buckets, [10, 30, 50, 75, 100, 1000, 2000]},
%% Simple file-based logging of RADIUS requests and metadata
{logging, true},
%% Path to log file
{logfile, "./radius.log"},
%% List of upstream RADIUS servers pools
%% List of upstream RADIUS servers pools
{servers_pool, [
{pool_name, [
{{127, 0, 0, 2}, 1812, <<"secret">>, [{retries, 3}]},
Expand All @@ -272,7 +273,9 @@ Example of full configuration with keys which can use in `eradius`:
{server_status_metrics_enabled, false},
{counter_aggregator, false},
%% Size of RADIUS receive buffer
{recbuf, 8192}
{recbuf, 8192},
%% The minimum size of the send buffer to use for the RADIUS
{sndbuf, 131072}
]}].
```

Expand Down
3 changes: 2 additions & 1 deletion src/eradius.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
{counter_aggregator, false},
{server_status_metrics_enabled, false},
{logfile, "./radius.log"},
{recbuf, 8192}
{recbuf, 8192},
{sndbuf, 131072}
]},
{maintainers, ["Andreas Schultz", "Vladimir Tarasenko", "Yury Gargay"]},
{licenses, ["MIT"]},
Expand Down
3 changes: 2 additions & 1 deletion src/eradius_client_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ init([SocketIP, Client, PortIdx]) ->
ExtraOptions = [{ip, SocketIP}]
end,
RecBuf = application:get_env(eradius, recbuf, 8192),
{ok, Socket} = gen_udp:open(0, [{active, once}, binary , {recbuf, RecBuf} | ExtraOptions]),
SndBuf = application:get_env(eradius, sndbuf, 131072),
{ok, Socket} = gen_udp:open(0, [{active, once}, binary , {recbuf, RecBuf}, {sndbuf, SndBuf} | ExtraOptions]),
{ok, #state{client = Client, socket = Socket, pending = maps:new(), mode = active, counter = 0}}.

handle_call(_Request, _From, State) ->
Expand Down
20 changes: 10 additions & 10 deletions src/eradius_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@

-optional_callbacks([validate_arguments/1]).

-callback validate_arguments(Args :: list()) ->
-callback validate_arguments(Args :: list()) ->
boolean() | {true, NewArgs :: list()}.

-callback radius_request(#radius_request{}, #nas_prop{}, HandlerData :: term()) ->
-callback radius_request(#radius_request{}, #nas_prop{}, HandlerData :: term()) ->
{reply, #radius_request{}} | noreply | {error, timeout}.

-spec start_link(atom(), inet:ip4_address(), port_number()) -> {ok, pid()} | {error, term()}.
Expand All @@ -110,10 +110,9 @@ stats(Server, Function) ->
%% @private
init({ServerName, IP, Port, Opts}) ->
process_flag(trap_exit, true),
ExtraServerOptions = proplists:get_value(socket_opts, Opts, []),
DefaultRecBuf = application:get_env(eradius, recbuf, 8192),
ExtraServerOptionsWithBuf = add_recbuf_to_options(DefaultRecBuf, ExtraServerOptions),
case gen_udp:open(Port, ?DEFAULT_RADIUS_SERVER_OPTS(IP) ++ ExtraServerOptionsWithBuf) of
Opts = proplists:get_value(socket_opts, Opts, []),
SockOpts = ?DEFAULT_RADIUS_SERVER_OPTS(IP) ++ add_sock_opt(recbuf, 8192, Opts) ++ add_sock_opt(sndbuf, 131072, Opts),
case gen_udp:open(Port, SockOpts) of
{ok, Socket} ->
{ok, #state{socket = Socket,
ip = IP, port = Port, name = ServerName,
Expand Down Expand Up @@ -170,11 +169,12 @@ handle_info(_Info, State) ->
{noreply, State}.

%% @private
-spec add_recbuf_to_options(pos_integer(), proplists:proplist()) -> proplists:proplist().
add_recbuf_to_options(RecBuf, Opts) ->
case proplists:get_value(recbuf, Opts) of
-spec add_sock_opt(recbuf | sndbuf, pos_integer(), proplists:proplist()) -> proplists:proplist().
add_sock_opt(OptName, Default, Opts) ->
Buf = application:get_env(eradius, OptName, Default),
case proplists:get_value(OptName, Opts) of
undefined ->
[{recbuf, RecBuf} | Opts];
[{OptName, Buf} | Opts];
_Val ->
Opts
end.
Expand Down
3 changes: 2 additions & 1 deletion test/eradius_config_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ config_socket_options(_Config) ->

config_options(_Config) ->
Opts = [{socket_opts, [{recbuf, 8192},
{sndbuf, 131072},
{netns, "/var/run/netns/net1"}]}],
?match(Opts, eradius_config:validate_options(Opts)),
ok.
test_validate_server(_Config) ->
SocketOpts = [{socket_opts, [{recbuf, 8192}, {netns, "/var/run/netns/net1"}]}],
SocketOpts = [{socket_opts, [{recbuf, 8192}, {sndbuf, 131072}, {netns, "/var/run/netns/net1"}]}],
Opts = {{127, 0, 0, 1}, 1812, SocketOpts},
?match(Opts, eradius_config:validate_server(Opts)),
Opts2 = {{127, 0, 0, 1}, "1812", SocketOpts},
Expand Down

0 comments on commit 35adaca

Please sign in to comment.