From 434ec52ff2db86d5d351a108da751c67610576dd Mon Sep 17 00:00:00 2001 From: vk Date: Fri, 21 Oct 2022 16:43:33 +0300 Subject: [PATCH] Add support of sndbuf --- README.md | 9 ++++++--- src/eradius.app.src | 3 ++- src/eradius_client_socket.erl | 3 ++- src/eradius_server.erl | 20 ++++++++++---------- test/eradius_config_SUITE.erl | 3 ++- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7ceea927..5665eb81 100644 --- a/README.md +++ b/README.md @@ -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, [ @@ -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}]}, @@ -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} ]}]. ``` diff --git a/src/eradius.app.src b/src/eradius.app.src index 924907b6..ced16030 100644 --- a/src/eradius.app.src +++ b/src/eradius.app.src @@ -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"]}, diff --git a/src/eradius_client_socket.erl b/src/eradius_client_socket.erl index c90f1b02..d03ff2b3 100644 --- a/src/eradius_client_socket.erl +++ b/src/eradius_client_socket.erl @@ -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) -> diff --git a/src/eradius_server.erl b/src/eradius_server.erl index 7edbbc2c..85de3678 100644 --- a/src/eradius_server.erl +++ b/src/eradius_server.erl @@ -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()}. @@ -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, @@ -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. diff --git a/test/eradius_config_SUITE.erl b/test/eradius_config_SUITE.erl index a09756a2..39edc763 100644 --- a/test/eradius_config_SUITE.erl +++ b/test/eradius_config_SUITE.erl @@ -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},