summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicael Karlberg <bmk@erlang.org>2021-07-28 17:20:29 +0200
committerMicael Karlberg <bmk@erlang.org>2021-08-30 17:56:42 +0200
commitf4b903aba38eb6aac4a6d5598604c01a31cf1ff4 (patch)
tree55288ecea5ffc8b32e36d4b5cdc87db2b2d2d1a1
parent80444ecbc703d1032150806c2c153c1a1fe2171a (diff)
downloaderlang-f4b903aba38eb6aac4a6d5598604c01a31cf1ff4.tar.gz
[snmp|manager] Use of inet-backend for gen_udp
Make it possible to configure manager transport (net-if) to use the inet_backend gen_udp option. OTP-17526
-rw-r--r--lib/snmp/src/manager/snmpm_net_if.erl139
1 files changed, 87 insertions, 52 deletions
diff --git a/lib/snmp/src/manager/snmpm_net_if.erl b/lib/snmp/src/manager/snmpm_net_if.erl
index 1eb5ae216a..5ff8f91a6e 100644
--- a/lib/snmp/src/manager/snmpm_net_if.erl
+++ b/lib/snmp/src/manager/snmpm_net_if.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2004-2020. All Rights Reserved.
+%% Copyright Ericsson AB 2004-2021. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
@@ -81,7 +81,11 @@
-record(transport,
{socket,
- domain = snmpUDPDomain}).
+ port_info,
+ opts,
+ domain = snmpUDPDomain,
+ inet_backend = []
+ }).
-define(DEFAULT_FILTER_MODULE, snmpm_net_if_filter).
-define(DEFAULT_FILTER_OPTS, [{module, ?DEFAULT_FILTER_MODULE}]).
@@ -285,15 +289,29 @@ do_init(Server, NoteStore) ->
?vdebug("Log: ~w", [Log]),
{ok, DomainAddresses} = snmpm_config:system_info(transports),
- ?vdebug("DomainAddresses: ~w",[DomainAddresses]),
+ ?vdebug("DomainAddresses: ~w", [DomainAddresses]),
CommonSocketOpts = common_socket_opts(Opts),
- BindTo = get_opt(Opts, bind_to, false),
+ BindTo = get_opt(Opts, bind_to, false),
+ InetBackend = case get_opt(Opts, inet_backend, use_default) of
+ use_default -> [];
+ IB -> [{inet_backend, IB}]
+ end,
case
[begin
{IpPort, SocketOpts} =
socket_params(Domain, Address, BindTo, CommonSocketOpts),
- Socket = socket_open(IpPort, SocketOpts),
- #transport{socket = Socket, domain = Domain}
+ %% The 'inet-backend' option has to be first,
+ %% so we might as well add it last.
+ Socket = socket_open(IpPort, InetBackend ++ SocketOpts),
+ ?vtrace("socket created: "
+ "~n Ip Port: ~p"
+ "~n Socket Opts: ~p"
+ "~n Socket: ~p", [IpPort, SocketOpts, Socket]),
+ #transport{socket = Socket,
+ port_info = IpPort,
+ opts = SocketOpts,
+ inet_backend = InetBackend,
+ domain = Domain}
end || {Domain, Address} <- DomainAddresses]
of
[] ->
@@ -318,9 +336,9 @@ do_init(Server, NoteStore) ->
end.
socket_open(IpPort, SocketOpts) ->
- ?vtrace("socket_open -> entry with~n"
- " IpPort: ~p~n"
- " SocketOpts: ~p", [IpPort, SocketOpts]),
+ ?vtrace("socket_open -> entry with"
+ "~n IpPort: ~p"
+ "~n SocketOpts: ~p", [IpPort, SocketOpts]),
case gen_udp:open(IpPort, SocketOpts) of
{error, _} = Error ->
throw(Error);
@@ -354,6 +372,7 @@ socket_params(Domain, {IpAddr, IpPort} = Addr, BindTo, CommonSocketOpts) ->
_ ->
socket_params(SocketOpts, Addr, BindTo)
end.
+
%%
socket_params(SocketOpts, {IpAddr, IpPort}, BindTo) ->
case BindTo of
@@ -1253,9 +1272,9 @@ error_msg(F, A) ->
%%%-------------------------------------------------------------------
-% get_opt(Key, Opts) ->
-% ?vtrace("get option ~w", [Key]),
-% snmp_misc:get_option(Key, Opts).
+%% get_opt(Key, Opts) ->
+%% ?vtrace("get option ~w", [Key]),
+%% snmp_misc:get_option(Key, Opts).
get_opt(Opts, Key, Def) ->
?vtrace("get option ~w with default ~p", [Key, Def]),
@@ -1265,10 +1284,19 @@ get_opt(Opts, Key, Def) ->
%% -------------------------------------------------------------------
get_info(#state{transports = Transports}) ->
- ProcSize = proc_mem(self()),
- [{process_memory, ProcSize}
- | [{port_info, get_port_info(Socket)}
- || #transport{socket = Socket} <- Transports]].
+ ProcSize = proc_mem(self()),
+ Counters = get_counters(),
+ TransportInfo = [#{tdomain => Domain,
+ port_info => PI,
+ opts => Opts,
+ socket_info => get_socket_info(Socket)} ||
+ #transport{socket = Socket,
+ port_info = PI,
+ opts = Opts,
+ domain = Domain} <- Transports],
+ [{counters, Counters},
+ {process_memory, ProcSize},
+ {transport_info, TransportInfo}].
proc_mem(P) when is_pid(P) ->
case (catch erlang:process_info(P, memory)) of
@@ -1279,35 +1307,9 @@ proc_mem(P) when is_pid(P) ->
end.
-get_port_info(Id) ->
- PortInfo =
- case (catch erlang:port_info(Id)) of
- PI when is_list(PI) ->
- [{port_info, PI}];
- _ ->
- []
- end,
- PortStatus =
- case (catch prim_inet:getstatus(Id)) of
- {ok, PS} ->
- [{port_status, PS}];
- _ ->
- []
- end,
- PortAct =
- case (catch inet:getopts(Id, [active])) of
- {ok, PA} ->
- [{port_act, PA}];
- _ ->
- []
- end,
- PortStats =
- case (catch inet:getstat(Id)) of
- {ok, Stat} ->
- [{port_stats, Stat}];
- _ ->
- []
- end,
+get_socket_info(Id) when is_port(Id) ->
+ Info = inet:info(Id),
+
IfList =
case (catch inet:getif(Id)) of
{ok, IFs} ->
@@ -1315,6 +1317,29 @@ get_port_info(Id) ->
_ ->
[]
end,
+
+ BufSz =
+ case (catch inet:getopts(Id, [recbuf, sndbuf, buffer])) of
+ {ok, Sz} ->
+ [{buffer_size, Sz}];
+ _ ->
+ []
+ end,
+
+ [{socket, Id}, {info, Info}] ++ IfList ++ BufSz;
+
+get_socket_info(Id) ->
+ Info = inet:info(Id),
+
+ %% Does not exist for 'socket' ... yet
+ %% IfList =
+ %% case (catch inet:getif(Id)) of
+ %% {ok, IFs} ->
+ %% [{interfaces, IFs}];
+ %% _ ->
+ %% []
+ %% end,
+
BufSz =
case (catch inet:getopts(Id, [recbuf, sndbuf, buffer])) of
{ok, Sz} ->
@@ -1322,13 +1347,9 @@ get_port_info(Id) ->
_ ->
[]
end,
- [{socket, Id}] ++
- IfList ++
- PortStats ++
- PortInfo ++
- PortStatus ++
- PortAct ++
- BufSz.
+
+ [{socket, Id}, {info, Info}] ++ BufSz.
+
%%-----------------------------------------------------------------
@@ -1356,6 +1377,20 @@ counters() ->
inc(Name) -> inc(Name, 1).
inc(Name, N) -> snmpm_config:incr_stats_counter(Name, N).
+get_counters() ->
+ Counters = counters(),
+ get_counters(Counters, []).
+
+get_counters([], Acc) ->
+ lists:reverse(Acc);
+get_counters([Counter|Counters], Acc) ->
+ case snmpm_config:get_stats_counter(Counter) of
+ {ok, CounterVal} ->
+ get_counters(Counters, [{Counter, CounterVal}|Acc]);
+ _ ->
+ get_counters(Counters, Acc)
+ end.
+
%% ----------------------------------------------------------------